/* stop IE6 flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

// Return true if browser is Internet Explorer and version 6 or earlier
$.IE6Below = function() {
	if ($.browser.msie) {
		try {
			if (ScriptEngineMajorVersion() <= 5 && ScriptEngineMinorVersion() < 7) { // ie 6- only
				return true;
			}
		} catch(err) { return false; };
		return false;
	} else {
		return false;
	};
};

/* rotating images */
var initialized = false;
var current_image_number = 0;
var rotating_images = new Array();
var initial_rotation_delay = 3000; // lower this if you want the first image to fade very soon
var rotation_delay = 7000; // normal delay between image rotations
var fade_speed = 2000; // milliseconds e.g. 1000 or words e.g. "slow"

function rotate_images() {
	// if we haven't initialized let's do that now
	if (! initialized)
	{
		// use jquery to find any image with a class of "rotating_image" and add them to our array
		$(".rotating_image").each(function(i) {
			rotating_images[i] = this;
		});
		// randomize image order (comment this section out if not needed)
		function randomizeArray(myArray) {
		  var i = myArray.length;
		  if ( i == 0 ) return false;
		  while ( --i ) {
		     var j = Math.floor( Math.random() * ( i + 1 ) );
		     var tempi = myArray[i];
		     var tempj = myArray[j];
		     myArray[i] = tempj;
		     myArray[j] = tempi;
		   }
		}
		randomizeArray(rotating_images);
		$(rotating_images[0]).addClass('first_rotating_image');
		initialized = true;
		setTimeout("rotate_images()", initial_rotation_delay);
		return;
	}
	// if the first image doesn't exist, just exit
	if (! rotating_images[0]) return;
	// if the first image has downloaded, continue
	if (rotating_images[0].complete || rotating_images[0].complete == undefined)
	{
		// set the next image we should load
		var next_image_number = current_image_number + 1;
		if (next_image_number >= rotating_images.length) next_image_number = 0;
		// has this image been loaded
		if (rotating_images[next_image_number].complete || rotating_images[next_image_number].complete == undefined)
		{
			// ok, switch to this new image			
			$(rotating_images[current_image_number]).fadeOut(fade_speed);
			$(rotating_images[next_image_number]).fadeIn(fade_speed);
			// change the current image to this one
			current_image_number = next_image_number;
		}
		setTimeout("rotate_images()", rotation_delay);		
	}
	else
	{
		// not downloaded yet, check back later
		setTimeout("rotate_images()", initial_rotation_delay);
	}
};

// ** add active class to navs **
// **                          **
$(function(){ // (executes when the dom is ready)
	// search for nav-something or snav-something in the body tag
	// this tells us if a link should be active by default
	// (this is because each of our nav links uses an id, e.g. nav-home, so we define
	//  active links by simply declaring this id in the body tag of each page as a class name)
	$("body[@class*=nav-]").each(function(i) {
		var bodyClasses = $(this).attr("class").split(" ");
		var regex1 = /\bnav-\b/;
		var regex2 = /\bsnav-\b/;
		$.each(bodyClasses, function(i, n) {
			if (regex1.test(n) || regex2.test(n)) {
				// add class "active" to the first link in the li#nav-something or li#snav-something
				//$('#'+n+'>a').addClass("active");
				// see if we're dealing with a sub nav, if so it uses class instead of id
				if (regex2.test(n)) {
					//$('.'+n+'>a').addClass("active");
					$('.'+n).parent().addClass("active");
				};
			};
		 });
	});
});


