function launchMedia(mpVars){
	var queryStringParams="?";
	var parentWinUrl = mpVars.url;
	var currentFullURL = document.location;
	var parentWinReturn="";
	
	var itemId = "";
	if ((mpVars.itemId!=undefined)&&(mpVars.itemId!="none")&&(mpVars.itemId!=""))itemId = mpVars.itemId;
	
	var popWin=null;
	var popWinUrl;
	var popWinWidth;
	var popWinHeight;
	var popWinScrollbars;
	var popWinResizable;
	var popWinLocation;
	var popWinToolbar;
	var popWinBlocked=false;
	var popWinStatus;
	var localHost="";	
	//if(!window.hasCCHeader)localHost="http://www.comedycentral.com";
	if(mpVars){
		if(mpVars.linkType=="photoGallery"){
/*************************************************** PHOTO GALLERY ***************************************************/
			messageType="photo gallery";
			var imgId="";
			if ((mpVars.imgId!=undefined)&&(mpVars.imgId!="none")&&(mpVars.imgId!=""))imgId = mpVars.imgId;
			popWinUrl=localHost+"/sitewide/photo_gallery/photo_gallery.jhtml?pgid="+itemId+"&imgId="+imgId;
			popWinName="cc_photo_gallery";
			popWinWidth=750;
			popWinHeight=500;
			popWinScrollbars=0;
			popWinResizable="no";
			popWinStatus="no";
			popWinLocation=0;
			popWinToolbar=0;
		} else if(mpVars.linkType=="game"){
/*************************************************** GAME ***************************************************/
			messageType="game";
			popWinUrl=localHost+"/sitewide/game_player/gameplayer_popup.jhtml?game="+itemId;
			popWinName="cc_game_player";
			popWinWidth = mpVars.width;
			popWinHeight = mpVars.height;
			if ((popWinWidth==undefined)||(popWinWidth=="")||(popWinWidth==null)) popWinWidth = 690;
			if ((popWinHeight==undefined)||(popWinHeight=="")||(popWinHeight==null)) popWinHeight = 495;
			popWinScrollbars=0;
			popWinResizable="no";
			popWinStatus="no";
			popWinLocation=0;
			popWinToolbar=0;
		} else{			
/*************************************************** MOTHERLOAD VIDEO ***************************************************/
			messageType="motherload video";				
			popWinName="cc_motherload_player";
		   			
			var ml_collection;
			var playerUrl = "/videos/index.jhtml";
            var portalUrl = "/funny_videos/index.jhtml";
            
			if ((mpVars.ml_video!=undefined)&&(mpVars.ml_video!="none")&&(mpVars.ml_video!=""))itemId = mpVars.ml_video;
			if ((mpVars.ml_collection!=undefined)&&(mpVars.ml_collection!="none")&&(mpVars.ml_collection!="")){
				ml_collection = mpVars.ml_collection;
				itemId="";
			}
			else ml_collection = "";
            
            if(itemId!=null && itemId!="") popWinUrl = playerUrl + "?videoId=" + itemId;
            else if (ml_collection!=null && ml_collection!="") popWinUrl = playerUrl + "?collectionId=" + ml_collection;
            else popWinUrl = portalUrl;
            document.location=popWinUrl;
            return;
		}
		
		if((parentWinUrl=="undefined")||(parentWinUrl==null)||(parentWinUrl=="")){
			parentWinUrl="#";
			parentWinReturn="return false;";			
		}	popWin=window.open(popWinUrl,popWinName,"width="+popWinWidth+",height="+popWinHeight+",scrollbars="+popWinScrollbars+",resizable="+popWinResizable+",location="+popWinLocation+",toolbar="+popWinToolbar+",status="+popWinStatus);		
		if(popWin!=null&&typeof (popWin)!="undefined"){
			if((parentWinUrl!="undefined")&&(parentWinUrl!=null)&&(parentWinUrl!="#")&&(parentWinUrl!="none"))document.location=parentWinUrl;
			popWin.focus();
		}else{
			if(popWinName=="cc_motherload_player"){
				//popUpBlockReport();
				document.location=popWinUrl;
				//window.resizeTo(popWinWidth+30,popWinHeight+160);
			} else {			
				var errorAlertText="You may have a pop-up blocker interfering with your ability to view this "+messageType+".\nPlease consult the red box on this page for further instructions on how to launch the "+messageType+" you requested.";
				var errorDivText="Uh-oh -- you may have a pop-up blocker interfering with your ability to view this "+messageType+". To see the " + messageType + ", <strong>click here</strong></a><br /><br />";
				var errorDivHref="<div class=\"text\" style=\"text-align:center;\"><a href=\""+parentWinUrl+"\" onClick=\"window.open(\'"+popWinUrl+"\',\'"+popWinName+"\',\'width="+popWinWidth+",height="+popWinHeight+",scrollbars="+popWinScrollbars+",resizable="+popWinResizable+",location="+popWinLocation+",toolbar="+popWinToolbar+",status="+popWinStatus+"\');"+parentWinReturn+"\">" + errorDivText + "</a></span>";			
			
				if (document.getElementById("popUpError")) popErrorEvent(errorAlertText,errorDivHref);
				else addLoadEvent(popErrorEvent(errorAlertText,errorDivHref));
			}				
		}		
	}else alert("Improperly Formatted Launch Media Call\nPlease pass an appropriate object.");
}

