/*
Copyright (c) 2009 eSpeakers.com
*/


/* google.load("prototype", "1.6.0.3"); */


var resultvideoReq = getXmlHttpRequestObject(); //the AJAX object for loading the video preview

var videoURLs = new Object(); //used as temporary storage for the video player URLs. If you close one, it wipes out the ifram href. If it's redisplayed, we need to restore that href.


//show/hide an list of elements
function toggle() {
	for (var i=0; i < arguments.length; i++) {
		var name = arguments[i];
		var element;
		if (document.getElementById) {
			element = document.getElementById(name);
		} else if (document.all) {
			element = eval("document.all."+name);
		}
		element.style.display = (element.style.display == "none") ? "block" : "none"; //don't use inline; Safari oddities ensue
	}
}


function toggleClass(name, class1, class2) {
	var element;
	var currentclass;
	
	if (document.getElementById) {
		element = document.getElementById(name);
		currentclass = element.getAttribute("class");
	} else if (document.all) {
		//for ie
		element = eval("document.all."+name);
		currentclass = element.getAttribute("className");
	}
	
	
	var newclass = (currentclass == class1) ? class2 : class1;
	element.setAttribute("class", newclass);
	element.setAttribute("className", newclass);
}



//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here.
		//and inform the user they might want to upgrade
		//their browser.
		alert("Some features on this page require a newer web browser than you are using. Try our favorite: Firefox, free at http://www.getfirefox.com");
	}
}


//the resultVideo AJAX call has come in, so populate the target area
function handleReceiveResultVideo(sid) {
	//Check to see if the XmlHttpRequests state is finished.
	if (resultvideoReq.readyState == 4 && resultvideoReq.status == 200) {

		//Set the contents of our select to the result of the asyncronous call.
		var e = document.getElementById('extra'+sid);
		if (e){
			//var newdiv = document.createElement("div");
			//newdiv.innerHTML = resultvideoReq.responseText;
			//e.appendChild(newdiv);
			e.innerHTML = resultvideoReq.responseText;
		}
	}
}




//user has clicked the 'show video' button, so we need to retrieve the contents via AJAX
function showResultVideo(sid, url){
	//expand the space
	//$('extra'+sid).toggle();
	//$('showmore'+sid).toggleClassName('closemore');
	toggle('extra'+sid);
	toggleClass('showmore'+sid, 'closemore', 'showmore');
	
	//if we're opening (as opposed to closing) the div, then AJAX in the content
	e = document.getElementById('extra'+sid);
	if (e) {
		
		//toggle('extra'+sid);
		//toggleStyleClass('showmore'+sid, 'closemore', 'showmore');
		
		if (e.style.display != 'none') {
			if (resultvideoReq.readyState == 4 || resultvideoReq.readyState == 0) {
				//receiveReq.onreadystatechange = handleReceiveSelect(receiveReq, pSelect, selectedvalue);
				resultvideoReq.open("GET", url, false);
				resultvideoReq.send(null);
				handleReceiveResultVideo(sid);
			} else {
				//alert('busy')
			}
		}
	}
}


function toggleVideoPlayer(sid) {
	e = document.getElementById('video'+sid);
	evp = document.getElementById('videoplayer'+sid);
	if (e && evp) {
		
		if (e.style.display != 'none') {
			//shut off the playing video by setting the iframe src to empty
			videoURLs["v"+sid] = evp.src; //store this to restore it later
			evp.src = "";
		} else {
			//play the video
			
			//restore the playing video if needed (ie, it was shown, then closed, now shown again)
			if (videoURLs["v"+sid] != null) {
				evp.src = videoURLs["v"+sid];
			}
		}
	}
	toggle('video'+sid);
	//$('video<?=$sid?>').toggle(); 
}