dojo.require('flt.Core');
FLT.newsStoryArray = new Array ();
FLT.init = function ()
{
	var mktContent = dojo.query (".marketing_area_main > li");
	
	var mktArea = dojo.query ("#marketing_area_select");
	var ul = dojo.create ("ul", null, "marketing_area_select", "first");
	
	
	// create the buttons
	dojo.forEach(mktContent, 
		function(item){
			var li = dojo.create ("li", { innerHTML: "<a href='#'>Read More</a>"}, ul);
			var thisClass = dojo.attr(item, "class");
			dojo.addClass(li, thisClass);
			dojo.connect (li, "onclick", function(e){FLT.setCarousel(thisClass)});
	});
	
	// initialize for the first button
	FLT.setCarousel (dojo.attr(dojo.query(".marketing_area_main li")[0],"class"));
	
		
	var homeProducts = dojo.query ("#homebox_products ul li a img");
	dojo.forEach (homeProducts,
		function (item) {
			
			dojo.connect (item, "onmouseover", function(e){FLT.setProductContent (dojo.attr(item, "title"), dojo.attr(item, "alt"));});
		});
		
	
	dojo.query ("div#news_scroller div.left_arrow a").connect ("onclick", function(){FLT.setNewsStory (0);});
	dojo.query ("div#news_scroller div.right_arrow a").connect ("onclick", function(){FLT.setNewsStory (1);});
	// build
	var newsStories = dojo.query ("#news_scroller ul li");
	
	dojo.forEach (newsStories,
		function (item) {
			FLT.newsStoryArray.push (item);
		});
	
	FLT.currentNewsIndex = 0;
	var firstStory = FLT.newsStoryArray[0];
	dojo.style (firstStory, "display", "block");
	
		
	// preload the marketing images
	dojo.require("dojox.image");
	dojo.addOnLoad(function(){
	    dojox.image.preload(["/images/marketing_powerful.jpg", "/images/marketing_minimal.jpg", "/images/marketing_environmental.jpg", "/images/marketing_smart.jpg", "/images/marketing_adaptive.jpg"]);
	});
}

FLT.setCarousel = function (thisClass)
{
	
	var mktContent = dojo.query (".marketing_area_main li");
	
	dojo.forEach(mktContent, 
			function(item){
				if (dojo.attr(item, "class") == thisClass)
				{
					//dojo.addClass(item, "current");
					dojo.style(item, "display", "block");
					dojo.fadeIn ({node: item}).play();
				}
				else
				{
					//dojo.removeClass(item, "current");
					//dojo.style(item, "display", "none");
					dojo.fadeOut ({node: item}).play();
				}
				FLT.selectedClass = thisClass;
				return false;
		});
		
	var mktButtons = dojo.query ("#marketing_area_select li");
	dojo.forEach(mktButtons, 
			function(item){
				if (dojo.attr(item, "class") == thisClass)
				{
					dojo.addClass(item, "current");
					//dojo.style(item, "display", "block");
				}
				else
				{
					dojo.removeClass(item, "current");
					//dojo.style(item, "display", "none");
				}
				FLT.selectedClass = thisClass;
		});
	
	
	
}

FLT.setProductContent = function (title, body)
{
	var titleHolder = dojo.query ("#product_info h4")[0];
	var bodyHolder = dojo.query ("#product_info p.description")[0];
	
	titleHolder.innerHTML = title;
	bodyHolder.innerHTML = body;
}

FLT.setNewsStory = function (index)
{
	
	var newItem;
	var newIndex = FLT.currentNewsIndex;
	switch (index)
	{
		case 0:
			newIndex -= 1;
			break;
		case 1:
			newIndex += 1;
			break;
	}
	
	if (newIndex < 0)
	{
		newIndex = FLT.newsStoryArray.length - 1;
	} 
	else if (newIndex > FLT.newsStoryArray.length - 1)
	{
		newIndex = 0;
	}
	
	newItem = FLT.newsStoryArray[newIndex];
	
	if (FLT.currentNewsIndex != null)
	{
		dojo.fadeOut ({node: FLT.newsStoryArray[FLT.currentNewsIndex]}).play();
	}
		//dojo.style(FLT.newsStoryArray[FLT.currentNewsIndex], "display", "none");
		
	
	dojo.style(newItem, "display", "block");
	dojo.fadeIn ({node: newItem}).play();
	FLT.currentNewsIndex = newIndex;
}

dojo.addOnLoad(FLT.init);
