// JavaScript Document
{
/*
'rss' being the class object, its elements are 'channel' and 'items'
'channel' elements:title,link,description,language,copyright,managingEditor,webMaster,pubDate,lastBuildDate
'items' element is an array of 'item' objects
'item' elements: title,link,description,author,category,comments,guid,pubDate,wid,siteURL,rateSum,rateClicks,rating
*/
var width = 570;
var height = 380; //66% aspect ratio
var wmp;
var buffStarted = false;
var featured_wid;
var featured_cid;
var featured_channel;
var featured_item;
var featured_targetElement;
function featured_myCallback(rss, targetElement)
{
  //these must set prior to calling any methods...
	featured_channel = rss.channel;
	featured_item = rss.items[0];
	featured_wid = featured_item.wid;
	featured_cid = featured_item.guid;
	featured_targetElement = targetElement;
	featured_writePlayer(targetElement);
	watchCount();
}

var FEATURED_next = false;
function FEATURED_goNext()
{
	FEATURED_next = true;
}
var FEATURED_i = 0;
var video = qsParm['V'];

function featured_writePlayer(targetElement)
{
  //RESET EACH TIME///////////////
	if(wmp)
	  wmp.close();
  buffStarted = false;
  width = 570;
  height = 380; //66% aspect ratio

  ////////////////////////////////

  if(!targetElement)
    targetElement = featured_targetElement;
/*
	var image1 = new Image();
	image1.src = featured_item.link +'/tour/v2.jpg';
	image1.onLoad = FEATURED_goNext();
	if(FEATURED_i < 9 && !FEATURED_next)
	{
		FEATURED_i++;
		setTimeout("featured_writePlayer()",550);
		return;
	}
*/
	var featured_title = "Featured Video";
	if(!video || video == null)
		featured_title = "Now Playing";

  //INNERHTML for targetElement
	{
		var text = '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr><td align="center" valign="top">';
		text += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		text += '<tr><td height="25" align="left" valign="middle"><span class="style6">'+ featured_title +' - <span class="style14px">';
		text += featured_item.name +' - <i>'+ featured_item.pubDate +'</i> </span></span></td></tr>';
    text += '<tr><td align="center" class="blockBottomBorder">';
		text += '<div id="CONTENT_FEATURED_PLAYER" style="padding-top:5px;text-align:center;position:relative;"> loading... </div>';
  	text += '<div id="CONTENT_FEATURED_PLAYER_LINKS" style="padding-top:8px;padding-left:10px;text-align:left"></div><br /></td></tr><tr><td>';
    text += '<a href="javascript:join();"><img src="images/btn_tube_seperatorclickhere.gif" width="576" height="77" alt="FULL VIDEOS" border="0"></a>';
		text += '<div id="CONTENT_MORE"> loading...</div>';
    text += '<a href="javascript:join();"><img src="images/btn_tube_seperatorclickhere.gif" width="576" height="77" alt="FULL VIDEOS" border="0"></a>';
    text += '</td></tr></table></td></tr></table>';
		var element = document.getElementById(targetElement);  //alert(element);
		element.innerHTML = text;
	}

  //CONTENT_FEATURED_PLAYER
  {
    featured_item.link = CONTENTDOMAIN +'/content_tour/'+ featured_item.studioName +'/'+ featured_item.titleName +'/'+ featured_item.sceneName;
    var element = document.getElementById("CONTENT_FEATURED_PLAYER");
    element.style.width = width;
    element.style.height = height;
    var text = '<div align="center" style="width:'+ width +'px;height:'+ height +'px;text-align:center;padding-left:0px;cursor:hand;cursor:pointer;position:relative;">';
    text += '<img src="'+ featured_item.link +'/tour/v1.jpg" width="'+ width +'" height="'+ height +'" onClick="wmvPlay()" style="z-index:2;position:relative;border:3px solid white;">';
    text += '<img id="VIDPIC" alt="CLICK TO PLAY" src="/images/wmvplay.png" onClick="play()" style="z-index:9;position:absolute;top:0px;left:0px;cursor:hand;" width="'+ width +'" height="'+ height +'"></div>';
    element.innerHTML = text;
  }

  //CONTENT_FEATURED_PLAYER_LINKS
  {
    video = WMSDOMAIN + featured_item.titleName +'/'+ featured_item.titleName +'_'+ featured_item.sceneName +'_trailer.wmv';
    var text = '<div style="font-size:16px;padding-bottom:8px;">'+ featured_item.desc1 +'</div>Linking Code: <input type="text" value="http://'+ URL +'/index.shtml?WID='+ WID +'&CID='+ featured_item.guid +'" style="width:55%;height:18px;background-color:#000000;color:#ffffff;font-size:12px;border:1px;border-style:solid;border-color:#666666" readonly onclick="this.select();"><br />';
    text += '<a href="'+ video +'">watch in standalone windows media player</a><br />';
    //this randomNum is needed to ensure a new contentset is loaded and not the last cached
    var randomNum = (Math.random() * 999);
    text += '<a href="javascript:loadXMLDoc(\'/S/ContentSet?WID='+ WID +'&R='+ randomNum +'\', featured_myCallback, \'MAIN\');">Click for a random video</a>';
    document.getElementById("CONTENT_FEATURED_PLAYER_LINKS").innerHTML = text;
  }

  //CONTENT_MORE
  {
    if(CONTENT_SETSBYTITLE_titleName != featured_item.titleName || !element)
    {
      loadXMLDoc('/S/SearchResults?ST='+ featured_item.titleName +'&WID='+ WID, CONTENT_SETSBYTITLE_myCallback, '');
    }
    else
    { //if same title / search, then just reshow existing content...
      CONTENT_SETSBYTITLE_write();
    }
  }

	video = null;
	window.location = "#FOCUS";
}

function play()
{
  var waitTime = 5000;
  var autoStart = "true";
  var text = ""
  var element = document.getElementById("CONTENT_FEATURED_PLAYER");
//not using WMP as default for IE, right now only FLV player is used
  if(bd.browser == "ExplorerX" || (video != null && video.indexOf(".wmv") != -1) || false)
	{
    waitTime = 9000;  //for WMP we can detect when bufferign done, so running the preview longer is good
    var pHeight = 460;
    element.style.width = width;
    element.style.height = height;
    if(video == null || video == "")
    {
      //this should be a domain served by wms
      video = WMSDOMAIN + featured_item.titleName +'/'+ featured_item.titleName +'_'+ featured_item.sceneName +'_trailer.wmv';
    }
    else
    {
      video = featured_item.link + video;
    }
    text = '<OBJECT CLASSID="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" ID="WMP" width="'+ width +'" height="'+ pHeight +'" style="visibility:hidden">';
    text += '<PARAM NAME="Name" VALUE="WMP1"><PARAM NAME="URL" VALUE="'+ video +'"><param name="uiMode" value="full"><param name="AutoStart" value="'+ autoStart +'"><param name="stretchToFit" value="true"><param name="showFrame" value="true">';
    text += '<embed type="application/x-mplayer2" autostart="'+ autoStart +'" autosize="true" showFrame="true" stretchToFit="false" uiMode="full" src="'+ video +'" name="WMP" width="'+ width +'" height="'+ pHeight +'"></embed>';
    text += '</OBJECT>'; //<br /><img src="" alt="FULL SCREEN" onClick="fullScreen();">
    text += '<div id="CONTENT_FEATURED_PLAYER_OVERLAY" style="position:absolute;top:0px;left:0px;z-index:999;"><img src="/images/bookend_front.gif" width="'+ width +'" height="'+ height +'" border="0" /></div>';
    element.innerHTML = text;

    wmp = document.getElementById("WMP");
	  if(wmp)
	    wmp.attachEvent("playStateChange",wmpEvents);
	}
	else
	{
    var pHeight = 404;
    element.style.height = height;
		if(video == null || video == "")
			video = "/tour/trailer.flv";
		text += '<EMBED src="resources/mediaplayer570w.swf" width="'+ width +'" height="'+ pHeight +'" allowscriptaccess="always" allowfullscreen="true" wmode="transparent"';
		text += 'flashvars="enablejs=true&bufferlength=6&autostart='+ autoStart +'&height='+ height +'&width='+ width +'&file='+ featured_item.link + video +'&image='+ featured_item.link +'/tour/v1.jpg&backcolor=0xEEEEEE&frontcolor=0x000000&lightcolor=0x000000&screencolor=0x000000&overstretch=false&showstop=true&usefullscreen=true&allowfullscreen=true&recommendations=recommendations.xml"';
		text += 'NAME="FLVP" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">';
		text += '</EMBED>';
//ADD to flashvars FOR postback to GOOGLE analytics
//callback=analytics
    text += '<div id="CONTENT_FEATURED_PLAYER_OVERLAY" style="position:absolute;top:0px;left:0px;z-index:999999;"><img src="/images/bookend_front.gif" width="'+ width +'" height="'+ height +'" border="0" /></div>';
    element.innerHTML = text;
	}

  //if(showPreview)

  setTimeout("showVid()", waitTime); 
}
  function showVid()
  {
	  showPreview = false;
		var element = document.getElementById("CONTENT_FEATURED_PLAYER_OVERLAY");
    element.style.visibility = "hidden";
    if(wmp)
      wmp.style.visibility = "visible";
    else if(document.getElementById("FLVP"))
    {
      document.getElementById("FLVP").style.visibility = "visible";
    }
		//return;
  }
  //wmp script events
	function wmpEvents()
	{
    if(wmp && wmp.playstate == 6)
      buffStarted = true;
    if(wmp && buffStarted && wmp.playstate == 3)
    {
      showVid();
    }
		else if(wmp && wmp.playstate == 8 || wmp.playstate == 1)
		{
			wmp.close();
			//alert(wmp.playstate);
			var element = document.getElementById("CONTENT_FEATURED_PLAYER");
			//var text = '<a href="javascript:join();"><img id="VIDPIC" alt="INSTANT ACCESS" width="'+ element.style.width +'" height="'+ element.style.height +'" src="/images/join.jpg" style=" border: 3px solid white;"></a>';
			var text = '<a href="javascript:join();"><img id="VIDPIC" alt="INSTANT ACCESS" width="'+ element.style.width +'" height="'+ element.style.height +'" src="/images/join.jpg" style="z-index:99; border: 3px solid white;"></a>';
			//element.style.focus();
			element.innerHTML = text;
		}
	}
  //swf script events
  function sendEvent(swf,typ,prm)
  {
    thisMovie(swf).sendEvent(typ,prm);
  }
  function getUpdate(typ,pr1,pr2,swf) {};
  function thisMovie(swf)
  {
    if(navigator.appName.indexOf("Microsoft") != -1)
    {
      return window[swf];
    }
    else
    {
      return document[swf];
    }
  }

function watchCount()
{
  var cookie = readCookie("SCENEVIEWS");
  //cookie = 0;
  if(cookie != null)
  {
    if(cookie > 20)
    {
    //showAdd();
      var element = document.getElementById('CONTENT_FEATURED_PLAYER');
      element.innerHTML = '<a href="#"><img src=images/previewlimit.gif alt="Seen enough previews, Watch the Full Video Now!" border="0" onclick="join();" /></a>';
    }
    else
    {
      cookie++;
      createCookie("SCENEVIEWS", cookie, "1");
    }
  }
  else
    createCookie("SCENEVIEWS", 1, "1");
}

}
