media = new Array();

function showMedia(id, elem) {
	// Put the current media_id in the hidden field
	jQuery('#yp-id-'+media[id][0]).val(media[id][1][elem][1]);
	
	// Create the player with content
	var player = makePlayer(media[id][0], media[id][1], elem);
	if (player != null) player.write('yp-media-'+media[id][0]);
	
	// Put the title of the current media
	var title = media[id][1][elem][2];
	jQuery('#yp-title-'+media[id][0]).html(title);
	
	// Put the votes of the current media
	countVotes(media[id][0]);
	
	// Retrive the visits to the current media
	getVisits(media[id][0]);
	
	// Put controls of the widget
	jQuery('#yp-controls-'+media[id][0]).html(obtenerListado(id));
	
	// Make a visit
	makeVisit(media[id][0]);
	
	getViews(media[id][0]);
	//p = document.getElementById('ply-'+media[id][0]);
	//alert(p);
	//p.addControllerListener("PLAY", "handlerPlayEvent");
}

// Handling the event of each player
function playerReady(thePlayer) {
	//var player = document.getElementById(thePlayer.id);
	//player.addControllerListener("PLAY", "handlerPlayEvent");
}
// Handling the event PLAY of each player
function handlerPlayEvent(obj) {
	//for(e in o) alert(e+":"+o[e]);
	//alert(obj.state);
	/*if (obj.state == true) {
		//makeView(o['id'].substring(7));
		//id = o['id'].substring(7);
		alert(o.id);
	}*/
}

/**
 * Visits
 */
function makeVisit(nWidget) {
	id = jQuery('#yp-id-'+nWidget).val();
	jQuery.get(path+"php/yp-visits.php", {op: 'mv', id: id}, function(data) {
		updateVisits();
	});
}
function getVisits(nWidget) {
	id = jQuery('#yp-id-'+nWidget).val();
	jQuery.get(path+"php/yp-visits.php", {op: 'cv', id: id}, function(data) {
		jQuery('#yp-visits-'+nWidget).html("Visitas: "+data);
	});
}
function updateVisits() {
	for (l = 0; l < media.length; l++) {
		nWidget = media[l][0];
		getVisits(nWidget);
	}
}

/**
 * Views
 */
function makeView(nWidget) {
	id = jQuery('#yp-id-'+nWidget).val();
	jQuery.get(path+"php/yp-views.php", {op: 'mv', id: id}, function(data) {
		updateViews();
	});
}
function getViews(nWidget) {
	id = jQuery('#yp-id-'+nWidget).val();
	jQuery.get(path+"php/yp-views.php", {op: 'cv', id: id}, function(data) {
		jQuery('#yp-views-'+nWidget).html("Reproducciones: "+data);
	});
}
function updateViews() {
	for (l = 0; l < media.length; l++) {
		nWidget = media[l][0];
		getViews(nWidget);
	}
}

/**
 * Voting System
 */
function countVotes(nWidget) {
	media_id = jQuery('#yp-id-'+nWidget).val();
	jQuery.get(path+"php/yp-vote.php", {op: 'vc', id: media_id}, function(data) {
		jQuery('#yp-votes-'+nWidget).html("Votos: "+data);
	});
}
// Update count votes of all widgets
function updateCountVote() {
	for (l = 0; l < media.length; l++) {
		nWidget = media[l][0];
		countVotes(nWidget);
	}
}


function loadWidget(id) {
	showMedia(id,0);
}

function makePlayer(nWidget, coleccion, num) {
	var elemento = coleccion[num];
	var player = new SWFObject(path + 'reproductor/player.swf', 'player-'+nWidget, '190', '180', '9', '#ffffff');
	player.addParam('allowfullscreen','true');
	player.addParam('allowscriptaccess','always');
	player.addParam('menu','false');
	player.addVariable('id', 'player-'+nWidget);
	player.addVariable('name', 'player-'+nWidget);
	if (elemento[0] == 0) {
		player.addVariable('file', datos_path + "media/" + elemento[1] + ".flv");
		player.addVariable('image', datos_path + "capturas/" + elemento[1] + ".png");
		player.addVariable('plugins', "viral-1,yourlytics-1");
	} else if (elemento[0] == 1) {
		player.addVariable('file', datos_path + "media/" + elemento[1] + ".mp3");
		player.addVariable('image', path + "img/mimetypes/mp3.png");
		player.addVariable('plugins', "revolt-1,viral-1,yourlytics-1");
	} else if (elemento[0] == 2) {
		player.addVariable('file', datos_path + "media/" + elemento[1] + ".png");
		player.addVariable('image', datos_path + "capturas/" + elemento[1] + ".png");
		player.addVariable('plugins', "viral-1,yourlytics-1");
		player.addVariable('autostart', true);
	}
  	player.addVariable('yourlytics.callback', path + 'reproductor/yp-stats.php');
	return player;
}

// Obtener Listado de enlaces del widget
function obtenerListado(id) {
	var enlaces = "<ul>";
	for (i=0;i<media[id][1].length;i++) {
		enlaces += "<li><a href='javascript:showMedia("+id+","+i+")'>"+media[id][1][i][2]+"</a></li>";
	}
	enlaces += "</ul>";
	return enlaces;
}

// Cargar medios en los Widgets
function loadMedia() {
	for (k = 0; k < media.length; k++) {
		loadWidget(k);
	}
}

jQuery(document).ready(function(){
	loadMedia();
});

