// JavaScript Document
// Estes core enhancements
/*globals window */
var ESTES = (function () {
	var my = {};
	return my;
}());


ESTES.view = (function () {
	var my = {},
		pL = {Lname: "", Lclass: ""};
	
	my.highlightEl = function (elName) {
		var L = document.getElementById(elName),
			myL = "";
		if (pL.Lname !== "") {	//replace w prev class
			myL = document.getElementById(pL.Lname);
			myL.className = 'required';
			myL.className = pL.Lclass;
		}
		pL.Lname = elName;
		pL.Lclass = L.className;
		//if (jQuery) {
		//	$(L).fadeTo(1, .5, function () {$(this).addClass("highlighted").delay(500).fadeTo(1000, .2);});
		//} else {
			L.className = "highlighted";
		//}
		return true;
	};
	
/*	my.dialog = function (message, cssClass, attachToID, id, openMethod, closeMethod) {
		var id = id || "estesDialog",
			cssClass = cssClass || "dialog",
			attachToID = $("#" + attachToID) || $('body'),
			openMethod = openMethod || "",
			closeMethod = closeMethod || "",
			closeLink = $("<a>").attr("href","#").attr("title","Close").text("Close").wrap("<div>"),
			dialog = $("<div>").attr("id", id).addClass(cssClass)
				.hide().appendTo(parent).append(message).append(closeLink);
		if (openMethod == "slide") {
			$('#id').slideDown('slow', function () {
				$('#id').click( function (e) {
					ESTES.view.dialog.dismiss($(e.target).attr("id"), "slide");
				});
			});
		} else if (openMethod == "fade") {
			$('#id').fadeIn('slow', function () {
				$('#id').click( function (e) {
					ESTES.view.dialog.dismiss($(e.target).attr("id"), "fade");
				});
			});
		} else {
			$('#id').show();
		}
		//set a timer to auto close.
			
	};
	my.dialog.slideOpen = function (id) {ESTES.view.dialog.dismiss(id,"slide");};
	my.dialog.dismiss = function (id, effect) {
		effect = effect || "";
		//add logic to remove "curtain" or "screenFader" or whatever I will call it.
		if (effect == "fade") {
			$("#" + id).fadeOut('fast', function () {$(this).remove();});
		} else 	if (effect == "slide") {
			$("#" + id).slideUp('fast', function () {$(this).remove();});
		} else {
			$("#" + id).remove();
		}
	}
*/
	my.onDocReady = function () {
		
		(function expireCheck($) {
			var curDate = new Date();
			$('*[data-expire]').each(function (i) {
				var elDate = $(this).attr('data-expire');
				if (new Date('20' + elDate.slice(0, 2), ((elDate.slice(2, 4) - 1) || 0), (elDate.slice(4, 6) || 0), (elDate.slice(6, 8) || 0), (elDate.slice(8, 10) || 0)) < curDate) {
					$(this).remove();
				}
			});
		}(jQuery));

		(function headerChange($) {
			var imgList = [];
			$('*[data-randimg]').each(function (i) {
				imgList = $(this).attr('data-randimg').split(',');
				imgList.push($(this).attr('src'));
				$(this).attr('src', imgList[Math.floor(Math.random() * imgList.length)]);
			});
		}(jQuery));
		
		
		(function onLoadHighlighting() {
			var id = window.location.hash;
			if (id !== "") {
				id = id.substr(1);
				ESTES.view.highlightEl(id);
			}
		}());
	
	};
	
		
	return my;
}());

ESTES.control = (function () {
	var my = {},
		ready = false,
		jQueryLoaded = false,
		jEaseLoaded = false;
	
	my.init = function () {
		ready = true;
	};
	
	my.readyCheck = function () {
		return ready;
	};
	
	my.PDFtrack = function () {
		$('a[href$=".pdf"]').click(function (e) {
			pageTracker._trackPageview($(this).attr('href'));
		});
	};

	my.loadjQuery = function () {
		var jQloader = document.createElement('script'); 
		jQloader.type = 'text/javascript'; 
		//jQloader.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js';
		jQloader.src = '/java/jquery-1.5.1.min.js';
		document.body.appendChild(jQloader);
		jQueryLoaded = true;
	};
	
	my.jQueryCheck = function (forceLoad) {
		forceLoad = jQueryLoaded && 1;
		if (forceLoad) {
			my.loadjQuery();
		}
		return jQueryLoaded;
	};

	my.loadjEasing = function () {
		var jeLoader = document.createElement('script'); 
		jeLoader.type = 'text/javascript'; 
		jeLoader.src = '/js/jquery.easing.1.3.js';
		my.jQueryCheck(1);
		document.body.appendChild(jeLoader);
		jEaseLoaded = true;
	};
	
	my.jEasingCheck = function () {
		return {'jquery': jQueryLoaded, 'ease': jEaseLoaded};
	};
	
	my.onDocReady = function () {
		ESTES.view.onDocReady();
		
/*		(function glossaryInit() {
			$('a.gloss').click(function (e) {
				var termID = $(this).attr("href"),
					message = $('<div>');
				termID = TermID.slice(TermID.indexOf("#"));
				message.load('/resources/transportation.html #' + termID, function(response, status, xhr) {
					if (status == "error") {
						return true;
					} else {
						e.preventDefault();
						ESTES.view.dialog(message, "glossary", "Page", "Glossary", "slide", "slide")});
					}
				});
		}()); */
	
	}

	return my;
}());

$(document).ready(function () {
	ESTES.control.onDocReady();
	$('*[id*="uick"] a').click(function(e) {pageTracker._trackEvent("quickLinks", "quickLink-click", $(this).attr('href'));});

}); // --- end $document.ready

function highlightElement(el) {
	ESTES.view.highlightEl(el);
}
