/**
 * Javascript
 *
 */


// do this stuff once the DOM is ready
$(document).ready(function() {
						   
	initRollovers($(".rollover"));		// pass where to apply rollover events
	preloadImages($(".rollover"));		// preload rollover images

});





/* Functions
----------------------------------------------------------------------*/




/**
 * Binds event handlers to swaps images for rollovers.
 *
 *	Expects: 			one or more DOM elements.
 *
 *  Elements should be:	an IMG node,
 *						an INPUT node of type 'image',
 *						an A or DIV node containing either of the above.
 *
 *  Image names must end with _over.* and _off.* to qualify for swapping.
 *		(ex. "btn-home_off.gif" triggers "btn-home_over.gif" on mouseover)
 *
 */
function initRollovers(domChunk) {

	var elems;
	var overSrc;

	// assign event handlers
	domChunk.bind("mouseover", function () {
		// find images that need to swapped
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_off.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}

		// do the swap
		elems.each(function () {
			overSrc = this.src.replace(/\_off\./, "_on.");
			this.src = overSrc;
		});
	});
	domChunk.bind("mouseout", function () {
		// find images that need to swapped
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_on.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}
		
		// do the swap
		elems.each(function () {
			overSrc = this.src.replace(/\_on\./, "_off.");
			this.src = overSrc;
		});
	});
}


/**
 * Preload images used in rollovers.
 */
function preloadImages(domChunk) {
	
	var elems;
	var preloadSrc;
	var preloadObj;
	
	domChunk.each(function () {
		
		// find images inside element that need to be preloaded
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_off.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}
		
		// preload them
		elems.each(function () {
			preloadSrc = this.src.replace(/\_off\./, '_over.');
			preloadObj = new Image();
			preloadObj.src = preloadSrc;
		});
		
	});
}




// fix scrollbar bug in Firefox/Mac
function tb_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
$(document).ready(function(){ 
	if (tb_detectMacXFF()) {
		$("#nav div").bind("mouseover", function() {
			$("#content").css("overflow", "hidden");
		});
		$("#nav div").bind("mouseout", function() {
			$("#content").css("overflow", "auto");
		});
	}
});




/**
 * Function:	Loads the main flash movie (the one not in a popup.)
 *
 */
function loadMainMovie() {
	var video = mainVideo;
	var so = new SWFObject("flash/videoplayer.swf", "flash", "265", "230", "9", "#ffffff");
	so.addParam("scale", "noscale");
	so.addParam("quality", "best");
	so.addVariable("video", video);
	so.addVariable("autoplay", false);
	so.write("media");
}

/**
 * Function:	Rewinds and stops the main flash movie (the one not in a popup.)
 *
 */
function rewindMainMovie() {
	if (document.getElementById('flash')) {
		document.getElementById('flash').stopVideo();
	}
}

function setupFlash () {
	loadMainMovie(); // Loads the main flash movie (the one not in a popup.)
	
	$("a.flashbox").click(function () {
		$("div.media").children().css("visibility", "hidden"); // hide the non-popup video.
		rewindMainMovie();
	});


	var oldFunc = FB_remove;
	var newFunc = function () {
		$("div.media").children().css("visibility", "visible"); // show the non-popup video.
		oldFunc();
	}
	FB_remove = newFunc; // override Flashbox remove function
}