function popErrorEvent(alertText,divHref){
	alert(alertText);
	document.getElementById("popUpError").style.display="block";
	document.getElementById("popUpError").innerHTML="<div class='popupErrorPad'>" + divHref + "</div>";
	if (document.getElementById("globalNavHolder")) document.getElementById("globalNavHolder").style.top="150px";
}

function setMotherloadCookie() {}

/* ONLY ON MOTHERLOAD LAUNCH PAGE */
function launchMotherloadHome(){
	var userLocation = window.location + "";
	var queryStart = userLocation.indexOf('?') + 1;	
	var queryString = userLocation.substring(queryStart,userLocation.length);	
	var nameValuePairs = queryString.split("&");
	var nameValuePairParts;
	var launchMediaObjectParams = "launchMedia({ml_context:'show'";
	if (queryStart > 0){
		for (i=0; i<nameValuePairs.length;i++){
			nameValuePairParts = nameValuePairs[i].split("=");
			launchMediaObjectParams = launchMediaObjectParams + "," + nameValuePairParts[0] + ":'" + nameValuePairParts[1] + "'";
		}
		launchMediaObjectParams = launchMediaObjectParams + "})";
		eval(launchMediaObjectParams);
	} 
	else launchMedia({itemId:'',linkType:'video'});	
}
/* Gameplayer Popup Launch */
function launchGamePlayerPopup(popupVars){
	var gameId = popupVars.gameId;
	if ((gameId!=undefined)&&(gameId!="")&&(gameId!=null)) launchMedia({itemId:''+gameId+'', width:''+popupVars.width+'', height:''+popupVars.height+'', url:''+popupVars.moreInfoUrl+'', linkType:'game' });
}
function playMedia(pmVar){
	pmVar.linkType="video";
	launchMedia(pmVar);
}
function popGallery(pgVar) {
	var launchMediaObj = new Object();
	if(typeof(pgVar)=="string"){
		launchMediaObj.itemId=pgVar;
		launchMediaObj.url="";
	} else launchMediaObj=pgVar;
	launchMediaObj.linkType="photoGallery";
	launchMedia(launchMediaObj);	
}
function membersLogout(){document.logoutform.submit();}
function mediaPopper(builtURL){
	var popUrl="/sitewide/media_player/"+builtURL;
	var winFeatures;
	var isSaf=(navigator.userAgent.indexOf("afari")!=-1)?true:false;
    if(isSaf)winFeatures="status=yes,width=749,height=495,scrollbars=no,resizable=no";	
	else winFeatures="status=yes,width=750,height=495,scrollbars=no,resizable=no";	
	playerWindow=window.open(popUrl,"cc_media_player",winFeatures);
	if(playerWindow!=null&&typeof (playerWindow)!="undefined") playerWindow.focus();
	else return "blocked";
}
function sendToAFriend(stfVars){
	var stfVarsString="";
	var url="";
	if(stfVars){
		if(!stfVars.url)stfVarsString+=autoSendToAFriendUrl();
		for(var i in stfVars){
			stfVarsString+=i.toString()+"="+stfVars[i]+"&";
		}
	} else stfVarsString+=autoSendToAFriendUrl();
	var sendToAFriend = window.open("/members/emailafriend.jhtml?"+stfVarsString,"cc_email_friend", "width=457,height=428,scrollbars=no,resizable=no");
	sendToAFriend.focus();
}
function autoSendToAFriendUrl(){
	var url=document.location.pathname;
	if(document.location.search)url+=document.location.search;
	url=escape(url);
	return "url="+url+"&";
}
function popWindow(url,options){
	var poppedWindow = window.open(url,"cc_pop_window",options);
	poppedWindow.focus();
}
function ccPopWindow(pgVars){
	var popUrl = pgVars.url;
	var windowName;
	var windowAttributes;
	if (pgVars.windowName=="" || pgVars.windowName=="undefined") windowName="cc_pop_window";
	else windowName=pgVars.windowName;
	if (pgVars.windowAttributes=="" || pgVars.windowAttributes=="undefined") windowAttributes="";
	else windowAttributes=pgVars.windowAttributes;
	var poppedWindow = window.open(popUrl,windowName,windowAttributes);
}