// ** Misc **
// **      **
$(function(){ // (executes when the dom is ready)
	// make logo a link to go back to home page
	$("#logo-box h1").click(function(){
		document.location = "/";
	}).css("cursor","pointer");
	// make any links with rel="external" to open in new windows
	$("a[@rel$='external']").click(function(){
		this.target = "_blank";
	});
	// make rel="popup" open pop up windows
	$("a[@rel$='popup']").click(function(){
		window.open(this.href, '', 'menubar=0, toolbar=0, scrollbars=0, resizable=0, width=775, height=530');
		return false;
	});
	// make close button close the window (used with popup window)
	$("#close").click(function(){
		self.close();
	}).css("cursor","pointer");
	// stripe our table
	$("tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$("tr:even").addClass("even");
	// stripe out navigation
	$(".sub-nav-list>li:first").addClass("first");
	$(".sub-nav-list li ul li:first").addClass("first");
	$(".sub-nav-list>li:odd").addClass("even");
	// fix arrows
	if ($.os.name=="win") {
		$("#client-login a").addClass("winos");
	};
});

/* Tabbed page
*******************************************************/
// set up the navigation for the tabbed content 
$(function() {
	var tabContainers = $('#tab-content > div:not(#tab-head)');
	// initial hide/show of sections
	tabContainers.hide().filter(':first').show();
	
	$('ul#tab-secnav a').click(function () {
		// hide/show sections
		tabContainers.hide().filter(this.hash).fadeIn('slow');
		$(this).parent().addClass('active').siblings().removeClass('active');
		// if this is the first section, style with the 'initial' class
		if ($(this).parent().hasClass('first')) {
			$('#tab-head').addClass('initial');
		}
		else {
			$('#tab-head').removeClass('initial');
		}
		// set next link
		var nextLocation = $(this).parent().next().children().attr('href');
		if (nextLocation && !($(this).parent().next().hasClass('next'))) {
			if ($("ul#tab-secnav li.next").length == 0) {
				$("ul#tab-secnav").append('<li class="next"><a href="'+nextLocation+'">&nbsp;</a></li>');
				$('ul#tab-secnav li.next a').click(function () {
					$('ul#tab-secnav li.active').next().children().trigger('click');
				});
			}
			else {
				$("ul#tab-secnav li.next a").attr('href', nextLocation);
			}
		}
		else {
			$("ul#tab-secnav li.next").remove();
		}
		// set previous link
		var prevLocation = $(this).parent().prev().children().attr('href');
		if (prevLocation && !($(this).parent().prev().hasClass('previous'))) {
			if ($("ul#tab-secnav li.previous").length == 0) {
				$("ul#tab-secnav").prepend('<li class="previous"><a href="'+prevLocation+'">&nbsp;</a></li>');
				$('ul#tab-secnav li.previous a').click(function () {
					$('ul#tab-secnav li.active').prev().children().trigger('click');
				});
			}
			else {
				$("ul#tab-secnav li.previous a").attr('href', prevLocation);
			}
		}
		else {
			$("ul#tab-secnav li.previous").remove();
		}
		return false;
	}).filter(':first').click();
});

// links that trigger the tabbed content navigation
$(function() {
	$("#tab-content > div a[href^='#tab-section']").click(function() {
			var target = $(this).attr('href');
			$("ul#tab-secnav li:not(.next) a[href="+target+"]").trigger('click');
			return false;
	});
});

// overlap the main nav tabs
$(function() {
	$("ul#tab-nav li").each(function(i) {
		$(this).attr("style", "position:relative; left:-"+i+"px");
	});
});

// add next and previous links to tab subnav
$(function() {
	var currentLocation = window.location.pathname;
	var currentLink = $("ul#tab-subnav a[href='"+currentLocation+"']");
	var nextLocation = currentLink.parent().next().children().attr('href');
	var prevLocation = currentLink.parent().prev().children().attr('href');
	if (nextLocation) $("ul#tab-subnav").append('<li class="next"><a href="'+nextLocation+'">&nbsp;</a></li>');
	if (prevLocation) $("ul#tab-subnav").prepend('<li class="previous"><a href="'+prevLocation+'">&nbsp;</a></li>');
});

/* additional browser detection for jquery */
(function($){$.browserTest=function(a,z){var u='unknown',x='X',m=function(r,h){for(var i=0;i<h.length;i=i+1){r=r.replace(h[i][0],h[i][1]);}return r;},c=function(i,a,b,c){var r={name:m((a.exec(i)||[u,u])[1],b)};r[r.name]=true;r.version=(c.exec(i)||[x,x,x,x])[3];if(r.name.match(/safari/)&&r.version>400){r.version='2.0';}if(r.name==='presto'){r.version=($.browser.version>9.27)?'futhark':'linear_b';}r.versionNumber=parseFloat(r.version,10)||0;r.versionX=(r.version!==x)?(r.version+'').substr(0,1):x;r.className=r.name+r.versionX;return r;};a=(a.match(/Opera|Navigator|Minefield|KHTML|Chrome/)?m(a,[[/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/,''],['Chrome Safari','Chrome'],['KHTML','Konqueror'],['Minefield','Firefox'],['Navigator','Netscape']]):a).toLowerCase();$.browser=$.extend((!z)?$.browser:{},c(a,/(camino|chrome|firefox|netscape|konqueror|lynx|msie|opera|safari)/,[],/(camino|chrome|firefox|netscape|netscape6|opera|version|konqueror|lynx|msie|safari)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/));$.layout=c(a,/(gecko|konqueror|msie|opera|webkit)/,[['konqueror','khtml'],['msie','trident'],['opera','presto']],/(applewebkit|rv|konqueror|msie)(\:|\/|\s)([a-z0-9\.]*?)(\;|\)|\s)/);$.os={name:(/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase())||[u])[0].replace('sunos','solaris')};if(!z){$('html').addClass([$.os.name,$.browser.name,$.browser.className,$.layout.name,$.layout.className].join(' '));}};$.browserTest(navigator.userAgent);})(jQuery);