var playersReady = 0;
var totalViewsUpdateTimeout = 20;

// Build jCarousel items
function getItemHTML(item) {
    var html = '';
    var imgurl = '/photos/otherinfimg.gif';
    item['Views'] = formatNumber(item['Views']); // Add commas

    html += '<a href="/?v=' + item['VideoName'] + '&amp;p=1" style="color:white" title="' + item['Title'] + ', viewed ' + item['Views'] +  ' times">'+
	     '<img src="'+item['ImageURL']+'" style="float:left; height:50px; width:50px; margin-right:5px;"/>'+
	    '<strong>'+item['Title']+'</strong>'+' by '+
		item['Publisher']+'<br/>'+
        '<strong>' + item['Views']+'</strong> views'+
        '</a>';
    
    return html;
}

// Add to jCarousel
function itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, car_data.length);
    carousel.add(i, getItemHTML(car_data[idx - 1]));
}

// Remove from jCarousel
function itemVisibleOutCallback(carousel, item, i, state, evt) {
    //carousel.remove(i);
}

// Enables auto update of infringement count
function onUpdateTotalViews(data) {
    if (data['totalviews']) {
	$('#totalviews').html(data['totalviews']);
	setTimer(totalViewsUpdateTimeout);
    }
}

// Enables auto update of infringement count
function setTimer(s) {
    //setTimeout("$.getJSON('/ajax/gettotalviews.php', {}, onUpdateTotalViews);", s*1000);
}

// Setup jCarousel
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
	wrap: "circular",
	vertical: true,
	auto: 3,
	scroll: 1,
	itemVisibleInCallback: {onBeforeAnimation: itemVisibleInCallback},
	itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback},
	buttonNextHTML: null,
	buttonPrevHTML: null,
	size: car_data.length
    });
    setTimer(1);
});

// Launch autoPlay
jQuery(document).ready(function() {
   if ((autoPlay) && (autoPlay==1)) {
     $('#launchplayer').click();
   }
});


// play infringing video
function playInf(obj) {
	$('#loadingplayers').show();
	playersReady = 0;
	var PubURL = publisherVideo['VideoURL'] + '';
	var id = (obj.id.substr(7));
	var DistributorURL = inflist[id]['VideoURL'] + '';
	var DistPos = inflist[id]['DistPos'] + '';
	var PubPos = inflist[id]['PubPos'] + '';
	
	$('#inf_dist').html(inflist[id]['Distributor'] + '');
	$('#inf_views').html('<a title="view source in new window" href="' + inflist[id]['DistributorURL'] + '" target="_blank">Viewed ' + formatNumber(inflist[id]['Views']) + ' times</a>');

    var player = $('#playerembed').get(0);
    player.loadVideos ( PubURL, PubPos, DistributorURL, DistPos );
	//player.loadVideo('Left',PubURL, PubPos);      // seek original video
	//player.loadVideo('Right',DistributorURL, DistPos);  // load infringing video

	return false;
}


// -----------------------------------------------------------------
function playerCallbackHandler(e)
{
	 if (e.type == 'loaded') 
	 {
		 $('#loadingplayers').hide();
		 /*
        playersReady ++;		
		if (playersReady == 2) {
			$('#loadingplayers').hide();
			playersReady = 0;
			var player = $('#playerembed').get(0);
			if (player) {player.playVideo();}  // play videos
		}
		*/
	 }
 }