document.observe("dom:loaded", function(){
		try{
			if($('carousel_section')!=null){
				window.cauroselSection = new Caurosel({selectors:{item:"#carousel_section ul li",groupDiv:"#carousel_section ul",controlsParent:"#carousel"},groupSize:4,offsetDistance:226,duration:0.5});
			}
			if($('primary_navigation')!=null){
				//window.flyout_menu = new Flayout({selectors:{item:"#primary_navigation ul li a.main_a", flyoutWrapper:"#primary_navigation ul li div"},duration:0.5});
			new Flyouts();
			}
			
			if($$(".HomePage_Template #center_stage li")[0]){
				CHP_hero.init();
			}
			
	}catch(err){
		alert("error! "+err.toString());
	}
});


var Caurosel = Class.create({
	/*
	 * options:
	 *    selectors: {item,groupDiv,controlsParent}
	 *    groupSize
	 *    offsetDistance
	 *    duration
	 *    MODIFICATION: changed from original version so to have 8 promos (2x4) in 2 sets and not having disabled buttons
	 *    MODIFICATION: when buttons are disabled will get back to next / previous set
	 */
		initialize: function(options){
			
			if($$(options.selectors.item)[0] && $$(options.selectors.item).size()>1){
				
				// configuration
				this.modulesDiv = $$(options.selectors.groupDiv)[0];
				this.controlsParent = $$(options.selectors.controlsParent)[0];
				this.groupSize = options.groupSize;
				this.offsetDistance = options.offsetDistance; // pixel value, width of one item + padding / margin
				this.duration = options.duration;
				this.numberOfModules = $$(options.selectors.item).size();
				this.totalModules = new Number(this.numberOfModules);
				this.currentModule=1;
				
				if(this.groupSize>1){
					this.numberOfModules = Math.ceil(this.numberOfModules/this.groupSize);
					this.offsetDistance = parseInt(this.groupSize*this.offsetDistance,10);
				}
				
				this.previousA = new Element("a",{href:""}).update("Previous");
				this.nextA = new Element("a",{href:""}).update("Next");
				var previousLI = new Element("li",{className:"previous_disabled"}).update(this.previousA);
				var nextLI = new Element("li",{className:"next"}).update(this.nextA);
				this.controlsUL = new Element("ul",{className:"controls"})
					.insert({bottom:previousLI})
					.insert({bottom:nextLI})
					.hide();
				
				this.controlsParent.insert({bottom:this.controlsUL});
				if(this.totalModules>this.groupSize){
					this.controlsUL.appear({duration:this.duration});
				}
				
				this.addEvents();
			}
		},
	addEvents: function(){
		this.previousA.observe("click",function(event){
			event.stop();
			if(this.previousA.up("li").hasClassName("previous_disabled")) {
				this.next();
				}
			else{ 
				this.previous();			
				}
			
		}.bind(this));
		
		this.nextA.observe("click",function(event){
			event.stop();
			if(this.nextA.up("li").hasClassName("next_disabled")) {
				this.previous();
				}
			else{ 
				this.next();			
				}
		}.bind(this));
	},
	adjustDisabled: function(){
		if(this.currentModule===1){
			this.previousA.up("li").removeClassName("previous").addClassName("previous_disabled");
		}else{
			this.previousA.up("li").removeClassName("previous_disabled").addClassName("previous");
		}
		if(this.currentModule===this.numberOfModules){
			this.nextA.up("li").removeClassName("next").addClassName("next_disabled");
		}else{
			this.nextA.up("li").removeClassName("next_disabled").addClassName("next");
		}
	},
	moveModules: function(){
		var newMarginLeft = "-"+((this.currentModule-1)*this.offsetDistance)+"px";
		this.modulesDiv.morph('margin-left:'+newMarginLeft+';',{duration:0});
		//this.modulesDiv.morph('margin-left:'+newMarginLeft+';',{duration:this.duration});
	
	},
	previous: function(){
		if(this.currentModule!==1){
			this.currentModule--;
			this.moveModules();
			this.adjustDisabled();
		}
	},
	next: function(){
		if(this.currentModule!==this.numberOfModules){
			this.currentModule++;
			this.moveModules();
			this.adjustDisabled();
		}
	}
});

