$(function(){                
	// animates the play icon on rollover/rollout of thumbs
	$("#sidebar div#playlist div#scroll_content > ul li div").hover(
		function(){
			ie = $.browser.msie;
			if ( ie )
				$(this).find("b").hide();
			else
				$(this).find("b").stop().animate({ opacity:0.4 }, 160 );
			KalturaThumbRotator.start($(this).find("img"));
		}, 
		function(){
			ie = $.browser.msie;
			if ( ie )
				$(this).find("b").show();
			else
				$(this).find("b").stop().animate({ opacity:1 }, 160 );
			KalturaThumbRotator.end($(this).find("img"));
		}
	);
	
	$('#scroll_content').jScrollPane({scrollbarMargin:15, scrollbarWidth:17, dragMinHeight:30, dragMaxHeight:30});

});


var mykdp = new KalturaPlayerController("kaltura-static-playlist");

function init_page(entry_id,tag) {
	mykdp.currentEntryId = entry_id;
	$('#'+tag+'_'+entry_id).addClass('active_entry');
	$('#videoBoxMenu li a').each(function(){
		$(this).click(function(){
		toggle_visibles(this.id);
		a_id = this.id;
		li_parent = $(this).parent('li');
		$('#videoBoxMenu li').removeClass('active');
		$('#videoBoxMenu li').attr('style','');
		$(li_parent).addClass('active');
		$(li_parent).attr('style','background-color:#'+color_for_id(a_id));
		});
	});
	
	
	$("div#scroll_content > ul li").each(function(){
		if ( this.id ) {
			$(this).click(function(){ 
				switch_player_item(this.id);
			});
		}
	});
}

function switch_player_item(entry_id,tag) {
	mykdp.insertEntry(entry_id,true);
	mykdp.currentEntryId = entry_id;
	if ( tag ) {
		$('#'+tag+'_'+entry_id).addClass('active_entry');
	}
	// populate name and description
	$("#bubble h2").text($("div#scroll_content > ul li#" + entry_id + " p+p").text());
	$("#bubble p").text($("div#scroll_content > ul li#" + entry_id).attr("title"));
}

KalturaThumbRotator = {

	slices : 16, // number of thumbs per video
	frameRate : 1000, // frameRate in milliseconds for changing the thumbs
	
	timer : null,
	slice : 0,
	img  : new Image(),
	
	thumbBase : function (o) // extract the base thumb path by removing the slicing parameters
	{
		var path = o.attr('src');
		
		var pos = path.indexOf("/vid_slice");
		if (pos != -1)
			path = path.substring(0, pos);
			
		return path;
	},
	

	change : function (o, i) // set the Nth thumb, request the next one and set a timer for showing it
	{
		slice = (i + 1) % this.slices;

		var path = this.thumbBase(o);
		
		o.attr('src', path + "/vid_slice/" + i + "/vid_slices/" + this.slices);
		o.attr('src', path + "/vid_slice/" + slice + "/vid_slices/" + this.slices);

		i = i % this.slices;
		i++;
		
		this.timer = setTimeout(function () { KalturaThumbRotator.change(o, i) }, this.frameRate);
	},
	
	start : function (o) // reset the timer and show the first thumb
	{
		clearTimeout(this.timer);
		var path = this.thumbBase(o);
		this.change(o, 1);
	},

	end : function (o) // reset the timer and restore the base thumb
	{
		clearTimeout(this.timer);
		o.attr('src', this.thumbBase(o));
	}
};


$('document').ready(function(){
	init_page();
});
