$(document).ready(function(){
	(function news (){
		var 
			pageSettings = {
				playNews : true,
				currentNewsItem : undefined
			};
	
		//argument is a jquery object
		function newsSelect( obj ) {
			var 
				aInd = $("div.newsSquare").index(obj),
				container = $("div#newsScrollInner"),
				containerW = container.width(),
				divW = containerW / $("div.newsSquare").size(),
				currLeft = (function(){
					var ret = container.css("left");
					ret = ret.substr(0,ret.length-2);
					return ret;
				})(),
				targ = 0 - (aInd * divW),
				distLeft = currLeft - targ;
		
			//so animation doesn't stall if clicked twice
			if (aInd == pageSettings.currentNewsItem ){
				return false;
			}
			pageSettings.currentNewsItem = aInd;
		
			//if animation is going it stops
			container.stop();

			obj.siblings("div.newsSquare").removeClass("current");
			obj.addClass("current");		
			container.animate({"left":"-="+distLeft},{"easing":"swing","duration":500});
			
			//container.animate({"left":"-="+distLeft},500);
			return false;
		}
	
	
		function cycleNews(){
			var 
				n = $("div.newsSquare").size(),
				i = 0,
				playNum = 0;
		
			function run(){
				setTimeout(function(){
					if(pageSettings.playNews && i < n){
						playNum = i+1;
						if( playNum == n ){
							playNum = 0;
							pageSettings.playNews = false;
						}
						newsSelect( $("div.newsSquare:eq("+playNum+")") );
						i++;
						run();
					}
				},4000);			
			}
			run();
		
			return true;
		}

		$("div.newsSquare").click(function(){
			pageSettings.playNews = false;
			newsSelect( $(this) );
			return false;
		});
	
		//cycleNews();
		
		/*
			Just quickly show all the stories
		*/
		
		function quickCycleNews(){
			//because it's hidden
			$("div#newsBox").css({"opacity":0,"visibility":"visible"}).animate({"opacity":1},500,"swing",function(){
				newsSelect( $("div.newsSquare:first") );
			});
		
			return true;

		}
		
		newsSelect( $("div.newsSquare:last") );
		setTimeout(quickCycleNews, 500);
	
	})(); //end news
	
	(function mailingList (){
		$("form#mailList").submit(function(){
			var
				fN = $("input#firstname").val(),
				lN = $("input#lastname").val(),
				e = $("input#email").val(),
				empt = $("input#nameEmpt").val();
			
			$.post("subscribe.cfm",{"aj":1,"firstname":fN,"lastname":lN,"email":e,"nameEmpt":empt},function(r){
				if( r.indexOf("SUCCESS") == -1 ){
					alert("There was an error processing your information.");
				}else{
					$("div#emailSubscribe").children("form#mailList,div.fullW").remove();
					$("div#emailSubscribe").append("Thank you for subscribing to EAM eNews.");
				}
			});
		
			return false;
		
		});

		$('input#firstname').preVal({inputVal:'First Name'});
		$('input#lastname').preVal({inputVal:'Last Name'});
		$('input#email').preVal({inputVal:'Email'});
	})(); //end mailingList	

});	


function Features (){
};
Features.prototype.arFeatures = [];
Features.prototype.imgIndex = 0;
Features.prototype.init = function(arFeatures){
	var 
		self = this,
		img = [];

	if(arFeatures.length){
		self.arFeatures = arFeatures;
		$.each(self.arFeatures,function(i,item){
			img.push( new Image() );
			$(img[img.length - 1]).attr({ "src":item['src'] }).load(function(){
				self.arFeatures[i]['isLoaded'] = true;
			});
		});
		
		self.setIntervalToDefault();
	}
	
	return true;
}
Features.prototype.loadImage = function(){	
	var 
		self = this,
		imgInfo = self.arFeatures[self.imgIndex],
		newA = $('<a class="featureImage" href="'+imgInfo['link']+'"><img src="'+imgInfo['src']+'" alt="'+imgInfo['alt']+'" /></a>');
		newA.filter("a").css({"opacity":0});
		$("div#homeFeature a.featureImage:last").after(newA);
		$("div#homeFeature a.featureImage:last").animate({"opacity":1},800,"swing",function(){
			$("div#homeFeature a.featureImage:first").remove();
			self.setIntervalToDefault();			
		});
	return false;
}
Features.prototype.setIntervalToDefault = function(){
	var self = this;
	if(self.changeInterval){
		clearInterval(self.changeInterval);
	}
	self.changeInterval = setInterval(function(){
		self.changeImage("next");
	},5000);
}

Features.prototype.changeImage = function(num){
	var self = this,
		nextNum;
	
	if( $("a.featureImage").size() == 1 ){ // means not currently animating
		if(num == "next"){
			nextNum = ( (self.imgIndex + 1) == self.arFeatures.length ? 0 : (self.imgIndex + 1) );
		} else if(num = "prev"){
			nextNum = ( (self.imgIndex - 1) < 0 ? self.arFeatures.length-1 : (self.imgIndex - 1) );
		} else if(num >= 0 && num <= 100){
			nextNum = num;
		}

		if(nextNum >= 0 && self.arFeatures[nextNum]['isLoaded']){
			self.imgIndex = nextNum;
			self.loadImage();
		}
	}

	return true;
}