var Active = {
			activeFlyout:false,
			setActive:function(event){
				//stop(event);
				//Object.toHTML(this);
				this.active= event.nextSiblings('div');
				this.selected= event.up("li");
				if(this.selected){
					if(this.selected.down("div")){
						event.up("li").down("div ol ul li a").stopObserving('mouseover')
						this.selected.addClassName('selected');
						this.selected.down("div").addClassName('active');
						activeID=this.selected.down("div").id;
						// left position for all flyouts is defined in here
						if(activeID=="flyout_1") var leftPositionForThisElement='-5px';
						if(activeID=="flyout_2") var leftPositionForThisElement='-152px';
						if(activeID=="flyout_3") var leftPositionForThisElement='-124px';
						if(activeID=="flyout_4") var leftPositionForThisElement='-23px';
						if(activeID=="flyout_5") var leftPositionForThisElement='-23px';
						if(activeID=="flyout_6") var leftPositionForThisElement='-745px';
						this.selected.down("div").style.left=leftPositionForThisElement;
						this.isActive(this.selected.down("div"));
					}
					
				}
				if(this.active){
						this.active.invoke("observe", "mouseout", function() {Active.removeActive(this.up("li")) });
					}
				else {
					$$("li.main_li a").invoke("observe", "mouseout", function() {Active.removeActive(this) });
					}
				//$(this).down("div").style.left="0px";		
			},
			removeActive:function(event){
					this.active= event.nextSiblings('div');
					this.selected= event;
					if(this.selected.down("div")){
								if(this.selected.hasClassName('selected')){
									this.selected.removeClassName('selected'); 
									if(this.selected.down('div.active')) {
										this.selected.down('div.active').removeClassName('active').style.left='-99999px';
										//$(element).down('div').hide();
									}
								}
					}
			},
			isActive:function(obj){
				if(obj!==null) 	this.activeFlyout = true;
				return this.activeFlyout;
				//$$("li.main_li div").invoke("observe", "mouseout", function() {Active.removeActive(this) });
			}
}

