//jQuery

//ShadowBox Effect for Images
$(document).ready(function() {
	//Embed KB Flash on Index
	if($('#cKbFlash')){
		var flashvars = {};
		flashvars.fileName = '/flash/slideshows/kb/kbFlash.swf';
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		swfobject.embedSWF("/flash/slideshows/slideLoader.swf", "cKbFlash", "752", "300", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
	};
	//Embed TA Flash on Index
	if($('#cTaFlash')){
		var flashvars = {};
		flashvars.fileName = '/flash/slideshows/ta/taFlash2-12.swf';
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		swfobject.embedSWF("/flash/slideshows/slideLoader.swf", "cTaFlash", "752", "300", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
	};
	//Embed PK Flash on Index
	if($('#cPkFlash')){
		var flashvars = {};
		flashvars.fileName = '/flash/slideshows/pk/pkFlash.swf';
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		swfobject.embedSWF("/flash/slideshows/slideLoader.swf", "cPkFlash", "752", "300", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
	};
	//Add gif to indicate PDF Link
	var pdfImg = new Image();
	with(pdfImg){
		src = '/images/layout/pdf_logo.gif';
		height = 20;
		width = 20;
		alt = 'PDF File Link';
		className = 'noformat';
	}
	$("a[href$='.pdf']").append(pdfImg);
	
	//Add gif to indicate external sites
	var extImg = new Image();
	with(extImg){
		src = '/images/layout/external.gif';
		height = 10;
		width = 10;
		alt = 'External Link';
		className = 'noformat';
	}
	$("a[href^='http://']").append(extImg);
	
	//Open Image Function
	$("a[href^='/images/']").click(function(){
		$('#pActiveMedia').children().remove();
		$('#pActiveCaption').children().remove();
		$('#pActiveOverlay').fadeTo(1,0, function(){
			$('#pActiveOverlay').show().fadeTo(500,0.70).css({
				"height" : $(document).height()
			});
		});
		$("#pActiveOuter").css({
			'top' : Math.floor(($(window).height() - $("#pActiveOuter").height()) / 2 + $(window).scrollTop())
		}).fadeIn();
		
		var captionTxt = document.createTextNode(this.title); //text node for caption
		var contentAnchor = document.createElement('img'); //container for the preload image
		var preloadImg = new Image();  //preload the clicked image
		with(contentAnchor){
			src = this.href;
			alt = this.title;
			id = "media";
		}
		preloadImg.onload = function(){
			var imgH = preloadImg.height;
			var imgW = preloadImg.width;
			
			with(contentAnchor){
				height = imgH;
				width = imgW;
			}
			
			$('#pActiveCaption').append(captionTxt);
			$('#pActiveMedia').append(contentAnchor);
			
			var winH = $(window).height() * .8;
			var winW = $(window).width() * .8;
			var scale = imgW / imgH;
			
			if(winH <= imgH){
				newH = winH;
				newW = newH * scale;
			}else if(winW <= imgW){
				newW = winW;
				newH = newW / scale;
			}else{
				newH = imgH;
				newW = imgW;
			}
			
			$('#media').attr({
				height : newH,
				width : newW
			});
			
			$('#pActiveOuter').animate({
				"top" : Math.floor(($(window).height() - newH - 45) / 2 + $(window).scrollTop())
			},500, function(){
				$('#pActiveContainer').animate({
					"height" : Math.floor(newH + 35),
					"width" : Math.floor(newW)
				},500, function(){
					$("#pActiveMedia").css({height : Math.floor(newH)});
					$("#pActiveContent").fadeIn(500);
				});
			});
		}
		preloadImg.src = this.href;
		return false;
	});
	//Close Button Functionality
	$("a[href='#close']").click(function(){
		$('#pActiveOverlay').fadeOut(function(){
			$('#pActiveOverlay').hide();
		});
		$("#pActiveOuter").fadeOut(function(){
			$('#pActiveMedia').empty();
			$('#pActiveCaption').empty();
			$("#pActiveContent").hide();
		});
		return false;
	});
	
	//Open Flash Video Function
	$("a[href^='/topair/video/vidlib/']").click(function(){	
		$('#pActiveMedia').children().remove();
		$('#pActiveCaption').children().remove();
		$('#pActiveOverlay').fadeTo(1,0, function(){
			$('#pActiveOverlay').show().fadeTo(500,0.70).css({
				"height" : $(document).height()
			});
		});
		$("#pActiveOuter").css({
			'top' : Math.floor(($(window).height() - $("#pActiveOuter").height()) / 2 + $(window).scrollTop())
		}).fadeIn();

		var flashDiv = document.createElement('div');//create container div for flash to load into
		with(flashDiv){
			id = 'flashDiv';
		};
		$('#pActiveMedia').append(flashDiv);
		var flashvars = {};
		flashvars.videofile = this.href;
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		if(this.href.indexOf('.flv') != -1){
			swfobject.embedSWF("/flash/flvplayer/allplayer2.1.swf", "flashDiv", "600", "400", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
		}else{
			swfobject.embedSWF(this, "flashDiv", "550", "400", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
		}
		
		$('#pActiveOuter').animate({
			"top" : Math.floor(($(window).height() - 400 - 45) / 2 + $(window).scrollTop())
		},500, function(){
			$('#pActiveContainer').animate({
				"height" : Math.floor(400 + 35),
				"width" : Math.floor(600)
			},500, function(){
				$("#pActiveMedia").css({height : Math.floor(400)});
				$("#pActiveContent").fadeIn(500);
			});
		});
		
		return false;
	});
});