/*
Copyright (c) 2009 eSpeakers.com

depends on jQuery 1.6+ being loaded in the parent page

*/



//add the indexOf() function if it doesn't exist already
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
      for (var i = (start || 0); i < this.length; i++) {
            if (this[i] == obj) {
	            return i;
		          }
		      }
	        }
	}


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
	}
}


//show or hide a list of elements. used primarily to switch tabs on the directory bio layout.
function showhide(onArray, offArray) {
	
	var combinedList = onArray.concat(offArray);
	
	for (var i=0; i<combinedList.length; i++) {
		
		var name = combinedList[i];
		var element;
		if (document.getElementById) {
			element = document.getElementById(name);
		} else if (document.all) {
			element = eval("document.all."+name);
		}
		
		if (element) {
			var newclass = (onArray.indexOf(name) > -1) ? "tabshown" : "tabhidden";
			element.setAttribute("class", newclass);
			element.setAttribute("className", newclass);
		}
	}
}



//used to switch older style tabs.
function expand()
{
        for (var i=0; i<expand.arguments.length; i++) {
                var name = expand.arguments[i];
                var element;
                if (document.getElementById) {
                        element = document.getElementById(name);
                } else if (document.all) {
                        element = eval("document.all."+name);
                }
                
                if (element)
                	element.style.display = (element.style.display == "none") ? "inline" : "none";
        }
}


//load an iFrame with a given URL. 
//used to load the Social Media frame on demand
function loadFrame(frameid, url) {
	var element;
	if (document.getElementById) {
		element = document.getElementById(frameid);
	} else if (document.all) {
		element = eval("document.all."+frameid);
	}
	
	if (element) {
		if (element.src != url)
			element.src = url;
	}
}




//change an element's CSS class
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){
			e.innerHTML = resultvideoReq.responseText;

			//use fancybox to open the video iframe
			jQuery(".videos .fancybox").fancybox({
				'width'    : 772,
				'height'   : 361,
				'autoScale':false,
				'type':'iframe'
			});
		}
	}
}




//user has clicked the 'show video' button, so we need to retrieve the contents via AJAX
function showResultVideo(sid, url){
	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')
			}
		}
	}
}


/**
 * In the results list, user has clicked on the video thumbnail and wants to watch the video now
 *
 */
function toggleVideoPlayer(sid, url) {
	var e = document.getElementById('video'+sid);
	var 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
			evp.src = "";
		} else {
			//play the video
			evp.src = url;
		}
	}
	toggle('video'+sid);
}


//used on the 'send to a friend' script to center the popup
function window_pos(popUpDivVar) {
	var obj = document.getElementById('btnSendtofriend');
	var popUpDiv = document.getElementById(popUpDivVar);
	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
        }
	}

	
	if (obj && popUpDiv) {
		//popUpDiv.style.top = e.style.top + e.style.height;
		//popUpDiv.style.left = e.style.left + e.style.width + 325;
		popUpDiv.style.top = curtop;
		popUpDiv.style.left = curleft;
		return;
	}
	
	
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	
	window_width=window_width/2-150;//150 is half popup's width
	
	popUpDiv.style.top = window_width + 'px';
	popUpDiv.style.left = window_width + 'px';
}




//used on the 'send to a friend' script to disable the background
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}

//used on the 'send to a friend' to make the CSS layer visible
function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);
}



//detect if a string appears in the URL anywhere. Useful for 'opentomedia', 'opentocalendar', etc...
function isInUrl(lookfor) {
	return (window.location.href.toUpperCase().indexOf(lookfor.toUpperCase()) > -1)
}


/**
 * Return the value of a parameter from the URL
 * 
 */
function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);

    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}