function addLoadEvent(func){
	try{ 
    	if(window.attachEvent){window.attachEvent("onload",func);} 
        if(window.addEventListener){window.addEventListener("load",func,false);} 
        return this; 
    }catch(e){} 
}
/* Converts first letter of every word to upper case */
function changeEveryFirstLetterToCap (stringToConvert){
	var lowerCaseString = stringToConvert;
	var upperCaseString = "";
	var ch;       // One of the characters in str.
	var prevCh = '';   // The character that comes before ch in the string.
	var i;         // A position in str, from 0 to str.length()-1.
	for ( i = 0;  i < lowerCaseString.length;  i++ ) {
		ch = lowerCaseString.charAt(i).toLowerCase();
		if ( (ch!=' ')  &&  ( (prevCh==' ') || (prevCh=='') )) upperCaseString+=ch.toUpperCase();
		else upperCaseString+=ch;
		prevCh = ch;
	}
	return upperCaseString;
}
/* mimics java's URLEncode */
function URLencode(urlToEncode){
  return escape(urlToEncode).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}
/* START: Cookie Functions */
function setCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}//end if
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function deleteCookie(name){
	setCookie(name,"",-1);
}				
/* END: Cookie Functions */

/* Active X Install */
function installFirefoxActiveX(version){	
	if (version=="1.0.7") xpi={'Mozilla Firefox 1.0.7 ActiveX Plug-in':'/global/apps/broadband/installers/viacomplugin.xpi'}; 
	else xpi={'Mozilla Firefox 1.5 ActiveX Plug-in':'/global/apps/broadband/installers/viacomplugin15.xpi'};
	InstallTrigger.install(xpi);	
}
/* Mini Motherload Init */
function miniMotherloadInit(mmiVars){
	
	var mlOpenOrClosed = getCookie("miniMotherloadVisibility");
	var testmode= "off";
	if (mmiVars) {
		if ((mmiVars.xmlId!=undefined)&&(mmiVars.xmlId!="")&&(mmiVars.xmlId!=null)&&(mmiVars.xmlId!="none"))mlXmlId = mmiVars.xmlId;
		if ((mmiVars.testmode!=undefined)&&(mmiVars.testmode!="")&&(mmiVars.testmode!=null)&&(mmiVars.testmode!="none"))testmode = mmiVars.testmode;
	}	
	var startOpen;			
	if (mlOpenOrClosed=="closed") startOpen="false";
	else startOpen="true";
	
	document.getElementById("miniPlayer").style.display="block";
	document.getElementById("miniPlayer").src="/motherload/mini_player.jhtml?start_open=" + startOpen + "&testmode=" + testmode + "&ml_xml=" + URLencode("/motherload/xml/data_mini.jhtml?xml_id=" + mlXmlId);
	//alert("/motherload/xml/data_mini.jhtml?xml_id=" + mlXmlId);
	
}
/* Mini-Motherload visiblity */
function changeMiniVisibility(state,setUserCookie){
	//alert("changeMiniVisibility:\nState: " +state +"\nSet User Cookie: "+setUserCookie);
	if (setUserCookie!='false'){
		setCookie("miniMotherloadVisibility", state, '730');
	}
	if (state == "open") document.getElementById('adFrame').style.left=55;		
	else document.getElementById('adFrame').style.left=500;
}
/* div manipulation */
function changeDivStyle(divId,styleToChange,styleValue){
	//alert("changeDivStyle:\nDiv ID: " +divId +"\nStyle To Change: "+styleToChange +"\nStyle Value: "+styleValue);
	if (document.getElementById(divId)){
		var theDiv = document.getElementById(divId);
		if (styleToChange=="src") theDiv.src = styleValue;
		else theDiv.style[styleToChange] = styleValue;
	}
}
function testWindowLocation(url){
	if( (this.location != this.parent.location) && (document.referrer.indexOf('yahoo.com') == -1) && (document.referrer.indexOf('www.theshowbizshow') == -1))
	{
		if(url) parent.location.href = url;
		else parent.location.href =  this.location.href;
	}
}
function uncensoredSignin(videoId){
	if ((videoId!=undefined)&&(videoId!="")&&(videoId!=null)) document.location="/members/signin.jhtml?destination=/motherload/player.jhtml" + URLencode("?ml_video=" + videoId);
	else document.location="/members/signin.jhtml?destination=/members/my_account.jhtml";
}

