/*
bg.js: the javascript framework

By Guillaume Molter for B+G & Partners SA

Some rights reserved.
*/

/* ========= vars for sliders management increment =========== */
//main slider
var totIncrement = 0;
var increment = 900;
//thumbnail slider
var totIncrement2 = 0;
var increment2 = 131;
//keeping track of the position in slider
var step = 1;
var steps = 21;

/* ========= handle logo opacity in thumbnail slider =========== */
function setOpacity(){
	$$('#list2 li').each(function(item){
	    	item.setStyle('opacity','0.2');
		});
	if(step!=1){
		$$('#list2 li')[(step-2)].setStyle('opacity','1');
	}
}

/* ========= handle click on logo and position the slider =========== */
function moveSlider(moveTo){
	var fx = new Fx.Tween($('list'), {duration: 1000, transition: 'back:in:out'});
	var fxb = new Fx.Tween($('list2'), {duration: 1000, transition: 'back:in:out'});
	//Thumbnails > forward
	if(moveTo>6 && moveTo<(steps-1)) {
        totIncrement2=-(moveTo-6)*increment2;
		fxb.start('margin-left', totIncrement2);
    }
   	//Thumbnails > backward
    else if(moveTo<step && step>10){
    	totIncrement2=-(moveTo-8)*increment2;
    	if(totIncrement2>0){
    		totIncrement2=0;
    	}
    	
		fxb.start('margin-left', totIncrement2);
    }
	//Main stage > forward and backward
	if(moveTo!=step){
		totIncrement=-(moveTo-1)*increment;
		fx.start('margin-left', totIncrement);
		step=moveTo;
		setOpacity();
	}
}

