var StatoChat     = false;
var contaRicarica = false;
var timer1, timer2;


$(document).ready(function() {
    ChatFrm();
	SocialBtn();
	$('.linkfeed').click(function(e) {
		e.preventDefault();
		var q = $(this).attr('href');
		onClickFeedWeek(q.replace("?q=", ""));
	});
});



function onClickFeedWeek(str){
	$('.gsc-input').val(str);
	$('.gsc-search-button').trigger('click');
	_gaq.push([ '_trackEvent', 'RicercaFeed', str ]);
	void(0);
}
  
function installMotore(){
	try {
		window.external.AddSearchProvider('http://www.guardafilmstreaming.it/motore.xml?v=100');
		_gaq.push(['_trackEvent', 'Motore', 'installato']);
	} catch (e){
		alert('Impossibile installare il motore di ricerca sul tuo browser.\nErrore: ' + e.description);
		_gaq.push(['_trackEvent', 'Motore', 'errore', e.description]);
	} 
	void(0);
}

function installGadget(){
	if(!window.open('res/inc/downloadGadget.asp','gadget')){
		_gaq.push(['_trackEvent', 'Gadget', 'errore']);
		alert('Il browser ha impedito l\'apertura di una nuova pagina.\nAbilita Guardafilmstreaming.it all\'uso dell pop-up.');
	} else {
		_gaq.push(['_trackEvent', 'Gadget', 'installato']);
	}
}

function chatbar() {
	if (StatoChat != true) {
		$('#cboxdiv').css('visibility','visible');
		cboxmain.Carica()
		RicaricaChat();
		StatoChat = true;
	}else {
		$('#cboxdiv').css('visibility','hidden');
		StatoChat = false;
		clearTimeout(timer1);
		clearTimeout(timer2);
	}
}

function ApriChat() {
	if ( StatoChat != true ){
		StatoChat = true;
		$('#cboxdiv').css('visibility','visible');
		if (contaRicarica != true){
			RicaricaChat();
			contaRicarica = true;
		}
	}
		
}
function RicaricaChat(){
	timer1 = setTimeout("cboxmain.Carica()", 10000);
	timer2 = setTimeout("RicaricaChat()", 10000)
}

function Carica(data_sub) {
	if(data_sub == "" || data_sub == "undefined" || data_sub == undefined){
		$('#stat').html("<img src='/res/img/3.gif' />");
		$.post("load.asp", 
			function(data){ 
				$('#Msg').html(data); 
				$('#stat').html("<a href='javascript:Carica()'><img src='/res/img/3.jpg' /></a>");
		});
	}else{
		$('#stat').html("<a href='javascript:Carica()'><img src='/res/img/3.jpg' /></a>");
		alert(data_sub);
	}
	
}

function Blocca() {
	var nome  = $('#nome').val();
	var testo = $('#testo').val();
	if (nome != "" && testo != "" && nome != "Il tuo nome" && testo != "Il tuo messaggio" ){
		$('#stat').html("<img src='/res/img/3.gif' />");
		$.post("post.asp", {NOME: ""+nome+"", TESTO: ""+testo+""}, 
			function(data){ 
				Carica(data); 
		});
		$('#testo').val("");
		$('#testo').select();
	 } else {
	 
		 if( nome == "" || nome == "Il tuo nome" ){
			 alert("Devi inserire un NOME per interagire nella chat");
			$('#nome').select();
		 }else if(testo == "" || testo == "Il tuo messaggio"){
			 alert("Devi inserire un TESTO per interagire nella chat");
			$('#testo').select();
		 } else {
			 $('#stat').html("Errore");
		 }
	 }
	 return false;
	 
}

function GPlusBtn(){
	return '<g:plusone size="medium"></g:plusone>';
}

function TwitterBtn(){
    var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://platform.twitter.com/widgets.js';
    s1.parentNode.insertBefore(s, s1);
	return '<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a>';
}
function FacebookBten(){
	return '<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.facebook.com/pages/guardafilmstreamingit/319201228013&amp;layout=button_count&amp;show_faces=false&amp;width=408&amp;action=like&amp;font=verdana&amp;colorscheme=light" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:130px; height:22px"></iframe>';
}

function SocialBtn(){
	$('#icone').html( GPlusBtn() + TwitterBtn() + FacebookBten());
}

function ChatFrm(){
	$('#cboxdiv').html('<iframe frameborder="0" width="240" height="325" src="film/chat/form.asp" marginheight="2" marginwidth="2" scrolling="no" name="cboxmain" id="cboxmain"></iframe>');
}

