/* NETJETS ~ JAVASCRIPT UTILS

	Author: Joel Nagy | dotglu
	Last Updated: August 28, 2008 11:48 AM EST
	
	Dependency: jQuery 1.2.1+
*/

var BREADCRUMBMINWIDTH = 100;
var Timer = null;
var PrepComplete = false;
var TopNavSWF = null;

// Populate Query String Object, iterate over each name-value pair
window.query = new Object();
window.query.length = 0;
window.location.search.replace(
	new RegExp('([^?=&]+)=([^&#]*)?', 'g'),
	// populate the object with the values by name
	function($0, $1, $2) { window.query[$1] = $2; window.query.length++; }
);

jQuery(document).ready(function() {
try {
	// Bread Crumb Initiate
	breadCrumbs();

	// LivePerson Poll Looping
	prepLPnav();
	
	// check for autoload
	autoLoading();
} catch (e) { }
});

function autoLoading() {
	var autoload = window.query['autoload'];

	switch(autoload) {
		case 'vid':
			var vid = window.query['vid'];
			showEventsVideo(vid , '#EventsInnerDiv'+ vid);
			break;
	}
}

function prepLPnav(loop) {
try {
	if (PrepComplete) { 
		clearTimeout(Timer);
		Timer = null;
		return;
	}

	// Prep Flash for communication
	var $swf = jQuery('#TopNavSWF');
	var $object = jQuery('#TopNav object');
	var $embed = jQuery('#TopNav embed');

	if ($swf && $swf.length > 0) {
		PrepComplete = true;
		TopNavSWF = $swf[0];
	} if ($object && $object.length > 0) {
		PrepComplete = true;
		$object.attr('id', 'TopNavSWF');
		$object.attr('name', 'TopNavSWF');
		TopNavSWF = $object[0];
	} else if ($embed && $embed.length > 0) {
		PrepComplete = true;
		$embed.attr('id', 'TopNavSWF');
		$embed.attr('name', 'TopNavSWF');
		$embed.attr('swLiveConnect', 'true');
		TopNavSWF = $embed[0];
	}
	
	clearTimeout(Timer);
	Timer = null;
	if (PrepComplete) {
		// LivePerson Poll Looping
		loopLP(); // call when LivePerson is ready to go Live!!!
	} else {
		if (loop == null || loop == true)
			Timer = setTimeout('prepLPnav()', 9000);
	}
} catch (e) { clearTimeout(Timer); Timer = null; PrepComplete = true; }
}

function breadCrumbs() {
// Handles the autocollapsing of long breadcrumbs
	var x = 0;
	jQuery('#NavWrap div').each(function(){ x += parseInt(jQuery(this).width()) + 9; }); // gather the width of the breadcrumbs
	if (x+BREADCRUMBMINWIDTH-10 < 862) // if the width of the crumbs is less than the space alloted then no need to squeeze
		return;

	// Get the width of the outermost container div.
	var NavMin = jQuery('div.NavMin', jQuery('#NavBar'));
	var NavMinA = jQuery('div.NavMin a', jQuery('#NavBar'));
	var NavDivs = jQuery('div#NavWrap div', jQuery('#NavBar'));

	var Z = 25;
	NavDivs.not(".NavFilter").each(function() {
		$t = jQuery(this);
		$t.css('z-index', ++Z);
		$t.css("overflow", "hidden");
	});

	NavMinA.mouseover(function(e) {
		if (e.target == this) {
			var nav = jQuery(this).parent(), w = nav.attr("maxwidth"), go = nav.attr("expand");
			if ((go == "false" || go == null) && parseInt(w) > 0)
					eval('nav.animate({width: '+ w +'}, "fast", null, function(){ nav.attr("expand", "true"); });');
		}
		return false;
	});

	NavMinA.mouseout(function(e) {
		if (e.target == this) {
			var nav = jQuery(this).parent(), go = nav.attr("expand");
			if (go == "true")
				nav.animate({width: BREADCRUMBMINWIDTH}, "fast", null, function(){ nav.attr("expand", "false"); nav.css("width", BREADCRUMBMINWIDTH +"px"); });
		}
		return false;
	});

	// attach maxwidth
	for (var i = 0, l = NavMin.length; i < l; i++) {
		var nav = jQuery(NavMin[i]);
		nav.attr("maxwidth", nav.width());
		if (nav.width() > BREADCRUMBMINWIDTH)
			nav.css("width", BREADCRUMBMINWIDTH +"px");
		else
			nav.attr("maxwidth", "0");
	}

	// Crop in on the menus that are higher up in the crumbs
//	NavMin.css("overflow", "hidden");
	////NavMin.css("width", BREADCRUMBMINWIDTH +"px");
}