function ccHttpRequest(url, parameters, successFunction) {		
	cc_http_request = false;
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		cc_http_request = new XMLHttpRequest();
		if (cc_http_request.overrideMimeType) 
		{
			cc_http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try {
			cc_http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				cc_http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!cc_http_request) {
		//alert('Cannot create XMLHTTP instance');
		return false;
	}
     
	cc_http_request.onreadystatechange = function(){
		if (cc_http_request.readyState == 4) { 
			if (cc_http_request.status == 200){
				eval(successFunction);
			}
			//else alert("xml status error: " + cc_http_request.statusText);	
		}
	};
	cc_http_request.open('POST', url, true);
	cc_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	cc_http_request.setRequestHeader("Content-length", parameters.length);
	cc_http_request.setRequestHeader("Connection", "close");
	cc_http_request.send(parameters);
}

function truncateText(id,length){
	var len = length;
	var p = document.getElementById(id);
	if (p) {	
		var trunc = p.innerHTML;
		if (trunc.length > len) {		
			trunc = trunc.substring(0, len);
			trunc = trunc.replace(/\w+$/, '');		
			trunc += '<a href="#" ' + 'onclick="this.parentNode.innerHTML=' + 'unescape(\''+escape(p.innerHTML)+'\');return false;">' + ' more...<\/a>';
			p.innerHTML = trunc;
		}
	}
}

function clearSearch(theText) {
  	if (theText.value == theText.defaultValue) {
		theText.value = "";
	}
}

function blurSearch( theText ){
	if( theText.value == "" ){
		theText.value = theText.defaultValue;
	}
}

/* no jquery on old site */
function submitSearch( form ){
	var field = document.getElementById("searchterm");
/*	var field = $(form).find('input[type="text"]');*/
	var term = field.value;
	/*var term = field.attr("value");*/
	var info = "Search comedycentral.com"
	if( info == term ){
		alert( "Please enter a search term." );
		return false;
	}
	return true;
}

function loginsubmit(){
	document.flux_signin.ref.value=location.href;
	document.flux_signin.submit();
}
