// 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 SEARCH_RESULTS_MAXTOTAL = 90;
  var SEARCH_RESULTS_MAXDISPLAY = 9;
  var SEARCH_RESULTS_COUNT;
  var SEARCH_RESULTS_currentX = 0;
  var SEARCH_RESULTS_rss;
	var SEARCH_RESULTS_targetElement;

  function SEARCH_RESULTS_myCallback(rss, targetElement)
  {
		SEARCH_RESULTS_currentX = 0;
    SEARCH_RESULTS_rss = rss;
		if(rss.items.length > SEARCH_RESULTS_MAXTOTAL)
			SEARCH_RESULTS_COUNT = SEARCH_RESULTS_MAXTOTAL;
		else
      		  SEARCH_RESULTS_COUNT = rss.items.length;
		//SEARCH_RESULTS_currentX = SEARCH_RESULTS_COUNT-1;
		SEARCH_RESULTS_targetElement = targetElement;
    SEARCH_RESULTS_write(targetElement);
  }
  function SEARCH_RESULTS_write(targetElement)
  {
		//make sure to close the wmv player
		if(wmp)
		  wmp.close();

	  if(!targetElement)
		  targetElement = SEARCH_RESULTS_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">'+ SEARCH_RESULTS_rss.channel.title +'</span></td></tr>';
		text += '<tr><td align="right" class="blockheader">';
    text += '<a href="javascript:loadXMLDoc(\'/S/ContentSet?WID='+ WID +'&R='+ (Math.random() * 999) +'\', featured_myCallback, \'MAIN\');">Random Video</a> | ';
    text += '<a href="javascript:highLight(\'keyword\', \'searchtd\');">Search</a> | ';
    text += '<a href="javascript:BROWSE(\'MAIN\');">Browse</a>';
		text += '</td></tr></table>';

    var item;
    var s;
    var link;
		if(SEARCH_RESULTS_COUNT == 0)
		{
			text += '<p><i>'+ SEARCH_RESULTS_rss.channel.title +' : <b> none </b></i></p>';
		}
		else
		{
			for(var x = SEARCH_RESULTS_currentX; x < SEARCH_RESULTS_COUNT && x < (SEARCH_RESULTS_MAXDISPLAY + SEARCH_RESULTS_currentX); x++)
			{
				item = SEARCH_RESULTS_rss.items[x];
				var keywordString = "";
				var keywords = item.category.split(",");
				for(var i = 0; i < keywords.length; i++)
				{
				  if(keywords[i].charAt(0) == '!')
					  continue;
					//  use the g flag for replace all '/_/g', string to search for is in / /
          keywordString += ' <a href="javascript:loadXMLDoc(\'/S/SearchResults?ST='+ keywords[i].replace("\'","\\\\'") +'&WID='+ WID +'\', SEARCH_RESULTS_myCallback, \'MAIN\');">'+ keywords[i] +'</a>';
        }
				var starPicArray = new Array();
				var starCount = (parseInt(item.rateSum) / parseInt(item.rateClicks)) / 2;
				for(var i = 1; i <= 5; i++)
				{ //alert(starCount);
					if(starCount > i)
						starPicArray[i-1] = "images/star.gif";
					else
						starPicArray[i-1] = "images/star_disable.gif";
				}
        link = CONTENTDOMAIN +'/content_tour/'+ item.studioName +'/'+ item.titleName +'/'+ item.sceneName;
				text += '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="blockBottomBorder"><tr><td width="158">';
				text += '<a href="javascript:loadXMLDoc(\'/S/ContentSet?WID='+ WID +'&CID='+ item.guid +'\', featured_myCallback, \'MAIN\');">';
      	text += '<img src="'+ link +'/tour/t1.jpg" onmouseover="startImageSwap(this)" onmouseout="stopImageSwap()" width="150" height="150" alt="" style="background-color: #000000" class="thumbstop" border="0"/></a></td>';
				text += '<td align="left" valign="top" class="blockPad gradientLightUp"><table width="100%" height="150" border="0" cellspacing="0" cellpadding="0"><tr><td align="left" valign="top">';
        text += '<a href="javascript:loadXMLDoc(\'/S/ContentSet?WID='+ WID +'&CID='+ item.guid +'\', featured_myCallback, \'MAIN\');" class="style12px">'+ item.name +'</a></td></tr>';
				text += '<tr><td class="justify" valign="top" style="font-size:12px">'+ item.desc2 +'</td></tr><tr><td class="justify" style="font-size:11px">Tags: '+ keywordString +'</td></tr></table></td>';
				text += '<td width="120" align="left" valign="top" nowrap="nowrap" class="blockPad gradientLight">';
				text += '<strong>Added:</strong><br />'+ item.pubDate +'<br /><strong>Views:</strong> '+ item.clicks +'<br />';
				text += '<table border="0" cellspacing="0" cellpadding="0"><tr>';
      	text += '<td align="center" valign="middle"><a href="javascript:join();"><img id="SEARCH_RESULTS_'+ item.guid +'_1" src="'+ starPicArray[0] +'" onmouseover="showStars(\'SEARCH_RESULTS_'+ item.guid +'\', 1, \'images/star.gif\');" onmouseout="clearStars(\'SEARCH_RESULTS_'+ item.guid +'\', 1, \'images/star_disable.gif\');" width="20" height="19" border="0" /></a></td>';
        text += '<td align="center" valign="middle"><a href="javascript:join();"><img id="SEARCH_RESULTS_'+ item.guid +'_2" src="'+ starPicArray[1] +'" onmouseover="showStars(\'SEARCH_RESULTS_'+ item.guid +'\', 2, \'images/star.gif\');" onmouseout="clearStars(\'SEARCH_RESULTS_'+ item.guid +'\', 2, \'images/star_disable.gif\');" width="20" height="19" border="0" /></a></td>';
        text += '<td align="center" valign="middle"><a href="javascript:join();"><img id="SEARCH_RESULTS_'+ item.guid +'_3" src="'+ starPicArray[2] +'" onmouseover="showStars(\'SEARCH_RESULTS_'+ item.guid +'\', 3, \'images/star.gif\');" onmouseout="clearStars(\'SEARCH_RESULTS_'+ item.guid +'\', 3, \'images/star_disable.gif\');" width="20" height="19" border="0" /></a></td>';
        text += '<td align="center" valign="middle"><a href="javascript:join();"><img id="SEARCH_RESULTS_'+ item.guid +'_4" src="'+ starPicArray[3] +'" onmouseover="showStars(\'SEARCH_RESULTS_'+ item.guid +'\', 4, \'images/star.gif\');" onmouseout="clearStars(\'SEARCH_RESULTS_'+ item.guid +'\', 4, \'images/star_disable.gif\');" width="20" height="19" border="0" /></a></td>';
        text += '<td align="center" valign="middle"><a href="javascript:join();"><img id="SEARCH_RESULTS_'+ item.guid +'_5" src="'+ starPicArray[4] +'" onmouseover="showStars(\'SEARCH_RESULTS_'+ item.guid +'\', 5, \'images/star.gif\');" onmouseout="clearStars(\'SEARCH_RESULTS_'+ item.guid +'\', 5, \'images/star_disable.gif\');" width="20" height="19" border="0" /></a></td>';
        text += '</tr></table>';
				text += '<br /><a href="javascript:join();"><img src="images/favorites.gif" border="0" /></a>'
				text += '</td></tr></table>';
			}
		}
		var currentPg = Math.ceil((SEARCH_RESULTS_currentX + SEARCH_RESULTS_MAXDISPLAY) / SEARCH_RESULTS_MAXDISPLAY);
    var totalPgs = Math.ceil(SEARCH_RESULTS_COUNT/ SEARCH_RESULTS_MAXDISPLAY);
    text += '<div align="right"><b>Page '+ currentPg +' of '+ totalPgs +' &nbsp;&nbsp;';

    if(SEARCH_RESULTS_currentX >= SEARCH_RESULTS_MAXDISPLAY)
    {
      text += '<a href="javascript:SEARCH_RESULTS_previous();"><< previous </a>... ';
    }
    for(var i = 1, x = 0; i <= SEARCH_RESULTS_COUNT; i += SEARCH_RESULTS_MAXDISPLAY, x++)
    {
      text += ' <a href="javascript:SEARCH_RESULTS_currentX = ('+ x +' * SEARCH_RESULTS_MAXDISPLAY);SEARCH_RESULTS_write();">'+ (x + 1) +'</a>';
    }
    if((SEARCH_RESULTS_currentX + SEARCH_RESULTS_MAXDISPLAY) < SEARCH_RESULTS_COUNT && (SEARCH_RESULTS_currentX + SEARCH_RESULTS_MAXDISPLAY) < SEARCH_RESULTS_MAXTOTAL)
    {
      text += ' ...<a href="javascript:SEARCH_RESULTS_next();"> next >></b></div>';
    }

    var element = document.getElementById(targetElement);
    element.innerHTML = text;
    window.location = "#FOCUS"+ SEARCH_RESULTS_rss.channel.title;
  }
  function SEARCH_RESULTS_previous()
  { // alert(currentX - (MAXDISPLAY));
    SEARCH_RESULTS_currentX = SEARCH_RESULTS_currentX - SEARCH_RESULTS_MAXDISPLAY;
    SEARCH_RESULTS_write();
    //window.location = "#SR"+ SEARCH_RESULTS_rss.channel.title + SEARCH_RESULTS_currentX;
    window.location = "#FOCUS";
  }
  function SEARCH_RESULTS_next()
  {
    SEARCH_RESULTS_currentX = SEARCH_RESULTS_currentX + SEARCH_RESULTS_MAXDISPLAY;
    SEARCH_RESULTS_write();
    //window.location = "#SR"+ SEARCH_RESULTS_rss.channel.title + SEARCH_RESULTS_currentX;
    window.location = "#FOCUS";
  }
  //loadXMLDoc('/S/SearchResults?ST='+ qsParm["ST"], SEARCH_RESULTS_myCallback, 'MAIN');
}
