var pathImgWmv=yHost+"imgs/archivio/";
var pathImgFlv=yHost+"";

function callJson(path){
	var newmills = new Date().getTime();
	request = path+"&t="+newmills;
	aObj = new JSONscriptRequest(request);
	aObj.buildScriptTag();
	aObj.addScriptTag();	
}

function goLogin(){
	location.href="/user/login";
}

function viewUser() {
	callJson("/?type=json&method=user"); 
}

function getUser(response) {

	if (response[0]!= null){
		document.getElementById("userName").innerHTML="Benvenuto <span>"+response[0]+"</span>";
		document.getElementById('modifica_link').href = "/user/"+response[1]+"/edit";
		document.getElementById('modifica_link').setAttribute('uid', response[1]);
		document.getElementById('modifica').style.display = "block";
		document.getElementById('modificaVideo').style.display = "block";
		document.getElementById('logout').style.display = "block";
	} else {
		document.getElementById('login').style.display = "block";
	}
	
}
function viewCount(response){
	//alert(response.items + " " +response.items[0].node_counter_totalcount);
	if(response == 0)
		document.getElementById('numViewVideo').innerHTML = 0
	else
		document.getElementById('numViewVideo').innerHTML = response.items[0].node_counter_totalcount;
}


function getRatingVoto(iVote) {
	var ulbg=document.getElementById("ulvotedv-50142226");
	if (iVote==0) {
		ulbg.style.backgroundPosition="0 0px";
	} 
	if (iVote==20) {
		ulbg.style.backgroundPosition="0 -16px";
	} 
	if (iVote==40) {
		ulbg.style.backgroundPosition="0 -32px";
	}
	if (iVote==60) {
		ulbg.style.backgroundPosition="0 -48px";
	}
	if (iVote==80) {
		ulbg.style.backgroundPosition="0 -64px";
	}
	if (iVote==100) {
		ulbg.style.backgroundPosition="0 -80px";
	}
}

function getRatingVotoInRange(iVote) {
	var ulbg=document.getElementById("ulvotedv-50142226");
	if (iVote==0) {
		ulbg.style.backgroundPosition="0 0px";
	} 
	if (iVote>0 && iVote<=20) {
		ulbg.style.backgroundPosition="0 -16px";
	} 
	if (iVote>20 && iVote<=40) {
		ulbg.style.backgroundPosition="0 -32px";
	}
	if (iVote>40 && iVote<=60) {
		ulbg.style.backgroundPosition="0 -48px";
	}
	if (iVote>60 && iVote<=80) {
		ulbg.style.backgroundPosition="0 -64px";
	}
	if (iVote>80 && iVote<=100) {
		ulbg.style.backgroundPosition="0 -80px";
	}
}
//response, id
function setLinkVoting(vId){
	document.getElementById('linkVoto_1').href = 'javascript:sendVote('+vId+', 20)';
	document.getElementById('linkVoto_2').href = 'javascript:sendVote('+vId+', 40)';
	document.getElementById('linkVoto_3').href = 'javascript:sendVote('+vId+', 60)';
	document.getElementById('linkVoto_4').href = 'javascript:sendVote('+vId+', 80)';
	document.getElementById('linkVoto_5').href = 'javascript:sendVote('+vId+', 100)';
}

function sendVote(vId, voto){
	url = 'http://www.youimpresa.it/rating.php?op=vote&id='+vId+'&vote='+voto;
	image = new Image();
	image.src = url;
	//callJson('http://www.youimpresa.it/rating.php?op=view&id='+vId+'&callBack=elaboraVoto');
	//callJson('http://www.youimpresa.it/rating.php?op=view&id='+vId+'&callBack=messaggioVoto');
}

function elaboraVoto(response){
	iVote = response.voting_total / response.voting_count;	
	getRatingVotoInRange(iVote);	
}

function mostraStatsNodo(response, id){
	document.getElementById(id).innerHTML = response.view;
	elaboraVoto(response);
}

function messaggioVoto(response){
	divMsg = 'msgnearstarsdv-50142226';
	document.getElementById(divMsg).innerHTML = response.stato_votazione;	
}

