var timeout = 30000;
$(document).ready(function() {
	
    //setTimeout("autoBanner()",timeout);
    		
	$(function() {
		$("a[rel='_blank']").attr("target","_blank");				
	});
	
  	//$('a[rel*=facebox]').facebox();
	//$('a.modal').facebox();
	
	$.urlParam = function(name){
		var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
		if(results) {
			return results[1] || 0;
		}
	};
	
	jQuery.fn.delay = function(time,func){
		this.each(function(){
			setTimeout(func,time);
		});	
	return this;
	}; 
	
	// INIZIO FUNZIONE MENU BOXES
	$(function() {
		function addBox(){
  			$(this).find('.menu-box').fadeIn();
			$(this).find('a:first').addClass('_hover');
		}
		function removeBox(){
  			$(this).find('.menu-box').fadeOut();
			$(this).find('a:first').removeClass('_hover');
		}
		var menuBox = {    
     		interval: 100,
     		sensitivity: 2,
			over: addBox,
			timeout: 100,
     		out: removeBox
     	};
		$('li.toggler').hoverIntent(menuBox);
		$('a#get-radio, a#get-musica, a#get-notizie, a#get-radio, a#get-eventi, a#get-programmi, a#get-archivio, a#get-extra').click(function(){
			return false
		});
	});
	// FINE FUNZIONE FUNZIONE MENU BOXES
	
	// INIZIO FUNZIONE ROUNDED LINK BOXES
	$(function() {
		$('.rounded-text-link').each(function(){
			var _htmlStr = $(this).html();
			$(this).html('<span class="left-corners"></span><span class="text-link">'+_htmlStr+'</span><span class="right-corners"></span>');
		});
	});
	// FINE FUNZIONE ROUNDED LINK BOXES
	
	// INIZIO FUNZIONE POPUP
	$(function() {
		var popupRE = /external\[(\d+),(\d+)\]/gi;
		var links = $('a[rel^="external"]');
		var rel, width, height;
 
		links.click(function() {
  			rel = $(this).attr('rel');
  			if(rel == "external") {
    			window.open($(this).attr('href'));
    			return false;
  			} else if(rel.match(popupRE)) {
    			width = RegExp.$1;
    			height = RegExp.$2;
    			window.open($(this).attr('href'), '', 'width=' + width + ',height=' + height + ', scrollbars=no', 'resizable=no', 'status=no', 'menubar=no', 'toolbar=no');
    			return false;
  			}
		});		
	});
	// FINE FUNZIONE POPUP
		
	// INIZIO FUNZIONE SEARCH BOX
	$(function() {
		$('#search-site').bind("click", function(){
			$('.search-site-options').fadeIn();
		});
		$('#search-site').bind("dblclick", function(){
      		$('.search-site-options').fadeOut();
    	});
		/*$('html').bind("click", function(){
      		$('.search-site-options').fadeOut();
			return false
    	});*/
		$('.option-radiogold').bind("click", function(){
      		$('.search-site-options').fadeOut();
			return false
    	});
		$('.option-google').bind("click", function(){
      		$('.search-site-options').fadeOut();
			return false
    	});
	});
	// FINE FUNZIONE SEARCH BOX
	
	// INIZIO FUNZIONE FOOTER CONTENTS
	$(function() {
		$('#footer-toggler').show();
		$('#footer-contents').hide();
		
		$('#footer-toggler').find('a').click(function(){
			$(this).parent().parent().toggleClass('show-contents');
			$('#footer-contents').slideToggle();
			var highestCol = Math.max($('#footer-summary-1 ul').height(),$('#footer-summary-2 ul').height(),$('#footer-summary-3 ul').height(),$('#footer-summary-4 ul').height());
			$('.summary ul').height(highestCol);
		return false
		});
	});
	// FINE FUNZIONE FOOTER CONTENTS
	
	// INIZIO FUNZIONE FOOTER COLUMNS EQUAL HEIGHT
	/*$(function() {
		var highestCol = Math.max($('#footer-summary-1 ul').height(),$('#footer-summary-2 ul').height(),$('#footer-summary-3 ul').height(),$('#footer-summary-4 ul').height());
		$('.summary ul').height(highestCol);
	});*/
	// FINE FUNZIONE FOOTER COLUMNS EQUAL HEIGHT
	
	// INIZIO FUNZIONE DL PALINSESTO
	$(function() {
		if($('dl.palinsesto-list').length) {
			$(this).find('span.description').hide();
			$('dl.palinsesto-list dd').find('a').click(function(){
				$(this).parent().find('span.description').toggle();
				var ddHeight = $(this).parent().height();
				$(this).parent().prev().css('height',ddHeight);
			return false
			});
			$('.palinsesto-nav').find('a').click(function(){
				$('.palinsesto-nav li a').removeClass('current');
				$(this).addClass('current');
			});
		}
	});
	// FINE FUNZIONE DL PALINSESTO
	
	// INIZIO FUNZIONE FONT SCALER
	$(function() {
		if(!$('.news-related-toggler').length) {
			$('#news').css('position','relative');
			$('#news').append('<div id="font-scaler"> <a href="#" class="small" title="dimensione font standard">small</a> <a href="#" class="medium" title="dimensione font media">medium</a> <a href="#" class="large" title="dimensione font massima">big</a> </div>');
			$(function() {
				cookieResize('#font-scaler a', 'small');
			});
		}
	});
	// FINE FUNZIONE FONT SCALER
	
	// INIZIO FUNZIONE HIDE/SHOW COMMENTI
	/*$(function() {
		if($('.news-comments').length) {
			$('.news-comments').hide();
			$('#comments-list-toggler').click(function(){
				$('.news-comments').show();
			});
			$('#comment-post-toggler').click(function(){
				$('.news-comments').show();
			});
		}
	});*/
	// FINE FUNZIONE HIDE/SHOW COMMENTI
	
	// INIZIO FUNZIONE APPEND ICONA LINK MULTIMEDIA
	/*$(function() {
		if($('.get-more').length) {
			$('.get-more').each(function(){
				var _getimgWidth = $(this).find('img').width();
				var _getimgHeight = $(this).find('img').height();
				var _imgHeight = _getimgHeight+"px";
				var _imgWidth = _getimgWidth+"px";
				$(this).css({'width':_imgWidth,'height':_imgHeight});
				$(this).append('<span></span>');
			});
		}
	});*/
	$('.get-more').getMore();
	// FINE FUNZIONE APPEND ICONA LINK MULTIMEDIA
	
	// INIZIO FUNZIONE HIDE/SHOW FORMS NEWSLETTER
	$(function() {
		if($('#subscribe').length) {
			$('#unsubscribe').hide();
			$('#intro-unsubscribe').hide();
			$('#unsuscribe-toggler').click(function(){
				if(!$('#unsubscribe').is(':visible')) {
					$('#intro-subscribe').fadeOut();
					$('#subscribe').fadeOut();
					$('#unsubscribe').fadeIn();
					$('#intro-unsubscribe').fadeIn();
				}
				return false
			});
		}
	});
	// FINE FUNZIONE HIDE/SHOW FORMS NEWSLETTER
    
    $('.get-prev-item,.get-next-item').click(function(){
        //$('#pic-holder-box-content').fadeOut();
		$('#pic-holder-box-content').slideUp();
		$('.pic-holder').addClass('loading');
        var start = $(this).attr('rel');
        $(this).delay(1000,function(){
            nextPhoto(start);
        });
        //$('#pic-holder-box').fadeIn(1000);
    });
	
    
    $.get(
    	"include/ticker_musica.php", {}, function(data)
    	{
    		$("#sezione-musica").append(data).find("ul").newsTicker();
    	}
    )
});


