$j(document).ready(function(){
	/* Get and set the hash value */
	$j.fullHash = self.document.location.hash;
	$j.strHash = self.document.location.hash.toString().split('#')[1];
	/* CONFIGURE jQUERY VIDEO BOX */
	$j("a[rel^='videobox']")
		.each(function(e){ })
		.bind('click',function(event){
			event.preventDefault();
			var arr = $j(this).attr('rel').split('^');
			var videoLink = $j(this).attr('href');
			var maxHeight = 500;
			var contLeft = 0;
			var contTop = 0;
			if (arr[4] == 'fullscreen') {
				var calcHeight = $j(window).height() - 80;
				var contHeight = (maxHeight > calcHeight) ? calcHeight : maxHeight;
				var contWidth = mcms_EmbedVideo.scalePurportionally(parseInt(arr[2]),parseInt(arr[3]),contHeight);
			} else {
				var contHeight = parseInt(arr[3]);
				var contWidth = parseInt(arr[2]);
			}
			function updateWidths() {
				var contLeft = ($j(window).width()/2) - (contWidth/2) - 35;
				var contTop = ($j(window).height()/2) - (contHeight/2) - 10 + $j(window).scrollTop();
				$j('#videoContainer').css({ left:contLeft, top:contTop, width:contWidth+20, height:contHeight })
				$j('#videoOverlay').css({ width: $j(document).width(), height: $j(document).height() });
			}
			function clearVideo() {
				$j('#videoContainer').remove();
				$j('#videoOverlay').fadeOut(250, function(){ $j(this).remove(); });
				$j(window).unbind('resize',updateWidths);
			}
			$j('body').append('<div id="videoOverlay"></div><div id="videoContainer"><div id="videoViewer"></div><a href="#" id="videoCloseTab">Close Video</a></div>');
			$j(window).bind('resize',updateWidths);
			$j(window).triggerHandler('resize');
			$j('#videoViewer').css({ width:contWidth, height:contHeight });
			$j('#videoCloseTab').click(function(){ clearVideo(); return false; });
			$j('#videoOverlay')
				.click(function(){
					clearVideo();
				})
				.fadeIn(250,function(){
					$j('#videoContainer')
						.fadeIn(250, function() {
							switch(arr[1]) {
								case 'flv' :
									mcms_EmbedVideo.flv(eval('flvObj'+arr[4]),'videoViewer',contWidth,contHeight-35);
									break;
								case 'iframe':
									mcms_EmbedVideo.iframe(videoLink,'videoViewer',contWidth,contHeight);
									break;
								case 'script':
									mcms_EmbedVideo.script(videoLink,'videoViewer',contWidth,contHeight);
									break;
								default:
									mcms_EmbedVideo.flash(videoLink,'videoViewer',contWidth,contHeight);
							}
							$j('div#videoViewerEmbed').css({ padding:0 });
						});
				});
		});
	if($j.fullHash.length > 0) {
		$j.scrollTo($j('a[name="'+$j.strHash+'"]'),500,{ offset:-10, onAfter:function(){ $j('a[id="'+$j.strHash+'-poplink"]').triggerHandler('click'); }});
	}
	$j('span.vidlink').localScroll({ duration:500, offset:-10 });
});