function getSocialBookUrl(sb, nid){
	var pagina = document.location;//'http://www.youimpresa.it/?q=content/'+nid;
	//var pagina = 'http://www.youimpresa.it/?q=content/'+nid;
	var titolo = document.getElementById('boxVideo_title').innerHTML;
	var url = '';
	switch(sb){
		case 'oknotizie':
			url = 'http://oknotizie.alice.it/login.html.php?uri='+pagina+'&title='+titolo;
			break;
		case 'facebook':
			url = 'http://www.facebook.com/sharer.php?u='+pagina+'&t='+titolo;
			break;
		case 'delicious':
			url = 'http://del.icio.us/post?url='+pagina+'&title='+titolo;
			break;
		case 'myspace':
			url = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+pagina+'&t='+titolo;
			break;
		case 'google':
			url = 'http://www.google.com/bookmarks/mark?op=add&bkmk='+pagina+'&title='+titolo;
			break;
		case 'technorati':
			url = 'http://www.technorati.com/faves?add='+pagina+'&title='+titolo;
			break;
		case 'bebo': //da modificare 
			url = 'http://www.bebo.com/post?u='+pagina+'&t='+titolo;
			break;
	}
	window.location = url;
}

function elaboraMediaVoto(response){
	var mediaVoti = 0;
	var votazioni = 0;
	for(i=0; i<response.items.length; i++){
		mediaVoti += parseInt(response.items[i].votingapi_vote_node_percent_vote);
		votazioni++;
	}
	if(votazioni > 0 && !isNaN(mediaVoti)){
		mediaVoti /= votazioni;
		getRatingVoto(mediaVoti);
	}
	else
		getRatingVoto(0);
}



var pagSucc=2;
var pagPrec=-1;

function showBox(where){
  if (where=='prev'){
  	where=""+pagPrec;
  }
  if (where=='next'){
  	where=""+pagSucc;
  }
  try{
    if (where=="1"){
    	document.getElementById('navTabPV_1').className = 'att';
    	document.getElementById('navTabPV_2').className = '';
    	if (document.getElementById('navTabPV_3')) {
    		document.getElementById('navTabPV_3').className = '';
    	}
    	document.getElementById('box_1').style.display = 'block';
    	document.getElementById('box_2').style.display = 'none';
    	if (document.getElementById('box_3')) {
    		document.getElementById('box_3').style.display = 'none';
    	}
    	document.getElementById('butt_prec').style.display = 'none';
    	document.getElementById('butt_succ').style.display = 'block';
    	pagSucc=2;
    	pagPrec=-1;
    } else if (where=="2"){
    	document.getElementById('navTabPV_1').className = '';
    	document.getElementById('navTabPV_2').className = 'att';
    	if (document.getElementById('navTabPV_3')) {
    		document.getElementById('navTabPV_3').className = '';
    	}
    	document.getElementById('box_1').style.display = 'none';
    	document.getElementById('box_2').style.display = 'block';
    	if (document.getElementById('box_3')) {
    		document.getElementById('box_3').style.display = 'none';
    		document.getElementById('butt_prec').style.display = 'block';
    		document.getElementById('butt_succ').style.display = 'block';
    	} else {
	    	document.getElementById('butt_prec').style.display = 'block';
	    	document.getElementById('butt_succ').style.display = 'none';
    	}
    	pagSucc=3;
    	pagPrec=1;
    } else if (where=="3"){
    	document.getElementById('navTabPV_1').className = '';
    	document.getElementById('navTabPV_2').className = '';
    	document.getElementById('navTabPV_3').className = 'att';
    	document.getElementById('box_1').style.display = 'none';
    	document.getElementById('box_2').style.display = 'none';
    	document.getElementById('box_3').style.display = 'block';
    	document.getElementById('butt_prec').style.display = 'block';
    	document.getElementById('butt_succ').style.display = 'none';
    	pagSucc=-1;
    	pagPrec=2;
    }
  }catch(e){}
  document.getElementById('pag_att').innerHTML = where;
/*
alert(where);
	if(page > 0 && page < 4){
		switch(where){
			case 'next':
				page = (page+1 > 3) ? 3 : page+1;
				if(page>1)
					document.getElementById('butt_prec').style.display = 'block';
				if(page==3)
					document.getElementById('butt_succ').style.display = 'none';
				break;
			case 'prev':
				page = (page-1 < 1) ? 1 : page-1;
				if(page<3)
					document.getElementById('butt_succ').style.display = 'block';
				if(page==1)
					document.getElementById('butt_prec').style.display = 'none';
				break;
			case 1 || 2 || 3:
				page = where;
			case 1:
				document.getElementById('butt_succ').style.display = 'block';
				document.getElementById('butt_prec').style.display = 'none';
				break;
			case "2":
			alert("2");
			document.getElementById('navTabN_1').style.className = '';
			document.getElementById('navTabN_2').style.className = 'att';
			document.getElementById('navTabN_3').style.className = '';
				document.getElementById('butt_succ').style.display = 'block';
				document.getElementById('butt_prec').style.display = 'block';
				break;
			case 3:
				document.getElementById('butt_succ').style.display = 'none';
				document.getElementById('butt_prec').style.display = 'block';
				break;
		}
		for(i=1; i<=range.length; i++){
			if(i!=page){
				document.getElementById('box_'+i).style.display = 'none';
				document.getElementById('navTabN_'+i).className = '';
			}
		}
		document.getElementById('pag_att').innerHTML = page;
		document.getElementById('box_'+page).style.display = 'block';
		document.getElementById('navTabN_'+page).className = 'att';
	}
	*/
}