// INIZIO FUNZIONE INIZIALIZZO MODALE
$(function(){
		$.superbox.settings = {
			overlayOpacity: .8, // Background opaqueness
			loadTxt: "Loading...", // Loading text
			closeTxt: "Chiudi",
			nextTxt: "Successiva",
			prevTxt: "Precedente"
		};
		$.superbox();
});
// FINE FUNZIONE INIZIALIZZO MODALE


// INIZIO FUNZIONE AGGIUNGI COMMENTO POST
function addComment() 
{
    $('#form_commenti .textinput, #form_commenti .textarea').each(function(){
       if(!$(this).val())
       {
            $(this).addClass('missing-field');
       }
       else
       {
            $(this).removeClass('missing-field');
       }
    });
    if ($('.missing-field').length == 0)
    {
        //var postStr = $('#form_commenti').formSerialize();
        //postStr = $.URLEncode(postStr);
        var postStr = "";
        postStr += "&nome="+$.URLEncode($("#form_commenti input:text[name='nome']").val());
        postStr += "&titolo="+$.URLEncode($("#form_commenti input:text[name='titolo']").val());
        postStr += "&messaggio="+$.URLEncode($("textarea#messaggio").val());
        postStr += "&securcode="+$.URLEncode($("#form_commenti input:text[name='securcode']").val());
        postStr += "&id_articolo="+$.URLEncode($("#form_commenti input:hidden[name='id_articolo']").val());
        
        
        $.post('include/add_comment.php',postStr,function(data){
            $('#comment-post').slideUp(1000);
            var target = $('#start2watch'); 
            $(document).stop().scrollTo( target , 800 );
            $('#comments-list').html(data);
            $('#comments-list-toggler').html($('#new-comments-list-toggler').text());
            $('#comments-list li:first').slideDown(1500);
        });
    }
    return false;
}
// FINE FUNZIONE AGGIUNGI COMMENTO POST

