// JavaScript Document

//Hide script from older browsers // script by http://www.hypergurl.com var urlAddress 
function addToFavorites() { 
	var urlAddress = document.URL;
	try{
		if (window.external) { 
			window.external.AddFavorite(urlAddress,document.title) 
		} else { 
			alert("Sorry, can't add to favourites."); 
		}
	}catch(err){
		alert("Sorry, can't add to favourites."); 		
	}
}

function removeFromFavorites(){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, can't remove from favourites."); 
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var favDiv = document.getElementById('favsDiv');
			switch (xmlHttp.responseText){
				case "removed"	:	favDiv.innerHTML = '<img src="images/icon_fav.gif" align="top"/><a>Add to Favourites</a>';
									favDiv.onclick = addToMyFavorites;
									var vidResp = document.getElementById('videoResponse');
									vidResp.innerHTML = '<div class="embed_btm_content1">Video removed from favourites.</div>';
									break;

			}			
		}
	}
	
	xmlHttp.open("GET","chunks/removefav.php?videoID=" + mainVideoID,true);
	xmlHttp.send(null);
}

function addToMyFavorites(){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, can't add to favourites.");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var favDiv = document.getElementById('favsDiv');
			switch (xmlHttp.responseText){
				case "added"	:	favDiv.innerHTML = '<img src="images/icon_fav.gif" align="top"/><a>Remove from Favourites</a>';
									favDiv.onclick = removeFromFavorites;
									var vidResp = document.getElementById('videoResponse');
									vidResp.innerHTML = '<div class="embed_btm_content1">Video added to favourites.</div>';
									break;

			}			
		}
	}
	
	xmlHttp.open("GET","chunks/addfav.php?videoID=" + mainVideoID,true);
	xmlHttp.send(null);
}



/*the following functions operate tabbed boxes on the site. They use the following GLOBAL variables:

	page_colour			- a string describing a colour of the page, relating to a folder in the images directory

	tabBoxRightTitle1		- a string containing the title for the right tab box, tab 1
	tabBoxRightTitle2		- a string containing the title for the right tab box, tab 2

	tabBoxWideTitle1		- a string containing the title for the wide tab box, tab 1
	tabBoxWideTitle3		- a string containing the title for the wide tab box, tab 2
	tabBoxWideTitle2		- a string containing the title for the wide tab box, tab 3
*/	
function makeEqualHeights(objs){
	var i;
	var maxHeight = 0;
	/*for (i = 0; i < objs.length; i++){
		document.getElementById(objs[i]).style.height = '400px';
	}*/
	
	for (i = 0; i < objs.length; i++){
		objs[i] = document.getElementById(objs[i]);	
		if (objs[i].offsetHeight > maxHeight){
			maxHeight = objs[i].offsetHeight;	
		}
	}
	for (i = 0; i < objs.length; i++){
		objs[i].style.height = maxHeight + 'px';	
	}
}

function rightTab1(){
		
		var content1 = document.getElementById('right_tab_content1');
		if (content1.style.display != 'none'){
			return;
		}
		
		var tab1 = document.getElementById('right_tabs_tab1');
		var tab2 = document.getElementById('right_tabs_tab2');
		
		var content2 = document.getElementById('right_tab_content2');
		
		content1.style.display = 'block';
		content2.style.display = 'none';
		tab1.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_on.png);"><span style="background-image:url(images/' + page_colour + '/tabright_on.png)">' + tabBoxRightTitle1 + '</span></a>';
		tab2.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxRightTitle2 + '</span></a>';
}

function rightTab2(){
		
		var content2 = document.getElementById('right_tab_content2');
		if (content2.style.display != 'none'){
			return;
		}
		
		var tab1 = document.getElementById('right_tabs_tab1');
		var tab2 = document.getElementById('right_tabs_tab2');
		
		var content1 = document.getElementById('right_tab_content1');
		
		content1.style.display = 'none';
		content2.style.display = 'block';
		
		tab2.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_on.png);"><span style="background-image:url(images/' + page_colour + '/tabright_on.png)">' + tabBoxRightTitle2 + '</span></a>';
		tab1.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxRightTitle1 + '</span></a>';
}

