$(function(){
	var oggi = new Date(); 
	var orario = ""; 	
	orario+=oggi.getHours()<10?"0"+oggi.getHours()+":":oggi.getHours()+":"; 
	orario+=oggi.getMinutes()<10?"0"+oggi.getMinutes()+":":oggi.getMinutes();
	$('#orario').text(orario);
	$('.comment-text-short').show();
	$('.comment-text-long').hide();
	$('.abstract-short').show();
	$('.abstract-long').hide();
	
	$.ajax({
		url: '/on-line/cloud.jsp?instance=1&channel=100001&interval=30&int=cloud&size=20',
		dataType: 'html',
		beforeSend: function(){			
			$('#loading').show();
		},
		success: function(data,textStatus){
			$('#loading').hide();
			$('#menutagcloud').html(data);
		},
		error: function(){
			$('#menutagcloud').html('<h2 style="text-align: center;">Servizio non disponibile</h2>');
			$('#loading').hide();
		}
	});
	
	/* ALTEZZA ACTION-HP */
	var arr =  $(".primopiano").get();
	$.each(arr, function(n,val) {
		var identificatore = $(".primopiano").eq(n);
		var altezza = identificatore.height() - 50;
		var id = identificatore.attr("id").substring(2);
		var actionhp = "#action-hp"+id+" .icon-expand";
		$(actionhp).height(altezza);
	});
	/* FINE ALTEZZA */			
	
	/* ALTEZZA MENU BASSI */
	var altmenu = $("#menubar").height();
	$("#menubar .menublock").height(altmenu);
	/* FINE ALTEZZA MENU */
	
	/* MENU */
	$(".menublock[class*='closed'] .menu").hide();
	$(".menublock[class*='closed'] .apri").show();
	//$(".menublock[class*='closed'] .arrowgrey").attr({"class":"arrowgrey closed",title: "Apri"});	

	$(".menublock .titlemenu .arrowgrey").click(
		function(){
			controlmenu($(this));
		}
	);
	$(".menublock .apri").click(
		function(){
			controlmenu($(this));
		}
	);
	/* FINE MENU */
	
	/* MENU AZZURRI */
	$(".arrowciano").click(
		function(){
			controlmenuciano($(this));
		}
	);
	/* FINE MENU AZZURRI */
	
	/* TAGCLOUD */
	/*
	$("#menutagcloud a").tagcloud({
        size: {
          start: parseInt("11"), 
          end: parseInt("26"), 
          unit: ("px")
        }, 
        color: {
          start: "#008fb5", 
          end: "#0088ad"
        }               
   })
   */
   /* FINE TAGCLOUD */       
	
	/* ACTIONNEWS */
	$(".newscontainer .actionnews").bind({
	    click: function() {
			var newsabstract = $(this).prev(".news").find(".newsabstract");
			var displayabstract = $(newsabstract).is(':visible');
			if (displayabstract) {
				$(newsabstract).hide();
				$(this).find("img").attr({ 
		          src: img_dir+"/opennews.gif",
		          title: "Apri abstract",
		          alt: "Apri abstract"
		        });
			}
			else {
				$(newsabstract).show();
				$(this).find("img").attr({ 
		          src: img_dir+"/closenews.gif",
		          title: "Chiudi abstract",
		          alt: "Chiudi abstract"
		        });
			}
	    }
	})
	/* FINE ACTIONNEWS */
	
	/* INCLUSIONE METEO */
	$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20item.condition.code%20from%20weather.forecast%20where%20location%3D'ITXX0147'&format=json&diagnostics=false&callback=?",		
		function(data){
			var dominio = "http://l.yimg.com/a/i/us/we/52/";
			var codice = data.query.results.channel.item.condition.code;
			$("#meteo").append('<img src="'+dominio+codice+'.gif"/>');			
		}
	);		
	/* FINE METEO */
	
	/* INCLUSIONE CALENDARIO TURISMO */
	var container = $("#calendarturismo");	
	if (container.length > 0) {
		$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fturismo.reggiocal.it%2FHomePage.aspx%22%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22dv_event_home_body%22%5D%2F%2Ftable%5B%40class%3D%22event_tbl%22%5D%2Ftr%5B%40class!%3D%22header%22%5D%2Ftd%5B%40class!%3D%22event_tbl_c_col%22%5D'&format=json&diagnostics=false&callback=?",
			function(data){
				//JSON
				var dominio = "http://turismo.reggiocal.it/";
				$.each(data.query.results.td, function(i,item) {
					var mod = i%2;
					var classe = i%2 ? 'no-bordo-top p-t-zero' : 'no-bordo-bottom p-b-zero';
					var cont1 = '<div class="calendar-items '+classe+'"><div class="calendar-item">';
					var cont2 = '';
					var cont3 = '';
					var cont4 = '</div><div class="clear">&#160;</div></div>'
					
					if (mod == 1){
						cont2 = '<div class="calendar-date">Fino al ' + item.p + '</div>';
					}
					else {
						cont3 = '<a href="'+dominio+item.a.href + '" title="' + item.a.content + '">' + item.a.content + '</a></div><div class="action-hp"><div class="icon-expand"><a href="'+dominio+item.a.href +'"><img src="'+img_dir+'/icon-expand.gif" /></a></div>';
					}

					$(container).append(cont1 + cont2 + cont3 + cont4);
				})
				$(".calendar-item").first().addClass("first");
				$(".calendar-item").last().addClass("last");
				$(container).append('<div class="calendarturismo-all"><a href="http://turismo.reggiocal.it/GeowfPage.aspx?id=219&amp;page=agenda&amp;view=calendar#mainmenu">Vai al portale turismo »</a></div>');
			}
		);
	}
	/* FINE CALENDARIO */

	/* INCLUSIONE WEBCAM */	
	setInterval("webcam()",180000);
	/* FINE CALENDARIO */
	
	/* COMMENTI */
		/* BOX COMMENTI */
			$('#arrowboxcomment').click(function() {
				var boxcommenti = $("#boxcomment").is(':visible');
				if (boxcommenti) {
					$("#boxcomment").hide();
					$("#arrowboxcomment img").attr({ 
			          src: img_dir+"/arrow-open-tagcloud.jpg",
			          title: "Apri",
			          alt: "Apri"
			        });
				}
				else {
					$("#boxcomment").show();
					$("#arrowboxcomment img").attr({ 
			          src: img_dir+"/arrow-close-tagcloud.jpg",
			          title: "Chiudi",
			          alt: "Chiudi"
			        });
				}
			});
		/* SINGOLO COMMENTO */
		$('.singlecomment .action-hp').click(function(){
			var idcomment = $(this).parent(".singlecomment");
			var commentocorto = $(idcomment).find(".comment-text-short");
			var commentolungo = $(idcomment).find(".comment-text-long")
			var controllocommento = commentolungo.is(':visible');
			
			if (controllocommento) {
				$(commentolungo).hide();
				$(commentocorto).show();
				$(this).find("img").attr({ 
		          src: img_dir+"/opennews.gif",
		          title: "Apri commento",
		          alt: "Apri commento"
		        });
			}
			else {
				$(commentolungo).show();
				$(commentocorto).hide();
				$(this).find("img").attr({ 
		          src: img_dir+"/closenews.gif",
		          title: "Chiudi commento",
		          alt: "Chiudi commento"
		        });
			}
		});
	/* FINE COMMENTI */
	
	/* ARTICOLO PRESENTAZIONE TEMI */
	$("#abstract-short").bind({
	    click: function() {
			var displaylongabstract = $(".abstract-short").is(':visible');

			if (displaylongabstract) {
				$(".abstract-long").show();
				$(".abstract-short").hide();
				$(this).find("img").attr({ 
		          src: img_dir+"/closenews.gif",
		          title: "Chiudi abstract",
		          alt: "Chiudi abstract"
		        });
			}
			else {
				$(".abstract-long").hide();
				$(".abstract-short").show();
				$(this).find("img").attr({ 
		          src: img_dir+"/opennews.gif",
		          title: "Apri abstract",
		          alt: "Apri abstract"
		        });
			}
	    }
	})
	/* FINE ARTICOLO PRESENTAZIONE */
});

