var cache = [];      // cache preloading immagini
var counter=0;       // image roller counter
var delayBG=10000;   // 10 secondi
var start_h=$(window).height(); // calcola l'altezza iniziale dlel finestra per capire come deve slidare il menu laterale successivamente al resize
var firstAccess=0; // variabile del primo accesso per la comparsa del menu laterale senza sovrappore al logo
var firstOpenArchivio=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenGallery=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenMenu1=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenNews=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenSpeciali=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenDouble=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenPress=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news
var firstOpenPartners=true; // impostazione per l'apertura del primo livello dei contenuti: archivio/2/titolo-news

var local_path=''; // for localhost -> '' for web

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})(jQuery);



function layerDouble(url){
   
    $('#content').animate({width: "600px"},1000,'swing',function(){
                            $.ajax({
                                type: "GET",
                                url: "inizio.inc.php",
                                data:{},
                                async: false,
                                success: function(msg)
                                {
                                     $('#content > div').html(msg).show(200);
                                },
                                error: function(){
                                     alert('Server Request Error');
                                }
                            });
    })
}

function layerOne(url){
   
    $('#content').animate({width: "300px"},1000,'swing',function(){
                            $.ajax({
                                type: "GET",
                                url: local_path+''+url+".inc.php",
                                data:{},
                                async: false,
                                success: function(msg)
                                {
                                     $('#content > div').html(msg).show(200);
                                },
                                error: function(){
                                     alert('Server Request Error');
                                }
                            });
    })
}

function layerTwo(url,p){
       var panel2_w= $('#content-third').width();
       if (panel2_w>0){
            $('#content-third > div').hide();
            $('#content-third').animate({width: "0px"},1000,'swing')
       }
       $('#content-third').animate({width: "300px"},1000,'swing',function(){

                            $.ajax({
                                type: "POST",
                                url: local_path+''+url+".inc.php",
                                data:p,
                                async: false,
                                success: function(msg)
                                {
                                     $('#content-third > div').html(msg).show(200);
                                },
                                error: function(){
                                     alert('Server Request Error');
                                }
                            });
    })
}


/* funzione che attiva il cambio di background */
function change_bg(){
	
      var iArray=new Array('1.jpg','2.jpg','3.jpg','4.jpg');
	  if(firstAccess!=0){
      counter==iArray.length-1 ? counter=0 : counter+=1;
	  }
      $('#body-background>img').hide(0,function(){
         
          $(this).attr('src','images/1600x1200/'+iArray[counter]).show()
      });
    
	
}


function adjust_menu(){
      /* spostamento del menu laterale al resize della finestra del browser */
   $(window).resize(function() {


        delay(function(){
                  var window_h = $(this).height();

                  var menu_h = (Math.floor(window_h/3.5)*2)-40

                  var ul_h=$('#menu ul').height();

                  var new_menu_pos=(Math.floor(menu_h-ul_h))-20;

                  if (start_h>window_h){

                      /* riduco schermo */
                      if (new_menu_pos<120){
                          new_menu_pos=160
                      }


                        $('#menu ul').animate({top:new_menu_pos+'px'},'fast','swing');

                        $('#menu').animate({height:menu_h+'px'},'slow','swing');


                  }else{
                      if (new_menu_pos<120){
                          new_menu_pos=160
                      }
                      /* Espando schermo */

                        $('#menu ul').animate({top:new_menu_pos+'px'},'slow','swing');

                        $('#menu').animate({height:menu_h+'px'},'fast','swing',function(){
                                if (firstAccess==0){
                                     firstAccess=1;
                                     $('#menu ul').show();
                                }

                        });

                  }

                  start_h=window_h;

            }, 1000);
    });
}


function closeAllContent(){
    var panel1_w= $('#content').width();
    var panel2_w= $('#content-third').width();
    

    if ((panel1_w>0)){
        $('#content > div').hide(200);
        $('#content').animate({width: "0px"},1000,'swing',function(){        
		   change_bg();
        });
        
    }

    if (panel2_w>0){
        $('#content-third > div').hide(200);
        $('#content-third').animate({width: "0px"},1000,'swing');     
    }
}