function wideTab1(){
		var content1 = document.getElementById('wide_tab_content1');
		if (content1.style.display != 'none'){
			return;
		}
		
		var tab1 = document.getElementById('wide_tabs_tab1');
		var tab2 = document.getElementById('wide_tabs_tab2');
		var tab3 = document.getElementById('wide_tabs_tab3');		

		var content2 = document.getElementById('wide_tab_content2');
		var content3 = document.getElementById('wide_tab_content3');
		
		content1.style.display = 'block';
		content2.style.display = 'none';
		content3.style.display = 'none';
		
		tab1.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_on.png);"><span style="background-image:url(images/' + page_colour + '/tabright_on.png)">' + tabBoxWideTitle1 + '</span></a>';
		tab2.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle2 + '</span></a>';
		tab3.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle3 + '</span></a>';		
}

function wideTab2(){
		var content2 = document.getElementById('wide_tab_content2');
		if (content2.style.display != 'none'){
			return;
		}
		
		var tab1 = document.getElementById('wide_tabs_tab1');
		var tab2 = document.getElementById('wide_tabs_tab2');
		var tab3 = document.getElementById('wide_tabs_tab3');		

		var content1 = document.getElementById('wide_tab_content1');
		var content3 = document.getElementById('wide_tab_content3');
		
		content2.style.display = 'block';
		content1.style.display = 'none';
		content3.style.display = 'none';
		
		tab1.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle1 + '</span></a>';
		tab2.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_on.png);"><span style="background-image:url(images/' + page_colour + '/tabright_on.png)">' + tabBoxWideTitle2 + '</span></a>';
		tab3.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle3 + '</span></a>';
}

function wideTab3(){
		var content3 = document.getElementById('wide_tab_content3');
		if (content3.style.display != 'none'){
			return;
		}
		
		var tab1 = document.getElementById('wide_tabs_tab1');
		var tab2 = document.getElementById('wide_tabs_tab2');
		var tab3 = document.getElementById('wide_tabs_tab3');		

		var content2 = document.getElementById('wide_tab_content2');
		var content1 = document.getElementById('wide_tab_content1');
		
		content3.style.display = 'block';
		content2.style.display = 'none';
		content1.style.display = 'none';
		
		tab1.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle1 + '</span></a>';
		tab2.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_off.png);"><span style="background-image:url(images/' + page_colour + '/tabright_off.png)">' + tabBoxWideTitle2 + '</span></a>';
		tab3.innerHTML = '<a style="background-image:url(images/' + page_colour + '/tableft_on.png);"><span style="background-image:url(images/' + page_colour + '/tabright_on.png)">' + tabBoxWideTitle3 + '</span></a>';
}



//this function just selects the text of a named text field
function selectText(obj){
	try{
		document.getElementById(obj).select();
	}catch(err){
		return;	
	}
}
function doEmbed(){
	var vidResp = document.getElementById('videoResponse');
	vidResp.innerHTML = '<div class="embed_btm_content1">Embed:</div><div class="embed_btm_content2"><input id="embed_code" type="text" size="35" value="" border="0" /></div>';
	
	//the emed code for the video
	embedCode = document.getElementById('embed_code');
	embedCode.value = embedString;
	
	selectText('embed_code');
}

function reveal(obj){
	try{
		var myobk = document.getElementById(obj).style.display = 'block';
	}catch(err){
		return;	
	}	
}

function hide(obj){
	try{
		document.getElementById(obj).display = 'none';
	}catch(err){
		return;	
	}	
}

function revealHide(obj){
	try{
		var myobk = document.getElementById(obj);
		if (myobk.style.display == 'none'){
			myobk.style.display = 'block';
		}else{
			myobk.style.display = 'none';			
		}
	}catch(err){
		return;	
	}	
}