function nextPhoto(start) 
{
    var postStr = 'start='+start;
    $.post('include/next_photo.php',postStr,function(data){
        $('#pic-holder-box-content').delay(1000, function(){
            $("#pic-holder-box-content").html(data);
            document.getElementById("img-ajax").onload = function (){
                $('.pic-holder').removeClass('loading');
				//$("#pic-holder-box-content").fadeIn();
				$("#pic-holder-box-content").slideDown();
                $('#pic-holder-box-content').delay(1000, function(){
                    $("#pic-holder-box-content").find('.get-more').getMore();
                });                  
            };            
            $('.get-prev-item,.get-next-item').click(function(){
                //$('#pic-holder-box-content').fadeOut();
				//var presentHeight = $('#pic-holder-box-content').height();
				//var nextHeight = presentHeight+100+"px";
				//alert(nextHeight);
				$('#pic-holder-box-content').slideUp();
				//$('.pic-holder').css('height',nextHeight).addClass('loading');
				$('.pic-holder').addClass('loading');
                var start = $(this).attr('rel');
                nextPhoto(start);
            });            
            $(document).ready(function(){        		        		
                $.superbox.settings = {
        			overlayOpacity: .8, // Background opaqueness
        			loadTxt: "Loading...", // Loading text
        			closeTxt: "Chiudi",
        			nextTxt: "Successiva",
        			prevTxt: "Precedente",
                    classSelector: ".superbox"
        		};                
                $.superbox();
            });
        });        
    });
}

// CHECK CAMPI NEWSLETTER
function checkSubscribe() 
{
    var err = 0;
    
    if(!$('#nome').val())
    {
        $('#nome').addClass('missing-field');
        err++;
    }
    else
        $('#nome').removeClass('missing-field');
    
    if(!$('#cnome').val())
    {
        $('#cnome').addClass('missing-field');
        err++;
    }
    else
        $('#cnome').removeClass('missing-field');
        
    if(!$('#mail').val())
    {
        $('#mail').addClass('missing-field');
        err++;
    }
    else
        $('#mail').removeClass('missing-field');
        
    if(!$('#securcode').val())
    {
        $('#securcode').addClass('missing-field');
        err++;
    }
    else
        $('#securcode').removeClass('missing-field'); 
           
    if(!$('#consenso').attr('checked'))
    {
        $('#consenso').parent().find('span').addClass('missing-field');
        err++;
    }
    else
        $('#consenso').parent().find('span').removeClass('missing-field'); 
    /* controllare anche il checkbox */
    if (err == 0)
        return true;
    else
        return false;
}

function reloadVideoTab(id_link) {
    var postStr = 'id_link='+id_link;
    $.post('include/get_tab_video.php',postStr,function(data){
        $('#tab-video').html(data);
		$('.get-more').append('<span></span>');
    });
}

function reloadVideoMusicaliTab(page_video_musicali) {
    var postStr = 'page_video_musicali='+page_video_musicali;
    $.post('include/sidebar_video_musicali.php',postStr,function(data){
        $('#video-musicali-box').html(data);
    });
}

function autoBanner(){
    $("#banner-558").fadeOut(1000);
    $.post("include/getBannerAuto.php",{id_categoria: '5fe6ce280af32ba2816b0f0d4cff5e63'}, function (data){
        $('#banner-558').delay(1000, function(){
            $("#banner-558").html(data);
            $("#banner-558").fadeIn(1000);
        });        
    });

    $("#banner-390-1").fadeOut(1000);
    $.post("include/getBannerAuto.php",{id_categoria: 'd6f21258c28da5af1123d73e47aecc1e'}, function (data){
        $('#banner-390-1').delay(1000, function(){
            $("#banner-390-1").html(data);
            $("#banner-390-1").fadeIn(1000);
        });        
    });
    
    $("#banner-390-2").fadeOut(1000);
    $.post("include/getBannerAuto.php",{id_categoria: 'WEB_TVxxxxxxxxxxxxxxxxxxxxxxxxxx'}, function (data){
        $('#banner-390-2').delay(1000, function(){
            $("#banner-390-2").html(data);
            $("#banner-390-2").fadeIn(1000);
        });        
    });
    
    setTimeout("autoBanner()",timeout);
}

