// JavaScript Document

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; 

	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "Current: " + currentState + 
		"<br>Previous: " + 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 Pending: " + 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: <b>" + obj.id + "</b>)"; 
		} 

		printItemData(currentItem);
	}
}


function printItemData(theIndex) {
	var plst = null;
		
	plst = player.getPlaylist();

	if (plst) {
	
  	var vfile = plst[theIndex].file.slice(-3);
    	
    var vimg = '';
    vimg += '<img class="vinfo" src="' + plst[theIndex].image + '" alt="' + plst[theIndex].title + '" title="' + plst[theIndex].title + '" />';
  
    var txt = '';
		
    txt += '<li><b>Title:</b> ' + plst[theIndex].title + '</li>';
		if (plst[theIndex].description){
    txt += '<li><b>Details: </b>' + plst[theIndex].description + '</li>';
    }
    if (plst[theIndex].tags){
    txt += '<li><b>Tags: </b>' + plst[theIndex].tags + '</li>';
 	  }
	  if (plst[theIndex].id && showpro){
    txt += '<li><a href="' + siteurl + '/index.php?option=com_videoflow&task=addtolist&id=' + plst[theIndex].id + '&media=' + vfile + '&Itemid=' + vitemid + '&title=' + plst[theIndex].title +'">Dodaj na moju Playlistu</a></li>'; 
	  txt += '<li><a href="' + siteurl + '/index.php?option=com_videoflow&task=removefromlist&id=' + plst[theIndex].id + '&Itemid=' + vitemid + '&title=' + plst[theIndex].title +'">Ukloni sa moje Playliste</a></li>';
		if (canvasurl){
		txt += '<li><a href="' + canvasurl + '&task=addfavs&id=' + plst[theIndex].id + '&ftype=' + vfile + '&ftitle=' + plst[theIndex].title + '&pix=' + siteurl + '/' + plst[theIndex].image + '" onclick="targetBlank(this.href);return false;"> Add to My Facebook Playlist</a></li>';
    }
    }
    if (plst[theIndex].id){
    txt += '<li><a href="' + siteurl + '/index.php?option=com_videoflow&task=email&id=' + plst[theIndex].id + '&Itemid=' + vitemid + '&title=' + plst[theIndex].title + '&pix=' + siteurl + '/' + plst[theIndex].image + '&no_html=1" onclick="targetBlankpop(this.href);return false;"> Posalji email-om prijatelju</a></li>';                  
    }
    
    if (downloads && showpro && vmode){
		txt += '<li><a href="' + plst[theIndex].file + '">Download</a></li>';
		}
    if (plst[theIndex].link){
    linktxt = '<h2>Media Link</h2><div id = "linktext"> <textarea rows="3" cols="30" readonly ="readonly">' + plst[theIndex].link + '</textarea></div>';
    }
		
    var tmpimg = document.getElementById("itmsImg");
		if (tmpimg) { tmpimg.innerHTML = vimg; }
    
    var tmp = document.getElementById("itmsDat");
		if (tmp) { tmp.innerHTML = txt; }
		var tmplink = document.getElementById("linkbox");
		if (tmplink) { tmplink.innerHTML = linktxt; }
			} 	
}


function getPlaylistData() { 
	var plst = null;
	plst = player.getPlaylist();

	if (plst) { 
		currentPlaylist = plst; 

		var txt = ''; 
		for(var i in currentPlaylist) { 
	
	   if (isInteger (i)){
      txt += '<input type="checkbox" id="cb' + i + '" /> &nbsp; ';  
		  txt += '<a href="javascript:player.sendEvent(\'ITEM\',' + i + ')">' + currentPlaylist[i].title + '</a>';
      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 (isInteger (i)){
      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,
					meta:currentPlaylist[i].meta,
					tags:currentPlaylist[i].tags,
					identifier:currentPlaylist[i].identifier
				}
				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 isInteger(val){
      if(val==null){return false;}
      if (val.length==0){return false;}
      for (var i = 0; i < val.length; i++) {
            var ch = val.charAt(i)
            if (ch < "0" || ch > "9") {
            return false
            }
      }
      return true;
}

function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}

function targetBlankpop (url) {
  blankWin = window.open(url,'_blank','menubar=no,toolbar=no,location=yes,directories=no,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}

