$(function() {
    
  //LANGUAGES  
  var block = $('.languages');
  $('.language-switcher, .languages').hover(function() {
    block.show();
  }, function() {
    block.hide();
  });
  
  //jselect
  $(".default-usage-select").selectbox();
  
  
  
  //footer left menu
  var links = $('#footer-menu ul li a').click(function() {
    links.removeClass('active');
    $(this).addClass('active');
  }); 


  $('#footernews3').hover(function() {
    $('#toolbar3').show();
    $('#toolbar4').hide();
    $('#toolbar5').hide();
  });

  $('#footernews3').mouseleave(function() {
    $('#toolbar3').hide();
  }); 
  
  
  $('#footernews4').hover(function() {
    $('#toolbar3').hide();
    $('#toolbar4').show();
    $('#toolbar5').hide();
  }); 

  $('#footernews4').mouseleave(function() {
    $('#toolbar4').hide();
  });

  
  $('#footernews5').hover(function() {
    $('#toolbar3').hide();
    $('#toolbar4').hide();
    $('#toolbar5').show();
  }); 
  
  $('#footernews5').mouseleave(function() {
    $('#toolbar5').hide();
  });  
  
  
  
  
  $('#t3b3').hover(function() {
    $('#toolbar3').show();
    $('#toolbar4').hide();
    $('#toolbar5').hide();
  }); 
  
  $('#t3b3').mouseleave(function() {
    $('#toolbar3').hide();
  });   
  
  $('#t4b4').hover(function() {
    $('#toolbar3').hide();
    $('#toolbar4').show();
    $('#toolbar5').hide();
  });
  
  $('#t4b4').mouseleave(function() {
    $('#toolbar4').hide();
  });   
   
  
  $('#t5b5').hover(function() {
    $('#toolbar3').hide();
    $('#toolbar4').hide();
    $('#toolbar5').show();
  }); 

  $('#t5b5').mouseleave(function() {
    $('#toolbar5').hide();
  });   



  
  //scrollpanels
  $('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:73});
  $('.scrolling').jScrollPane({
          showArrows: true,
          scrollbarWidth: 10,
          dragMaxHeight: 73,
          height: 150
          
  });;  
  
  
  
  //FOOTER MENU!!!
  //hide toolbar and make visible the 'show' button
  $("span.downarr a").click(function() {
    $("#toolbar").slideToggle("fast");
    $("#toolbarbut").fadeIn("slow");    
  });
  
  //show toolbar and hide the 'show' button
  $("span.showbar a").click(function() {
    $("#toolbar").slideToggle("fast");
    $("#toolbarbut").fadeOut();    
  });
  
  //don't jump to #id link anchor 
  $("span.downarr a, span.showbar a").click(function() {
   return false;                                         
  });  
  
  $('.closable').click(function(e){
        if($('#pagecontent').is(":visible"))
        {
            //$('#pagecontent').stop(true,true).fadeOut('fast');
            $('#pagecontent').remove();   
            $('#pagecontainer').css('background','none'); 
        }
    });
    
   
   
  //VIDEO  
    var msie7 = ($.browser.msie && jQuery.browser.version.charAt(0)==7);
    var movieFile = '/commons/swf/Antrax2011.flv';
    if (msie7) movieFile = '/commons/swf/Antrax2011.flv';
    
    //var movieFile = '/commons/swf/antrax.flv';
    //if (msie7) movieFile = '/commons/swf/antraxIE.flv';
    
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    var attributes = {};
    //params.FlashVars = "config={'plugins': { 'controls': null }, 'playlist':[ '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"', '"+movieFile+"']   }";
    
    //ORIG
    //params.FlashVars = "config={'plugins': { 'controls': null }, 'playlist':[ '"+movieFile+"', '"+movieFile+"', '"+movieFile+"']  }";
    //swfobject.embedSWF("/commons/swf/flowplayer-3.2.5.swf", "bgflash", "300", "200", "10.0.0", null, flashvars, params, attributes );
    //swffit.fit("bgflash",50,40,4000,4000,true,true);
    
    var exec_seek = true;

    //flowplayer("bgflash",{ src: "/commons/swf/flowplayer-3.2.5.swf", wmode:"transparent" } ,{ plugins: { controls: null }, playlist:[ movieFile,movieFile,movieFile,movieFile,movieFile,movieFile,movieFile,movieFile ], onStart:function() { if(get_cookie('seek_player') && parseFloat(get_cookie('seek_player')) > 0) this.seek(parseFloat(get_cookie('seek_player'))-1); console.log((parseFloat(get_cookie('seek_player'))-1))  }  } );
    if(get_video_bg)
    {
        flowplayer("bgflash",{ 
                                src: "/commons/swf/last/flowplayer-3.2.7.swf", 
                                wmode:"opaque" 
                             }, 
                             { 
                                //debug: true,
                                plugins: 
                                        { controls: {
                                                        //url: "/commons/swf/flowplayer-controls/flowplayer.controls-3.2.5.swf",
                                                        url: "/commons/swf/last/flowplayer.controls-3.2.5.swf",
                                                        top: 0,
                                                        right: 0,
                                                        //bottom: 240,
                                                		//left: 0,
                                                        autoHide:true
                                                        /*{
                                                            hideStyle: 'fade',
                                                            hideDelay: 1000,
                                                            mouseOutDelay: 500,
                                                            enabled: true
                                                        }*/
                                                        ,
                                                		opacity: 0.7,
                                                        width: 190,
                                                        
                                                        
                                                
                                                		// styling properties (will be applied to all plugins)
                                                		//background: '#114455 url(/my/custom/controls.png) no-repeat 3px 6px',
                                                		//backgroundGradient: 'low',
                                                /*
                                                        buttonColor: 'rgba(0, 0, 0, 0.9)',
                                            			buttonOverColor: '#000000',
                                            			backgroundColor: '#D7D7D7',
                                            			backgroundGradient: 'medium',
                                            			sliderColor: '#FFFFFF',
                                            			
                                            			sliderBorder: '1px solid #808080',
                                            			volumeSliderColor: '#FFFFFF',
                                            			volumeBorder: '1px solid #808080',
                                                */
                                                		// controlbar specific settings
                                                		timeColor: '#00ff00',
                                                        durationColor: '#EA8E21',
                                                        progressColor: '#ffffff',
                                                		
                                                        all: false,
                                                		
                                                        play: true, 	           //Should the play/pause button be visible?
                                                        volume:	true, 	           //Should the volume control be visible?
                                                        mute: true, 	           //Should the mute button be visible?
                                                        time: false, 	           //Should the time display be visible?
                                                        stop: true, 	           //Should the stop button be visible?
                                                        playlist: false, 	       //Should the playlist buttons be visible? The playlist buttons allow navigation through the playlist entries of the current playlist. These buttons are always hidden if there are less than two entries in the playlist.
                                                        fastBackward: false,        //Should the fast backward button be visible? The slow motion buttons allow controlling the playback speed (from 1/8 to 8x the normal speed). These buttons are always hidden if the slowmotion plugin is not loaded, and only available with the "Modern" skin.
                                                        fastForward: false, 	       //Should the fast forward button be visible?
                                                        slowBackward: false, 	   //Should the slow backward button be visible?
                                                        slowForward: false, 	       //Should the slow forward button be visible?
                                                        fullscreen: true, 	       //Should the fullscreen button be visible?
                                                        scrubber: false             //Should the scrubber be visible? 'Scrubber' is a well known nickname for the timeline/playhead combination. 
                                                        
                                                        
                                                    }
                                                    
                                        }, 
                                        playlist:[ 
                                                    
                                                  {
                                                        url: movieFile,
                                                        autoPlay: true,
                                                        autoBuffering: true,
                                                        loop: true
                                                        //onLastSecond: function() { setTimeout(this.play(),800); }
                                                        ,onBeforeFinish:function() { return false; }
                                                        ,onFinish: function() { this.play(0); exec_seek = false; }
                                                   }
                                                   
                                                ]
                                        , 
                                        onStart:function() { 
                                                                //console.log("onStart_pre",parseFloat(get_cookie('seek_player')));
                                                                if(exec_seek && get_cookie('seek_player') && parseFloat(get_cookie('seek_player')) > 0) 
                                                                {
                                                                    this.seek(parseFloat(get_cookie('seek_player')) - parseFloat('0.9')); 
                                                                 
                                                                    delete_cookie('seek_player');
                                                                    //console.log(document.cookie);
                                                                }
                                                           } 
                                         
                             });
                             
        swffit.fit("bgflash",50,40,4000,4000,true,true);
        
    
        $(window).load(function(){ if(get_cookie('seek_player') == null) set_cookie("seek_player",parseFloat('0.1'), 1);  });
    
        $(window).unload(function(){
            
           delete_cookie('seek_player');
           if($f("bgflash") && $f("bgflash").getTime() > 0)
           {
                var tmp_date = new Date();
                
                //alert("time: "+$f("bgflash").getTime()+" , y: "+tmp_date.getFullYear()+" , m: "+(tmp_date.getMonth()+1)+" , d: "+(tmp_date.getDate() + 1));
                //console.log("onunload",parseFloat(get_cookie('seek_player')));
                
                $f("bgflash").pause();
                
                delete_cookie('seek_player');
                //console.log("deleted",parseFloat(get_cookie('seek_player')));
                //console.log(document.cookie);
                //set_cookie("seek_player",$f("bgflash").getTime(),tmp_date.getFullYear(),(tmp_date.getMonth()+0),tmp_date.getDate() + 1 );
                set_cookie("seek_player",$f("bgflash").getTime(), 1);
                //alert("time: "+$f("bgflash").getTime()+" , y: "+tmp_date.getFullYear()+" , m: "+(tmp_date.getMonth()+0)+" , d: "+(tmp_date.getDate() + 1));
                //return false;
                //alert(1);
           }
           
        });
    }
  
    function set_cookie(cookie_name, value, exp) { $.cookie(cookie_name, value, {expires: exp} ); }
    
    function delete_cookie(cookie_name) { $.cookie(cookie_name, null ); }
    
    function get_cookie(cookie_name) { return $.cookie(cookie_name); }


  function onload() {
    var wnd_width = $('body').width(),
        wnd_height = $('body').height();
    /*
    body = body || $('body');
    pseudo = pseudo || $('#pseudo-body');
    footer = footer || $('#footer');
    */
    //alert(11);
    body = $('body');
    //alert(12);
    pseudo = $('#pseudo-body');
    //alert(13);
    //footer = $('#footer');
    //alert(footer);
    wnd = $('window');
    //alert(2);
    /*body.css({
      height: wnd.height() - 128,
      paddingBottom: 128
    });*/
    var bg = $('#background').find('img');
    var ratio = 12/8;
    bg.attr({
      width: wnd_width,
      height: wnd_width / ratio
    });
    if (bg.height() < wnd_height) {
      bg.attr({
        width: wnd_height * ratio,
        height: wnd_height
      });
    }
    //main = main || $('#main-wrapper');
    main = $('#main-wrapper');
    var width = main.outerWidth(),
        height = main.outerHeight();
    main.css({
      left: Math.max(0, (wnd_width - width)/2),
      top: (Math.max(0, (wnd_height - height)/2)-30),
      //top: (Math.max(0, (wnd_height - height)/2)),
      position: 'absolute'
    });
    
    
    
  }   
   
  setTimeout(onload, 200); 

  
});





function show(){
    if (document.getElementById('leftmenu')==null || document.getElementById('leftmenu').style.display=='' || document.getElementById('leftmenu').style.display=='block'){
        $('#leftmenu').hide();   
        $('#logo').css('border-bottom', '0');        
        $('#container-top').css('background-color', 'transparent');
        $('#container-bottom').hide();
        $('#pagecontainer').hide();
        $('#languageselector').hide();
        $('#homepage').hide();
        $('#TOPCLOSE').hide();
        $('#TOPOPEN').show();        
    } else{
        $('#leftmenu').show();   
        $('#container-top').css('background-color', 'white');
        $('#logo').css('border-bottom', '1px dashed #bababa');
        $('#container-bottom').show();
        $('#pagecontainer').show();
        $('#languageselector').show();
        $('#homepage').show();                
        $('#TOPOPEN').hide();
        $('#TOPCLOSE').show();
   }    
   return false;
}


function approfondimenti(id){
    //console.log($('#'+id));
    
    $('#'+id).click();
    
    //$('.details').addClass('noprint');
    /*
    $('[id*="app-pv-'+id+'-"], [id*="app-po-'+id+'-"]').each(function(i,el) { 
       var tmp_src = $(el).attr("src");
       if(tmp_src.search(strToAddBeforePathImg) == 0) $(el).attr("src", tmp_src.replace(strToAddBeforePathImg,"") );
       else if(tmp_src.search(strToAddBeforePathImg) > 0) $(el).attr("src", tmp_src.replace("/"+strToAddBeforePathImg,"") );
       
       if(tmp_src.search(strToAddBeforePathImg) >= 0) {
            var p1 = tmp_src.substring(0,tmp_src.search(strToAddBeforePathImg));
            var p2 = tmp_src.substring((tmp_src.search(strToAddBeforePathImg)+strToAddBeforePathImg.length));
            $(el).attr("src", p1+p2);  
       } 
    });
    */
    
    
    
    //var tmp_prod_id = id.substring((id.length-1),(id.length));
    //set_src_img_app(tmp_prod_id);
    
    
    return false;
}

function trackProductPage(lang,cat_id,cat_name,prod_id,prod_name)
{   
    /*
    var c = 0;
    $('[id*="app-pv-'+prod_id+'-"], [id*="app-po-'+prod_id+'-"]').each(function(i,el) { 
       c++;
       var tmp_src = $(el).attr("src");
       if(tmp_src.search(strToAddBeforePathImg) == 0) $(el).attr("src", tmp_src.replace(strToAddBeforePathImg,"") );
       else if(tmp_src.search(strToAddBeforePathImg) > 0) $(el).attr("src", tmp_src.replace("/"+strToAddBeforePathImg,"") );
       
       if(tmp_src.search(strToAddBeforePathImg) >= 0) {
            var p1 = tmp_src.substring(0,tmp_src.search(strToAddBeforePathImg));
            var p2 = tmp_src.substring((tmp_src.search(strToAddBeforePathImg)+strToAddBeforePathImg.length));
            $(el).attr("src", p1+p2);  
       }
    });
    */
    
    set_src_img_app(prod_id);
    
    //console.log(lang,cat_id,cat_name,prod_id,prod_name,'/'+lang+'/'+cat_name+'~'+cat_id+'/'+prod_name+'~'+prod_id+'');
    _gaq.push(['_trackPageview', '/'+lang+'/'+cat_name+'~'+cat_id+'/'+prod_name+'~'+prod_id+'']);
}


function set_src_img_app(prod_id)
{
    var c = 0;
    var done = false;
    
    $('img[id*="app-pv-'+prod_id+'-"], img[id*="app-po-'+prod_id+'-"]').each(function(i,el) { 
       c++;
       done = false;
       
       var tmp_src = $(el).attr("src");
       if(tmp_src)
       {
           if(tmp_src.search(strToAddBeforePathImg) == 0) $(el).attr("src", tmp_src.replace(strToAddBeforePathImg,"") );
           else if(tmp_src.search(strToAddBeforePathImg) > 0) $(el).attr("src", tmp_src.replace("/"+strToAddBeforePathImg,"") );
           
           //ripeto il controllo per IE7
           tmp_src = $(el).attr("src");
           
           if(tmp_src.search(strToAddBeforePathImg) >= 0) {
                var p1 = tmp_src.substring(0,tmp_src.search(strToAddBeforePathImg));
                var p2 = tmp_src.substring((tmp_src.search(strToAddBeforePathImg)+strToAddBeforePathImg.length));
                $(el).attr("src", p1+p2);  
           }
       }
    });
}



