function queryString(varname)
{
	varname = varname.replace(/[\[]/,"\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + varname + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)
	{
		return "";
	}
	else
	{
		return results[1];
	}
}
function visibleHeight() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		return window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		return document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		return document.body.clientHeight;
	  }
}
function recoverFullScreen() {
	window.scrollTo(0,scrollTop()+1);
}
function scrollTop() {
	if (window.pageYOffset) {
        return window.pageYOffset;
    } else {
        return (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
}
function rateVideo(videoID, rating) {			
	var q = 'nocache='+new Date().getTime();
		q += '&videoID='+videoID;
		q += '&rating='+rating;

	var myAjax = new Ajax.Request(
	'/ajax/rate-video.php', 
	{
		parameters: q,
		onComplete: function(response) { eval(response.responseText);}
	});
	
	$('ratingControl').innerHTML = 'Thank you for voting';
}

function addVideoFavorite(videoID) {			
	var q = 'nocache='+new Date().getTime();
		q += '&videoID='+videoID;

	var myAjax = new Ajax.Request(
	'/ajax/add-favorite-video.php', 
	{
		parameters: q,
		onComplete: function(response) { eval(response.responseText);}
	});
	
	$('favoriteControl').innerHTML = '<p><img src="/images/heart.png" alt="" /> This video is marked as one of your favorites</p>';
}

function postComment(videoID, comment) {	
	var q = 'nocache='+new Date().getTime();
		q += '&videoID='+videoID;
		q += '&comment='+encodeURIComponent(comment);

	var myAjax = new Ajax.Request(
	'/ajax/post-comment.php', 
	{
		parameters: q,
		onComplete: function(response) { eval(response.responseText);}
	});
}

function lowerLights() {
	
	//new Insertion.Top(document.body, '<div id="lights_overlay"></div>');
	$('lights_overlay').style.display = 'block';
	
	Event.observe(window, 'resize', resizeOverlay, true);
	Event.observe(window, 'scroll', resizeOverlay, true);
	resizeOverlay();
}
function raiseLights() {
	//Element.remove('lights_overlay');
	$('lights_overlay').style.display = 'none';
	
	Event.stopObserving(window, 'resize', resizeOverlay, true);
	Event.stopObserving(window, 'scroll', resizeOverlay, true);
}

function resizeOverlay() {
	if (document.getElementById('lights_overlay')) {
		document.getElementById('lights_overlay').style.height = document.documentElement.scrollHeight+"px";
	}
}
function positionVideo() {
	if (document.getElementById('video_overlay')) {		
		document.getElementById('video_overlay_bg').style.height = document.documentElement.scrollHeight+"px";
		
		var w = self.innerWidth || document.body.clientWidth || (de&&de.clientWidth);
		document.getElementById('video_overlay').style.top = (visibleHeight()/2-207+scrollTop())+"px";
		document.getElementById('video_overlay').style.left = (w/2-290)+"px";
	}
}
/*
function flv(video,playhead_position,media_type,reset){
	if (!playhead_position) playhead_position=0;
	if (!reset) reset=0;
	output = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	output += 'width="540" height="390" id="640_tt" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="swf/540_tt.swf" />';
	output += '<param name=FlashVars value="video='+video+'&playhead_position='+playhead_position+'&media_type='+media_type+'" /> <param name="menu" value="false" />';
	output += '<param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#4d4d4d" />';
	output += '<embed src="swf/540_tt.swf?reset='+reset+'" FlashVars="video='+video+'&playhead_position='+playhead_position+'&media_type='+media_type+'" menu="false" quality="high" ';
	output += '	wmode="transparent" bgcolor="#4d4d4d" width="540" height="390" name="640_tt" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" ';
	output += '	pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	document.write(output);
}*/
function flv(path, videoTitle, playhead_position, reset, videoID, ratingVal, width, height, id, popup, category, participantID, topicID) {
	if (!path) 		return;
	if (!width) 	width 	= 615;
	if (!height) 	height 	= 427;
	if (!id) 		id 		= 'videoPlayer';
	if (!videoID)	videoID = 0;
	if (!category)	category = '';
	if (!participantID)	participantID = 0;
	if (!topicID)	topicID = 0;
	if (!ratingVal)	ratingVal = 0;
	
	if (!playhead_position) playhead_position	= 0;
	if (!reset) 			reset				= 0;
	if (!popup)				popup				= 0;
	
	//id = getUniqueID(id);
	
	//document.write('<div id="'+id+'"></div>');
	var so = new SWFObject("/swf/new_540_tt.swf?20100812", "video_"+id, width, height, "8", "#000000");
    so.addVariable("video", path);
    so.addVariable("reset", reset);
    so.addVariable("playhead_position", playhead_position);
    so.addVariable("media_type", 'flv');
    so.addVariable("videoID", videoID);
    so.addVariable("category", category);
    so.addVariable("participantID", participantID);
    so.addVariable("topicID", topicID);
    so.addVariable("ratingVal", ratingVal);
    so.addVariable("popup", popup);
    so.addVariable("videoTitle", videoTitle);
    so.addParam("allowFullScreen", "true");
    so.addParam("wmode", "transparent");
    so.addParam("allowScriptAccess", "always");
    so.addParam("quality", "high");
    so.addParam("menu", "false");
    so.addParam("align", "middle");
    
    so.write(id);
}

// utility function to make sure IDs aren't duplicated
function getUniqueID(id) {
	if (document.getElementById(id)) {
		i = 1;
		do {
			i++;
			newID = id + '_' + i;
		} while (document.getElementById(newID))
		id = newID;
	}
	return id;
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