var CHP_hero = {
	duration: 0.9,
	intervalMS: 10000,
	init: function(){
		if($$(".HomePage_Template #center_stage li")[0]){
			this.promos = new Array();
			this.listItems = new Array();
			this.current = 0;
			var linksDiv = new Element("div",{className:"heroNavigation"}).hide();
			var linksDivWrapper = new Element("div",{className:"heroOpenWindow"});
			var linksUL = new Element("ul");
			
			linksDivWrapper.update(linksUL);
			
			$$(".HomePage_Template #center_stage li").each(function(el,i){
				if(el.down("img")){
					this.promos.push(el);
					
					var thisLI = new Element("li");
					var thisLink = new Element("a",{href:""}).update(i);
					//var thisLink = new Element("a",{href:""}).update(el.down("img").clone(true));
					thisLI.update(thisLink);
					
					if(i>0){
						el.addClassName("off");
						el.hide();
						thisLI.addClassName("off");
					}else{
						el.addClassName("sel");
						thisLI.addClassName("sel");
					}
					
					thisLink.observe("click",function(evt){
						evt.stop();
						this.select(i);
					}.bind(this));
					
					linksUL.insert({bottom:thisLI});
					this.listItems.push(thisLI);
				}
			}.bind(this));
			
			$$(".HomePage_Template #center_stage")[0].insert({bottom:linksDiv});
			$$(".HomePage_Template #center_stage .heroNavigation")[0].insert({top:linksDivWrapper})
			linksDiv.appear({duration:this.duration});
			this.slidingControls = new SlidingItems({selectors:{item:"#center_stage .heroNavigation ul li",groupDiv:"#center_stage .heroNavigation ul",controlsParent:"#center_stage"},groupSize:5,offsetDistance:190,duration:0.7});
			
			this.start();
		}
	},
	start: function(){
		this.interval = setInterval("CHP_hero.next()",this.intervalMS);
	},
	stop: function(){
		clearInterval(this.interval);
	},
	select: function(index){
		try{
			var controlModuleGroup = Math.floor(index/4)+1;
			var thisPromo = $(this.promos[index]);
			var thisLI = $(this.listItems[index]);
			if(!thisLI.hasClassName("sel")){
				this.current = index;
				var parallelArray = new Array();
				
				// currently selected must fade
				$$(".HomePage_Template #center_stage>ul li.sel").each(function(sibling){
					parallelArray.push(new Effect.Fade(sibling,{sync:true}));
				}.bind(this));
				// newly selected must be moved left and appear
				parallelArray.push(new Effect.Appear(thisPromo,{sync:true}));
				new Effect.Parallel(parallelArray,{duration:this.duration,
					beforeStart:function(){
					thisPromo.setStyle({"left":"0"});
				},
				afterFinish: function(){
					$$(".HomePage_Template #center_stage>ul li.off").each(function(sibling){
						sibling.setStyle({"left":"-9999px"});
					});
				}
				});
				
				$A(this.promos).invoke("removeClassName","sel").invoke("addClassName","off");
				$A(this.listItems).invoke("removeClassName","sel").invoke("addClassName","off");
				thisPromo.addClassName("sel").removeClassName("off");
				thisLI.addClassName("sel").removeClassName("off");
				
				this.stop();
				this.start();
			}else{
				//eHA.log.create("this hero image is already showing");
			}
		}catch(err){
			//eHA.log.create(err.toString(),eHA.log._ERROR);
		}
	},
	next: function(){
		try{
			var nextIndex = parseInt(this.current,10) + parseInt(1,10);
			if(nextIndex>=this.promos.size()){
				nextIndex = 0;
			}
			//eHA.log.create("showing spotlight index:"+nextIndex);
			this.select(nextIndex);
		}catch(err){
			//eHA.log.create(err.toString(),eHA.log._ERROR);
		}
	}
};

var SlidingItems = Class.create({
	/*
	 * options:
	 *    selectors: {item,groupDiv,controlsParent}
	 *    groupSize
	 *    offsetDistance
	 *    duration
	 */
		initialize: function(options){
			
			if($$(options.selectors.item)[0] && $$(options.selectors.item).size()>1){
				
				// configuration
				this.modulesDiv = $$(options.selectors.groupDiv)[0];
				this.controlsParent = $$(options.selectors.controlsParent)[0];
				this.groupSize = options.groupSize;
				this.offsetDistance = options.offsetDistance; // pixel value, width of one item + padding / margin
				this.duration = options.duration;
				this.numberOfModules = $$(options.selectors.item).size();
				this.totalModules = new Number(this.numberOfModules);
				this.currentModule=1;
				
				if(this.groupSize>1){
					this.numberOfModules = Math.ceil(this.numberOfModules/this.groupSize);
					this.offsetDistance = parseInt(this.groupSize*this.offsetDistance,10);
				}
				
				this.previousA = new Element("a",{href:""}).update("Previous");
				this.nextA = new Element("a",{href:""}).update("Next");
				var previousLI = new Element("li",{className:"previous_disabled"}).update(this.previousA);
				var nextLI = new Element("li",{className:"next"}).update(this.nextA);
				this.controlsUL = new Element("ul",{className:"controls"})
					.insert({bottom:previousLI})
					.insert({bottom:nextLI})
					.hide();
				
				this.controlsParent.insert({bottom:this.controlsUL});
				if(this.totalModules>this.groupSize){
					this.controlsUL.appear({duration:this.duration});
				}
				
				this.addEvents();
			}
		},
	addEvents: function(){
		this.previousA.observe("click",function(event){
			event.stop();
			this.previous();
		}.bind(this));
		
		this.nextA.observe("click",function(event){
			event.stop();
			this.next();
		}.bind(this));
	},
	adjustDisabled: function(){
		if(this.currentModule===1){
			this.previousA.up("li").removeClassName("previous").addClassName("previous_disabled");
		}else{
			this.previousA.up("li").removeClassName("previous_disabled").addClassName("previous");
		}
		if(this.currentModule===this.numberOfModules){
			this.nextA.up("li").removeClassName("next").addClassName("next_disabled");
		}else{
			this.nextA.up("li").removeClassName("next_disabled").addClassName("next");
		}
	},
	moveModules: function(){
		var newMarginLeft = "-"+((this.currentModule-1)*this.offsetDistance)+"px";
		this.modulesDiv.morph('margin-left:'+newMarginLeft+';',{duration:this.duration});
	},
	previous: function(){
		if(this.currentModule!==1){
			this.currentModule--;
			this.moveModules();
			this.adjustDisabled();
		}
	},
	next: function(){
		if(this.currentModule!==this.numberOfModules){
			this.currentModule++;
			this.moveModules();
			this.adjustDisabled();
		}
	}
});



