/* 
Methods for resizing the flash stage at runtime.

setFlashWidth(divid, newW)
divid: id of the div containing the flash movie.
newW: new width for flash movie

setFlashWidth(divid, newH)
divid: id of the div containing the flash movie.
newH: new height for flash movie

setFlashSize(divid, newW, newH)
divid: id of the div containing the flash movie.
newW: new width for flash movie
newH: new height for flash movie

canResizeFlash()
returns true if browser supports resizing flash, false if not. 
*/






function videoUpload(mythis){
	alert(mythis.parentNode.firstChild.id);
}

	function checkIfFileExists(formdata,uploadtype){
//		alert(document.getElementById('f1_container').offsetTop + " - " + document.body.scrollTop + " - " +formdata.offsetY + " - " +formdata.offsetTop);
		document.getElementById('f1_container').style.top = formdata.offsetTop;
		document.getElementById('f1_container').style.left = formdata.offsetLeft +  formdata.offsetWidth+20;
//		alert(document.getElementById('f1_container').offsetTop + " - " + document.body.scrollTop);
//	console.log(formdata);
		URL = 'aj_response.php?task=checkIfFileExists&filename=' + formdata.value + '&uploadtype=' + uploadtype ;
//	alert(URL);
		labelname = 'f1_result';
	 	document.getElementById(labelname).textContent = "checking whether file exists";
	 	var d = new Date();
		var req = null; 
		if(window.XMLHttpRequest)
			req = new XMLHttpRequest(); 
		else if (window.ActiveXObject)
			req  = new ActiveXObject(Microsoft.XMLHTTP); 
		req.containingdiv = formdata.form.myid.value;
		req.task = formdata.form.task.value+'form_';
//alert (req.task);
//alert (req.containingdiv);
		req.onreadystatechange = function()
		{ 
			if(req.readyState == 4)
			{
				if(req.status == 200)
				{
//					document.getElementById('status').innerHTML=req.responseText;	
					if(req.responseText=="OK"){
//						document.getElementById(labelname).textContent = "UPLOADING FILE";
//						alert(req.task+req.containingdiv);
						document.getElementById('f1_result').innerHTML = 'file does not already exist - uploading';
						document.getElementById('f1_upload_process').style.visibility = 'visible';
//						alert(req.task+req.containingdiv );
						mythis = document.getElementById(req.task+req.containingdiv);
//						alert(req.task+req.containingdiv +  "!" + mythis.id);
						document.getElementById(req.task+req.containingdiv).style.visibility = 'hidden';
//						alert('uploaddownloadform_'+req.containingdiv);
//						document.getElementById('f1_upload_form').submit();
//						alert(document.getElementById(req.task+req.containingdiv).id);
//						document.getElementById(req.task+req.containingdiv).submit();
						mythis.submit();
						startUpload(mythis)
					}else{
						alert(req.responseText);
						document.getElementById('f1_result').innerHTML = req.responseText;
						//alert (document.getElementById('uploaddownloadform_'+req.containingdiv).submitBtn.style.visibility);
						document.getElementById(req.task+req.containingdiv).submitBtn.style.visibility = 'visible';
						document.getElementById('f1_container').style.left = 75 +parseInt( document.getElementById('f1_container').style.left);	
						return false;
					}
				}	
			} 
		}; 
		req.open("GET", URL + "&un=" + d.getTime(), true); 
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		req.send(null); 
		
	}
	function startUpload(mythis){
//		alert("wibble " + mythis.id);
		formid = mythis.myid.value
		task = mythis.task.value+'form_';
//		alert(task+"!"+formid);
		document.getElementById('f1_result').style.visibility = 'visible';
		document.getElementById('f1_result').innerHTML = 'uploading';
		document.getElementById(task+formid).style.visibility = 'hidden';
		document.getElementById(task+formid).submitBtn.style.visibility = 'hidden';
		document.getElementById('f1_upload_process').style.visibility = 'visible';
		bUploaded.start('f1_result');
		return true;
	}
	function stopUpload(uploadbox, updateid,returnedtext){
		var result = '';
//		alert(uploadbox + "!!!" + updateid + "!!!" + returnedtext);
		document.getElementById('newvideo').innerHTML = returnedtext;
//		document.getElementById('uploaddownloadform_'+updateid).myfile.value = '';
//		document.getElementById('imageuploadform_new').myfile.value = '';
		document.getElementById("f1_submitBtn").style.visibility = 'hidden';
		document.getElementById('f1_result').style.visibility = 'hidden';
		document.getElementById('f1_upload_process').style.visibility = 'hidden';
//		document.getElementById('uploaddownloadform_'+updateid).style.visibility = '';
//		document.getElementById('imageuploadform_new').style.visibility = '';
		document.getElementById('new-edit').style.display = 'none';
		document.getElementById('newvideo').innerHTML = "";
   var node=document.getElementById('newvideo');
   var elem=document.createElement('DIV');
//alert (node.id + " " + elem.id);
   elem.innerHTML=returnedtext;
//   document.getElementById(uploadbox).insertBefore(elem,uploadbox);
	document.getElementById(uploadbox).style.display='none'
	insertAfter( document.getElementById(uploadbox),elem);

		return true;
	}
	
	function insertAfter( referenceNode, newNode ){    
		referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
	}
	
	function stopUploadDownload(updateid,success){
		var result = '';
		document.getElementById(updateid).innerHTML = success;
//		document.getElementById('uploaddownloadform_'+updateid).myfile.value = '';
//		document.getElementById("f1_submitBtn").style.visibility = 'hidden';
		document.getElementById('f1_result').style.visibility = 'hidden';
		document.getElementById('f1_upload_process').style.visibility = 'hidden';
//		document.getElementById('uploaddownloadform_'+updateid).style.visibility = 'visible';
		return true;
	}


