/**Quepasa Corporation**/
/**VideoView.js**/
/**version 1.0 by valdemar**/
videoCount = 0;
thumbid= 0;
numTruncate = 70;
numTruncateTitle = 17;
videoid=0;
timer=setTimeout('void(0)',10000);
globallimit=3;
globaloffset=0;
videosXML = null;
videosTotal = 0;
function setIdVideo(numvideoid) {
	videoid=numvideoid;
}
function showOwnerAlbums(postData) {
	videosXML = postData;
	videosTotal = $('video',postData).length;
	if ($('video[id='+videoid+']',videosXML).length > 0) {
		$('video',videosXML).each(function(i){
			if(parseInt($(this).attr('id')) == parseInt(videoid)) {
				videoPosition = i;
				return;
			}
		});
	} else {
		videoPosition=-1;
	}
	if (videoPosition!=-1) {
		valPagination=(Math.floor(videoPosition/globallimit)*globallimit);
	} else {
		valPagination=globaloffset;
	}
	videoPagination(globallimit,valPagination);
}
function limitDuration(strDuration) {
	timearray=strDuration.split(':');
	if($(timearray).length>=3)return timearray[1]+':'+timearray[2];
	else return strDuration;
}
function showOwnerAlbumsError(postData) {
	//alert('ocurrio un error');
}
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 isComError(postError) {
}
function limita(area_texto,max)
{
	if(area_texto.value.length>=max){area_texto.value=area_texto.value.substring(0,max);}
}
function showOwnerAlbumsError(postData) {
}

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 addFriendAlt(usernum){
	var sURL='/user/actions/addFriend.php?iduser=';
	if (usernum > 0){
		sURL=sURL+usernum;
		var html = $.ajax({type: "GET",url: sURL,data:'',async: false}).responseText;
		if (html == '0'){ $("#addCommMsg").html( $("#aF0").html() ); }
		if (html == '1'){ $("#addCommMsg").html( $("#aF1").html() ); }
		if (html == '2'){ $("#addCommMsg").html( $("#aF2").html() ); }
		if (html == '-1' || html == '-2'){ $("#addCommMsg").html( $("#aF3").html() ); }
		if (html == 'no'){ $("#addCommMsg").html( $("#aF4").html() ); }
	}
}

function isComError(postError) {
}
function limita(area_texto,max)
{
	if(area_texto.value.length>=max){area_texto.value=area_texto.value.substring(0,max);}
}
function videoPagination(limit,offset) {
	if(videosTotal > 0) {
		var videoContainer = $('#albumscontent-ul');
		videoContainer.html('');
		$('video',videosXML).each(function(i){
			if(i>=offset && i<offset+limit) {
				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'));
				$('.title_video_front > a',featuredVideo).html($('title',this).text());
				$('.video-link',featuredVideo).attr('href',$('url',this).text());
				$('.video-desc',featuredVideo).html($('description',this).text());
				$('.video-time',featuredVideo).html(limitDuration($('time',this).text()));
				featuredVideo.attr('id','');
				videoContainer.append(featuredVideo);
			}
		});
		var selectedVideo = $("video[id='"+videoid+"']",videosXML);
		$("#video-title").html($("title",selectedVideo).text()).truncate(65,{chars:'', leave:false, trail:[false, "..", ""]});
		$("#video-description").html($("description",selectedVideo).text());
		$("#video-duration").html(limitDuration($("time",selectedVideo).text()));
		$("#albumscontent-ul > li:odd").addClass('video-featured-odd');
		$('.video-desc').each(function(){
			if($(this).html().length>numTruncate) {
				$(this).truncate(numTruncate,{chars:/\s/, leave:true, trail: [true,"...[<a href='javascript:void(0)'>+</a>]","&nbsp;[<a href='javascript:void(0)'>-</a>]"]});
			}
		});
		$('.video-link').each(function(){
			if($(this).html().length>numTruncateTitle) {
				$(this).truncate(numTruncateTitle,{chars:'', leave:false, trail:[false, "..", ""]});
			}
		});
		$('.video-thumb-anim',videoContainer).mouseover(startAnimateImage);
		$('.video-thumb-anim',videoContainer).mouseout(stopAnimateImage);
		var videoPag = $('#video-pag');
		$('#video-pag-info-total',videoPag).html(videosTotal);
		$('#video-pag-info-curr',videoPag).html((offset+limit>=videosTotal)?videosTotal:limit+offset);
		offset==0?$('#video-pag-prev').hide():$('#video-pag-prev').unbind('click').bind('click',function(){videoPagination(limit,offset-limit);}).show();
		(offset+limit>=videosTotal)?$('#video-pag-next').hide():$('#video-pag-next').unbind('click').bind('click',function(){videoPagination(limit,offset+limit);}).show();
		videoPag.show();
	} else {
		$('#video-pag').hide();
	}
}
function sendvideo(msj1,msj2,msj3,msj4){
	var mensaje = document.getElementById('mensaje');
    var mail = document.getElementById('mail');
    var mensavalue = mensaje.value;
    $("#sendimage").hide();
    $("#errorsendimage").hide();
    //Mensaje
   	if (mensaje.value==''){
	    alert(msj1);
		mensaje.focus();
		return false;
   	}
	if (mensavalue.length > 200){
		alert(msj2);
		mensaje.focus();
		return false;
   	}
	//Mail
    if (mail.value==''){
        alert(msj3);
        mail.focus();
        return false;
    }
    if ((mail.value.indexOf ('@',0)==-1) || (mail.value.indexOf ('.',0)==-1) || (mail.value.length <5)) {
		alert(msj4);;
		mail.focus();
		return false;
	}
	if (mail.value.indexOf ('@',0)==(mail.value.indexOf ('.',0)-1)){
		alert(msj4);;
		mail.focus();
		return false;
	}
	var mailsend= $('#mail').val();
	var mensaje = $('#mensaje').val();
	var Videoid = $('#videoid').val();
	var sURL='/multimedia/actions/sendvideo.php';
	var html = $.ajax({type: "POST",url: sURL,async: false,
	data:	'mail='+mailsend+'&mensaje='+mensaje+'&Videoid='+Videoid}).responseText;
	if(html!=0){
		$('#videobk').hide();
		$("#sendvideo").show();
        document.getElementById('mail').value="";
        document.getElementById('mensaje').value="";
	}else {
		$('#videobk').hide();
	    $("#errorsendvideo").show();
	}
}

function setFlagVideo(idVideo){
	$.ajax({type: "POST", url: "/multimedia/actions/editflagvideoItem.php", data: "idvideo="+idVideo, success: successSetFlagPhoto, timeout:500});
}
function successSetFlagPhoto(postData){
	var ok = $('result',postData).eq(0).attr('ok');
	if(ok == "true") {
		$('#txtSetFlagOk').show();
	}
}

var userId =0;
var numoffset =0;
var numowner =0;
function successDeleteComments(postData){
	//showComments(5,numoffset,videoid,numowner);
}