function controlmenu(identifier) {
	var obj = identifier.parents(".menublock");
	var apri = $(obj).find(".apri");
	var menu = $(obj).find(".menu");
	var img = $(obj).find(".titlemenu .arrowgrey");
	var toggle_open = $(img).hasClass('opened');
	var initpos = $(obj).position();
	
	// Resetto
	$(".menu").hide();
	$(".apri").show();
	$(".arrowgrey").attr({"class":"arrowgrey closed",title: "Apri"});
	//

	if (toggle_open == true) {
		$(apri).show();
		$(menu).hide();
		$(img).attr("title","Apri");
		$(img).removeClass("opened");
		$(img).addClass("closed");
	}
	else {
		$(apri).hide();
		$(menu).show();
		$(img).attr("title","Chiudi");
		$(img).removeClass("closed");
		$(img).addClass("opened");
	}
	
	var pos = $(obj).position();
	if(initpos.top >= $(window).height()) window.scrollTo(0,pos.top);
}

function controlmenuciano(identifier) {
	var obj = identifier.attr("id");
	var apri = "#apri"+obj;
	var menu = "#menu"+obj;
	
	if ($(apri).is(':visible')) {
		$(apri).hide();
		$(menu).show();
		$(identifier).removeClass("closed");
		$(identifier).addClass("opened");
	}
	else {
		$(apri).show();
		$(menu).hide();
		$(identifier).removeClass("opened");
		$(identifier).addClass("closed");
	}
}