function searchSubmit(q){
	var sf = document.getElementById('search_form');
	
	if (q){
		s = document.getElementById('s');
		s.value = q;
	}
	sf.submit();
}

function rollStyle(obj){
	obj.style.textDecoration = "underline";
	obj.style.color = "#0066CC";
}

function outStyle(obj){
	obj.style.textDecoration = "none";
	obj.style.color = "#000000"
}

function setMinHeight(obj,size){
	obj = document.getElementById(obj);
	
	if (obj.offsetHeight < size){
		obj.style.height = size + 'px';	
	}
}

function dontSendVideo(){
	var vidResp = document.getElementById('videoResponse');
	vidResp.innerHTML = '<div class="embed_btm_content1">You must be logged in to send a video</div>';
	alert("You must be logged in to send a video.");
}

function sendVideoForm(){
	var vidResp = document.getElementById('videoResponse');
	
	vidResp.innerHTML = '<div class="embed_btm_content1">Email:</div><div class="embed_btm_content2"><input id="embed_code" name="email" type="text" size="32" value="" border="0" /></div><div class="embed_btm_content3"><input type="image" src="images/button_send.gif" onclick="sendVideo()" value="send" border="0" width="50" height="28" /></div>';

	
	selectText('embed_code');
}

function sendVideo(){
	var eIn = document.getElementById('embed_code');
	
	if (eIn.value == ''){
		alert("You must provide an email address to send the video to.");
		return;
	}
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, can't send to video.");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var favDiv = document.getElementById('favsDiv');
			switch (xmlHttp.responseText){
				case "sent"		:	var vidResp = document.getElementById('videoResponse');
									vidResp.innerHTML = '<div class="embed_btm_content">Video sent.</div>';
									break;
				case "failed"	:	var vidResp = document.getElementById('videoResponse');
									vidResp.innerHTML = '<div class="embed_btm_content">Could not send video.</div>';
									break;
			}			
		}
	}

	
	sendData = 'email=' + eIn.value + '&videoID=' + mainVideoID;
	xmlHttp.open("POST","chunks/sendvideo.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", sendData.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(sendData);
}


function viewDiscussions(page_num,total_pages){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var content3 = document.getElementById('wide_tab_content3');
			content3.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","discussions/mydiscussions.php?page_num=" + page_num + '&max_pages=' + total_pages,true);
	xmlHttp.send(null);
}

function viewIndexDiscussions(page_num,total_pages,keywords){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, that function not available at this time.");				
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var content2 = document.getElementById('wide_tab_content3');
			content2.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","discussions/discussion_search.php?page_num=" + page_num + '&max_pages=' + total_pages + '&q=' + keywords,true);
	xmlHttp.send(null);
}

function viewSearchDiscussions(page_num,total_pages,keywords){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, that function not available at this time.");				
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var content2 = document.getElementById('wide_tab_content2');
			content2.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","discussions/discussion_search.php?page_num=" + page_num + '&max_pages=' + total_pages + '&q=' + keywords,true);
	xmlHttp.send(null);
}

function viewSearchVideos(page_num,total_pages,keywords){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, that function not available at this time.");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var content1 = document.getElementById('wide_tab_content1');
			content1.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","discussions/video_search.php?page_num=" + page_num + '&max_pages=' + total_pages + '&q=' + keywords,true);
	xmlHttp.send(null);
}

function viewMostWatched(page_num,total_pages,keywords){
	//set up the socket
	var xmlHttp;
	try{ 
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){
				alert("Sorry, that function not available at this time.");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4) {
			var content2 = document.getElementById('wide_tab_content2');
			content2.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","ajax/most_watched_pages.php?page_num=" + page_num + '&max_pages=' + total_pages + '&q=' + keywords,true);
	xmlHttp.send(null);	
}

var searchClicked = false;

function searchClick(){
	if (searchClicked == true) return;
	var searchBox = document.getElementById('s');	
	searchBox.value = '';
	searchClicked = true;
}