// Tell Vimeo what function to call
var videosCallback = 'showThumbs';
// All videos from channel
var allVideos = new Array();
		
				
// This function loads the data from Vimeo
function loadVimeoVideos() {
	var head = document.getElementsByTagName('head').item(0);
	
	var videosJs = document.createElement('script');
	videosJs.setAttribute('type', 'text/javascript');
	videosJs.setAttribute('src', videosUrl);
	head.appendChild(videosJs);
}

// This function goes through the clips and puts them on the page
function showThumbs(videos) {
	allVideos = videos;
	$("#thumbs").html('');

	for (var i = 0; i < videos.length; i++) {
		var thumb = document.createElement('img');
		thumb.setAttribute('src', videos[i].thumbnail_small);
		thumb.setAttribute('alt', videos[i].title);
		thumb.setAttribute('title', videos[i].title);
		thumb.onclick = new Function("showMainVideo('" + i + "')");
		
		var a = document.createElement('a');
		a.appendChild(document.createTextNode(videos[i].title));
		
		var br = document.createElement('br');
		
		var p = document.createElement('li');
		p.appendChild(thumb);
		p.appendChild(a);
		p.appendChild(br);
		p.appendChild(document.createTextNode(videos[i].description));
		
		$("#thumbs").append(p);
	}
	//Select main video to show
	if(videos.length > 0 )
	{
		showMainVideo(0);
	}
}

function showMainVideo(i){			

	var videoId = "<div id='videoID' style='display:none'>" + allVideos[i].id + "</div>";
	var videoInfo = '<p><a>'+ allVideos[i].title +'</a>.</p>';
	var videoPlayer = '<embed src="http://vimeo.com/moogaloop.swf?clip_id='+ allVideos[i].id +'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="590" height="340"></embed>';
	$("#mainVideo").html(videoId + videoInfo + videoPlayer);
	var videoViews = ' Views: (' + allVideos[i].stats_number_of_plays +')</p>';
	$("#videoViews").html(videoViews);
	getRates(allVideos[i].id);
	getVideoComments(allVideos[i].id);	
	return false;
}			

$(function() {
	loadVimeoVideos();
	
});			

function newAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function addCommentToDB(){
	var commentToPost = $("#commentToPost").val();
	$("#commentToPost").val("");
	if(commentToPost.length > 1)
	{
		var contenedor;
		var fanID  = userId;
		var vimeoVideoID = $("#videoID").html();
		var params = "fanID="+fanID+"&vimeoVideoID="+vimeoVideoID+"&comment="+commentToPost+"&channelName="+channelName;
		ajax=newAjax();
		ajax.open("GET", "../../AJAX/AJAXaddVideoNetworkComment.php"+"?"+params,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				$("#comments").html("");
				$("#comments").html(ajax.responseText);
			}
		};
		ajax.send(null);
	}
	else
	{
		alert('Please type down a comment!');
	}	
}
//Retorna los comentarios del video dado
function getVideoComments(vimeoVideoID){
	var params = "vimeoVideoID="+vimeoVideoID;
	ajax=newAjax();
	ajax.open("GET", "../../AJAX/AJAXgetVideoNetworkComments.php"+"?"+params,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			$("#comments").html("");
			$("#comments").html(ajax.responseText);
		}
	};
	ajax.send(null);
}

//Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
	var rating = sel.id.substring(1);
	var fanID  = userId;
	var vimeoVideoID = $("#videoID").html();
	var params = "fanID="+fanID+"&vimeoVideoID="+vimeoVideoID+"&stars="+rating+"&channelName="+channelName;
	ajax=newAjax();
	ajax.open("GET", "../../AJAX/AJAXaddVideoNetworkRating.php"+"?"+params,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {

		}
	};
	ajax.send(null);
}

//Obtiene el promedio de estrellas del video 
function getRates(vimeoVideoID){
	var params = "vimeoVideoID="+vimeoVideoID;
	ajaxRates=newAjax();
	ajaxRates.open("GET", "../../AJAX/AJAXgetVideoNetworkRatings.php"+"?"+params,true);
	ajaxRates.onreadystatechange=function() {
		if (ajaxRates.readyState==4) {
			resetRating();
			$("#avgRating").html("General Fan Rating: " + ajaxRates.responseText);
		}
	};
	ajaxRates.send(null);
}
