//jQuery
$(document).ready(function() {

	//declare globals					   
	var preloadImg = new Image();  //preload the clicked image
	var contentAnchor = document.createElement('img'); //container for the preload image
	var aArray;
	var aIndex;
	//attach behavior to prev image link
	$('#pPrevImg').click(function(){
		if(aIndex != 0){
			openContent(aArray[aIndex-1],true);
		}
		return false;
	})
	//attach behavoir to next image link
	$('#pNextImg').click(function(){
		if(aIndex != aArray.length){
			openContent(aArray[aIndex+1],true);
		}
		return false;
	})
	
	//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/taFlash3-10.swf';
		var params = {};
		params.quality = "high";
		params.wmode = 'transparent';
		params.scale = 'noscale';
		params.salign = "lt";
		var attributes = {};
		swfobject.embedSWF("/flash/slideshows/slideLoader.swf", "cTaFlash", "775", "333", "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);
	};
	//Embed PK Unloading Page Flash
	if($('#cPkAdMedia')){
		var flashvars = {};
		flashvars.videofile = '/parker/video/vidlib/unloading/video.mp4';
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		swfobject.embedSWF("/flash/parkeradmedia/unloadingMain.swf", "cPkAdMedia", "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(){
		openContent(this,false);
		return false;
	});
	
	function openContent(cThis,op){

		if(op){
			prepContainer();
		}else{
			prepContent();	
		}

		var captionTxt = document.createTextNode(cThis.title); //text node for caption
		
		with(contentAnchor){
			src = cThis.href;
			alt = cThis.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 - 40) / 2 + $(window).scrollTop())
			},350, function(){
				$('#pActiveContent').animate({
					"height" : Math.floor(newH)
				},350, function(){
					$('#pActiveContent').animate({
						"width" : Math.floor(newW)
					},350, function(){
						$("#pActiveMedia").css({height : Math.floor(newH)}).fadeIn(350);
						$('#pActiveCaption').css({
							width : Math.floor(newW),
							height : 'auto'
						}).fadeIn(200);
						//Test if image is part of an image table
						if(isPhotoGroup(cThis)){
						//set photo links to current table
						aArray = $(cThis).closest('.cThumb').find('a');
						aIndex = aArray.index(cThis);
						$('#pPrevImg').show();
						$('#pNextImg').show();			
						if(aIndex == 0){
							$('#pPrevImg').hide();
						}else if(aIndex+1 == aArray.length){
							$('#pNextImg').hide();
						}
					}
					});
				});
			});
		}
		preloadImg.src = cThis.href;
	};
	//Close Button Functionality
	$("a[href='#close']").click(function(){
		$('#pActiveOverlay').fadeOut(250,function(){
			$('#pActiveOverlay').hide();
		});
		$("#pActiveOuter").fadeOut(250,function(){
			prepContainer();
		});
		return false;
	});
	
	//Open Flash Video Function
	$("a[href$='.flv'],a[href$='.swf'],a[href$='.mp4']").click(function(){
		openFlash(this);
		return false;
	});
	function openFlash(cThis){
		prepContent();

		var flashDiv = document.createElement('div');//create container div for flash to load into
		with(flashDiv){
			id = 'flashDiv';
		};
		$('#pActiveMedia').append(flashDiv);
		var flashvars = {};
		flashvars.videofile = cThis.href;
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		if(cThis.href.indexOf('.flv') != -1 || cThis.href.indexOf('.mp4') != -1){
			swfobject.embedSWF("/flash/flvplayer/allplayer2.1.swf", "flashDiv", "600", "400", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
			fHeight = 400;
			fWidth = 600;
		}else{
			swfobject.embedSWF(cThis, "flashDiv", "550", "400", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
			fHeight = 400;
			fWidth = 550;
		}
		
		$('#pActiveOuter').animate({
			"top" : Math.floor(($(window).height() - fHeight - 40) / 2 + $(window).scrollTop())
		},350, function(){
			$('#pActiveContent').animate({
				"height" : Math.floor(fHeight),
				"width" : Math.floor(fWidth)
			},350, function(){
				$("#pActiveMedia").css({height : Math.floor(fHeight)}).fadeIn(350);
				$('#pActiveCaption').css({
					width : Math.floor(fWidth),
					height : '1.5em'
				}).fadeIn(200);
			});
		});
		return false;
	};
	
	function prepContent(){
		prepContainer();
		$('#pActiveOverlay').fadeTo(1,0, function(){
			$('#pActiveOverlay').show().fadeTo(350,0.75).css({
				"height" : $(document).height()
			});
		});
		$("#pActiveOuter").css({
			'top' : Math.floor(($(window).height() - $("#pActiveOuter").height()) / 2 + $(window).scrollTop())
		}).fadeIn(350);
	}
	
	function prepContainer(){
		$('#pActiveMedia').empty().hide();
		$('#pActiveCaption').empty().hide();
		$('#pPrevImg').hide();
		$('#pNextImg').hide();
	}
	
	function isPhotoGroup(clicked){
		return $(clicked).parents('.cThumb')[0];
	}
	
});