function playEmbed(resourceId, partner_guid, clipInfoCallback)
{
    swfobject.embedSWF(api.getPlayer(), "FPlayer_obj", "414", "340", "8", "expressInstall.swf",
        { embedId: resourceId, pid: partner_guid },
        { allowScriptAccess: "always", allowFullScreen: "true" },
        { xiRedirectUrl: location.href });
        
    //if there's a callback function, supply the clip info
    if(clipInfoCallback)
    {
        api.Clip.getClipInfo(new Array(resourceId), function(data)
        { clipInfoCallback(data[0]); });
    }

    //If this is the first play, set initialPlay to false 
    //since the next time won't be the first play anymore
    if (firstLoad == true) firstLoad = false;    
}

function populateClipInfo(info)
{
	//Clean up the duration...
    var dur = (info.d%60).toString();
    if(dur.length != 2) dur = "0" + dur;
    dur = Math.floor(info.d/60) + ":" + dur;
    
    $("propTitle").innerHTML = info.t;
    $("propViews").innerHTML = info.views;
    $("propAuthor").innerHTML = info.author;
    //$("propCat").innerHTML = info.category;
    $("propAired").innerHTML = info.aired;
    $("propDuration").innerHTML = dur;
    $("propLink").href = info.stationurl;
    $("propSLogo").src = info.logo;
}

/* ---Description---
* The getParam function is used to collect parameters from the URL
* of the page. This is used in a case when a clip guid is specified
* in the URL, defaulting the player to that clip first.
* ---Parameters---
* name - The name of the parameter of which we want the value
*/
function getParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function selectClip(resourceId, element)
{
    //first set all the popular clips to default class
    var clipDivs = $A($("hotclipResultsDiv").childNodes);
    clipDivs.each(function(clipDiv) {
    clipDiv.setAttribute("class", "clip");
    });
    
    //now do it again for the regular clips
    var clipDivs = $A($("clipResultsDiv").childNodes);
    clipDivs.each(function(clipDiv) {
    clipDiv.setAttribute("class", "clip");
    });
        
    if(element) element.className = "clipON";
    playEmbed(resourceId,PARTNER_GUID,populateClipInfo);
}

function populatePopularClips(elementId, term, cat, limit) {
    api.Clip.getPopularClips(term, cat, POP_PARTNER_GUID, limit, function(data) {
        //Now we need to retrieve the info for each of the guids returned
        api.Clip.getClipInfo(data, function(newData) {
            //Once we have the clips' info, we can call the build function to display the DIVs
            buildClips(newData, elementId, function() {
                $("prevButton").style.display = "none";
                $("nextButton").style.display = "none";
            });
        });

        //For this example, we want the player to default load the first clip
        if (firstLoad) {
            selectClip(data[0]);
            firstLoad = false;
        }
    });
}

function searchClips(elementId,term,cat,limit)
{
    api.Clip.searchClips(term, cat, PARTNER_GUID, MAX_RESULTS, function (data)
    { 
		if(data != null)
		{
			paginator = new RedLasso.Util.Paginator(data,api,buildClips,limit);
			if(data.length <= CLIPS_PER_PAGE)
			{
				$("prevButton").style.display = "none";
				$("nextButton").style.display = "none";
			}
			else
			{
				$("prevButton").style.display = "inline";
				$("nextButton").style.display = "inline";

			}
		}
		else
		{
			$(elementId).innerHTML = "<i>No results.</i>";
			$("prevButton").style.display = "none";
			$("nextButton").style.display = "none";
		} 
	});
}

function doSearch()
{
    var term = $F("filterTerms");
    var cat = $F("clipCategory");
    searchClips("clipResultsDiv", term, cat,CLIPS_PER_PAGE);
}

function checkSearch(element, event)
{   //If the enter key has been pressed, do the search
    if(event.keyCode == 13) doSearch();
}

function buildClips(data,elementID,opt_fn)
{
    if(!elementID) elementID = "clipResultsDiv";
	$(elementID).innerHTML = "";
		
    for(var i=0; i<data.length; i++)
    {
        var title = data[i].t;
        if(title.length > 25)
            title = title.substr(0,25) + "...";
        var airDate = data[i].aired.substr(0,data[i].aired.indexOf(" "));
        var html = "<div title=\""+data[i].t+"\" onclick=\"javascript:selectClip('"+data[i].g+"',this);\" onmouseout=\"this.style.cursor='default';\" onmouseover=\"this.style.cursor='pointer';\" class=\"clip\" id=\""+data[i].g+"_div\" style=\"cursor: default;\"><img align=\"left\" style=\"float: left; width: 96px; height: 72px;\" id=\""+data[i].g+"_img\" src=\"images/wmmr_logo.png\"/><strong>"+title+"</strong><br/><br/><div><strong>Aired:</strong> "+airDate+"</div></div>";
        $(elementID).innerHTML = $(elementID).innerHTML + html;
    }
    if(opt_fn) opt_fn();
}

function getNext(){
	$("clipResultsDiv").innerHTML = "Loading...";
	if(paginator.next())
	{
		$("nextButton").disabled = false;
		$("nextButton").className = "arrowEnabled";
	}
	else
	{
		$("nextButton").disabled = true;
		$("nextButton").className = "arrowDisabled";
	}
	$("prevButton").disabled = false;
	$("prevButton").className = "arrowEnabled";
}

function getPrev()
{
	$("clipResultsDiv").innerHTML = "Loading...";
	if(paginator.prev())
	{
		$("prevButton").disabled = false;
		$("prevButton").className = "arrowEnabled";
	}
	else
	{
		$("prevButton").disabled = true;
		$("prevButton").className = "arrowDisabled";
	}
	$("nextButton").disabled = false;
	$("nextButton").className = "arrowEnabled";
}

var PARTNER_GUID = "557e0dcc-2c69-102b-a7e0-00142274adef";
var POP_PARTNER_GUID = "557e0dcc-2c69-102b-a7e0-00142274adef";
var MAX_RESULTS = 50;
var CLIPS_PER_PAGE = 5;
var api = new RedLasso.Util.JSAPI();
var pageinator;
var firstLoad = true;