/* *************************************  INIZIO PROCEDURE DI PAGINA ***********************************************/


$(document).ready(function() {
	
	$('#body-background').click(function(){
		closeAllContent();
		$('a.linkmenu').css('color','#fff');
		$.address.value('#');
		firstOpenArchivio=true;
        firstOpenGallery=true;
        firstOpenMenu1=true;
		firstOpenNews=true;
		firstOpenSpeciali=true;
		firstOpenDouble=true;
	});

    $('#social').animate({top:'-50px'},'slow');

    $('#social').hover(function(){
        $(this).stop().animate({top:'0px'},'slow');
    },function(){
         $(this).stop().animate({top:'-50px'},'slow');
    })

    /* preloading contenuti su chiamata ajax */
    $(document).ajaxStart(function() {
      $('#loading').fadeIn('slow');
      
    }).ajaxStop(function() {
      
      $('#loading').fadeOut('slow');
    });
    

    /* funzione precaricamento immagini */
     $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
          var cacheImage = document.createElement('img');
          cacheImage.src = arguments[i];
          cache.push(cacheImage);
        }
    }

   /* Array immagini di background */
   $.preLoadImages("images/1600x1200/1.jpg", "images/1600x1200/2.jpg", "images/1600x1200/3.jpg","images/1600x1200/4.jpg","images/loading.gif");
   
   /* Intervallo per il cambio di immagini di background*/
   /* setInterval("change_bg()", delayBG); */
      
   /* Auto resize immagini di background */
   $("#body-background").ezBgResize();
   
   /* Aggiusta la posizione del menu */
   adjust_menu();

   /* stato iniziale del menu laterale */
   $('#menu ul').hide();

   /* stato inizile del resize trigger alcaricamento*/
   $(window).trigger("resize");
   
   /* Roll over link laterali del menu con effetto fade */
       $('a.linkmenu').hover(function(){
           
              $(this).stop().animate({color:'#b08f59'},'fast','linear');
        },function(e){
            if (!$(this).hasClass('menu-selected')){
                $(this).stop().animate({color:'#fff'},'fast','linear')
            }
        })
       
       $('a.linkmenu').bind("click",function(e){
          
          $('a.linkmenu').each(function(msg){
              $(this).removeClass('menu-selected');
              if (!$(this).hasClass('menu-selected')){
                  $(this).css('color','#fff');
              }
           })
          $(this).addClass('menu-selected');
           $(this).css('color','#b08f59');
       })


      

    /* Roll over link interni nelle sezioni */

        $('a.linkinterni').live("mouseover",function(){
            $(this).stop().animate({color:'#b08f59'},'fast','linear');
        })

        $('a.linkinterni').live("mouseout",function(){
            $(this).stop().animate({color:'#fff'},'fast','linear');
        })
        
        $('a.linkinternisett').live("mouseover",function(){
            $(this).children().find('.desc-label').stop().animate({color:'#fff'},'fast','linear');
            $(this).children().find('.jp-label').stop().animate({color:'#b08f59'},'fast','linear');
            $(this).children().find('.it-label').stop().animate({color:'#b08f59'},'fast','linear');
        })

        $('a.linkinternisett').live("mouseout",function(){
            $(this).children().find('.desc-label').stop().animate({color:'#b08f59'},'fast','linear');
            $(this).children().find('.jp-label').stop().animate({color:'#fff'},'fast','linear');
            $(this).children().find('.it-label').stop().animate({color:'#fff'},'fast','linear');
        })
   
   /* swf address per deep linking */
      $.address.init(function(event) {

                //$('a[href="'+event.value+'"]').trigger("click");

      }).change(function(event) {

                /* Ogni volta che si verifica un evento di click chiudi tutti i pannelli */
                $('a').blur();
                //change_bg();
                if (event.value=='/'){
                    var titolo= '11clubroom - Eleven Club Milano';
                }else{
                    var titolo= '11clubroom - Eleven Club Milano - '+$('a[href="'+event.value+'"]').attr("title");
                }
                
                $.address.title(titolo);
                var tmp=event.value.split('/');
                var direzione='/'+tmp[1];
                switch(direzione){
                     case '/':
                         
                         
                        if (firstOpenDouble){
                            $.get('/check-home.php',function(stato){
                                if (stato==1){
                                    closeAllContent();
                                    layerDouble(direzione);
                                }
                            });
                            
                            firstOpenDouble=false;

                        }
                        var cat_id=parseInt(tmp[2],10);
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
						firstOpenNews=true;
						firstOpenSpeciali=true;
						firstOpenMenu1=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
					break;
                    case '/menu1':
                        if (firstOpenMenu1){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenMenu1=false;

                        }
                        var cat_id=parseInt(tmp[2],10);
                        if (cat_id>0){
                            layerTwo('/dettaglio-menu1','id='+cat_id);
                        }
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
						firstOpenNews=true;
						firstOpenSpeciali=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                    break;
		    case '/press':
                        if (firstOpenPress){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenPress=false;

                        }
                        var cat_id=parseInt(tmp[2],10);
                        if (cat_id>0){
                            layerTwo('/dettaglio-press','id='+cat_id);
                        }
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
                        firstOpenNews=true;
			firstOpenSpeciali=true;
			firstOpenDouble=true;
			firstOpenMenu1=true;
                        firstOpenPartners=true;
                    break;
                    
                    case '/partners':
                        if (firstOpenPartners){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenPartners=false;

                        }
                        var cat_id=parseInt(tmp[2],10);
                        if (cat_id>0){
                            layerTwo('/dettaglio-partners','id='+cat_id);
                        }
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
                        firstOpenNews=true;
			firstOpenSpeciali=true;
			firstOpenDouble=true;
			firstOpenMenu1=true;
                        firstOpenPress=true;
                    break;
                    
                    case '/menu3':
                    case '/map':
                    case '/reservation':
                    case '/about':
		 
                    case '/contacts':
                        closeAllContent();
                        layerOne(direzione);
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
                        firstOpenMenu1=true;
			firstOpenNews=true;
			firstOpenSpeciali=true;
			firstOpenDouble=true;
			firstOpenPress=true;
                        firstOpenPartners=true;
                    break;
                    case '/gallery':
                         if (firstOpenGallery){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenGallery=false;

                        }

                        /* processo l'apertura della gallery */

                        var gallery_id=parseInt(tmp[2],10);
                        if (gallery_id>0){
                            layerTwo('/dettaglio-gallery','id='+gallery_id);
                        }
                        firstOpenArchivio=true;
                        firstOpenMenu1=true;
						firstOpenNews=true;
						firstOpenSpeciali=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                    break;
                    case '/archivio':
                        if (firstOpenArchivio){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenArchivio=false;

                        }

                        /* processo l'apertura della news */

                        var news_id=parseInt(tmp[2],10);
                        if (news_id>0){
                            layerTwo('/dettaglio-news','id='+news_id);
                        }
                        firstOpenGallery=true;
                        firstOpenMenu1=true;
						firstOpenNews=true;
						firstOpenSpeciali=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                    break;
					case '/speciali':
                        if (firstOpenSpeciali){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenSpeciali=false;

                        }

                        /* processo l'apertura della news */

                        var news_id=parseInt(tmp[2],10);
                        if (news_id>0){
                            layerTwo('/dettaglio-speciali','id='+news_id);
                        }
                        firstOpenGallery=true;
                        firstOpenMenu1=true;
						firstOpenNews=true;
						firstOpenArchivio=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                    break;
					case '/news':
                        if (firstOpenNews){
                            closeAllContent();
                            layerOne(direzione);
                            firstOpenNews=false;

                        }
                        var cat_id=parseInt(tmp[2],10);
                        if (cat_id>0){
                            layerTwo('/dettaglio-news','id='+cat_id);
                        }
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
						firstOpenMenu1=true;
						firstOpenSpeciali=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                    break;
                    default:
                        closeAllContent();
						$('#loading').fadeOut('slow');
                        //layerOne('/about');
                        firstOpenArchivio=true;
                        firstOpenGallery=true;
                        firstOpenMenu1=true;
						firstOpenNews=true;
						firstOpenSpeciali=true;
						firstOpenDouble=true;
						firstOpenPress=true;
                                                firstOpenPartners=true;
                }

  
      })
      
}); 
