// -------------- Video Statistics --------------
var userClick=false;
var WMP=new Object();
window.onload=function(){
	initStats();
}
function initStats() {	
	if((navigator.appName=="Microsoft Internet Explorer"  || navigator.userAgent.indexOf("Chrome")>-1) && document.getElementById("MPlayer")){
		WMP=document.getElementById("MPlayer");
		WMP.attachEvent("playStateChange",stateChange);
		WMP.attachEvent("MouseDown",clickEvent);
	}
}
function clickEvent(){
	userClick=true;
}					
function stateChange(){    				
	// Do not count the firstplay in statistics
	if(userClick && WMP.playstate==3) {						
		var fn=WMP.url;
		if(fn.indexOf("playlist.asx?")>0){
			ajaxStats(fn.substr(fn.indexOf("=")+1,fn.length-fn.indexOf("=")));
			userClick=false;
		}
	}
}
function ajaxStats(id)
{		
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 	
	if (id>'') {
		var url="/stats/vidstats.asp?gid="+id;
		url+="&sid="+Math.random();		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);	
	}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();  
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("MSXML2.XMLHTTP");	
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	
    }
  }
return xmlHttp;
}
// -------------- Video Statistics --------------

function toggle_sel(obj) {
	if (obj.id.substring(0,1) == 'x') {
		obj.style.display = 'none';
		document.getElementById(obj.id.replace('x','y')).style.display = 'block';
	} else {
		obj.style.display = 'none';
		document.getElementById(obj.id.replace('y','x')).style.display = 'block';
	}	
}

function playHome(filepath,gal_id)
{		
	var gidPos = location.href.indexOf('&gid');
	if (gidPos==-1) 
		loc = location.href+'&gid='+gal_id;
	else
		loc = String(location.href).replace('&gid='+getQS('gid'),"&gid="+gal_id);
	
    if (navigator.appName == "Microsoft Internet Explorer" || navigator.userAgent.indexOf("Chrome")>-1)
    {
		document.getElementById('MPlayer').url=filepath;			
		document.getElementById('MPlayer').controls.play();
		window.scrollTo(0,500);
    }
    else
    {  		  
		document.location=loc;					
    }		
}

function playVideo(filepath,gal_id)
{	
	var loc
	
	document.getElementById('y'+gal_id).style.display = 'block';
	document.getElementById('x'+gal_id).style.display = 'none';
	
	var gidPos = location.href.indexOf('&gid');
	if (gidPos==-1) 
		loc = location.href+'&gid='+gal_id;
	else
		loc = String(location.href).replace('&gid='+getQS('gid'),"&gid="+gal_id);
		
	// Get rid of the #top
	loc = loc.replace("#top","");		
    if (navigator.appName == "Microsoft Internet Explorer" || navigator.userAgent.indexOf("Chrome")>-1)
    {
		if (document.getElementById('video_title')) {		
			document.getElementById('video_title').innerHTML=document.getElementById('t'+gal_id).innerHTML;
			document.getElementById('video_desc').innerHTML='<br><strong>URL:</strong><br><input type="text" value="'+loc+'" class="video_url" onClick="javascript:this.focus();this.select();" readonly="true" style="width:95%;font-size:9px;">';

			if (document.getElementById('t'+gal_id).rel) {
				document.getElementById('video_desc').innerHTML+="<div id=\"info\"><p><strong>Views: </strong>"+document.getElementById('t'+gal_id).rel+"</p></div>";
			}
		}
		
		document.getElementById('MPlayer').url=filepath;			
		document.getElementById('MPlayer').controls.play();
    }
    else
    {  		  
		document.location=loc;					
    }
	window.scrollTo(0,300);	
}

function toggleSchedule() {
	var schobj = document.getElementById('schedulePopup');
	if (schobj.style.display == 'none')
		schobj.style.display = 'block';
	else
		schobj.style.display = 'none';	
}

function getQS(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
	alert('Query Variable ' + variable + ' not found'); 
} 
