// Global Namespace
/// <reference path="classes/jquery-vsdoc.js">

var AppGlobal;
if (!AppGlobal) AppGlobal = {};

// Declare global variables
AppGlobal.BASE_URL = '/';
AppGlobal.CLASSPATH = 'js/classes/';
AppGlobal.overlay = null;
AppGlobal.liveHostname = "www.farmandfleet.com";

// Declare global variables
var win = null;
var $id = function(elem){return document.getElementById(elem);}
var $elem = function(tag){return document.createElement(tag);}
var $txt = function(text){return document.createTextNode(text);}

// Dynamic class loader
AppGlobal.loadClass = function(source) {
	var scriptname = AppGlobal.BASE_URL + AppGlobal.CLASSPATH + source.replace('.', '/') + '.js',
		srcid = source.replace('.', '_');
	if (self[srcid]) return;
    document.write('<script id="' + srcid + '" type="text/javascript" src="'+scriptname+'"><\/script>');
}

// Determine the asset and image prefixes
AppGlobal.assetPrefix = function() {
	return (document.location.protocol != "https:" && document.location.hostname == AppGlobal.liveHostname) ? "http://assets.farmandfleet.com" : "";
}
AppGlobal.imagePrefix = function() {
	return (document.location.protocol != "https:" && document.location.hostname == AppGlobal.liveHostname) ? "http://images.farmandfleet.com" : "";
}
AppGlobal.cssPrefix = function() {
	if (Browser.NAME == "Internet Explorer" && Browser.VERSION <= 6) return "";
	else return (document.location.protocol != "https:" && document.location.hostname == AppGlobal.liveHostname) ? "http://css.farmandfleet.com" : "";
}

// Sets a given element to a text value
AppGlobal.setText = function(elem, text) {
	while (elem.firstChild) elem.removeChild(elem.firstChild);
	var items = text.split("<br />");
	if (items.length > 1) {
		for (var x = 0; x < items.length; x++) {
			if (x > 0) elem.appendChild($elem("br"));
			elem.appendChild($txt(items[x]));
		}
	} else elem.appendChild($txt(text));
}

// Gets the value from an XML node
AppGlobal.getXMLValue = function(node) {
	return (node && node.firstChild) ? node.firstChild.nodeValue : "";
}

// Find product form methods
AppGlobal.onFocusFindProduct = function() {
	var form = $id("frmFindProduct"),
		field = form.t;
	if (field.value == 'Enter Keyword(s) or Blain Number') field.value = '';
}
AppGlobal.onFocusFindProductHeader = function() {
	var form = $id("frmFindProduct"),
		field = form.t;
	if (field.value == 'Find a Product') field.value = '';
}
AppGlobal.onBlurFindProduct = function() {
	var form = $id("frmFindProduct"),
		field = form.t;
	if (field.value == '') field.value = 'Enter Keyword(s) or Blain Number';
}
AppGlobal.onBlurFindProductHeader = function() {
	var form = $id("frmFindProduct"),
		field = form.t;
	if (field.value == '') field.value = 'Find a Product';
}
AppGlobal.onSubmitFindProduct = function() {
	var form = $id("frmFindProduct"),
		field = form.t;
	if (field.value == '' || field.value == 'Enter Keyword(s) or Blain Number' || field.value == 'Find a Product') {
		alert("Please enter one or more search terms.");
		return false;
	} else return true;
}

// Automatically tab to the specified field when the current field reaches a certain length
AppGlobal.autoTab = function(maxlength, field, next) {
	if (field.value.length >=  maxlength && next != null && next != undefined) next.select();
}

