videoCount = 0;
thumbid= 0;
timer=setTimeout('void(0)',10000);
videolimit = 10;    

function showFeaturedVideos(postData) {
	var videoContainer = $('#albumscontentr');
	$('video',postData).each(function(i){
		if(i<videolimit) {
			var featuredVideo = $('#featured-video-tpl').clone();
			$('img',featuredVideo).attr('src',$('thumb',this).text());
			$('img',featuredVideo).attr('rsrc',$('thumb',this).text());
			$('img',featuredVideo).attr('id',$(this).attr('id'));
			$('.thumb-container > a',featuredVideo).attr('href',$('viewurl',this).text());
			$('.fvtitle > a',featuredVideo).attr('href',$('viewurl',this).text()).html($('title',this).text()).truncate(10,{chars:'', leave:false, trail:[false, "..", ""]});
			$('.fvdescription > span',featuredVideo).html($('description',this).text()).truncate(32,{chars:'', leave:false, trail:[false, "..", ""]});
			$('.fvauthor > a',featuredVideo).attr('href',$('authorurl',this).text()).html($('author',this).text()).truncate(17,{chars:'', leave:false, trail:[false, "..", ""]});
			$('.fvduration > span',featuredVideo).html($('duration',this).text());
			$('.fvviews > span',featuredVideo).html($('views',this).text());
			videoContainer.append(featuredVideo);
		}
	});
	$('.video-thumb-anim',videoContainer).mouseover(startAnimateImage);
	$('.video-thumb-anim',videoContainer).mouseout(stopAnimateImage);
}
function startAnimateImage(eventObject) {
	clearAnimation();
	thumbid=eventObject.target.id;
    animateImage(eventObject.target);
}
function animateImage(image) {
   img = document.createElement('img');
   img.id=thumbid;
   $(img).load(function() {
        timer = setTimeout(function(){
        	if(thumbid==image.id)$(image).attr('src',$(img).attr('src'));
        	else $(image).attr('src',$(img).attr('rsrc'));
            videoCount>=9?videoCount=0:videoCount++;
        	animateImage(image);},800);
    });
    if(thumbid==image.id)$(img).attr('src',$(image).attr('rsrc')+'-'+videoCount);
}
function stopAnimateImage(eventObject) {
    clearAnimation();
    $(eventObject.target).attr('src',$(eventObject.target).attr('rsrc'));
}
function clearAnimation() {
	clearTimeout(timer);
    videoCount=0;
    thumbid=0;
    timer=setTimeout('void(0)',10000);
}



function getCountries(){
	$.ajax({type: "GET", url: "/locations/country/countries00.xml",data:'',dataType: 'xml', success: DisplayCountries, timeout:3000});
}


function DisplayCountries(postData){
	var sSelectedContry=$('#CountrySelected').attr('value');
	var root = postData.getElementsByTagName("countries")[0];
    var countries = root.getElementsByTagName("country");
    var ctySelect = document.getElementById("countries");
    for(var i = 0; i < countries.length; i++) {
    	if (sSelectedContry == countries[i].getAttribute("id")){
    		ctySelect.options[i+1] = new Option(countries[i].getAttribute("name"), countries[i].getAttribute("id"),true,true);
    	}else{
    		ctySelect.options[i+1] = new Option(countries[i].getAttribute("name"), countries[i].getAttribute("id"),false,false);
    	}
    }
	if ( typeof(sSelectedContry) == 'undefined'){
		var elSel = document.getElementById("countries");
		sSelectedContry = elSel.options[ elSel.selectedIndex ].value
	}
}

function ShowShareTab(tab){
	if(tab != 'sendMail')
	$('#sendMail').hide();
	if(tab != 'insertVideo')
	$('#insertVideo').hide();
	if(tab != 'getLink')
	$('#getLink').hide();
	$('#' + tab).toggle();
}


function showRateIt() {
	$('#rating').hide();
	$('#rate').show();
}

function hideRateIt() {
		restoreRateMsg();
		$('#rate').hide(); 
		$('#rating').show();
}

function rateIt(rate) {
	var sURL = prefixURL + rate;
	var sHTML = $.ajax({type: "GET", url: sURL, data:'', async: false}).responseText; 
	var result = eval('(' + sHTML + ')');
	if(result.status == "succesful") {
		//$('#rateMsg').html(result.status);
		$('#starText').hide();
		$('#successMsg').show();
		document.getElementById('rate').onmouseout=null;
		document.getElementById('rating').onmouseover=null;
		$('#rate').css('cursor','default');
		disableRating();
	} 
}

function showStars(numStars) {
	setTimeout('clearStars()','100');
	setTimeout('fillStars('+numStars+')', '100');

}
function fillStars(numStars) {
	for(var i=1; i <=numStars; i++) {
		$('#star-'+i).attr('src', imgSrcFull);
	}
	changeText(numStars-1);	
}

function restoreRateMsg() {
	$('#starText').hide();
}

function changeText(index) {
	$('#starText').text(messages[index]);
	$('#starText').show();
}

function clearStars() {
	for(var i=1; i <= 5; i++) {
		$('#star-'+i).attr('src', imgSrcEmpty);
	}
}

function disableRating() {
	for(var i=1; i <= 5; i++) {
		document.getElementById('star-'+i).onmouseover=null;
		document.getElementById('star-'+i).onclick=null;
	}
}


function sendVideo() {
	$('#sendvideo').hide();
	$('#invalidemail').hide();
	$('#errorsendvideo').hide();
	
	var strMails = $("#mail").val();

	var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))(,(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))*$/
	
	var strReceivers = strMails.split(','); 

	for(var i=0; i < strReceivers.length; i++) {
		var strReceiver = "";
		strReceiver = trim(strReceivers[i]);
		if(regex.test(strReceiver)) {
			var videoId = $('#videoid').val();
			var sURL = '/multimedia/actions/sendvideo.php';
			var html = $.ajax({
				type: "POST", url: sURL, async: false,
					data: 'mail=' + strReceiver + '&mensaje=&Videoid=' + videoId
			}).responseText;
			if(html != 0) {
                var oElement = document.getElementById('emailsent');
                oElement.innerHTML = strReceiver;
                document.getElementById('mail').value="";
				$('#sendvideo').show();
				setTimeout("$('#sendvideo').hide()", "3000");
			} else {
				$('#errorsendvideo').show();
				setTimeout("$('#errorsendvideo').hide()", "3000");
			}
		} else {
			$('#invalidemail').append("&nbsp;" + strReceiver);
			$('#invalidemail').show();
			setTimeout("$('#invalidemail').hide()", "3000");
		}
	}
}


function sendToLogin(urlLogin) {
	window.location.href = urlLogin;
}

function loginToRate() {
	$('#nologed').show();
}
