//script x prob ie con fade
jQuery.fn.fadeIn = function(speed, callback) {
    return this.animate({opacity: 'show'}, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

jQuery.fn.fadeOut = function(speed, callback) {
    return this.animate({opacity: 'hide'}, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

jQuery.fn.fadeTo = function(speed,to,callback) {
    return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};


//funzioni caricate al DOM Ready
$(document).ready(function() {

	//ticker news
	 $("ul#newsTicker").liScroll({travelocity: 0.02});
	
	//lunghezza titolo video +visti e +votati
	if ($('DIV').is('#boxVidPiu')) {
		//inserisce il testo presente nel tag A nell'attributo TITLE
		var tit = $('#boxVidPiu H4 A');
		for(var i = 0; i < tit.length; i++) {
			var txt = $('#boxVidPiu H4 A SPAN').eq(i).text();
			$('#boxVidPiu H4 A').eq(i).attr({ title: '' +txt+ ''});
		}
		//troncamento TXT titolo nel tag SPAN (titolo Video)
		$('.colSx #boxVidPiu H4 SPAN').truncatitle({limit:45}); //HP
		$('.colDx #boxVidPiu H4 SPAN').truncatitle({limit:100}); //Community
	}

	//gestione grafica voto attivo
	if ($('DIV').is('.boxVoto')) {
		var pos = $('.boxVoto .voto').css('backgroundPosition'); //posizione in cui e' il bkg
		var votato = false;
		$('.voto LI A').mouseover(function(){
			var ulbg = $(this).parent().parent(); //UL in cui cambiare il background
			var lk = $(this); //link
			grafVoto(lk,ulbg);
		}).mouseout(function(){
			var ulbg = $(this).parent().parent(); //UL in cui cambiare il background
			ulbg.attr('style','background-position:'+pos+';');
		}).click(function(){
			var ulbg = $(this).parent().parent(); //UL in cui cambiare il background
			var lk = $(this); //link
			grafVoto(lk,ulbg);
			if (votato==false) {
				$('.boxVoto .info01').html('Grazie per aver votato');
				votato = true;
			} else {
				$('.boxVoto .info01').html('<b>Hai gi&agrave; votato</b>');
			}
		});
	}

	//gestione condividi
	if ($('DIV').is('.boxCondividi')) {
		$('.boxCondividi LI A').mouseover(function(){
			var sp = $(this).next(); //span da riempire
			var el = $(this).text(); //testo presente nel link
			$(sp).html(el);
		}).mouseout(function(){
			var sp = $(this).next();
			$(sp).html('');
		});
	}

	//navigazione video #boxNavVid presente in HP/Scheda Video e Scheda Video Utente
	if ($('DIV').is('#boxNavVid')) {
		var box = $('#boxNavVid .b01'); //tutti i box
		//rollover
		box.mouseover(function(){
			if ($(this).is('.sel.b01')) {
				$(this).addClass('sel');
			} else {
				$(this).addClass('sel');
				$(this).mouseout(function(){
					$(this).removeClass('sel');
				});
			}
		});
		//accende il box su cui si e' cliccato (assegna classe att)
		box.click(function(){
			box.removeClass('att');
			$(this).addClass('att');
		});
		//troncamento TXT nel tag H4 (titolo Video) e Assegnazione link al box .b01
		if($('#boxNavVid H4 A').length == 0){
			$('#boxNavVid H4').truncatitle({limit:50}); //troncamento TXT
		} else {
			$('#boxNavVid H4 A').truncatitle({limit:50}); //troncamento TXT
			$('#boxNavVid .b01').click(function(){ //Assegnazione link al box .b01
				var boxlk = $(this);
				var alk = $(this).find('H4 A').attr('href');
				window.location.href = alk;
			});
		}
		//troncamento titolo categoria nel tag H5
		$('#boxNavVid H5').truncatitle({limit:20});

		//gestione navi video
		$('#boxNavVid .navTabPS A').click(function(){
			var tabBoom = $(this).parent().parent().parent(); //il tab da cui e' partita l'azione
			var navNums1 = '#boxNavVid .navTabN LI'; // tutti i LI
			
			if ($(this).is('.tab01')) {
				$('#boxNavVid #tab01').show();
				$(navNums1).removeClass('att');
				$(navNums1+'.tab01').addClass('att');
				tabBoom.hide();
				return false;
			}
			if ($(this).is('.tab02')) {
				$('#boxNavVid #tab02').show();
				$(navNums1).removeClass('att');
				$(navNums1+'.tab02').addClass('att');
				tabBoom.hide();
				return false;
			}
			if ($(this).is('.tab03')) {
				$('#boxNavVid #tab03').show();
				$(navNums1).removeClass('att');
				$(navNums1+'.tab03').addClass('att');
				tabBoom.hide();
				return false;
			}
		});
		$('#boxNavVid .navTabN A').click(function(){
			var navNum = $(this).parent(); //LI da cui e' partita l'azione
			var navNums2 = $('#boxNavVid .navTabN LI'); //tutti i LI
			var tabs = $('#boxNavVid .tabBox'); //tutti i Tabs
			if (navNum.is('.tab01')) {
				tabs.hide();
				$('#boxNavVid #tab01').show();
				$(navNums2).removeClass('att');
				$(navNum).addClass('att');
				return false;
			}
			if (navNum.is('.tab02')) {
				tabs.hide()
				$('#boxNavVid #tab02').show();
				$(navNums2).removeClass('att');
				$(navNum).addClass('att');
				return false;
			}
			if (navNum.is('.tab03')) {
				tabs.hide()
				$('#boxNavVid #tab03').show();
				$(navNums2).removeClass('att');
				$(navNum).addClass('att');
				return false;
			}
		});
	}

	//gestione Menu Archivio / Archivio Mese BLOG
	if ($('DIV').is('#menuArch')) {
		var list = $('#menuArch .tendina'); //Listing
		var chCla = $('#menuArch'); //box in cui cambiare classe
		$('#menuArch .bt01 A').toggle( //
			function(){
			chCla.toggleClass('open');
			list.slideToggle();
			$(this).attr({ title: 'Chiudi'}).html('Chiudi');
		},
		function(){
			chCla.toggleClass('open');
			list.slideToggle();
			$(this).attr({ title: 'Apri'}).html('Apri');
		});
		$('#menuArch .bt02 A').click(function(){
			var chTxt = $('#menuArch .bt01 A'); //bt apri/chiudi
			chCla.removeClass('open');
			list.slideUp();
			chTxt.attr({ title: 'Apri'}).html('Apri');
			return false;
		});
		//inserisce il testo presente nel tag H4 nell'attributo TITLE 
		var tit = $('#menuArch LI A');
		for(var i = 0; i < tit.length; i++) {
			var txt = $('#menuArch LI A').eq(i).text();
			$('#menuArch LI A').eq(i).attr({ title: '' +txt+ ''});
		}
		//troncamento TXT nel tag A (titolo)
		$('#menuArch LI A').truncatitle({limit:45});
	}

	//lanci tematici
	if ($('DIV').is('#boxLancTema')) {		
		//inserisce il testo presente nel tag B nell'attributo TITLE 
		var tit = $('#boxLancTema .box01 H3 B');
		for(var i = 0; i < tit.length; i++) {
			var txt = $('#boxLancTema .box01 H3 B').eq(i).text();
			$('#boxLancTema .box01 H3 A').eq(i).attr({ title: '' +txt+ ''});
		}
		//troncamento TXT nel tag A (titolo lancio)
		$('#boxLancTema .box01 H3 B').truncatitle({limit:50});
		//lunghezze info/intro
		$('#boxLancTema .box01 .info').truncatitle({limit:115});
		//$('#boxLancTema .box01 .intro').truncatitle({limit:125});
	}

	//sondaggio
	if ($('DIV').is('.boxSondaggio')) {
		$('.boxSondaggio .bt119x52 A').click(function(){			
			var boxSond = $(this).parent().parent().parent().parent(); //box contenente sondaggio
			var nid = this.getAttribute('nid');
			votaSondaggio(nid, boxSond);
			//boxSond.addClass('votato');
		});
		$('.boxSondaggio .btRisultato A').click(function(){
			var risPerc = $('.boxSondaggio OL SPAN'); //percentuali
			if($('.boxSondaggio').length == 1) { //pagine con un solo sondaggio
				risPerc.show();
			} else { //pagine con + sondaggi
				$(this).parent().parent().prev().find('SPAN').show();
			}
		});
		$('.boxSondaggio').eq(0).addClass("first");
		$('.boxSondaggio').eq(2).addClass("third");
	}

	//navi2L/navi3L
	if ($('DIV').is('.naviBox')) {
		$('#navi2L LI:last').css({borderRight:'0'});
		$('#navi3L LI:last').css({borderRight:'0'});
	}
	
	//troncamento TXT nel tag P (intro Blog)
	if ($('DIV').is('.bInt623 .bLiPost')) {
		$('.bInt623 .bLiPost .intro').truncatitle({limit:255});
	}

	//lunghezza titolo lista video
	if ($('UL').is('.liVid623')) {
		$('.liVid623 LI H3 SPAN').truncatitle({limit:40});
		$('.liVid623 LI .intro').truncatitle({limit:115});
	}

	//lunghezza titolo lista video community (utente)
	if ($('UL').is('.liVid623c')) {
		$('.liVid623c LI H4 SPAN').truncatitle({limit:24});
		$('.liVid623c LI .intro').truncatitle({limit:55});
	}

	//lunghezza titolo lista risultato ricerca
	if ($('UL').is('.boxLVT')) {
		$('.boxLVT LI H4 SPAN').truncatitle({limit:56});
	}

	//lista video utente
	if ($('UL').is('.boxLVU')) {
		//lunghezza titolo
		$('.boxLVU LI H4 SPAN').truncatitle({limit:56});
		
		/*alert di conferma Elimina Video
		$('.boxLVU LI .lkGV > a:first-child').click(function() {
			jConfirm('Conferma elimina video?', 'ConfirmEV', function(r) {
				if(r){
				jAlert('success', 'Video eliminato');
				}
			});
		});
		*/
	}

	//lista domande
	if ($('DIV').is('.boxDomande')) {
		$('.boxDomande LI:last').css({borderBottom:'0'});
	}

	//lunghezza titolo lanci blog hp community
	if ($('DIV').is('.boxLBlog')) {
		var tit = $('.boxLBlog .bLiPost H4 A');
		for(var i = 0; i < tit.length; i++) {
			var txt = $('.boxLBlog .bLiPost H4 A').eq(i).text();
			$('.boxLBlog .bLiPost H4 A').eq(i).attr({ title: '' +txt+ ''});
		}
		$('.boxLBlog .bLiPost H4 A').truncatitle({limit:65});
	}

	//colonne pagine statiche
	if ($('BODY').is('#pStatic')) {
		hColonne('.colSx3','.colDx3');
	}

	//colonne pagine blog
	if ($('BODY').is('#pBlog')) {
		hColonne('.colSx1','.colDx');
	}

});

//f x grafica voto
function grafVoto(lk,ulbg) {
	if (lk.is('.lk01')) {
		ulbg.attr('style','background-position:0 -16px;');
	} 
	if (lk.is('.lk02')) {
		ulbg.attr('style','background-position:0 -32px;');
	}
	if (lk.is('.lk03')) {
		ulbg.attr('style','background-position:0 -48px;');
	}
	if (lk.is('.lk04')) {
		ulbg.attr('style','background-position:0 -64px;');
	}
	if (lk.is('.lk05')) {
		ulbg.attr('style','background-position:0 -80px;');
	}
}

//f video +visti e +votati
function shVVV(s,h){
	$(s).show();
	$(h).hide();
	$('#boxVidVist').toggleClass('vist');
}

//f per aprire e chiudere
function appare(s){
	$(s).slideToggle(500);
}

//f altezza due colonne
function hColonne(colA,colB){
	$(colB).css({'height' : 'auto'});
	var hA = $(colA).height();
	var hB = $(colB).height();
	hMax = Math.max(hA,hB);
	if(hA>hB){
		$(colB).css({'height' : +hMax+ 'px'});
		//alert('piu alto colSx'+hA+'|'+hB);
	} else {
		$(colA).css({'height' : +hMax+ 'px'});
		//alert('piu alto colDx'+hB+'|'+hA);
	};
}

//f open popup
function openPU(url,w,h){
	popup = window.open('' +url+ '','','location=0,status=0,scrollbars=0,width=' +w+ ',height=' +h+ '');
}