function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";		
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash(){
	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");
	if( document.getElementById ){
		if(opera == -1) return true;
		else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
	}
	return false;
}

var searchReq = getXmlHttpRequestObject();

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();	
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");	
	}
}

function updateData(mythis){
//	alert (mythis.name + "!" + mythis.value + "!" + mythis.type)
	mythis.style.backgroundColor = "#ff0000";
	if(mythis.type=="checkbox"){
		if(mythis.checked==true) mythis.value = 1;
		else mythis.value = 0;
	}

//	document.getElementById('sendupdates').style.visibility='visible';
	today = new Date( );          // set today's date

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		searchReq.id = mythis.id
		searchReq.open("GET", 'aj_response.php?task=update' + 
		'&edittype=' + mythis.id +
		'&editvalue=' + encodeURIComponent(mythis.value) +
		'&unique=' + today.getTime(), true);		
		searchReq.onreadystatechange = handleUpdate; 		
		searchReq.send(null);	
	}
}

function getEdit(mythis){
 	var d = new Date();
	var req = null; 
	if(window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
		req  = new ActiveXObject(Microsoft.XMLHTTP); 
	req.containingdiv = mythis.parentNode.parentNode.parentNode.id + "-edit";
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4 &&  req.status == 200){
			document.getElementById(req.containingdiv).innerHTML = req.responseText;
			document.getElementById(req.containingdiv).style.display = 'block';
		} 
	}; 
	req.open("GET", 'aj_response.php?task=getedit' + '&id=' + mythis.parentNode.parentNode.parentNode.id +	'&unique=' + "&un=" + d.getTime(), true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
}

function handleGetEdit() {
	if (searchReq.readyState == 4) {
		document.getElementById(searchReq.id).style.backgroundColor = "#ffffff";
	}
}


function deleteVideo(mythis){
	var okToDelete = confirm( "Are you sure that you want to delete " + document.getElementById(mythis.parentNode.parentNode.id + "-title").value);
	if (okToDelete){
		mythis.style.backgroundColor = "#ff0000";
		mythis.parentNode.parentNode.style.display='none';
		if(mythis.type=="checkbox"){
			if(mythis.checked==true) mythis.value = 1;
			else mythis.value = 0;
		}
	
	//	document.getElementById('sendupdates').style.visibility='visible';
		today = new Date( );          // set today's date
	
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			searchReq.id = mythis.id
			searchReq.open("GET", 'aj_response.php?task=delete' + 
			'&edittype=' + mythis.id +
			'&editvalue=' + encodeURIComponent(mythis.value) +
			'&unique=' + today.getTime(), true);		
			searchReq.onreadystatechange = handleUpdate; 		
			searchReq.send(null);	
		}
	}
}

function deleteDownload(mythis){
	var okToDelete = confirm( "Are you sure that you want to delete " + document.getElementById(mythis.parentNode.parentNode.id + "-downloadlink").value);
	if (okToDelete){
		mythis.style.backgroundColor = "#ff0000";
		mythis.parentNode.parentNode.style.display='none';
		if(mythis.type=="checkbox"){
			if(mythis.checked==true) mythis.value = 1;
			else mythis.value = 0;
		}
	
	//	document.getElementById('sendupdates').style.visibility='visible';
		today = new Date( );          // set today's date
	
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			searchReq.id = mythis.id
			searchReq.open("GET", 'aj_response.php?task=deletedownload' + 
			'&edittype=' + mythis.id +
			'&editvalue=' + encodeURIComponent(mythis.value) +
			'&unique=' + today.getTime(), true);		
			searchReq.onreadystatechange = handleDownloadDeleteUpdate; 		
			searchReq.send(null);	
		}
	}
}

function handleDownloadDeleteUpdate() {
	if(searchReq.readyState == 4 &&  searchReq.status == 200){
		myelement = document.getElementById(searchReq.id).parentNode.parentNode;
		document.getElementById(searchReq.id).parentNode.innerHTML = searchReq.responseText;
		myelement.style.display='block';
	} 
}

function handleUpdate() {
	if (searchReq.readyState == 4) {
		document.getElementById(searchReq.id).style.backgroundColor = "#ffffff";
	}
}


/* FLOATING IMAGE */

/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var offsetfrommouse=[5,5]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 270;	// maximum image size.

if (document.getElementById || document.all){
	document.write('<div id="trailimageid">');
	document.write('</div>');
}