// LivePerson
var lpNumber = "38849319"; // Site ID (LP-NUMBER)
var lpServerName = "server.iad.liveperson.net";

// DEFINED REP STATES :
// var LPREPSTATEONLINE = "online" : Reps available online
// var LPREPSTATEOFFLINE = "offline" : No available reps online - offline or away
// var LPREPSTATEOCCUPIED = "occupied" : Rep in "back in 5" state
// var LPREPSTATEUNKNOWN = "unknown" : Unable to contact server

function repStateNotifyFunction(state) {
try {
	if (PrepComplete == false) { return prepLPnav(); }

	// If btwn 9am & 5:59pm EST perform check for LP
	var D = new Date();
	var hour = D.getUTCHours()-4; // GMT-EST
	if (hour < 9 || hour >= 18)  // between 9am and 5:59pm
		state = 'offline';

	var swf = TopNavSWF;
	if (swf == null) {
		return;
	}

	if (state == LPREPSTATEONLINE) {
		// call flash to display link
		swf.setChat(true);
	} else {
		// call flash to hide link
		swf.setChat(false);
	}
} catch (e) { clearTimeout(Timer); Timer = null; }
}

function liveChat() {
	window.open('http://server.iad.liveperson.net/hc/38849319/?cmd=file&file=visitorWantsToChat&site=38849319&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a&referrer='+escape(document.location),'chat38849319','width=475,height=400,resizable=yes');
/*
<!-- BEGIN LivePerson Button Code --><div  ><a id="_lpChatBtn" href='http://server.iad.liveperson.net/hc/38849319/?cmd=file&file=visitorWantsToChat&site=38849319&byhref=1&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a' target='chat38849319'  onClick="javascript:" ><span style="font:normal 11px Arial, Helvetica, sans-serif;color:#0000ff">Live Chat</span></a></div><!-- END LivePerson Button code -->
*/
}

function loopLP() {
try {
	clearTimeout(Timer);
	Timer = null;
	Timer = setTimeout("loopLP()", 9000);
	lpCheckFindRepstate();
} catch (e) {  }
}

// Events Video
function showEventsVideo(id, scrollTo) {
	var $v = jQuery('#eventsvideo'+ id);
	var $l = jQuery('#eventsvideolink'+ id);
	var i = $v[0].innerHTML;
	var $scroll = scrollTo? jQuery(scrollTo): $l;
	
	if (i.indexOf('xframe') >= 0) {
		i = i.replace(/xframe/g, 'iframe');
		$v[0].innerHTML = i;
	}
	$v.slideToggle('normal', function() {
		var h = $l[0].innerHTML;
		if (h.indexOf('Show') >= 0) {
			$l[0].innerHTML = 'Close Video';
			$('html, body').animate({ scrollTop: $scroll.offset().top }, 600);
		} else {
			$l[0].innerHTML = 'Show Video';
			// need to make the video player stop
			i = i.replace(/iframe/g, 'xframe');
			$v[0].innerHTML = i;
		}
	});
	$l[0].blur();
}

// dbg(): provides on screen debugging strings
// usage: jQuery().dbg(STRING|OBJECT: WHAT_TO_PRINT, BOOLEAN: PRINT_AS_HTML_OR_TEXT, BOOLEAN: TO_CLEAR_PREVIOUS_MESSAGES);
// displays text or objects into a dbg field on the page
_dbgPrint = true;
_dbgCounter = 0;
dbg = function(message, asHTML, clear, err) {
	if (!_dbgPrint) return;

	if (typeof(message) == 'object') {
		string = '{';
		$.each(message, function(key, value) {
		  string += key + ': ' + value + ', ';
		});
		message = string + '}';
	} else if (message+'' == 'undefined')
		message = message+'';
	else
		message = '» '+ message;

	var $dbg = jQuery('#dbg');
	if ($dbg[0] == null) {
		jQuery('body').prepend('<div id="dbg" style="position: absolute; z-index: 999; background: white; font-size: 11px; color: blue; left: 0; top: 0; text-align:left;"></div>');
		$dbg = jQuery('#dbg');
		$dbg.css('opacity', 0.8);
	}

	if (clear != null && clear == true) $dbg.html('');
	var d = jQuery('<div class="log-message'+ (_dbgCounter++ % 2? ' log-altcolor': '') + (err? ' log-err': '') +'" />');
	if (asHTML) d.html(message).appendTo($dbg);
		else d.text(message).appendTo($dbg);
}; //: dbg()
