
var menus = {};

menus.currHelper = null;

menus.helper = function (ID){

if(menus.showcard ) ID = 'shopping_card'
    var x = document.getElementById('home_menu_helper_' + menus.currHelper)
    if(x) x.className = 'home_menu_helper_'+ menus.currHelper
    
    menus.currHelper = ID 
    var x = document.getElementById('home_menu_helper_' + menus.currHelper)
    if(x) x.className = 'home_menu_helper_'+ menus.currHelper +'_on'

    var x = document.getElementById('helper_stage_content');
    if(x) x.innerHTML = '<img style="position:absolute; top:142px; left:107px;"src="template/sven/img/wait.gif">';
    
    
 if(menus.showcard ) ID = 'shopping_card_btn'   ;
    switch(ID){
        
        case 'shopping_card':
            if(menus.currSub != 'info'){
                
                menus.selectSub('info_sc');  
            }else{
                ajaxgeturl('ajax/loadplugin.php?plug=card&sub=show','helper_stage_content');
            }
            break;
    case 'shopping_card_btn':
            //alert('card');
            //DO NOTHING just seeting the menu icon
            break;
            
        default :
            ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=generic&code=' + ID, 'helper_stage_content');
            break;
    }
    return false;
}



menus.currMain = 'home' ;
menus.selectMain = function (ID){

    
    var str = '';
    str += '<div style="position:absolute;top:0px; z-index:11111; left:0px; width:697px; height:399px; background:URL(\'template/sven/img/bg_alfa.png\')"';
    str += '</div>';
    str += '<img  style="position:absolute; z-index:1112; top:174px; left:321px;" src="/promoservice.pt/template/sven/img/wait1.gif">';

    document.getElementById('stage_l').innerHTML += str ;


    
    var x = document.getElementById('main_' + menus.currMain)
    if(x) x.className = 'main_'+ menus.currMain
    
    menus.currMain = ID 
    var x = document.getElementById('main_' + menus.currMain)
    x.className = 'main_'+ menus.currMain +'_on'
    
    switch(ID){
        case 'home':
            //ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home_helper', 'stage_r');
            
           if(this.currSub != 'info') this.selectSub('info');
           ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home', 'stage_l');
            break;
            
       case 'gifts':
        ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=gifts', 'stage_l');
       
        break;
            
        default :
            ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=products_helper_menu&code='+ themes.currCode+'&theme=' + themes.currTheme, 'home_menu_helper');
            if(this.currSub == 'info') this.selectSub('letters');

            ajaxgeturl('ajax/loadplugin.php?plug=products&code='+ID, 'stage_l');
            
            break;
    }
    
    
    
    return false;
}


menus.currSub = 'info';
menus.showcard = false;

menus.selectSub = function (ID){
    
    var x = document.getElementById('helpermenu_' + menus.currSub);
    if(x) x.className = 'helpermenu_'+ menus.currSub;
    
    menus.currSub = ID ;
    var x = document.getElementById('helpermenu_' + menus.currSub);
    if(x) x.className = 'helpermenu_'+ menus.currSub +'_on';

    document.getElementById('helper_stage').innerHTML = '<img style="position:absolute; top:142px; left:107px;"src="template/sven/img/wait.gif">';
    //ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=generic&code=' + ID, 'helper_stage');
    
    //reload theme menu
   
   themes.currCode = ID ;
    
     switch(ID){
        case 'info':
            ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home_helper', 'stage_r');
            
            //ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home', 'stage_l');
        break;

       case 'info_sc':
            menus.currSub = 'info';    
            ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home_helper&action=sc', 'stage_r');
            
            //ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home', 'stage_l');
        break;
            
        default :
            if(this.currMain == 'home') menus.selectMain('tshirts');
            ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=products_helper_menu&code='+ themes.currCode+'&theme=' + themes.currTheme, 'home_menu_helper');
            ajaxgeturl('ajax/loadplugin.php?plug=products&sub=' + ID +'&code='+ themes.currCode+'&theme=' + themes.currTheme, 'helper_stage');
            //ajaxgeturl('ajax/loadplugin.php?plug=products&product='+ID, 'stage_l');
            
            //ajaxgeturl('ajax/loadplugin.php?plug=pages&sub=home_helper', 'stage_r');
            break;
    }
    
    refs.currRef = null;
    custom.imgname = null;
    refs.txt = null;
    
    return false;
}


var prodmenu = {};

prodmenu.currMEnu = null ;
prodmenu.currSize = null ;
prodmenu.currColor = null;

prodmenu.setPrice= function(ID, PVP){
    document.getElementById('product_price_top').innerHTML = PVP + ' &euro;';
}   

