/*
Homepage scripts
*/

/*
Variables
*/
var intMouseOutMenuSlideDelay = 250;
var elFlashAnimation;		
var fxMenuSlide;
var fxMenuControlSlide;
var fxMenuIcon;
var fxMenuIconClose;
var objDivMenuWrapper, objDivMenu, objDivLeftNav, objSlideIn, objDivMenuMask, objDivMenuIconWrapper, objDivMenuIcon;
var objDivMenuIconCloseWrapper, objDivMenuIconClose;
var bolPauseVideo=true;
var bolVideoFunctional=true;
var bolSlideInProgress=false;
var bolMenuOpen=false;
var intElPosTop=null, intWindowHeight=null, intContentHeight=null, intMenuHeight=null, intMenuMaxTop=null;
var elImgIcon, elImgIconHover;
elImgIcon=new Asset.image('/imgs/syst/home_ln_closed.png');
elImgIconHover=new Asset.image('/imgs/syst/home_ln_closed_hover.png');


/*
Functions
*/
function initHomePageDomReady(){
	//1) Inject sifr elements
	//replace homepage h1
	var elH1=objPageElements.elbody.getElement('h1.far-homepage_title')
	objFlashProperties={
		src: strUrlHeaderNormalSwf,
		color: '#005AFF',
		width: 740,
		height: 25,
		content:elH1.get('text'),
		fontsize:'21px',
		thickness:1,
		sharpness:-80
	}
	//objFlashProperties.src=strUrlHeaderNormalSwf;
	injectSifrElement(elH1, objFlashProperties)

	//title for the snippets
	var elH2=objPageElements.elbody.getElement('h2.far-homepage_title')
	objFlashProperties={
		src: strUrlHeaderNormalSwf,
		color: '#992c96',
		width: 300,
		height: 26,
		content:elH2.get('text'),
		fontsize:'21px',
		thickness:1,
		sharpness:-80
	}
	injectSifrElement(elH2, objFlashProperties)


	//title for most popular content
	var elTd=objPageElements.elbody.getElement('table.far-homepage_mostpopular .p-top')
	objFlashProperties={
		src: strUrlHeaderNormalSwf,
		color: '#992c96',
		width: 180,
		height: 34,
		content:elTd.get('text'),
		fontsize:'21px',
		thickness:1,
		sharpness:-80
	}
	injectSifrElement(elTd, objFlashProperties)

	//title for most performance highlights
	elTd=objPageElements.elbody.getElement('table#far-homepage_perf_highl .p-top')
	objFlashProperties={
		src: strUrlHeaderNormalSwf,
		color: '#992c96',
		width: 162,
		height: 21,
		content:elTd.get('text'),
		fontsize:'17px',
		thickness:-1,
		sharpness:-80
	}
	injectSifrElement(elTd, objFlashProperties)
	
	/*
	Setup the lightbox effects
	*/
	var elPerformaceHighlightsTable=objPageElements.elbody.getElement('table#far-homepage_perf_highl');
	//0) remove click events on a nodes inside the highlights
	elPerformaceHighlightsTable.getElements("a").each(function(el, index){
		el.addEvent('click', function(event){
			event.preventDefault();
		})	
	});
	
	
	
	
}

