
uiFadeRolloverSpeed = 500;
uiHomeAnimationTime = "";
uiPauseHomeAnimation = false;
//Fires when the DOM is ready to be traversed.
$(document).ready(function()
{
	

	if(!$.browser.safari)
	{
		$(".wrap_center_vert").each(function(){
			var wrapper = "<table class=\"main_table\"><tr><td align=\"center\" valign=\"middle\"></td></tr></table>";										 
			$(this).wrap(wrapper);
		});
	}
	else
	{
		$(".wrap_center_vert").each(function(){
			var wrapper = "<div class=\"main_table\"></div>";										 
			$(this).wrap(wrapper);
		});
	}

	
	loadBehaviors();


});


//Fires when all content is fully loaded
$(window).load(function()
{

	waitForContentToLoad(function()
	{
		$(".main_div").fadeIn(300);
		advanceHomeAnimation();
		
		var curloc = new String(window.location.href);
				
		if(curloc.indexOf("#") > -1 && curloc.length > curloc.indexOf("#") + 1)
		{
			curloc = curloc.substr(curloc.indexOf("#") + 1, curloc.length - curloc.indexOf("#"));
			showPopin(curloc);
		}					  
	});

});

function loadScrollPane()
{
	$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 10, arrowSize: 5, dragMaxHeight: 9});	
}

function loadBehaviors()
{
	
	
			
	$(".ui-work-swap-image").each(function()
	{
		if($(this).attr("effect_loaded-ui-work-swap-image") == "true") return;		
		$(this).attr("effect_loaded-ui-work-swap-image", "true"); 
		$(this).bind("click", function(){
			if($(this).attr("href").indexOf(".flv") > -1)
			{
				playVideo($(this).attr("href"), getFLVHeightWidthFromClass($(this).attr("class")))
			}
			else
			{
				swapImageFade("bc_swap_image", $(this).attr("href"));
			}
			return false;
		});
															 
	});
	
	
	$(".nav_items ul li a").each(function(){
		if($(this).attr("effect_loaded-list-animation") == "true") return;		
		$(this).attr("effect_loaded-list-animation", "true"); 
		$(this).bind("click", function(){
			return animateNavigation($(this), true);
		});
															 
	});
	
	
	$(".ui-navigate-popin, .ui-navigate-popin-partial").each(function(){
		if($(this).attr("effect_loaded-ui-navigate-popin") == "true") return;
		$(this).attr("effect_loaded-ui-navigate-popin", "true"); 								  
	
		$(this).bind("click", function(e){
			showPopin($(this).attr("href"));
			return false;
		});
	});
	
	$(".ui-swap-rollover").each(function(){
		if($(this).attr("effect_loaded-ui-swap-rollover") == "true") return;
		$(this).attr("effect_loaded-ui-swap-rollover", "true"); 
		
		if($(this).attr("src").indexOf("_off") > 0)
		{
			$(this).bind("mouseenter", function(e){
				$(this).attr("src", $(this).attr("src").replace("_off", "_over"));
			});
			$(this).bind("mouseleave", function(e){
				$(this).attr("src", $(this).attr("src").replace("_over", "_off"));
			});
		}
	
	});	
	
	$(".ui-fade-rollover").each(function(){
		if($(this).attr("effect_loaded-ui-fade-rollover") == "true") return;
		$(this).attr("effect_loaded-ui-fade-rollover", "true"); 
		if($(this).attr("alt") != null && curNavSection != "" && $(this).attr("alt").indexOf(curNavSection) != -1)
		{
			$(this).attr("src", $(this).attr("src").replace("_off", "_over"));
		}
		var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";
		if($(this).parent().get(0).tagName == "A")
		{
			$(this).parent().wrap(wrapper);
		}
		else
		{
			$(this).wrap(wrapper);
		}
		
		$(this).bind("mouseenter", function(e){
			$(this).stop().animate({opacity: 0}, uiFadeRolloverSpeed);
		});
		$(this).bind("mouseleave", function(e){
			$(this).stop().animate({opacity: 1}, uiFadeRolloverSpeed);
		});
	
	});		
}

function launchFLVs()
{
	
	
	$(".flash_movie").each(function() {
			var height_width = getFLVHeightWidthFromClass($(this).attr("class"));
			var width = height_width.split("x")[0];
			var height = height_width.split("x")[1];
		   $(this).flash(
				{ src: '/flowplayer-3.0.7.swf', height: height, width: width, wmode: 'transparent' },
				{ version: 8 },
				function(htmlOptions) {
					$this = $(this);
					$(this).hide();
					htmlOptions.flashvars.config = '{"clip":{"url":"' + $this.attr('href') + '","bufferLength":"5","fadeInSpeed":"2000"},"plugins":{"controls":{"autoHide":"always","backgroundColor":"transparent","volume":false,"fullscreen":false,"mute":false}},"logo":{"fullscreenOnly":true}}';
					
					
					
					
					$this.before($.fn.flash.transform(htmlOptions));
				}
			);
	});
	
	/*
		$(".flash_movie").each(function() {
			var height_width = getFLVHeightWidthFromClass($(this).attr("class"));
			var width = height_width.split("x")[0];
			var height = height_width.split("x")[1];
		   $(this).flash(
				{ src: '/flvplayer.swf', height: height, width: width, wmode: 'window' },
				{ version: 8 },
				function(htmlOptions) {
					$this = $(this);
					$(this).hide();
					htmlOptions.flashvars.file = $this.attr('href');
					htmlOptions.flashvars.allowfullscreen = "false";
					htmlOptions.flashvars.displayheight = height;
					htmlOptions.flashvars.autostart = "true";
					htmlOptions.flashvars.screencolor = "000000";
					htmlOptions.flashvars.bufferlength = 25;
					$this.before($.fn.flash.transform(htmlOptions));
				}
			);
	});*/
}