prodmenu.buy = function(){
    //common
    ItemSize = prodmenu.currSize;
    ItemID = prodmenu.currColor;
    RefColor = themes.style.color;
    RefType = refs.currRefType;
    
    
    //letters & IMAGES
    RefId = refs.currRef; 
    
    //IMAGES & CUSTOM
    RefText = refs.txt;
    //Custom
    CustomImagePos = custom.imgpos;
    CustomImageName = custom.imgname;
    
    switch(RefType){
        case 'text':
            if(RefId == null) {
                alert('Por favor escolha um texto');
                return;
            }
            
            break;
            
        case 'custom':
            if ((custom.imgname == null) && (RefText == null) ){
                alert('Por favor escreva um texto ou escolha uma imagem');
                return;
            }
        
            break;

    }
    
     
     str='ItemSize=' + ItemSize + "\n" +
        '&ItemID=' +  ItemID+"\n"+
        '&RefColor=' +  RefColor+"\n"+
        '&RefType=' +  RefType+"\n"+
        '&RefId=' +  RefId+"\n"+
        '&RefText=' +  RefText+"\n"+
        '&CustomImagePos=' +  CustomImagePos+"\n"+
        '&CustomImageName=' +  CustomImageName ;
    
    
    
    //loadding info menu
        menus.showcard = true;
        menus.selectSub('info');
        
    
   //ajaxgeturl('ajax/loadplugin.php?plug=card&sub=add&'+str,'home_helpser_stage');
   ajaxgeturl('ajax/loadplugin.php?plug=card&sub=add&'+str,'main_dummy');
    
}

prodmenu.selectProd = function(ID){
    var x = document.getElementById('product_item_categ_' + this.currMEnu);
    if(x) x.className = 'product_item_categ';
    if(document.getElementById('product_item_categ_size_' + this.currMEnu)) document.getElementById('product_item_categ_size_' + this.currMEnu).style.display='none';
   
    var x = document.getElementById('product_item_color_selector_' + this.currMEnu)
    if(x) x.style.display = 'none';
    
    
    this.currMEnu = ID ;
    var x = document.getElementById('product_item_categ_' + this.currMEnu);
    x.className = 'product_item_categ_on';
    document.getElementById('product_item_categ_size_' + this.currMEnu).style.display='block';
    
    
    var x = document.getElementById('product_item_categ_size_' + this.currMEnu).firstChild;
    if(x) x.onclick();
    
    var x = document.getElementById('product_item_color_selector_' + this.currMEnu);
    if(x) x.style.display = 'block';
    
    var x = document.getElementById('product_item_color_selector_' + this.currMEnu).firstChild;
    if(x) x.onclick();
    
    
    
    
    var x = document.getElementById('product_item_img');
    x.src='template/sven/img/wait.gif';
    
}

prodmenu.selectSize = function(ID){
    var x = document.getElementById('product_item_size_' + this.currSize);
    if(x) x.className = 'product_item_categ';
    
    this.currSize = ID ;
    var x = document.getElementById('product_item_size_' + this.currSize);
    x.className = 'product_item_categ_on';
    
    document.getElementById('product_item_size_desc_height').innerHTML = document.getElementById('product_item_size_desc_height_' + ID).innerHTML ;
    document.getElementById('product_item_size_desc_width').innerHTML = document.getElementById('product_item_size_desc_width_' + ID).innerHTML ;
    document.getElementById('product_item_size_extra').innerHTML = document.getElementById('product_item_size_extra_' + ID).innerHTML ;
}


prodmenu.selectColor = function(ID, IMG,X,Y,W,S){

    //prodmenu.currColor = ID;

    var x = document.getElementById('product_item_img');
    x.src='template/sven/img/wait.gif';
    
    prodmenu.img = new Image();
    prodmenu.img.src='usr/img/'+IMG;
    
    prodmenu.img.onload=function(){
    
        
        
        var x = document.getElementById('product_item_img');
        
        if ( (x) && (x.src != prodmenu.img.src) ){
            x.src= prodmenu.img.src;
        }
        
    }
    
    prodmenu.img.onerror=function(){ img.onload()}
    
    window.setTimeout('prodmenu.img.onload()',4000);
    
    var x = document.getElementById('lnk_product_item_color_' + ID);
    x.onmouseover();
    
    var old = prodmenu.currColor;
    prodmenu.currColor = ID;
    
    
    
    var x = document.getElementById('lnk_product_item_color_' + old);
    if(x) x.onmouseout();
    
    
    
    var x =document.getElementById('product_item_text');
    x.style.top = Y+'px';
    x.style.left = X+'px';
    x.style.width=W+'px';
    themes.style.fontSize= S + 'px';
    //themes.style.fontWeight='900';
    
    
    themes.setStyle();
        
}


prodmenu.colorRoll = function (ID, IMG){
    if (prodmenu.currColor == ID) return;
    var x = document.getElementById('img_product_item_color_' + ID);
    x.src='usr/img/'+IMG
}


var themes={};

themes.currCode = null;
themes.currTheme = null;

themes.style = {};
themes.style.color= 'ffffff';
themes.style.fontSize='14px';
themes.style.fontWeight='900';
themes.style.colorName='Branco';