function felice_click() {
	document.getElementById('emoticons_edtIconsSelect').value ='1'; 
	document.getElementById('emoticons_Opzioni').style.display = 'none'; 
	document.getElementById('emoticons_edtFromUrl').value = document.location.href; 
	return verify();
}

function neutro_click() {
	document.getElementById('emoticons_edtIconsSelect').value ='2'; 
	document.getElementById('emoticons_Opzioni').style.display = 'none'; 
	document.getElementById('emoticons_edtFromUrl').value = document.location.href; 
	return verify();
}
function triste_click() {
	document.getElementById('emoticons_edtIconsSelect').value ='3'; 
	document.getElementById('emoticons_Opzioni').style.display = 'block'; 
	document.getElementById('emoticons_edtFromUrl').value = document.location.href;
}
function Submit() {
	if (document.getElementById) {
		var email = document.getElementById('emoticons_edtEmail').value;
		var emotincons = document.getElementById('emoticons_edtIconsSelect').value;
		var fromurl = document.getElementById('emoticons_edtFromUrl').value;
		var motivivazione = document.getElementById('emoticons_cbTriste').value;
    }
	else if (document.all) {
		alert("ddd");
		var email = document.all['emoticons_edtEmail'].value;
		var emotincons = document.all['emoticons_edtIconsSelect'].value;
		var fromurl = document.all['emoticons_edtFromUrl'].value;
		var motivivazione = document.all['emoticons_cbTriste'].value;		
    }
    document.getElementById('emoticons_Votazione').style.display = 'none';
    document.getElementById('emoticons_Loading').style.display = 'block';
	
	$.ajax({
        type: "POST",
        url: "/on-line/emoticons.jsp",
        data: 'email=' + email + '&emoticons=' + emotincons + '&fromurl=' + fromurl + '&motivazione=' + motivivazione, 
		dataType: "text",  
		success: function(result) {
            document.getElementById('emoticons_Loading').style.display = 'none';
            document.getElementById('emoticons_Result').innerHTML = result;
            document.getElementById('emoticons_Result').style.display = 'block';
        }
    });
	
}

function verify() {
    if (document.getElementById("emoticons_edtEmail").value == "") {
        alert("Inserire il campo e-mail");
    }
    else {
        Submit();
    }
}

function webcam(){
	var camcontainer = $(".cam-container");
	var random = new Date().getTime();
	
	/*
	$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwebcam.reggiocal.it%2FViewCam.aspx%3Fcamid%3D6%26refresh%3D5'%20and%20xpath%3D'%2F%2Fform%5B%40id%3D%22aspnetForm%22%5D%2F%2Ftd%2Fimg%5B%40name%3D%22camimage%22%5D'&format=json&diagnostics=false&callback=?",
		function(data){
			var dominiocam = "http://webcam.reggiocal.it/";
			var imgcam = data.query.results.img.src+'&counter='+random;
			$(camcontainer).html('<img src="http://webcam.reggiocal.it/'+imgcam+'"/>');
		}
	);
	*/
	var imgcam = "http://webcam.reggiocal.it/GetImage.ashx?camid=3&winID=1237384350&counter="+random;
	$(camcontainer).html('<img src="'+imgcam+'"/>');
}