function gettrailobj(){
if (document.getElementById && document.getElementById("trailimageid"))
return document.getElementById("trailimageid").style
else if (document.all)
return document.all.trailimagid.style
}

function gettrailobjnostyle(){
if (document.getElementById && document.getElementById("trailimageid"))
return document.getElementById("trailimageid")
else if (document.all)
return document.all.trailimagid
}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail1(){
	
}

function showtrail(imagename,type,title,width,height,catid,copyright,description){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	if (typeof e != "undefined"){
		xcoord+=e.pageX
		ycoord+=e.pageY

	} else if (typeof window.event != "undefined"){
		xcoord+=truebody().scrollLeft+event.clientX
		ycoord+=truebody().scrollTop+event.clientY
	}

	document.onmousemove=followmouse;
	var newHTML = '<table border="0" width="240" cellpadding="3" cellspacing="3">';
	newHTML = newHTML + '<tr><td width="100%" valign="top">';
	newHTML = newHTML + '<table class="thin" width="222">';
	newHTML = newHTML + '<tr><td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">';
	newHTML = newHTML + '<tr><td width="100%"><p align="center">';

	if(type=='photo'){
		newHTML = newHTML + '<div align="center" style="padding:5px;background:#fff;border:solid 1px #000">';
		newHTML = newHTML + '<img src="' + imagename + '" border="0"">';
		newHTML = newHTML + '</div>';
	}
	if(type=='video' || type=='swf') {
		
		var browser=navigator.appName;
		var b_version=navigator.appVersion;
		var version=parseFloat(b_version);
		var temp1 ;
		if ((browser=="Microsoft Internet Explorer") && (version>=4)){
			extra1 = "";
			extra2 = "";
		}else{
			extra1 = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'">';
			extra2 = '</object>';
		}
		
		if(type=='video')temp1 = '		src="http://'+window.location.hostname+'/player.swf"';
		else temp1 = '		src="'+imagename+'";';
		
		temp = 	'<div style="border:solid 1px #bbac75;padding:4px;background-color:#000;width:'+width+'px;height:'+height+'px;">'+extra1+'<embed'
		+ temp1
		+'		width="'+width+'"'
		+'		height="'+height+'"'
		+'		allowscriptaccess="sameDomain"'
		+'		allowfullscreen="false"'
		+'		flashvars=autostart=true&repeat=true&height='+height+'&width='+width+'&file='+imagename+'&searchbar=false&showicons=false&shownavigation=false&showdigits=false&usefullscreen=false"'
		+'		/>'+extra2+'</div>';

		newHTML = newHTML + '<div align="center" style="border:solid 1px #000;position:absolute;display:hidden;">';
		newHTML = newHTML += temp;
		newHTML = newHTML + '</div>';
	}



	gettrailobjnostyle().innerHTML = newHTML;

	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
	gettrailobj().visibility="visible";

}


function hidetrail(){
	gettrailobj().visibility="hidden"
	gettrailobjnostyle().innerHTML=""
	document.onmousemove=""
	gettrailobj().left="-500px"
}


function followmouse(e){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	if (typeof e != "undefined"){
		xcoord+=e.pageX
		ycoord+=e.pageY

	} else if (typeof window.event != "undefined"){
		xcoord+=truebody().scrollLeft+event.clientX
		ycoord+=truebody().scrollTop+event.clientY
	}

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}

function shownew(section){
	
	while(document.getElementById(section).hasChildNodes()){	document.getElementById(section).removeChild(document.getElementById(section).lastChild);}
	
	var outputstring;
	document.getElementById(section).style.display='block';
	//alert(section);
	myform = document.createElement("form");
	myform.id = 'imageuploadform_'+section;
	myform.onsubmit= function(){startUpload(this);}
	myform.target="upload_target";
	myform.enctype="multipart/form-data"
	myform.method="post"
	myform.action="aj_response.php" 
	myform.setAttribute("class","f1_uploadform")
	myform.setAttribute("onsubmit","startUpload(this);")
	var mytext = document.createTextNode("New file to upload to MAAD Player: ");
	myform.appendChild(mytext);
	var mytask = document.createElement("input");
	mytask.name = "task";
	mytask.value = "imageupload";
	mytask.type = "hidden";
	myform.appendChild(mytask);
	var myid = document.createElement("input");
	myid.name = "myid";
	myid.value = section;
	myid.type = "hidden";
	myform.appendChild(myid);
	var myfile = document.createElement("input");
	myfile.name = "myfile";
	myfile.id = "myfile";
	myfile.value = '';
	myfile.type = "file";
	myfile.onchange = function(){checkIfFileExists(this,'main');}
	myfile.setAttribute("onchange","checkIfFileExists(this,'main');")
	myform.appendChild(myfile);
	var mysubmit = document.createElement("input");
	mysubmit.type = "submit";
	mysubmit.name = "submitBtn";
	mysubmit.id = "f1_submitBtn";
	mysubmit.value = 'Upload';
	mysubmit.style.visibility = "hidden";
	myform.appendChild(mysubmit);
	document.getElementById(section).appendChild(myform); 
	return(false);
}