function showVideo(flv,divCount,isMov,width,height,autoStart,autoLoop,startVol,centerControls,controlsColor,statusColor,bgColor,bgAlpha) {

	if (!isMov) isMov = false;
	if (!width) width = 320;
	if (!height) height = 240;
	if (!autoStart && autoStart != false) autoStart = true;
	if (!autoLoop && autoLoop != false) autoLoop = false;
	if (!startVol && startVol != 0) startVol = 75;
	if (!centerControls && centerControls != false) centerControls = true;
	if (!controlsColor) controlsColor = "E7E9DC";
	if (!statusColor) statusColor = "F5EEDA";
	if (!bgColor) bgColor = "50361B";
	if (!bgAlpha && bgAlpha != 0) bgAlpha = 65;
	
	//Turn off all unneeded div tags
	for (i=0;i<divList.length;i++) {
		//eval("document.getElementById('video-window"+i+"').innerHTML = ''");
		//eval("document.getElementById('video-frame"+i+"').style.display = 'none'");
		//eval("document.getElementById('videolist-entry"+i+"').style.border = '0px'");
		if (document.getElementById('video-window'+i)) {
			document.getElementById('video-window'+i).innerHTML = '';
			document.getElementById('video-frame'+i).style.display = 'none';
			document.getElementById('videolist-entry'+i).style.border = '0px';
		}
	}
	
	//set up the requested video div
	ele = 'video-frame' + divCount;
	eleVid = 'video-window' + divCount;
	eleEntry = 'videolist-entry' + divCount;
	
	//frame display
	document.getElementById(ele).style.display = 'block';
	document.getElementById(ele).style.display = 'none';
	document.getElementById(ele).style.display = 'block';
	//add highlight border to video entry
	document.getElementById(eleEntry).style.border = '10px #506C2F solid';
	document.getElementById(eleEntry).style.borderTop = '0px';
	//video window display
	document.getElementById(eleVid).style.display = 'block';
	document.getElementById(eleVid).style.display = 'none';
	document.getElementById(eleVid).style.display = 'block';
	
	//player variables
	mediaDir = '/media/';
	playerDir = '/flash/';
	
	//write iframe
	if (!isMov) //load flash player
		document.getElementById(eleVid).innerHTML = '<iframe src="video_gallery_player.cfm?flv='+flv+'&width='+width+'&height='+height+'" name="playerframe" id="playerframe" style="position:relative;display:block;" width="'+width+'" height="'+height+'" marginwidth="0" marginheight="0" hspace="0" vspace="0" scrolling="no" frameborder="0"></iframe>';
	else //load quicktime player
		document.getElementById(eleVid).innerHTML = '<iframe src="video_gallery_player.cfm?mov='+flv+'&width='+width+'&height='+height+'" name="playerframe" id="playerframe" style="position:relative;display:block;" width="'+width+'" height="'+(height+15)+'" marginwidth="0" marginheight="0" hspace="0" vspace="0" scrolling="no" frameborder="0"></iframe>';
}
function loadCategory(cat,div) {
	loadCFResults('/incs/video_categorylist.cfm?category='+cat+'&getCount=1','getValue','','rebuildDivArray(',')');
	loadCFResults('/incs/video_categorylist.cfm?category='+cat+'&getList=1','loadData',div);
}
function rebuildDivArray(num) {
	divList = null;
	divList = new Array();
	for(i=0;i<num;i++) {
		divList.push('video-frame'+i);
	}
}

/////////////////////
// AJAX FUNCTIONS
/////////////////////

function ajaxOpenObject() {
    var req = "";
	req = false;
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	/*
	if(req) {
		req.onreadystatechange=function() {
			if (req.readyState==4) {
				updateAJAXVar(req.responseText);
		 	}
		}
		req.open("GET", url, true);
		req.send(null);
	} else {
		updateAJAXVar('');
	}
	*/	
	return req;
}

function loadCFResults(url,type,div,funcStart,funcEnd) { //url = string, div = element id, type = "loadData" or "getValue"
	if (type == 'loadData') {
		//Loading text
		var startHTML = '<p style="margin:15px 0px 15px 0px">';
		var endHTML = '</p>';
		var loadHTML = '<img src="/images/ajax_loading.gif" align="absmiddle" style="margin-right:5px"><strong>Loading video list...</strong>';
		var errHTML = '<strong>We\'re sorry</strong> -- this info is not available.';
		
		containerDIV = document.getElementById(div);
		containerDIV.innerHTML = startHTML + loadHTML + endHTML;
	}
	//AJAX request call
	var req = ajaxOpenObject();
	if(req) {
		req.onreadystatechange=function() {
			if (req.readyState==4) {
				data = req.responseText;
				if (type == 'loadData') {
					updatepage(data,div);
				} else if (type == 'getValue') {
					eval(funcStart+'\''+data+'\''+funcEnd);
				}
				req = null;
		 	}
		}
		req.open("GET", url, true);
		req.send(null);
	} else {
		if (type == 'loadData') {
			containerDIV.innerHTML = startHTML + errHTML + endHTML;
		} else if (type == 'getValue') {
			return false;
		}
		req = null;
	}
}

function updatepage(str,div){
	//alert(str);
	document.getElementById(div).innerHTML = str;
	afterUpdate();
}

function afterUpdate() {

}