var Flyouts = Class.create({
		// define properties for DELAY
		FlayoutsShowUpSpeed: 0,
		effectDuration: 0,
		hideDelay: 0,
		initialize: function(){
			$$("a.main_a").invoke('observe', 'mousemove', this.onMouseOver_a.bind(this));
			$$("a.main_a").invoke('observe', 'mouseout', this.onMouseOut_a.bind(this));
			$$("a.main_a span").invoke('observe', 'mousemove', this.onMouseOver_a.bind(this));
			$$("a.main_a span").invoke('observe', 'mouseout', this.onMouseOut_a.bind(this));
			$$("#primary_navigation ul li div.flyout").invoke('observe', 'mousemove', this.onMouseOver_flayout.bind(this));
			$$("#primary_navigation ul li div.flyout").invoke('observe', 'mouseout', this.onMouseOut_flayout.bind(this));
		},
		// Appereance: you can choose some effect from scriptaculous if you want to
		showUpFunction: function(e, effectDuration){
				if(!e.visible()){
					e.show()
					//new Effect.Appear(e, { duration: effectDuration });
					//new Effect.BlindDown(e, {duration: effectDuration,queue: { position: 'end',scope: e.identify(),limit: 2}});
				}
		},
		// Disappereance: you can choose some effect from scriptaculous if you want to
		hidingFunction: function(e, effectDuration){
				// this will remove class from selected parent, usefull to have this selection to add more style in CSS
				e.up('li.main_li').removeClassName('selected'); 
				e.removeClassName('active').hide();	
				//new Effect.Fade(e, { duration: effectDuration });
				//new Effect.BlindUp(e, {duration: effectDuration,queue: {position: 'end',scope: e.identify(),limit: 2}});
		},
		// onMouseOut_a event handler
		onMouseOut_a: function(e){
			var playout = e.element().next();
			if (playout && playout.hasClassName('active')){
				this.hidingFunction(playout);
			}
		},
		//onMouseOver_a event handler
		onMouseOver_a: function(e){
			var playout = e.element().next();
			// Additional check if something wrong with menu structure
			if(!playout){
				return;
			}
			if (!playout.hasClassName('active')){
				playout.removeClassName('active');
			}
			if (!playout.hasClassName('active')){
				playout.up('li.main_li').addClassName('selected')
				playout.addClassName('active');
			this.showUpFunction(playout, this.FlayoutsShowUpSpeed);
			}
		},
		onMouseOut_flayout: function(e){
		var playout = e.findElement("div");
			if (playout && playout.hasClassName('active')){
				this.hidingFunction(playout);
				playout.removeClassName('active')
			}
			
		},
		onMouseOver_flayout: function(e){
		var playout = e.findElement("div");
			if (playout && !playout.hasClassName('active')){
				playout.removeClassName('active');
			}
			playout.up('li.main_li').addClassName('selected');
			playout.addClassName('active').show();
		}
});