themes.setStyle = function (){
    var x =document.getElementById('product_item_text');
      x.style.color = themes.style.color ;
      x.style.fontSize = themes.style.fontSize ;
      x.style.fontWeight = themes.style.fontWeight ;

        if(x.hasChildNodes){
           var children = x.childNodes;
           for (var i = 0; i < children.length; i++){
              var y = children[i];
              if(y.style) y.style.color = themes.style.color ;
              if(y.style) y.style.fontSize = themes.style.fontSize ;
              if(y.style) y.style.fontWeight = themes.style.fontWeight ;
        
           } 
        }
        
        var x =document.getElementById('leters_text_color_name');
        if ( (x) && (x.style)) x.style.color = themes.style.color ;
        if ((themes.style.colorName)&&(x)) x.innerHTML = themes.style.colorName;
        
        
}

themes.showlist = function (){
        document.getElementById('helper_stage').innerHTML = '<img style="position:absolute; top:142px; left:107px;"src="template/sven/img/wait.gif">';
        ajaxgeturl('ajax/loadplugin.php?plug=products&sub=reflist&code='+this.currCode, 'helper_stage');
}

themes.changeColor = function (HEX, NAME){
    themes.style.color= HEX
    themes.style.colorName = NAME;
    
    themes.setStyle()
}

themes.select = function(ID, CODE){
    this.currTheme = ID;
    if (CODE == 'undefined') CODE='letters';
    menus.selectSub(CODE);
}

themes.setThemeName = function (NAME){
    var x = document.getElementById('products_helper_theme_box');
    if(x) x.innerHTML = NAME ;
}


var refs={};

refs.currRef=null;
refs.currRefType=null;
refs.select =  function(ID){
    var x = document.getElementById('product_ref_' + this.currRef)
    if(x) x.className = 'product_ref';
    
    this.currRef = ID
    
    var x = document.getElementById('product_ref_' + this.currRef)
    if(x) x.className = 'product_ref_on';
    
    var x = document.getElementById('product_ref_cnt_' + this.currRef)
    document.getElementById('product_item_text').innerHTML = x.innerHTML;
   themes.setStyle();
}

refs.selectImage =  function(ID){
    var x = document.getElementById('product_ref_' + this.currRef)
    if(x) x.className = 'product_ref_image';
    
    this.currRef = ID
    
    var x = document.getElementById('product_ref_' + this.currRef)
    if(x) x.className = 'product_ref_image_on';
    
    var x = document.getElementById('product_ref_cnt_' + this.currRef)
   if(document.getElementById('product_item_text')) document.getElementById('product_item_text').innerHTML = x.innerHTML;
   themes.setStyle();
   
   this.setImageText ();
}

refs.txt = null;
refs.setImageText = function() {
    var x = document.getElementById('product_ref_image_text_cnt');
    var txt = document.getElementById('product_ref_image_text').value;

    txt = txt.replace(/\ /g,'&nbsp;');
    txt = txt.replace(/</g,'&lt;');
    txt = txt.replace(/>/g,'&gt;');
    txt = txt.replace(/\"/g,'&quot;');
    txt = txt.replace(/\n/g,'<br>');
    
    refs.txt = txt;
    if(x) x.innerHTML = txt ;
}





var custom = {};
custom.imgpos ='top';
custom.imgname ='';
custom.doupload= function (){

    
    document.getElementById('customimageupload').style.display = 'none';
    document.getElementById('custom_image_upload_preview').style.display = 'block';

    var x = document.getElementById('frm');
    x.submit();
}

custom.reset = function(){
    document.getElementById('customimageupload').value = '';
    document.getElementById('customimageupload').style.display = 'block';
    document.getElementById('custom_image_upload_preview').style.display = 'none';

}
custom.flipimg = function (POS){
    if(custom.imgpos == POS) return;
        
        
    custom.imgpos = POS;
    
    if(this.imgpos == 'top'){
        document.getElementById('product_ref_image_text_top').src=document.getElementById('product_ref_image_text_bottom').src ;
        document.getElementById('product_ref_image_text_top').style.display = 'block';
        document.getElementById('product_ref_image_text_bottom').src = 'template/sven/img/blank.gif' ;
        document.getElementById('product_ref_image_text_bottom').style.display = 'none';
    }else{
        document.getElementById('product_ref_image_text_bottom').src= document.getElementById('product_ref_image_text_top').src
        document.getElementById('product_ref_image_text_bottom').style.display = 'block';
        document.getElementById('product_ref_image_text_top').src='template/sven/img/blank.gif' ;
        document.getElementById('product_ref_image_text_top').style.display = 'none';
    }

}


custom.changeimage = function (IMG){
    
    this.reset();
    this.imgname = IMG;
    
    if(this.imgpos == 'top'){
        document.getElementById('product_ref_image_text_top').src='usr/files/' + IMG ;
        document.getElementById('product_ref_image_text_bottom').src='template/sven/img/blank.gif' ;
        
        document.getElementById('product_ref_image_text_top').style.display = 'block';
        document.getElementById('product_ref_image_text_bottom').style.display = 'none'
    }else{
        document.getElementById('product_ref_image_text_top').src='template/sven/img/blank.gif' ;
        document.getElementById('product_ref_image_text_bottom').src='usr/files/' + IMG ;
        
        document.getElementById('product_ref_image_text_bottom').style.display = 'block';
        document.getElementById('product_ref_image_text_top').style.display = 'none';
        
    }
}



