﻿new function() {

	eval(estrada.namespace);
	
	attachEvent(document, "layout", function() {
		document.body.className = "l-home";

		var layout = frame("page",
			area("sections"),
			cols(
				frame("home-flash",
					cols(
						area("flash1"),
						area("flash2")
					)
				),
				frame("header",
					area("tools"),
					area("search"),
					area("audience")
				)
			),
			cols(
				frame("main-left", 
					area("title"),
					cols(
						frame("left",
							area("announcements"),
							area("news")
						),
						frame("right",
							area("president"),
							area("events")
						)
					)
				),
				frame("ads",
					area("rotating-ads"),
					area("feature-ads")
				)
			),
			area("footer"),
			area("reviewed")
		).build(document.getElementById("form"));
		layout.push(grab(document.getElementById("context"), {
			reviewed: "reviewed",
			footer: "footer"
		}));
		layout.push(grab(document.getElementById("titling"), {
			site: "title"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			tools: "tools",
			sections: "sections",
			flash1: "flash1",
			flash2: "flash2",
			audience: "audience",
			"rotating-ads": "rotating-ads",
			"feature-ads": "feature-ads",
			announcements: "announcements",
			news: "news",
			events: "events",
			president: "president"
		}));
		layout.push({
			search: "search",
			reviewed: "reviewed"
		});		
		var oSections = getElements(document, "div", "sections");
		if (oSections) {
			foreach(map(oSections), function(oSection) {
				var oLinks = first(getElements(oSection, "div", "links"));
				if (oLinks) {
					if (oLinks.className) {
						oLinks.className = oLinks.className + " l-hide";
					} else {
						oLinks.className = "l-hide";
					}
				}
				tilt.attachEvent(oSection, "mouseout", toggleSection);
				tilt.attachEvent(oSection, "mouseover", toggleSection);
			});
		}

		var arrRotatingAds = [];
		var intAdChangeInterval = 5000;
		var oAdRotationTimer = null;
		var intCurrentAd = -1;
		///////////////////////////////////////////////////////////////////////
		// rotate through ads links every 5 seconds
		///////////////////////////////////////////////////////////////////////
		var oRotatingAdsDiv = first(getElements(document, "div", "rotating-ads"));
		if (oRotatingAdsDiv) {
			arrRotatingAds = getElements(oRotatingAdsDiv, "div", "Brick");
			if ((arrRotatingAds.length) > 0) {
				showRotatingAd();
			}
		}
		
		foreach(document.getElementsByTagName("ul"), function(ul) {
			foreach(ul.getElementsByTagName("li"), function(li) {
				li.className += " l-first";
				return true;
			});
		});
		
		var searchQuery = document.getElementById("search-query");
		if (searchQuery) {
			var defaultText = "Search";
			var blurClassName = "text l-blur";
			searchQuery.value = defaultText;
			searchQuery.className = blurClassName;
			attachEvent(searchQuery, "focus", function() {
				if (searchQuery.value == defaultText) {
					searchQuery.value = "";
					searchQuery.className = "text";
				}
			});
			attachEvent(searchQuery, "blur", function() {
				if (searchQuery.value.length == 0) {
					searchQuery.value = defaultText;
					searchQuery.className = blurClassName;
				}
			});
			attachEvent(searchQuery, "keydown", function(searchQuery, e) {
                if (e.keyCode == 13) {
                    window.setTimeout("document.getElementById(\"search-submit\").click()", 0);
                    return false;
                }
                return true;
			});
		}
		var searchSubmit = document.getElementById("search-submit");
		if (searchSubmit) {
			var parent = searchSubmit.parentNode;
			var div = parent.appendChild(document.createElement("div"));
			div.id = "l-search-submit";
			attachEvent(div, "click", function() {
				document.getElementById("search-submit").click();
			});
		}

		///////////////////////////////////////////////////////////////////////
	    // replace images with flash by convention
		///////////////////////////////////////////////////////////////////////
		foreach(map(document.getElementsByTagName("img")), function(img) {
			var oSrc = img.src;
			if (oSrc && oSrc.indexOf(".swf.") > 0) {
				replaceWithFlash(img);
			}
		});	

		function showRotatingAd() {
			if (arrRotatingAds.length > 0) {
				var intIdx;
				stopAdRotation();
				if (++intCurrentAd >= (arrRotatingAds.length)) {
					intCurrentAd = 0;
				}
				for(intIdx=0; intIdx < arrRotatingAds.length; ++intIdx) {
					if (intIdx == intCurrentAd) {
						arrRotatingAds[intIdx].className = "Brick l-show";
					} else {
						arrRotatingAds[intIdx].className = "Brick l-hide";
					}					
				}
				oAdRotationTimer = setInterval(showRotatingAd, intAdChangeInterval);
			}
		}
		function stopAdRotation()
		{
			if (oAdRotationTimer) {
				clearInterval(oAdRotationTimer);		
			}
		}
		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 + "\" wmode=\"opaque\"";
				return useNetscapePlugins ?
					"<embed type=\"application/x-shockwave-flash\" src=\"" + movie + "\" " + sizeAttributes + "></embed>"
					:
					"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " + sizeAttributes + "><param name=\"movie\" value=\"" + movie + "\" /><param name=\"wmode\" value=\"opaque\"></object>";
			}
			
			function detectFlash()
			{
				return getFlashVersion() >= version;
	
				function getFlashVersion()
				{
					if (useNetscapePlugins)
					{
						var plugin = navigator.plugins["Shockwave Flash"];
						if(plugin && plugin.description)
						{
							var description = plugin.description;
	   						return description.charAt(description.indexOf('.')-1);
						}
					} 
					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;
				}
			}
		}
	});
	function toggleSection(oElm) {
		if (oElm) {
			var oLinks = first(getElements(oElm, "div", "links"));
			if (oLinks) {
				if (oLinks.className) {
					if (oLinks.className.indexOf("show") >= 0) {
						oLinks.className = oLinks.className.replace("l-show", "l-hide");
					} else if (oLinks.className.indexOf("hide") >= 0) {
						oLinks.className = oLinks.className.replace("l-hide", "l-show");
					} else {
						oLinks.className = oLinks.className + " l-show";
					}
				} else {
					oLinks.className = "l-show";
				}
			}
		}
	}
	attachEvent(document, "layouterror", function(errors) {
		var msg = "";
		foreach(errors, function(error) {
			msg = "\n\t" + error.name + ": " + error.message;
		});
		alert("errors occured:" + msg);
	});
	function getElements(oElm, strTagName, strClassName) {
		var oElement;
		var arrReturnElements = [ ];
		if (oElm) {
			var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
			for(var i=0;i<arrElements.length;i++) {
				oElement = arrElements[i];
				if (oElement.className) {
					var oClasses = oElement.className.split(" ");
					var bolFound = false;
					var intXX = 0;
					while (!bolFound && (intXX < oClasses.length)) {
						if (oClasses[intXX] == strClassName) {
							bolFound = true;
						} else {
							++intXX;
						}
					}
					if (!bolFound) {
						if (strClassName == "*") {
							bolFound = true;
						}
					}
				} else {
					if (strClassName == "*") {
						bolFound = true;
					}
				}
				if (bolFound) {
					arrReturnElements.push(oElement);
				}
			}
		}
		return (arrReturnElements)
	}
}