category_id = null;
timeout = null;
input = null;
TypeCategory = 2;
img_sl = 'topo_PP.jpg';
img_SLBG = null;
get_Dominio = 1;
accId = 1;
store_front_id = '0001-0001-1999-0000';

//getDominio 0 stage 1 production

init_colors = function(example) {

   
    var pallet = playphone.$('color');
    
    

    var links = pallet.getElementsByTagName('a');
    
    for (i in links) {

        links[i].onclick = function() {
        
            var background = this.style.backgroundColor;
            
            if (background.indexOf('#') < 0) {
            
                var split = background.substring(4, background.length - 1).split(',');
                
                playphone.$(input).value = convert_rgb_hex(split[0], split[1], split[2]);
                
                example.style.backgroundColor = background;
            
            } else {
            
                playphone.$(input).value = background.replace(/#/g, '').toUpperCase();
                
                example.style.backgroundColor = convert_hex_rgb(background, true);
            }
            
            pallet.style.display = 'none';
            
            create_widget();
        }
    }
}

color_over = function(example, _input) {

    
    clearTimeout(timeout);
   
    
    if (_input) {
    
        input = _input;
    }
    
    if (example) {
    
        init_colors(example);
        
        var offsetTop = get_top_position(example);
        
        var position = offsetTop - 245;
        
        playphone.$('color').style.top = position + 190 + 'px';
        //playphone.$('color').style.top = position + -70 + 'px';
        
        
    }
    
       
    playphone.$('color').style.display = 'block';
}

color_out = function() {

    timeout = setTimeout(function() { playphone.$('color').style.display = 'none'; }, 500);
}

convert_rgb_hex = function(red, green, blue) {

    return get_hex(red) + get_hex(green) + get_hex(blue);
}

convert_hex_rgb = function(value, is_string_format) {

    var hex = {}, hex_string = value.replace(/^#?/, '');
    
    if (!/^([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/.test(hex_string)) {
    
        return hex_string;
    }
    
    hex.r = parseInt('0x' + RegExp.$1);
    hex.g = parseInt('0x' + RegExp.$2);
    hex.b = parseInt('0x' + RegExp.$3);
    
    return is_string_format ? 'rgb(' + hex.r + ',' + hex.g + ',' + hex.b + ')' : hex;
}

get_hex = function(value) {

    var chars = '0123456789ABCDEF';
    
    var media = value % 16;
    
    return chars.charAt((value - media) / 16) + chars.charAt(media);
}

get_top_position = function(obj) {

    if (obj.offsetParent) {
    
        for (var position_y = 0; obj.offsetParent; obj = obj.offsetParent) {
        
            position_y += obj.offsetTop;
        }
        
        return position_y;
    }
    
    return obj.y;
}

is_color = function(e) {

    var chars = 'ghijklmnopqrstuvwxyzGHIJKLMNOPQRSTUVWXYZ!@#$%¨&*()_+-=\|,<.>;:/?~^]}[{´`\'"';

    var code = (window.Event) ? e.which : e.keyCode;
    
	key = String.fromCharCode(code);
    
    if (chars.indexOf(key) == -1) {
    
        return true;
    }
    
    return false;
}

set_color = function(obj, example) {
    
    if (obj.value.length == 6) {
        
        obj.value = obj.value.toUpperCase();
        
        playphone.$(example).style.backgroundColor = convert_hex_rgb(obj.value, true);
        
        create_widget();
    }
}

create_widget = function() {
   
    var border = playphone.$('txtBorder').value;
    var title_text = playphone.$('txtTitleText').value;
    var title_background = playphone.$('txtTitleBackground').value;
    var lines = playphone.$('txtLines').value;
    var item_text = playphone.$('txtItemText').value;
    var item_background = playphone.$('txtItemBackground').value;
    var link = playphone.$('txtLink').value;
    var button_icon = playphone.$('txtButtonIcon').value;
    var button_background = playphone.$('txtButtonBackground').value;

    var so = new SWFObject('http://widgets.playphone.com/NewWidgets.swf', 'widgets', '183', '307', '8');
    //var so = new SWFObject('/NewWidgets.swf', 'widgets', '182', '307', '8');
    so.addVariable('category_id', category_id);
    so.addVariable('imgSL', 'http://widgets.playphone.com/Images/' + img_sl);
    so.addVariable('getDominio', get_Dominio);
    so.addVariable('TypeCategory', TypeCategory);
    so.addVariable('border', border);
    so.addVariable('category_background', title_background);
    so.addVariable('category_title', title_text);
    so.addVariable('lines', lines);
    so.addVariable('item_text', item_text);
    so.addVariable('item_background', item_background);
    so.addVariable('imgSLBG', 'http://widgets.playphone.com/Images/' + img_SLBG);
    so.addVariable('button_icon', button_icon);
    so.addVariable('button_background', button_background);
    so.addVariable('link', link);
    so.addVariable('accId', accId);
    so.addVariable('store_front_id', store_front_id);
    so.addVariable('linked', 'true');
    so.addParam('wmode', 'transparent');
    so.addParam('allowScriptAccess', 'always');
    so.addParam('allowNetworking', 'all');
    so.write('content_widgets');
          
    generate_code();
}

has_checked = function() {
    var patterns = document.getElementsByName('choice');
    for (i in patterns) {
        if (patterns[i].checked) {
            return true;
        }
    }
    return false;
}

set_category = function() {

    category_id = playphone.$('ctl00_ContentPlaceHolder1_lstTopRingtones').value;
    
    if (!has_checked()) { 
        playphone.$('pattern_common').checked = true;
        set_pattern('common');
    }
    
    if (document.getElementById('error') != null)
        document.getElementById('error').style.display = 'none';
        
    if (document.getElementById('success') != null)
        document.getElementById('success').style.display = 'none';
    
    create_widget(category_id);
    
    playphone.$('widget_color').style.display = 'block';
    
}

set_categoryNew = function(value, typeCategory) {

    category_id = value;
    TypeCategory = typeCategory;
    
    /*if (!has_checked()) { 
        playphone.$('pattern_common').checked = true;
        set_pattern('common');
    }*/
     //document.getElementById('ctl00$ContentPlaceHolder1$ddlThemes').SelectedIndex = 0;
    
    if (document.getElementById('error') != null)
        document.getElementById('error').style.display = 'none';
        
    if (document.getElementById('success') != null)
        document.getElementById('success').style.display = 'none';
    
    create_widget(category_id);
    
}

function generate_code() {
                
    var border = playphone.$('txtBorder').value;
    var title_text = playphone.$('txtTitleText').value;
    var title_background = playphone.$('txtTitleBackground').value;
    var lines = playphone.$('txtLines').value;
    var item_text = playphone.$('txtItemText').value;
    var item_background = playphone.$('txtItemBackground').value;
    var link = playphone.$('txtLink').value;
    var button_icon = playphone.$('txtButtonIcon').value;
    var button_background = playphone.$('txtButtonBackground').value;
    
    var code = new String();
    
       
    code += '<table border=0 cellpadding=0 cellspacing=0 style="padding:0; margin:0; border:0">';
    code += '<tr style="padding:0; margin:0; border:0"><td style="padding:0; margin:0; border:0">';
    
    code += '<object type="application/x-shockwave-flash" data="http://widgets.playphone.com/NewWidgets.swf" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version = 7,0,0,0" width="183" height="307">';
    code += '\r<param name="movie" value="http://widgets.playphone.com/NewWidgets.swf" />';
    
    //code += '<object type="application/x-shockwave-flash" data="/NewWidgets.swf" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version = 7,0,0,0" width="182.95" height="307">';
    //code += '\r<param name="movie" value="/NewWidgets.swf" />';

    
    code += '\r<param name="flashvars" value="category_id=' + category_id;
    code += '&amp;TypeCategory=' + TypeCategory;
    code += '&amp;imgSL=' + 'http://widgets.playphone.com/Images/' + img_sl;
    code += '&amp;getDominio=' + get_Dominio;
    code += '&amp;border=' + border;
    code += '&amp;category_background=' + title_background;
    code += '&amp;category_title=' + title_text;
    code += '&amp;lines=' + lines;
    code += '&amp;item_text=' + item_text;
    code += '&amp;item_background=' + item_background;
    code += '&amp;imgSLBG=' + 'http://widgets.playphone.com/Images/' + img_SLBG;
    code += '&amp;button_icon=' + button_icon;
    code += '&amp;button_background=' + button_background;
    code += '&amp;link=' + link;
    code += '&amp;accId=' + accId;
    code += '&amp;store_front_id=' + store_front_id;
    code += '&amp;linked=true" />';
    code += '\r<param name="quality" value="high" />';
    code += '\r<param name="wmode" value="transparent" />';
    code += '\r<param name="allowScriptAccess" value="always" />';
    code += '\r<param name="allowNetworking" value="all" />';
    code += '\r</object>';
    code += '\r</td></tr>';
    code += '\r<tr style="padding:0; margin:0; border:0"><td style="padding:0; margin:0; border:0">';
    code += '\r<a class="playp-widget" id="playp-widget" title="Download ringtones for your cell phone" href="http://www.playphone.com" target="_blank" style="margin:0; padding:0; background-image:url(http://design.playp.net/Widget-POT/pp-logo.gif); width:182px; height:24px; display:block" title="Get Ringtones"></a>';
    code += '\r<img style="visibility:hidden;width:0px;height:0px;" border=0 width=0 height=0 src="http://counters.gigya.com/wildfire/CIMP/bT*xJmx*PTEyMTIxNzc2NDkxNzYmcHQ9MTIxMjE3ODE4MDc5MSZwPTE4NjA5MSZkPSZuPSZnPTE=.jpg" />';
    code += '\r</td></tr>';
    code += '\r</table>';
    
    playphone.$('ctl00_ContentPlaceHolder1_txtCode').value = code;
}

set_pattern = function(value) {
    
    document.getElementById('ctl00_ContentPlaceHolder1_ddlThemes').value = 0;
    img_sl = 'topo_PP.jpg';
    img_SLBG = null;
    
     switch (value) {
    
        case 'common':
        
            playphone.$('customize').style.display = 'none';
            playphone.$('widget_preview').style.display = 'block';
            playphone.$('widget_code').style.display = 'block';
            
            playphone.$('txtBorder').value = '0077C0';
            playphone.$('txtTitleText').value = 'FFFFFF';
            playphone.$('txtTitleBackground').value = '0077C0';
            playphone.$('txtLines').value = 'BFDDEF';
            playphone.$('txtItemText').value = '333333';
            playphone.$('txtItemBackground').value = 'FFFFFF';
            playphone.$('txtLink').value = '0077C0';
            playphone.$('txtButtonIcon').value = 'FFFFFF';
            playphone.$('txtButtonBackground').value = '0077C0';
            create_widget();
            break;
        case 'dark':
            playphone.$('customize').style.display = 'none';
            playphone.$('widget_preview').style.display = 'block';
            playphone.$('widget_code').style.display = 'block';

            playphone.$('txtBorder').value = '000000';
            playphone.$('txtTitleText').value = 'FFFFFF';
            playphone.$('txtTitleBackground').value = '000000';
            playphone.$('txtLines').value = '666666';
            playphone.$('txtItemText').value = 'EEEEEE';
            playphone.$('txtItemBackground').value = '191919';
            playphone.$('txtLink').value = '000000';
            playphone.$('txtButtonIcon').value = '000000';
            playphone.$('txtButtonBackground').value = 'FFFFFF';
            create_widget();
            break;
        case 'red':
            playphone.$('customize').style.display = 'none';
            playphone.$('widget_preview').style.display = 'block';
            playphone.$('widget_code').style.display = 'block';
            
            playphone.$('txtBorder').value = 'c00000';
            playphone.$('txtTitleText').value = 'FFFFFF';
            playphone.$('txtTitleBackground').value = 'c00000';
            playphone.$('txtLines').value = 'b37a06';
            playphone.$('txtItemText').value = 'FFFFFF';
            playphone.$('txtItemBackground').value = '680000';
            playphone.$('txtLink').value = 'c00000';
            playphone.$('txtButtonIcon').value = 'FFFFFF';
            playphone.$('txtButtonBackground').value = 'c00000';
            create_widget();

            break;
        case undefined:
            set_color(playphone.$('txtBorder'), 'spnBorder');
            set_color(playphone.$('txtTitleText'), 'spnTitleText');
            set_color(playphone.$('txtTitleBackground'), 'spnTitleBackground');
            set_color(playphone.$('txtLines'), 'spnLines');
            set_color(playphone.$('txtItemText'), 'spnItemText');
            set_color(playphone.$('txtItemBackground'), 'spnItemBackground');
            set_color(playphone.$('txtLink'), 'spnLink');
            set_color(playphone.$('txtButtonIcon'), 'spnButtonIcon');
            set_color(playphone.$('txtButtonBackground'), 'spnButtonBackground');
            
            playphone.$('customize').style.display = 'block';
            playphone.$('widget_preview').style.display = 'block';
            playphone.$('widget_code').style.display = 'block'
            break;
    }
}



set_patternDb = function(Border, TitleText, TitleBackground, Lines, link,  ItemText, ItemBackground, ItemBackGroundImage, ButtonIcon, ButtonBackground, imgSl )
{

        playphone.$('txtBorder').value = Border;
        playphone.$('txtTitleText').value = TitleText;
        playphone.$('txtTitleBackground').value = TitleBackground;
        playphone.$('txtLines').value = Lines;
        playphone.$('txtItemText').value = ItemText;
        playphone.$('txtItemBackground').value = ItemBackground;
        playphone.$('txtLink').value = link;
        playphone.$('txtButtonIcon').value = ButtonIcon;
        playphone.$('txtButtonBackground').value = ButtonBackground;
        
        
        if(imgSl != undefined && imgSl != '')
        {
            img_sl = imgSl;
        }
        
        if(ItemBackGroundImage != undefined && ItemBackGroundImage != '')
        {
            img_SLBG = ItemBackGroundImage;
        }
        
        playphone.$('customize').style.display = 'none';
        playphone.$('widget_preview').style.display = 'block';
        playphone.$('widget_code').style.display = 'block'
        
        /*document.getElementById('pattern_common').checked = false;
        document.getElementById('radio2').checked = false;
        document.getElementById('radio3').checked = false;
        document.getElementById('radioCustom').checked = false;*/
        
       
        create_widget();
}