function getFLVHeightWidthFromClass(in_class) {
	var classArr = in_class.split(" ");
	
	for (var i = 0; i < classArr.length; i++) {
		if (classArr[i].indexOf("flv_") != -1) {
			return classArr[i].substr(4);
		}
	}
	return "460x345";
}	

function playVideo(in_flv, in_dimensions)
{
	
	$("#video_player div.video").html("<a class=\"flash_movie flv_" + in_dimensions + "\" href=\"" + in_flv + "\"></a>");

	launchFLVs();

	$("#video_player").fadeIn(200);		
	
	
	
}

function closeVideo()
{
	$("#video_player div.video").html("");
	$("#video_player").fadeOut(300);
}

function animateNavigation(item, navigate) {


    if ($(item).parent("li").children("ul:first").is(":visible")) {
        $(item).parent("li").children("ul").slideUp(300);
    }
    else {
        $(item).parent("li").children("ul").slideDown(300);
    }

    $(item).parent("li").siblings().find("ul").slideUp(300);

	return false;

}

function persistNavigation(in_href)
{
	in_href = stripHost(in_href);
    var curloc = new String(window.location.href);

    if(curloc.indexOf("#") > -1)
        curloc = curloc.substr(0, curloc.indexOf("#") - 1);

    window.location.href = curloc + "#" + in_href;
}

function stripHost(in_val)
{

	if(in_val.indexOf(http_host) > -1)
	{
		in_val = in_val.substr(in_val.indexOf(http_host) + http_host.length - 1);
	}
	return in_val;
}

function swapImageFade(in_id, in_src)
{
	$(new Image()).load(function() {
		var obj = $("#" + in_id);
		
		if(!$(obj).is(":visible"))
		{
			$("#" + in_id + "_img").attr("src", in_src);
			$(obj).fadeIn(300);
		}
		else
		{
			$(obj).fadeOut(300, function(){
				$("#" + in_id + "_img").attr("src", in_src);
				$(this).fadeIn(300);						 
			});
			
		}
	}).attr('src', in_src);
	


}
var skipCount = 0;

function waitForContentToLoad(callbackFunction)
{
	if($.browser.msie)
	{
		callbackFunction();
	}
	else
	{
		skipCount = 0;
		vContentCheckInterval = setInterval(function() 
		{
			var bAllImagesAreLoaded = true;
			var images = $("img");
			
			for (var i = 0; i < images.length; i++) 
			{
				var img = images[i];
				if (img.complete == false)
				bAllImagesAreLoaded = false;
			}
			
			if (bAllImagesAreLoaded || skipCount > 40) 
			{
				callbackFunction();
				clearInterval(vContentCheckInterval);
			}
			else
			{
				skipCount ++;
			}
		}, 100);
	}

}

function showPopin(in_href)
{
	pauseHomeAnimation();
	in_href = stripHost(in_href);
	if($("#cover_layer").is(":visible"))
	{
		$("#popin_layer").fadeOut(300, function(){
			$(".popin_content_div").load(in_href + "?layout=AJAX&r=" + Math.random(), function(){
					waitForContentToLoad(function()
					{
						$("#popin_layer").fadeIn(300, function(){
							if ($.browser.msie) this.style.removeAttribute('filter');
							
						});
						loadBehaviors();  
						loadScrollPane();
					});	   
			});													 
		});
	}
	else
	{
		$(".popin_content_div").load(in_href + "?layout=AJAX&r=" + Math.random(), function(){
			waitForContentToLoad(function()
			{										  
				$("#cover_layer").css("opacity", 0);
				$("#cover_layer").show();
				$("#cover_layer").animate({opacity: .8}, 300, function(){
					$("#popin_layer").fadeIn(300);
					loadScrollPane();
				});	
				loadBehaviors();	

	
			});
				   
		});
	}
	

	
	persistNavigation(in_href);
	
	pageTracker._trackPageview(in_href);
	return false;
}


function closePopin()
{
	$("#popin_layer").fadeOut(300, function(){$(".popin_content_div").html("");});
	$("#cover_layer").fadeOut(400);
	
	persistNavigation("");
	uiPauseHomeAnimation = false;
	advanceHomeAnimation();
}

function animateSplashFade()
{

	if(!uiPauseHomeAnimation)
	{
		if($("#rotate_img").is(":visible"))
		{
			$(new Image()).load(function() 
			{
				$("#rotate_img").fadeOut(900, function()
				{
					$(this).attr("src", "/images/home/splash" + rotationCounter + ".jpg");
				});
				
					$("#rotate_img").fadeIn(900, advanceHomeAnimation);
				
			}).attr('src', "/images/home/splash" + rotationCounter + ".jpg");
		}
	}

}

function advanceHomeAnimation()
{
	if(!uiPauseHomeAnimation)
	{
		if($("#rotate_img").is(":visible"))
		{
			rotationCounter += 1;
			if(rotationCounter > 8)
			{
				rotationCounter = 1;	
			}
			
			uiHomeAnimationTime = setTimeout(animateSplashFade, 3000);
		}
	}

}

function pauseHomeAnimation()
{
	uiPauseHomeAnimation = true;
	clearTimeout(uiHomeAnimationTime);	
}