function initHomePage(){
	var bolIsIE;
	var objMenuTable;
	
	
	//1) find the flash dom object
	bolIsIE = Browser.Engine.trident;
	elFlashAnimation = (bolIsIE) ? window['homepageanimation_ie'] : document['homepageanimation'];
	
	
	//2) setup the animation effects
	//2a) find the required dom objects
	objMenuTable=$('p-table-left');	
	objDivMenuWrapper=$('far-menuwrapper');
	objDivMenu=$('far-menu');
	objDivLeftNav=$('p-leftnav');
	objSlideIn=$('slideIn');
	objDivMenuMask=$('far-menu_mask');
	objDivMenuIconWrapper=objDivMenuWrapper.getElement('div.far-openleftnav_wrapper');
	objDivMenuIcon=objDivMenuWrapper.getElement('div.far-openleftnav_icon');
	objDivMenuIconCloseWrapper=objDivMenuWrapper.getElement('div.far-closeleftnav_wrapper');
	objDivMenuIconClose=objDivMenuWrapper.getElement('div.far-closeleftnav_icon');
	
	//prevent the link from being clicked
	objSlideIn.addEvent('click', function(event){
		event.preventDefault();
	});

	
	//alert($defined(objDivMenuWrapper)+" - "+$defined(objSlideIn)+" - "+$defined(objMenuTable)+" - "+$defined(objDivLeftNav)+" - "+$defined(objDivMenu))
	
	if($defined(objDivMenuWrapper)==false || $defined(objSlideIn)==false || $defined(objMenuTable)==false || $defined(objDivLeftNav)==false || $defined(objDivMenu)==false ){
		//not all objects can be found - retry
		initHomePage.delay(700);
		return false;		
	}else{
	
		//correct the height of the layers so that the menu always fits
		var intHeight=objMenuTable.getSize().y;
		
		if(intHeight>430){
			objDivMenuWrapper.setStyle('height', intHeight+300);
			objDivMenu.setStyle('height', intHeight);
			objDivLeftNav.setStyle('height', intHeight);
		}
		
		//setup the effects
		//a) the menu
		fxMenuSlide = new Fx.Tween('far-menu',{
			property:'left',
			duration:600, 
			transition:Fx.Transitions.Sine.easeInOut,
			onStart: function(){
				//move the mask over the menu to prevent lyouts from being shown
				objDivMenuMask.setStyle('visibility', 'visible');			
			},
			onComplete: function(el){
				//hide the mask
				objDivMenuMask.setStyle('visibility', 'hidden');				
				var intStylePosLeft=el.getStyle('left').toInt();
				if(intStylePosLeft < -180){
					//menu was hidden
					if(bolPauseVideo)callFlashPlayVideo();
					objDivMenuWrapper.setStyle('width', '37px');
				}
				bolSlideInProgress=false;			
			}
		});
		//b) the arrows
		fxMenuControlSlide = new Fx.Tween('far-openleftnav', {
			property:'left',
			duration:200, 
			transition:Fx.Transitions.Sine.easeInOut
		});
		
	
		
		//3) start the stream (which in turn starts the menu animation)
		callFlashStartStream();
		//menuPreview()
	
		menuIconSetup();
		
	}
	
	statsSetupHomepage();
}

/*
Statistics
*/
function statsSetupHomepage(){
	var arrLinks;
	
	//more links in the slots
	arrLinks=objPageElements.elmaincolumn.getElements("table#p-slots td.p-content a");
	arrLinks.each(function(elLink){
		statsLinkSetEvent(elLink);		
	});
	
	//most popular
	arrLinks=objPageElements.elmaincolumn.getElements("table.far-homepage_mostpopular ul.p-link-list a");
	arrLinks.each(function(elLink){
		statsLinkSetEvent(elLink);		
	});
	
	//right column bottom links
	arrLinks=objPageElements.elmaincolumn.getElements("table#far-homepage_perf_highl ul.p-link-list a");
	arrLinks.each(function(elLink){
		statsLinkSetEvent(elLink);		
	});	

	
}



/*
Menu icon behavior
*/
function menuIconSetup(){

	fxMenuIcon = new Fx.Tween(objDivMenuIcon, {
		duration: 250,
		transition: Fx.Transitions.Cubic.easeInOut,
		onComplete: function(){

			var strPosition=objDivMenuIcon.getStyle('left');
			if(strPosition.contains('-') && bolMenuOpen){
				//the icon is hidden - now also hide the wrapper
				objDivMenuIconWrapper.setStyle('visibility', 'hidden');
				objDivMenuIcon.setStyle('left', -8);
			}else{
				
				
			}
		}
	});
		
	fxMenuIconClose = new Fx.Tween(objDivMenuIconClose, {
		duration: 250,
		transition: Fx.Transitions.Cubic.easeInOut,
		onComplete: function(){

			var strPosition=objDivMenuIconClose.getStyle('left');
			if(strPosition.contains('-') && !bolMenuOpen){
				//the icon is hidden - now also hide the wrapper
				objDivMenuIconCloseWrapper.setStyle('visibility', 'hidden');
				objDivMenuIconClose.setStyle('left', -8);
			}else{
				
				
			}
		}
	});		
	
	//open menu icon events
	objDivMenuIconWrapper.addEvents({
		mouseover: function(){
			if(!bolSlideInProgress){
				fxMenuIcon.cancel();
				fxMenuIcon.start('left', -5);
				objDivMenuIcon.addClass('hover');
			}	
		},
		mouseout: function(){
			if(!bolSlideInProgress){
				fxMenuIcon.cancel();
				fxMenuIcon.start('left', -8);
				objDivMenuIcon.removeClass('hover');
			}
		},
		click: function(event){
			//start showing the menu
			if(!bolSlideInProgress){
				statsStorePageEvent({eventname: 'click', value1: 'navopen'})
				bolSlideInProgress=true;
				fxMenuIcon.cancel();
				fxMenuIcon.start('left', -8);
								
				farSlideMenuIn();
								
				(function(){
					objDivMenuIconWrapper.setStyle('visibility', 'hidden');					
				}).delay(250);
				
				(function(){
					objDivMenuIconClose.setStyle("left", -30);
					objDivMenuIconCloseWrapper.setStyle('visibility', 'visible');
					fxMenuIconClose.start('left', -8);
					bolMenuOpen=true;
				}).delay(700);				
				
			}
		}
	});
	
	//stick in this position
	//objDivMenuWrapper.pin();
	//new simulateFixed('far-menuwrapper');
	
	//close menu icon events
	objDivMenuIconCloseWrapper.addEvents({
		mouseover: function(){
			//this.addClass('hover');			
			if(!bolSlideInProgress){
				fxMenuIconClose.cancel();
				fxMenuIconClose.start('left', -8);
			}			
		},
		mouseout: function(){
			//this.removeClass('hover');
			if(!bolSlideInProgress){
				fxMenuIconClose.cancel();
				fxMenuIconClose.start('left', -8);
			}
		},
		click: function(event){
			//start hiding the menu
			if(!bolSlideInProgress){
				statsStorePageEvent({eventname: 'click', value1: 'navclose'})
				bolSlideInProgress=true;
				
				fxMenuIconClose.start('left', -30);
							
				farSlideMenuOut.delay(intMouseOutMenuSlideDelay, this);
				
				(function(){
					objDivMenuIconCloseWrapper.setStyle('visibility', 'hidden');
					objDivMenuIcon.removeClass('hover');			
				}).delay(250);
				
				(function(){
					bolMenuOpen=false;
					objDivMenuIcon.setStyle("left", -30);
					objDivMenuIconWrapper.setStyle('visibility', 'visible');
					fxMenuIcon.start('left', -8);	
					setMenuPositionY();					
				}).delay(700);				
			
			} 

		}
	});	

}



