﻿window.homeBehavior = new function() {
	eval(estrada.namespace);
	
	this.oHomeNewsNav = null;

	this.intNumberOfStories = 0;
	this.intCurrentStoryNumber = 0;
	this.arrStories = [];

	tilt.attachEvent(document, "layout", function() {
		document.body.className = "l-home";

		////////////////////////////////////////////
		// put the content into the layout	
		////////////////////////////////////////////
		/*	
		$("#context .portal-nav, \
			#search").appendTo("#l-top");

		$("#feeds .home-news").appendTo("#l-upper");
			
		$("#context .home-nameplate, \
			#context .top-nav, \
			#context .location-nav, \
			#context .school-nav").appendTo("#l-lower");
			
		$("#context .logo, \
			#context .address, \
			#context .copyright").appendTo("#l-bottom");
		*/

		var layout = window.commonLayout.layout;	
		layout.push({
			"top": "search"
		});	
		layout.push(grab(document.getElementById("context"), {
			"portal-nav": "top"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"scene": "top"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"home-news": "upper"
		}));
		layout.push(grab(document.getElementById("context"), {
			"home-nameplate": "lower"
		}));
		layout.push(grab(document.getElementById("context"), {
			"top-nav": "lower"
		}));
		layout.push(grab(document.getElementById("context"), {

			"location-nav": "lower"
		}));
		layout.push(grab(document.getElementById("context"), {
			"school-nav": "lower"
		}));
		layout.push(grab(document.getElementById("context"), {
			logo: "bottom"
		}));
		layout.push(grab(document.getElementById("context"), { 
			address: "bottom", 
			copyright: "bottom"
		}));

		// bind behaviors
		$("h1.page").remove();
		layout.bind("pretty-search").to(document);
		layout.bind("home-news-nav").to(document);
		layout.bind("last-li").to("top");
		layout.bind("static-image-creation").to("top");
		layout.bind("profile-behavior").to("top");
		//layout.bind("scene-behavior").to("top");
		layout.bind("flash-replacement").to("top");
		$("#search").attr("class","search");
	});

	/////////////////////////////////////////////////////////////////////////
	// news navigation methods
	/////////////////////////////////////////////////////////////////////////
	this.nextStory = function() {
		homeBehavior.adjustStoryNumber(1);
	}
	this.prevStory = function() {
		homeBehavior.adjustStoryNumber(-1);
	}
	this.hideStories = function() {
		foreach(map(homeBehavior.arrStories), function(oStory) {
			oStory.style.display="none";
		});
	}
	this.adjustStoryNumber = function(intAdjustment) {
		homeBehavior.hideStories();
		switch (intAdjustment) {
			case 0:
				homeBehavior.intCurrentStoryNumber = 0;
				break;
			case -1:
				if (--homeBehavior.intCurrentStoryNumber < 0) {
					homeBehavior.intCurrentStoryNumber =
						homeBehavior.intNumberOfStories - 1;
				}
				break;
			case 1:
				if (++homeBehavior.intCurrentStoryNumber >=
						homeBehavior.intNumberOfStories) {
					homeBehavior.intCurrentStoryNumber = 0;
				}
				break;
			default:
				homeBehavior.intCurrentStoryNumber =
					homeBehavior.intNumberOfStories - 1;
				break;
		}
		homeBehavior.showStory(
			homeBehavior.intCurrentStoryNumber);
	}
	this.showStory = function(intStoryNumber) {
		var oStory = homeBehavior.arrStories[intStoryNumber];
		if (oStory) {
			oStory.style.display = "block";
		}
	}	
}
////////////////////////////////////////////////////////////////////
// add behavior to home page news feed
////////////////////////////////////////////////////////////////////
estrada.behavior('home-news-nav').is({
	bind: function(node) {
		$("#l-upper .home-news ul").wrap("<div class=\"l-news-wrapper\"></div>")
		var oHomeNews = first(getElements(node,"div","l-news-wrapper"));
		if (oHomeNews) {
			homeBehavior.oHomeNewsNav = oHomeNews.appendChild(document.createElement("div"));
			homeBehavior.oHomeNewsNav.className = "l-home-news-nav";
			homeBehavior.oHomeNewsNav.innerHTML = "<div class=\"l-prev-news\"></div>" +
				"<div class=\"l-next-news\"></div>"
			setControlAction("next", homeBehavior.nextStory);
			setControlAction("prev", homeBehavior.prevStory);
			
			homeBehavior.arrStories = $("#l-upper .home-news ul li").get();
			homeBehavior.intNumberOfStories =
				homeBehavior.arrStories.length;
			homeBehavior.intCurrentStoryNumber = 0;
			homeBehavior.hideStories();
			homeBehavior.arrStories[0].style.display = "block";	

		}
		function setControlAction(strAction, oCallBack) {
			var oControl = first(getElements(homeBehavior.oHomeNewsNav,
				"div", "l-" + strAction + "-news"));
			if (oControl) {
				tilt.attachEvent(oControl, "click", oCallBack);
			}
		}
	}
});
////////////////////////////////////////////////////////////////////
// mark the last list in an unordered list
////////////////////////////////////////////////////////////////////
estrada.behavior('last-li').is({
	bind: function(node) {
		foreach(map(node.getElementsByTagName("ul")), function(oUl) {
			if (oUl.hasChildNodes()) {
				var arrChildren = oUl.childNodes;
				var intLength = arrChildren.length;
				if (intLength > 0) {
					for (var i = (intLength-1); i >= 0; i--) {
						if (arrChildren[i].nodeName.toLowerCase() == 'li') {
							arrChildren[i].className += ' l-last';
							i = -1;
						}
					};
				}
			};		
		});
	}
});
///////////////////////////////////////////////////////////////////////
// static image creation
///////////////////////////////////////////////////////////////////////
estrada.behavior("static-image-creation").is({
	bind: function(node) {
		$(".scene .home-feed .content-query div.static-image").each(function() {
			$(this).replaceWith("<img src=\"" +
				$(this).html() + "\" " +
				"alt=\"Home Page Static Image\" width=700 " +
				"height=365 class=\"static-image\"></img>");
		});
		
	}
});
///////////////////////////////////////////////////////////////////////
// apply presentation and behavior to profiles
///////////////////////////////////////////////////////////////////////
estrada.behavior("profile-behavior").is({
	bind: function(node) {
		var arrProfiles, arrProfileLinks, intNumberOfProfiles, arrUserView;
		var intIdx, intSceneIdx;
		var oHomeProfileFeed, oProfileLinks, oProfileImages, arrImgWrappers;
		var arrLink, arrUserView;
		
		var arrScenes, intNumScenes;
		arrScenes = $(".scene > .content-query > .home-feed").hide();
		if (intNumScenes = arrScenes.length) {
			intSceneIdx = Math.floor(intNumScenes * Math.random());
			$(".home-profile-feed",arrScenes[intSceneIdx]).each(function() {
				oHomeProfileFeed = $(this);
				$("li.profile", oHomeProfileFeed).each(function() {
					arrLink = $("a.title", this);
					arrUserView = $(".user-view", this);
					if (arrUserView && arrUserView.length > 0) {
						arrLink.attr("href", arrUserView.text());
					}
				});
				$(oHomeProfileFeed).append("<div class=\"l-profile-images\"></div>");
				oProfileImages = $(".l-profile-images",oHomeProfileFeed);
				$("img.home-image", oHomeProfileFeed).each(
					function() {
						$(oProfileImages).append(this);
					}
				);
				arrProfiles = $("img.home-image", oProfileImages).wrap("<div class=\"l-img-wrapper\"></div>");
				arrImgWrappers = $(".l-img-wrapper", oProfileImages).hide();
				oHomeProfileFeed.append("<div class=\"l-profile-links\"></div>");
				oProfileLinks = $(".l-profile-links", oHomeProfileFeed);
				$(".profile a.title", oHomeProfileFeed).appendTo(oProfileLinks);
				$("> .content-query",oHomeProfileFeed).remove();
				$(".l-profile-links a",oHomeProfileFeed)
					.wrap("<div class=\"l-profile-link\"></div>")
					.wrap("<div class=\"l-profile-link-wrapper\"></div>");
				arrProfileLinks = $(".l-profile-link", oHomeProfileFeed).hide();
				if (intNumberOfProfiles = arrProfiles.length) {
					$(arrProfileLinks).each(function() {
						$(".l-profile-link-wrapper",this)
							.prepend("<div class=\"l-prev-profile\"></div>")
							.append("<div class=\"l-next-profile\"></div>");
						$(".l-profile-link-wrapper a", this)
							.append("<div class=\"l-more\">READ MORE</span>");
					});
					intIdx = Math.floor(intNumberOfProfiles * Math.random());
					profileInfo.intCurrentProfileNumber = intIdx;
					$(arrImgWrappers[intIdx]).show();
					$(arrProfileLinks[intIdx]).show();
				}
				profileInfo.arrProfileImages = arrImgWrappers;
				profileInfo.arrProfileLinks = arrProfileLinks;
				profileInfo.intNumberOfProfiles = profileInfo.arrProfileImages.length;
			});
			$(".l-next-profile").each(function() {
				tilt.attachEvent(this, "click", profileInfo.nextProfile);
			});
			$(".l-prev-profile").each(function() {
				tilt.attachEvent(this, "click", profileInfo.prevProfile);
			});
			$(arrScenes[intSceneIdx]).show();
		}
	}
});
///////////////////////////////////////////////////////////////////////
// apply presentation and behavior scenes
///////////////////////////////////////////////////////////////////////
estrada.behavior("scene-behavior").is({
	bind: function(node) {
		var arrScenes, intNumScenes, intIdx;
		arrScenes = $(".scene .home-feed").hide();
		if (intNumScenes = arrScenes.length) {
			intIdx = Math.floor(intNumScenes * Math.random());
			$(arrScenes[intIdx]).show()
			//$(".content-query",arrScenes[intIdx]).append($(".home-news"));
		}
	}
});
///////////////////////////////////////////////////////////////////////
// replace images with flash by convention
///////////////////////////////////////////////////////////////////////
estrada.behavior("flash-replacement").is({
	bind: function(node) {
		sceneFlashManager.flashReplacement();
	}
});
window.sceneFlashManager = new function() {

	this.flashReplacement = function() {
		foreach(map(document.getElementsByTagName("img")), function(img) {
			var oSrc = img.src;
			if (oSrc && oSrc.indexOf(".swf.") > 0) {
				replaceWithFlash(img);
			}
		});	
	};

	function replaceWithFlash(img) {
		var width = img.currentStyle ? img.currentStyle.width.replace(/px/, "") : img.width;			
		var height = img.currentStyle ? img.currentStyle.height.replace(/px/, "") : img.height;
		var src = img.src;
		if (width && height && src) {
			var movie = src.replace(/\.swf\..+/, ".swf");
			var version = 4;
			var useNetscapePlugins = navigator.plugins && navigator.mimeTypes.length;
			if (detectFlash())
			{
				var child = img;
				var oParent = img.parentNode;
				if (oParent.nodeName.toUpperCase() == "A") {
					child = oParent;
					oParent = oParent.parentNode;
				}
				var classname = child.className;
				var flashDiv = document.createElement("div");
				flashDiv.className = classname;
				flashDiv.innerHTML = createMovieHtml();
				oParent.replaceChild(flashDiv, child);
			}
			
		}

		function createMovieHtml()
		{
			var sizeAttributes = "width=\"" + width + "\" height=\"" + height + "\"";
			return useNetscapePlugins ?
				"<embed wmode=\"transparent\" type=\"application/x-shockwave-flash\" src=\"" + movie + "\" " + sizeAttributes + "></embed>"
				:
				"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " + sizeAttributes + "><param name=\"wmode\" value=\"transparent\"><param name=\"movie\" value=\"" + movie + "\" /></object>";
		};
		
		function detectFlash()
		{
			return getFlashVersion() >= version;

			function getFlashVersion()
			{
				if (useNetscapePlugins)
				{
					var plugin = navigator.plugins["Shockwave Flash"];
					if(plugin && plugin.description)
					{
						var description = plugin.description;
						var idx = description.indexOf('.')-2;
   					return description.substring(idx, idx+2);
					}
				} 
				else
				{
					var version = 0;
					for(var i = 4; i >= 3; i--)
					{
						var testObject;
						try
						{
							testObject = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." +  i);
						}
						catch(e)
						{
							continue;
						}
						return i;
					}
				}
				return 0;
			};
		};
	};
};