window.addEvent('domready', function(){
  

/* ========= Sliders Management =========== */
	// FX var for main stage slider
	var fx = new Fx.Tween($('list'), {duration: 1000, transition: 'back:in:out'});
	var fx2 = new Fx.Tween($('list'), {duration: 1000, transition: 'back:in:out'});
	// FX var for thumbnail slider
	var fxb = new Fx.Tween($('list2'), {duration: 1000, transition: 'back:in:out'});
	var fxb2 = new Fx.Tween($('list2'), {duration: 1000, transition: 'back:in:out'});
	
	//keeping track if welcome box should be reopen after closing contact box
	var reOpenWelcome= false;
	
	//reset sliders when click on logo
	$('logo').addEvent('click', function() {
     	 //here we simulate that we arrived at the end of the slideshow
     	 step=steps;
     	 $('next').fireEvent('click');
     });

	//handle previous button click
    $('previous').addEvent('click', function() {
    	fx.cancel();
        //if we are not at the fullscreen GMap Step
        if(step<(steps-1)) {
       		//main stage
	        if(step!=1) {
	            totIncrement += increment;
	            step--;
	            fx.start('margin-left', totIncrement);
	        }
	        //thumbnails
	        if(step>=6 && step<18) {
	            totIncrement2 += increment2;
	            fxb.start('margin-left', totIncrement2);
	        }
	        //display or hide boxes
	        if(step==1 && $('boxContact').getStyle('visibility')=="hidden" && $('boxSocial').getStyle('visibility')=="hidden") {
	            $('boxWelcome').setStyle('display','block').fade('in');
	        }
        }
        //if we are at the last step we should rewind the slideshow
        else {
        	totIncrement=totIncrement2=0;
			//if the welcome box is hidden we want to show it again & hide contact boxes
			document.id('boxContact').fade('out').setStyle('display','none');
			document.id('boxSocial').fade('out').setStyle('display','none');
			document.id('boxAmin').fade('out').setStyle('display','none');
			document.id('boxWelcome').setStyle('display','block').fade('in');
			//main stage
            fx2.start('margin-left', totIncrement);
            //thumbnails
            fxb2.start('margin-left', totIncrement2);
            
            step=1;
        }
        //handle opacity for thumbnails
        setOpacity();
    });
    
    //handle next button click
    $('next').addEvent('click', function() {
    	fx.cancel();
        //if we are not at the last step
        if(step<=(steps-2)) {
        	//main stage
            totIncrement -= increment;                   
            fx.start('margin-left', totIncrement);
            //thumbnails
            if(step>5 && step<18) {
            	totIncrement2 -= increment2;
            	fxb.start('margin-left', totIncrement2);
            }
            step++;
        }
        //if we are at the last step we should rewind the slideshow
        else {
        	totIncrement=totIncrement2=0;
			//if the welcome box is hidden we want to show it again & hide contact boxes
			document.id('boxContact').fade('out').setStyle('display','none');
			document.id('boxSocial').fade('out').setStyle('display','none');
			document.id('boxAmin').fade('out').setStyle('display','none');
			document.id('boxWelcome').setStyle('display','block').fade('in');
			//main stage
            fx2.start('margin-left', totIncrement);
            //thumbnails
            fxb2.start('margin-left', totIncrement2);
            
            step=1;
        }
        //handle opacity for thumbnails
        setOpacity();
    });
    
    //Attach click to logos
	$$('#list2 li').each(function(item){
		item.addEvent('click', function() {
     		moveSlider((item.getAttribute('rel')).toInt()+1);
    	});
	});

/* ========= Box management =========== */
    // close the welcome button
    $('closeBoxWelcome').addEvent('click', function() {
    	$('boxWelcome').fade('out').setStyle('display','none');
    });
    
    // open contact box 1
    $('openBoxContact').addEvent('click', function() {
     	 if(step==1 && $('boxWelcome').getStyle('visibility')=="visible"){
     	 	$('boxWelcome').fade('out').setStyle('display','none');
     	 	//welcome box need to be repoen on contact box close
     	 	reOpenWelcome = true;
     	 }
     	 $('boxSocial').fade('out').setStyle('display','none');
     	 $('boxContact').setStyle('display','block').fade('in');
    });
    
    // open contact box 1 from welcome box
    $('closeBoxContact').addEvent('click', function() {
    	$('boxContact').fade('out').setStyle('display','none');
    	if(reOpenWelcome == true) {
    		$('boxWelcome').setStyle('display','block').fade('in');
    		reOpenWelcome=false;
    	}
    });
    
    // open social box 1
    $('openBoxSocial').addEvent('click', function() {
     	 if(step==1 && $('boxWelcome').getStyle('visibility')=="visible"){
     	 	$('boxWelcome').fade('out').setStyle('display','none');
     	 	//welcome box need to be repoen on contact box close
     	 	reOpenWelcome = true;
     	 }
     	 $('boxContact').fade('out').setStyle('display','none');
     	 $('boxSocial').fade('in').setStyle('display','block');
    });
    
    // open contact box 1 from welcome box
    $('closeBoxSocial').addEvent('click', function() {
    	$('boxSocial').fade('out').setStyle('display','none');
    	if(reOpenWelcome == true) {
    		$('boxWelcome').setStyle('display','block').fade('in');
    		reOpenWelcome=false;
    	}
    });
    
    // open contact box 1 from main stage
    $('openContactBox2').addEvent('click', function() {
     	 $('boxWelcome').fade('out').setStyle('display','none');
     	 $('boxContact').setStyle('display','block').fade('in');
    });
    
    $('openSocialBox2').addEvent('click', function() {
     	 $('boxContact').fade('out').setStyle('display','none');
     	 $('boxSocial').setStyle('display','block').fade('in');
    });
    
    // open contact box 2 (Gmap) from contactBox 1
    $('fullMap').addEvent('click', function() {
    	//main stage
            totIncrement = (steps-1)*increment*-1;                   
            fx.start('margin-left', totIncrement);
            //thumbnails
            
            totIncrement2 = 0;
            fxb.start('margin-left', totIncrement2);

            step=(steps-1);
            $('boxContact').fade('out').setStyle('display','none');

    });
    
    // open Amin Page
    $('aminButton').addEvent('click', function() {
    	//main stage
            totIncrement = (steps)*increment*-1;                   
            fx.start('margin-left', totIncrement);
            //thumbnails
            
            totIncrement2 = 0;
            fxb.start('margin-left', totIncrement2);

            step=(steps);
    });
    
    // open AMIN Box
    $('openBoxAmin').addEvent('click', function() {
     	 $('boxSocial').fade('out').setStyle('display','none');
     	 $('boxContact').fade('out').setStyle('display','none');
     	 $('boxAmin').setStyle('display','block').fade('in');
    });
    
    // close Amin Box
    $('closeBoxAmin').addEvent('click', function() {
    	$('boxAmin').fade('out').setStyle('display','none');
    });
    
    /* Mobile devices hacks */
       if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))|| (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/android/i))) {
      		document.id('rollOverMask').destroy();
   		}
   		else{
   			gapi.plusone.go();
   			
   			var myTips1 = new Tips('#openBoxSocial',{fixed:'true',offset:{'x':-10, 'y': -25}});
			var myTips2 = new Tips('#openBoxContact',{fixed:'true',offset:{'x':-10, 'y': -25}});
			var myTips3 = new Tips('#enlargeMap',{fixed:'true',offset:{'x':-10, 'y': -25},text:''});
			var myTips4 = new Tips('#getDirections',{fixed:'true',offset:{'x':-10, 'y': -25},text:''});
			var myTips4 = new Tips('#aminButton',{fixed:'true',offset:{'x':-6, 'y': -25},text:''});
   		}
   		

    

/* ========= Google map management =========== */
	var map = new GMap2($("mapData"));
    
    var centerBG = new GLatLng(46.4331,6.9145); 

    map.setCenter(centerBG, 14);
    
    map.enableScrollWheelZoom();

    var bgIcon = new GIcon(G_DEFAULT_ICON);
    
    bgIcon.image = "img/pinMap.png";
    bgIcon.iconSize = new GSize(59, 33);
    bgIcon.shadow = null; 

    markerOptions = { icon:bgIcon };
    marker = new google.maps.Marker(centerBG,markerOptions);

    map.addOverlay(marker);

});

/* ========= Google analytics =========== */
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-16147619-16']);
	_gaq.push(['_trackPageview']);
	
	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
