/**
 * dependcies:
 * mootools v1.11 (http://mootools.net)
 * Author: Ritetek Enterprises, info@ritetek.com
 */
 
// IE doesn't support console.log
if (typeof console == "undefined") {
	var console = {
		log : function(){}
	};
}

// set Energy and Environment namespace if not defined already
if (typeof EE == "undefined") {
	var EE = {};
}

EE.winLoaded = false;
EE.domReady = false;
EE.section = null;
 
 /* ------------------------------------------------------------------------------------------------
 * Page initialization
 * ------------------------------------------------------------------------------------------------
 */


window.addEvent('domready',function() {
	EE.domReady = true;
	EE.section = document.body.id;
	if($defined($('emailSignUp'))) {
		EE.initEmail();
	}
});

 /* ------------------------------------------------------------------------------------------------
 * Home Page Services Scroller
 * ------------------------------------------------------------------------------------------------
 */

window.addEvent('load',function() {
	EE.winLoaded = true;
	
	if (EE.section == "services"){
		EE.HomeIntro.init(5);
		EE.HomeIntro.addEvent("onFinishIntro",EE.HomeCarousel.init.bind(EE.HomeCarousel));
		
		
		$('contentSliderWindow').addEvent('mouseover', function() {
			$clear(EE.HomeIntro.animation);
			$('intro').remove();
			EE.HomeIntro.fireEvent("onFinishIntro");
			this.removeEvents();
		});
		
	}
});

EE.HomeCarousel = {
	activeNum : 0,
	init : function(){
		this.triggers = $$("#mainMenu a");
		this.triggers[0].addClass("hover");
		this.triggers.each(function(lnk,i){
			lnk.addEvent("mouseenter",this.scrollTo.bind(this,i));
		}.bind(this));
		this.scrollEffect = new Fx.Scroll($("sectionCarousel"), {duration:800,wait:false});
		this.scrollEffect.scrollTo(0);
		//window.setTimeout("$('intro').remove();",EE.HomeIntro.fadeDuration);
	},
	
	scrollTo : function(i){
		this.triggers[this.activeNum].removeClass("hover");
		this.scrollEffect.scrollTo(490*i);
		this.triggers[i].addClass("hover");
		this.activeNum = i;
	}
}

EE.HomeIntro = {
	delay : 2000,
	fadeDuration : 800,
	notStarted : false,
	init : function(slideNum){
		this.slideNum = slideNum;
		this.activeNum = 0;
		this.slides = $$("#intro li");
		this.animation = this.fadeOutSlide.periodical(this.delay,this);
	},
	
	fadeOutSlide : function(){
		//alert(this.activeNum);
		var slide = this.slides[this.activeNum];
		var nextSlide = this.slides[(this.activeNum + 1 >= this.slideNum ? 0 : this.activeNum+1)] || null;
		if (nextSlide){
			nextSlide.setStyle("display","block");
			nextSlide.setOpacity(1);
		}
		var fadeOut = new Fx.Style(slide,"opacity",{duration:this.fadeDuration,onComplete:function(){
			slide.setStyle("display","none");
		}});
		fadeOut.start(0);
		this.activeNum++;
		if (this.activeNum>=this.slideNum){
			this.activeNum = 0;
		}
	}
}
$extend(EE.HomeIntro,new Events);

function initNewsTicker() {
	var url = 'includes/outputTickerNews.php';
	$('newsBar').setHTML('Loading News...');
	new Ajax(url, {
	  method: 'get',
	  onComplete: function(response) {
		$('newsBar').setHTML(response);
		EE.ticker = new Ticker('EE.ticker', 'newsBar', 1, 35);
		EE.ticker.start();
	  }
	}).request();
}

 /* ------------------------------------------------------------------------------------------------
 * Services Accordian
 * ------------------------------------------------------------------------------------------------
 */
 
 window.addEvent('domready', function(){
	var accordion = new Accordion('h3.atStart', 'div.atStart', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#ff3300');
		},
				
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#222');
		}
	}, $('accordion'));
	
	if(EE.section == 'home') {
		EE.homeMenuInit();
	}
	EE.initNavigation();
}); 
 
  /* ------------------------------------------------------------------------------------------------
 * Home Menu
 * ------------------------------------------------------------------------------------------------
 */

EE.homeMenuInit=function(){
	var szNormal = 210, szSmall  = 160, szFull   = 310;
	 
	var kwicks = $$("#homeMenu .kwick");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			fx.start(o);
		});
		kwick.addEvent("click", function(event) {
			document.location.href = this.getFirst().getFirst().href;
		});
	});
	 
	$("homeMenu").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
		fx.start(o);
	})
}

EE.initEmail = function() {
	$('emailSignUp').addEvent('click', function() {
		EE.addEmail();
	});
}

EE.addEmail = function() {
	var validEmail = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	var eEmail = $('emailAddress');
	var eName = $('emailName');
	var email = eEmail.value;
	var name = eName.value;
	if(name == '') {
		alert('Please enter a name.');
		eName.focus();
		eName.select();
	}
	else if(validEmail.test(email)) {
		var url = 'includes/insertEmailRemote.php?e=' + email + '&n=' + name;
		new Ajax(url, {
		  method: 'get',
		  onComplete: function(response) {
				if(response == "1") {
					alert("Thank you for signing up, you have been registered to receive the Energy and Environment weekly newsletter.");
				}
				else {
					alert("We are sorry, we could not sign you up for the Energy and Environment weekly newsletter at this time.  Please try again later.");
				}
		  }
		}).request();
	}
	else {
		alert("That is not a valid email");
		email.focus();
		email.select();
	}
}

EE.initNavigation = function() {
	$$('#mainNavigation li').each(function(elAye) {
		var children = elAye.getChildren();
		if(children.length > 1) {
			elAye.addEvents({
			'mouseover': function() {
				children[1].setStyle('display', 'block');
			},
			'mouseout': function() {
				children[1].setStyle('display', 'none');
			}
			});
			children[1].setStyle('display', 'none');
		}
	});
}