var blipurl = 'http://blip.tv/?1=1&s=posts&skin=rss';

var currentPlaylist = null;
var currentLength = 0;
var currentItem = -1; 
var previousItem = -1; 
var currentMute = false; 
var currentVolume = 80; 
var currentPosition = 0; 
var currentState = 'NONE';
var currentLoaded = 0;
var currentRemain = 0;

var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}

function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
		player.addControllerListener("MUTE", "muteListener");
		player.addModelListener("LOADED", "loadedListener");
		player.addModelListener("STATE", "stateListener");
		player.addModelListener("TIME", "positionListener");
		player.addViewListener("VOLUME", "volumeListener");

	} else {
		setTimeout("addListeners()",100);
	}
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	
	if (currentState == "COMPLETED") {
		//alert("current state: " + currentState + " - previous state: " + previousState);
		jQuery(function() {
			jQuery('#carousel-video').show();
		});
	}
	if (currentState == "PAUSED") {
	    //alert("play");
		jQuery(function() {
			jQuery('#carousel-video').show();
		});
	}
	if (currentState == "IDLE") {
	    //alert("play");
		jQuery(function() {
			jQuery('#carousel-video').show();
		});
	}
	if (currentState == "PLAYING") {
	    //alert("play");
		jQuery(function() {
			jQuery('#carousel-video').hide();
		});
	}		

	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "current state: " + currentState + 
		"<br>previous state: " + previousState; 
	}
}

function positionListener(obj) { 
	currentPosition = obj.position; 
	var tmp = document.getElementById("tim");
	if (tmp) { tmp.innerHTML = "position: " + currentPosition; }
}


function loadedListener(obj) { 
	currentLoaded = obj.loaded; 
	currentRemain = obj.total - currentLoaded;
	var tmp = document.getElementById("ldd");
	if (tmp) { 
		tmp.innerHTML = "bytes loaded: " + currentLoaded +
				"<br>bytes remaining: " + currentRemain; 
	}
}


function volumeListener(obj) { 
	currentVolume = obj.percentage; 
	var tmp = document.getElementById("vol");
	if (tmp) { tmp.innerHTML = "volume: " + currentVolume; }
}


function muteListener(obj) { 
	currentMute = obj.state; 
	var tmp = document.getElementById("mut");
	if (tmp) { tmp.innerHTML = "mute: " + currentMute; }
}


function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index;

		if (previousItem == -1) { getPlaylistData(); }

		var tmp = document.getElementById("itm");
		if (tmp) { 
			tmp.innerHTML = "current item: " + currentItem +
				"<br>previous item: " + previousItem;
		}

		var tmp = document.getElementById("item");
		if (tmp) { tmp.innerHTML = "item: " + currentItem; }

		var tmp = document.getElementById("pid"); 
		if (tmp) { 
			tmp.innerHTML = "(received from the player with the id: <i><b>" + obj.id + "</b></i>)"; 
		} 

		printItemData(currentItem);
	}
}


function printItemData(theIndex) {
	var plst = null;
	plst = player.getPlaylist();

	if (plst) {
		var txt = '';
		txt += '<li><b>item number: </b>' + theIndex + ':</li>';
		txt += '<li><b>title: </b>' + plst[theIndex].title + '</li>';
		txt += '<li><b>author: </b>' + plst[theIndex].author + '</li>';
		txt += '<li><b>file: </b>' + plst[theIndex].file + '</li>';
		txt += '<li><b>image: </b>' + plst[theIndex].image + '</li>';
		txt += '<li><b>link: </b><a href="' + plst[theIndex].link + '">' + plst[theIndex].link + '</a></li>';
		//txt += '<li><b>description: </b>' + plst[theIndex].description + '</li>';

		var tmp = document.getElementById("itmsDat");
		if (tmp) { tmp.innerHTML = txt; }
	} 	
}


function getPlaylistData() { 
	var plst = null;
	plst = player.getPlaylist();

	if (plst) { 
		currentPlaylist = plst; 

		var txt = ''; 
		for(var i in currentPlaylist) { 
			txt += '<input type="checkbox" id="cb' + i + '" checked="checked" /> &nbsp; ';
			txt += currentPlaylist[i].title;
			txt += '<br />';
		}
		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; }
	}	
}


function loadCheckedPlaylistData() { 
	if (currentPlaylist) { 
		var j = 0; 
		var lst = new Array();
		for(var i in currentPlaylist) { 
			if(document.getElementById('cb' + i).checked) {
				lst[j] = {
					author:currentPlaylist[i].author,
					description:currentPlaylist[i].description,
					duration:currentPlaylist[i].duration,
					file:currentPlaylist[i].file,
					link:currentPlaylist[i].link,
					image:currentPlaylist[i].image,
					start:currentPlaylist[i].start,
					title:currentPlaylist[i].title,
					type:currentPlaylist[i].type
				}
				j++;
			}
		}
		if(lst.length > 0) { player.sendEvent('LOAD', lst); }
	}	
}


/*function getLength() { currentLength = player.getPlaylist().length; return(currentLength); };

function loadFile(theFile) { currentItem = -1; previousItem = -1; player.sendEvent('LOAD', theFile); };*/

/*function createPlayer(theFile) {
            var flashvars = {
                    file:theFile, 
                    autostart:"true", 
                    shuffle:"false", 
                    playlistsize:"150",
                    playlist:"bottom"
            }
            var params = {
                    allowfullscreen:"true", 
                    allowscriptaccess:"always"
            }
            var attributes = {
                    id:"player1",  
                    name:"player1"
            }
            swfobject.embedSWF("player.swf", "placeholder1", "320", "350", "9.0.115", false, flashvars, params, attributes);
}*/

function stopPlayer() {
	if (player) {
		player.sendEvent('STOP');
	}
}