function getNodeImg(node){
	if (node.files_node_data_field_anteprima_video_filepath==undefined ||node.files_node_data_field_anteprima_video_filepath==""){
		return pathImgWmv+node.node_data_field_path_img_field_path_img;
	} else {
		return pathImgFlv + node.files_node_data_field_anteprima_video_filepath;
	}
}



function  checkSearchText(id){
	if(document.getElementById(id).value=="Inserisci il testo"){
		document.getElementById(id).value="";
	}
}

function cerca(){
	//alert(document.getElementById("search_text").value);
	location.href="/search/node/"+document.getElementById("search_text").value;
}

function openPU(url,w,h){
	popup = window.open(url,'','location=0,status=0,scrollbars=0,width='+w+',height='+h);
}

function viewVoting(nid){
	callJson(yHost+"rating.php?op=view&id="+nid+"&callBack=responseViewVoting");
}

function viewVotingPrefix(nid,prefix){
	callJson(yHost+"rating.php?op=view&id="+nid+"&div="+prefix+"&callBack=responseViewVotingPrefix");
}

function responseViewVotingPrefix(response,div){
	if (response.id!=undefined){
		if (document.getElementById(div+"view_"+response.id)!=null) {
			try {
				document.getElementById(div+"view_"+response.id).innerHTML=response.view;
			} catch(ex){}
			try {
				if (response.voting_total==0 ){
					iVote =0;
				} else {
					iVote = response.voting_total / response.voting_count;	
				}
				viewRatingVotoInRange(iVote,div+"voting_"+response.id);	
			} catch(ex){}
		}
	}
}

function responseViewVoting(response){
	responseViewVotingPrefix(response,"");
}

function viewRatingVotoInRange(iVote,divName) {
	try {
		var ulbg=document.getElementById(divName);
		if (ulbg!=null) {
			if (iVote==0) {
				ulbg.style.backgroundPosition="0 0px";
			} 
			if (iVote>0 && iVote<=20) {
				ulbg.style.backgroundPosition="0 -16px";
			} 
			if (iVote>20 && iVote<=40) {
				ulbg.style.backgroundPosition="0 -32px";
			}
			if (iVote>40 && iVote<=60) {
				ulbg.style.backgroundPosition="0 -48px";
			}
			if (iVote>60 && iVote<=80) {
				ulbg.style.backgroundPosition="0 -64px";
			}
			if (iVote>80 && iVote<=100) {
				ulbg.style.backgroundPosition="0 -80px";
			}
		}
	} catch(ex){}
}

function addNodeView(nid){
  callJson(yHost+"rating.php?op=count&id="+nid);
}
