// Find an argument passed in as URL parameter
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  
  var val ;
  if( results == null )
    val = "";
  else
    val = results[1];
  
  return val ;
}


function resetEmbedVideoSize ( embedOrig ) 
{
	var modifiedEmbed ;
	
	var isHulu = embedOrig.indexOf ( "hulu." ) ;
	if ( isHulu > 0 )
	{
		// Patch up the size of Hulu video DIV
	   modifiedEmbed = embedOrig.replace      ( /"512"/g, embedHuluWt  );
	   modifiedEmbed = modifiedEmbed.replace  ( /"341"/g, embedHuluHt );
	   //alert ("FIXED HULU EMBED: " + modifiedEmbed ) ;
	   return modifiedEmbed;
	}
	
	var vidWidth  = embedWt.substr (0,3)  ; 
    var vidHeight = embedHt.substr (0,3)  ;    
 
	modifiedEmbed = embedOrig;
	var indexOfWidthStart       = modifiedEmbed.indexOf ( "width=" );
	if ( indexOfWidthStart < 0 )
	  return modifiedEmbed ;
	  
	var indexOfWidthFirstQuote  = indexOfWidthStart + 6 ;
	var quoteChar               = embedOrig.substr (indexOfWidthFirstQuote, 1) ;
		
	var indexOfWidthSecondQuote = 0;
	
	if ( indexOfWidthStart > 0 ) 
	{
		indexOfWidthSecondQuote = embedOrig.indexOf ( quoteChar, indexOfWidthFirstQuote + 1 );
		if ( indexOfWidthSecondQuote > 0 ) 
		{
			modifiedEmbed = embedOrig.substring ( 0, indexOfWidthFirstQuote + 1 ) + 
			                vidWidth +
			                embedOrig.substring ( indexOfWidthSecondQuote );
		}
	}
	
	indexOfWidthStart       = modifiedEmbed.indexOf ( "height=" );
	indexOfWidthFirstQuote  = indexOfWidthStart + 7 ;
	if ( indexOfWidthStart > 0 ) 
	{
		indexOfWidthSecondQuote = modifiedEmbed.indexOf ( quoteChar, indexOfWidthFirstQuote + 1 );
		if ( indexOfWidthSecondQuote > 0 ) 
		{
			modifiedEmbed = modifiedEmbed.substring ( 0, indexOfWidthFirstQuote + 1 ) + 
			                vidHeight +
			                modifiedEmbed.substring ( indexOfWidthSecondQuote );
		}
	}
	
	return modifiedEmbed;
}
 
 
 
function flash_EmbedVideo ( embCode, xpos, ypos ) 
{
    // Find DIV where we put the EMBED...
	var mDiv = document.getElementById ("movieDiv") ;
	if ( mDiv == null )
	{
	   //alert ("No DIV for EMBED request:  movieDiv " ) ;
	   return ;
	}
	
	if ( xpos >= 0 && ypos >= 0 )
	{
        xpos = pageX + Math.floor( xpos * pageScale ) ;
		ypos = pageY + Math.floor( ypos * pageScale ) ;
		
		//alert ("Movie DIV MOVE x,y= " + xpos + ", " + ypos ) ;
		
		mDiv.style.left =  xpos  + 'px' ;
	  	mDiv.style.top  =  ypos  + 'px' ;
	}
	
	// Patch up the width & height of video DIV
	embCode = resetEmbedVideoSize (embCode) ;
	
	mDiv.innerHTML = embCode ;
	if ( embCode == "" )
	{
	   mDiv.style.display = "none" ;		
	}
	else
	{
	   mDiv.style.display = "block" ;
	}
}
 
// Set a cookie for the player via function in cookieSubs.js
function flash_setPlayer ( playerId, autoLogin )
{
  setUserCookie ( playerId, autoLogin ) ;
}



function hidePopupDiv ( )
{
	var pDiv = document.getElementById ("popupDiv") ;
	if ( pDiv != null )
	  pDiv.style.display = "none" ;
}

function showPopupDiv ( sTitle, sUrl ) 
{
	var pDiv = document.getElementById ("popupDiv") ;
	if ( pDiv == null )
	  return ;
	pDiv.style.display = "block" ;
	
    var pTitle = document.getElementById ( "popupDialogTitle" );
	if ( pTitle != null )
      pTitle.innerHTML = sTitle;
      
    var pHolder = document.getElementById ( "popupDialogContentsHolder" );
	if ( pHolder != null )
      pHolder.src = sUrl ;
}
 

var m_PopupInfoPath = "/content/affiliates/popjax/" ;

function flash_showTermsAndConditions ( )
{
	showPopupDiv ( "Terms and Conditions", m_PopupInfoPath + "TermsAndConditions.html" ) ;
}

function flash_popup ( title, fname )
{
	showPopupDiv ( title, m_PopupInfoPath + fname + ".html" ) ;
}

function extractReferer ()
{
	var ref = document.referrer;	

	var loc1 = ref.indexOf("://");
	if (loc1 < 0)
	  return;
	
	var substr1 = ref.substring(loc1+3);	
	var loc2    = substr1.indexOf("/");
	var domainName;
	if (loc2 < 0)
	{
		domainName = substr1;
	}
	else 
	{
		domainName = substr1.substring(0, loc2);
	}
	
	if (domainName == null)
		return;
	if (domainName == "")
		return;
	if (domainName == 'undefined')
		return;

	// If domainName is popjax or videojax, don't set it in the cookie
	var pLoc1 = domainName.indexOf("popjax.com");
	var pLoc2 = domainName.indexOf("videojax.com");
	if ((pLoc1 > -1) || (pLoc2 > -1))
	  return;

	document.cookie="pjReferDomainName="+domainName +"; expires= ; path=/";
}


function handleCampaign()
{
	var campaignName = gup("campaign");
	
	if (campaignName == null)
	  return;

	if (campaignName == "")
	  return;

	if (campaignName == 'undefined')
	  return;

	document.cookie="pjCampaignName="+campaignName+"; expires= ; path=/"; 
}

function handleRefererAndCampaign()
{
	extractReferer();
	handleCampaign();
}