window.addEvent('scroll',function(){
	setMenuPositionY();
	
});

function setMenuPositionY(){
	//sets the menu to the new height position
	var intSpace=100;
	
	
	if($defined(objDivMenuWrapper)){
		if(!bolMenuOpen){
			if(intElPosTop==null)intElPosTop=objDivMenuWrapper.getPosition().y;
			if(intContentHeight==null)intContentHeight=$(window).getScrollSize().y
			if(intMenuHeight==null){
				intMenuHeight=objDivMenuWrapper.getSize().y-300;
				intMenuMaxTop=intContentHeight-150;
			}
	
			var intWindowScroll=Math.ceil(window.getScroll().y);
			if(intWindowScroll>=intSpace){
				if(intWindowScroll>=intMenuMaxTop){
					objDivMenuWrapper.setStyle('top', intMenuMaxTop);
				}else{
					objDivMenuWrapper.setStyle('top', intElPosTop+intWindowScroll-intSpace);
				}
			}else{
				objDivMenuWrapper.setStyle('top', intElPosTop);
			}
		}	
	}
}


/*
Menu slide controls
*/
function farSlideMenuIn(){
	farSlideMenu('in')
};
function farSlideMenuOut(){
	farSlideMenu('out')
};
function farSlideMenu(mode) {

  var intDivMenuLeft = parseInt(objDivMenu.getStyle('left'));
  var intDivMenuWidth = parseInt(objDivMenu.getStyle('width'));
  
  var arrArgs=farSlideMenu.arguments
  var intArgs=arrArgs.length;  
  if(intArgs==2){
  	bolPauseVideo=arrArgs[1];
  }
      
  if (mode == 'in') {  	
  	if(intDivMenuLeft<-180){
  		if(bolPauseVideo)callFlashPauseVideo();
  		objDivMenuWrapper.setStyle('width', 700);
  	}
    if (intDivMenuLeft < 10) {
      fxMenuSlide.cancel().start(0);
      fxMenuControlSlide.cancel().start(-37);
     }
  } else if (mode == 'out') {
      if ((intDivMenuLeft + intDivMenuWidth) > 5) {      	
        fxMenuSlide.cancel().start(0 - (intDivMenuWidth+2));
        fxMenuControlSlide.cancel().start.delay(500,fxMenuControlSlide,0); 			
      }
  }    
}

function menuPreview(){
	/* the preview menu effect when the homepage loads */
	farSlideMenuIn.delay(100);	
	farSlideMenuOut.delay(1400);	
}


/*
Communication with the flash
*/
function callFlashPauseVideo() {
	if(bolVideoFunctional && $defined(elFlashAnimation))elFlashAnimation.pauseVideo();
}
function callFlashPlayVideo() {
	if(bolVideoFunctional && $defined(elFlashAnimation))elFlashAnimation.resumeVideo();
}
function callFlashStartStream(){
	//if($defined(elFlashAnimation) && bolVideoFunctional)elFlashAnimation.startStream();	
}


