function setMovieId( sender, id ) { 
	var flashMovie = document.getElementById('syndicatedSWF');
	litags = document.getElementsByTagName("LI");
	clipListindex = 0;
	clipListLen = litags.length;
	for (clipListindex =  0; clipListindex < clipListLen; clipListindex++)
	{
	    li = litags[clipListindex];
	    if (li.className == "clipRow_active") li.className = "clipRow";
	}
	
	if(sender.parentNode && sender.parentNode.className == "clipRow") sender.parentNode.className = "clipRow_active";

	if (flashMovie) flashMovie.setMovie( id );
	refreshAdCalls();
	populateMovieTitleAndDetails(id);
	return false;
}

// sets the state of list itemn to selected
function setItemSelected( id ) 
{
    var litags = document.getElementsByTagName("LI");
    var clipListindex = 0;
    var clipListLen = litags.length;
	
    for (clipListindex =  0; clipListindex < clipListLen; clipListindex++)
    {
        var li = litags[clipListindex];
	    
	    if (li.className == "clipRow" || li.className == "clipRow_active")
	    {
            if (li.getAttribute("id") != id)
            {
                if (li.className == "clipRow_active") li.className = "clipRow";
            }
            else 
            {
                li.className = "clipRow_active";
            }
        }
    }
    	populateMovieTitleAndDetails(id);
}   


// update ads in iframes
function populateMovieTitleAndDetails(id) {

    //$(".detailsPaneBody").load("http://localhost:2559/library/js/trailers/localhostajaxcalltest.htm",
    $(".detailsPaneBody").load("/services/GetClipDetail.aspx?format=html&clipid=" + id,
            function(){         
                var movieName = $('.detail-movieTitle .content').html();
                var movieYear = $('.detail-year .content').html();
                $('.detailsPaneHeader div.ajaxPopulate').html('<h3>' + movieName + '</h3> (' + movieYear + ')');}
            );
            
	return false;

}

// change movie title to link-friendly title with dashes
function addDashesToTitle(movieName)
{
    var post_slug_array = movieName.split(" ")
    var post_slug = post_slug_array.join("-")
    return post_slug;
}


// update ads in iframes
function refreshAdCalls() {
    ord = Math.random() * 10000000000000000;
    setCookie("__TEMP_ADORD", ord);
    $('iframe').each(function(i) {
        if (this.src != '' && this.src.indexOf('adsource.aspx') >= 0) {
            var index = this.src.indexOf('&adord=');
            if (index >= 0) {
                var iframeSrc = this.src.substring(this.src.indexOf('&adord='));
                iframeSrc = iframeSrc.substring(0, iframeSrc.indexOf(';'));
                this.src = this.src.replace(iframeSrc, '&adord=' + (Math.random() * 10000000000000000) + ';');
            }
            else {
                this.src = this.src + '&adord=' + (Math.random() * 10000000000000000) + ';';
            }
        }
    });
}

function showCompanionAd( url ) {
    //url = "http://ad.doubleclick.net/adi/rlz.hm/main;reelz=ad;test=yes;pos=preroll;tile=11;sz=300x250;ord";

    //var ord=Math.random()*10000000000000000;
    if (typeof ord=='undefined') { ord = getCookie("__TEMP_ADORD"); }
	var iFrame = document.getElementById( 'companion' );
	var iFrameContainer = document.getElementById( 'companionArea' );
	if ( !iFrame ) { return; }
    if (iFrame.src == '' || iFrame.src.indexOf('shim.gif') >= 0) { iFrame.src = url + ";ord=" + ord; }
    else { iFrame.src = iFrame.src; }
    if ( !iFrameContainer ) { return; }
    $( iFrameContainer ).fadeIn( 'normal' );
}
function hideCompanionAd() {
	var iFrame = document.getElementById( 'companion' );
	var iFrameContainer = document.getElementById( 'companionArea' );
	if ( !iFrame) { return; }
    if ( iFrameContainer ) { $( iFrameContainer ).fadeOut( 'normal' ); }
    iFrame.src = "http://cache.reelzchannel.com/assets/content/general/shim.gif";
}

$(document).ready( function() {    
    $('.clipList a').click( function( event ) {
        event.preventDefault();
        var urlparts = $(this).attr('href').split( '/' );
        var movieid = urlparts[ urlparts.length - 2 ];
        setMovieId( this, movieid );
     } );
     
} );

    // Find Reelz Channel Setup
    $(document).ready( setupFindReelz );
    function setupFindReelz() { $( '.findreelz a' ).click( showFindReelzResults ); }
    function showFindReelzResults( event ) {
	    event.preventDefault();
	$( '.myreelz' ).show( 'normal' );
    }
    function closeFindReelzResults( event ) {
	    event.preventDefault();
	    $( '.myreelz' ).hide( 'normal' );
    }
    
	// 'expando' movie details windowshade effect --%>
    $(document).ready(function(){
	    $(".detailsPaneBody").hide();
	    $(".detailsPaneToggle").toggle(function(){
		    $(".detailsPaneBody").slideDown(300);
		    $(".toggleIconOpen").css('display','inline-block');
		    $(".toggleIconClose").css('display','none');
		    $(".toggleText").text('Hide Details');

	    }, function(){
		    $(".detailsPaneBody").slideUp(300);
		    $(".toggleIconOpen").css('display','none');
		    $(".toggleIconClose").css('display','inline-block');
		    $(".toggleText").text('View Details');
	    });
    });
    
    // round corners jquery plugin from: 
    // http://plugins.jquery.com/project/corners 
    // Random Trailer Button Setup
$(document).ready( function() { 
    var firstClipID = $('li.clipRow_active').attr('id');
    populateMovieTitleAndDetails(firstClipID);
    $('#randomTrailer').click( function( event ) { 
 	var ordR=Math.random()*10000000000000000;
 	var url = "/services/GetRandomClip.aspx?ord=" + ordR;
        event.preventDefault(); 
        jQuery.get(url, function(data){
            setMovieId(this, data); 
        });
    }); 
    //$('.smallTab').corners("top 4px");
});