// Resizes the current browser window
AppGlobal.resizeWindow = function(valX, valY) {
	var maxX = Math.min(valX, screen.availWidth - 50),
		maxY = Math.min(valY, screen.availHeight - 50),
		moveX = 0, moveY = 0;
	if (window.screenLeft) {
		if (window.screenLeft + maxX > screen.availWidth) moveX = screen.availWidth - (window.screenLeft + maxX);
		if (window.screenTop + maxY > screen.availHeight) moveY = screen.availHeight - (window.screenTop + maxY);
		window.moveBy(moveX, moveY);
	} else if (window.screenX) {
		if (window.screenX + maxX > screen.availWidth) moveX = screen.availWidth - (window.screenX + maxX);
		if (window.screenY + maxY > screen.availHeight) moveY = screen.availHeight - (window.screenY + maxY);
		window.moveBy(moveX, moveY);
	} else {
		window.moveTo(0, 0);
	}
	if (window.innerWidth) {
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	} else if (document.documentElement.clientWidth) {
		iWidth = document.documentElement.clientWidth;
		iHeight = document.documentElement.clientHeight;
	} else {
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	iWidth = maxX - iWidth;
	iHeight = maxY - iHeight;
	window.resizeBy(iWidth, iHeight);
}

// Goes to the account registration page, including an e-mail address in the URL if one is filled in the form
AppGlobal.goToRegistration = function(a) {
	var txtEmail = $id("txtEmail"),
		strURL = a.href,
		strJoin = (strURL.indexOf("?") != -1) ? "&" : "?";
	if (txtEmail) {
		if (txtEmail.value.isEmailAddress()) strURL += strJoin + "email=" + encodeURIComponent(txtEmail.value);
	}
	location.href = strURL;
	return false;
}

// Goes to the password recovery page, including an e-mail address in the URL if one is filled in the form
AppGlobal.goToPasswordRecovery = function(a) {
	var txtEmail = $id("txtEmail"),
		strURL = a.href,
		strJoin = (strURL.indexOf("?") != -1) ? "&" : "?";
	if (!txtEmail) txtEmail = $id("txtVerifyEmail");
	if (txtEmail) {
		if (txtEmail.value.isEmailAddress()) strURL += strJoin + "email=" + encodeURIComponent(txtEmail.value);
	}
	location.href = strURL;
	return false;
}

// Sets up the navigation menu
AppGlobal.setupNavMenu = function() {
	var timer = null,
		resetMenus = function() {
		clearTimeout(timer);
		$("#navmenu > a, #navmenucnp > a").each(function(){$("span",this).removeClass('hover');$("#"+this.id+"_subs").hide();});
		$("#header div.navsub").each(function(){$("span","#"+ this.id.replace("_subs", "")).removeClass('hover');$(this).hide();});
	}
	var toggleDropDowns = function(val) {
		if ($.browser.msie && /6.0/.test(navigator.userAgent) ) {
			if (val) $("select").css('visibility', ''); else $("select").css('visibility', 'hidden');
		}
	};
	$("#navmenu > a, #navmenucnp > a").each(function() {
		var main = $("span", this),
			subs = $("#" + this.id + "_subs"),
			navitem = $(this);
		navitem.mouseenter(function(){resetMenus();toggleDropDowns(false);$(main).addClass('hover');$(subs).show();});
		navitem.mouseleave(function(){timer=setTimeout(function(){resetMenus();toggleDropDowns(true);},400);});
	});
	$("#header div.navsub").each(function() {
		var main = $("span", "#" + this.id.replace("_subs", "")),
			subs = $(this);
		subs.mouseenter(function(){resetMenus();toggleDropDowns(false);$(main).addClass('hover');$(subs).show();});
		subs.mouseleave(function(){timer=setTimeout(function(){resetMenus();toggleDropDowns(true);},400);});
	});
}

// Opens a popup window describing what a blain number is
AppGlobal.openBlainNumberPopup = function() {
	AppGlobal.overlay = new Overlay("blainnum-popupobj");
}

// Pricing OnLoad directive
AppGlobal.onLoad = function() {
	// Setup navigation menu
	if ($id("navmenu") || $id("navmenucnp")) AppGlobal.setupNavMenu();
	var cssprefix = AppGlobal.cssPrefix();
	// Preload popop images
	if ($id("message-popupobj")) {
		(new Image()).src = cssprefix + '/images/interface/popup_bkg.png';
		AppGlobal.overlay = new Overlay("message-popupobj");
		var aClosePopupMessage = $id("aClosePopupMessage");
		if (aClosePopupMessage) aClosePopupMessage.onclick = function() { AppGlobal.overlay.remove(); AppGlobal.overlay = null; }
	}
	if ($id("blainnum-popupobj")) (new Image()).src = cssprefix + '/images/interface/blainnum_bkg.png';
	if ($id("noecon-popupobj")) (new Image()).src = cssprefix + '/images/interface/popup2_bkg.png';
	// Setup home page banner rotation
	if ($id("HomePageRotation")) {
		var b = new BannerRotation();
		b.setScriptURL("/ajax/banners.aspx");
		b.setImageURL(AppGlobal.assetPrefix() + "/uploads/banner_rotation/");
		b.setBannerSize(518, 270);
		b.create("HomePageRotation");
	}
}

// Load jQuery
AppGlobal.loadClass("JSCommon20100505");

// Assign onload parameter
if (window.addEventListener) window.addEventListener("load", AppGlobal.onLoad, false);
else if (window.attachEvent) window.attachEvent("onload", AppGlobal.onLoad);

