var dateinterval = '';
var feedtype = 4;

function getTime() {
	var currentTime = new Date(); 
	var hours = currentTime.getHours(); 
	var minutes = currentTime.getMinutes(); 

	if (minutes < 10) minutes = "0" + minutes; 

	document.write("ore "+hours + ":" + minutes); 
}

function lastPostHour(postHour,postDay) {
	var currentTime = new Date();
	var hours = currentTime.getHours(); 
	var day = currentTime.getDate(); 
	
	if (hours < 10) hours = "0"+hours;
	if (day < 10) day = "0"+day;
	
	var hourAgo = hours + (24-postHour);
	var dayAgo = day - postDay;
	
	if (postDay == day) {
		hourAgo = hours - postHour;
		document.write(hourAgo + " ore fa");
	} else if (hourAgo > 24 || dayAgo > 1) {
		dayAgo>1?giorno=" giorni fa":giorno=" giorno fa";
		document.write(dayAgo + giorno);
	} else {
		document.write(hourAgo + " ore fa");
	}	
}

function galleryToggle(ids, offset, prefix) {
	for (i = 0; i < ids.length; i++) {
		var obj = ids[i];
		var matchObj = prefix+offset;
		$("#"+obj).removeClass();
		if (obj == matchObj) {
			$("#"+obj).addClass("gallery-on");
		} else {
			$("#"+obj).addClass("gallery-off");
		}
	}
}

/* setta la variabile dateinterval utlilizza come condizione sui feed*/
function setDateInterval(offset) {
	var intervals = new Array("interval1", "interval2", "interval3") ;
	if (offset ==  1) {	
		dateinterval = getLastDate(604800000);
		galleryToggle(intervals,offset,"interval");
		sendUrlToFlash(feedtype) ;
	} else if (offset ==  2) {
		dateinterval = getLastDate(2592000000);
		galleryToggle(intervals,offset,"interval");
		sendUrlToFlash(feedtype);
	} else {
		dateinterval = "";
		galleryToggle(intervals,offset,"interval");
		sendUrlToFlash(feedtype);
	}
}

function WebDate() {
	var now = new Date();
	var year= now.getYear();if (year < 1000) year += 1900;
	nameDay = new Array('Domenica','Lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato');
	nameMth = new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre','Dicembre');
	document.write(nameDay[now.getDay()]+ ' ' + now.getDate() + " " + nameMth[now.getMonth()] + " " + year);
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; 
	} else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName);
	}
}

/*Funzioni di creazione dei vari flash*/
function createplayer() {
	var so = new SWFObject("/contents/instance1/images/gallery.swf", "CAROUSEL", "948", "170", "8", "#fff");

	so.addParam("allowfullscreen", "true");
	so.addParam("allowScriptAcess", "true");
	so.addParam("align", "middle");
	so.addParam("swliveconnect", "true");
	so.addParam("bgcolor", "#ffffff");
	so.addParam("wmode", "transparent");
	so.write("mycarousel");
}

function createplayerEdicola() {
	var soEdicola = new SWFObject("/contents/instance1/images/gallery_edicola.swf", "CAROUSEL EDICOLA", "948", "210", "8", "#fff");
	
	soEdicola.addParam("allowfullscreen", "true");
	soEdicola.addParam("allowScriptAcess", "true");
	soEdicola.addParam("align", "middle");
	soEdicola.addParam("swliveconnect", "true");
	soEdicola.addParam("wmode", "transparent");
	soEdicola.addParam("bgcolor", "#ffffff");
	
	soEdicola.write("carouselEdicola");
}

function createplayerEdicola_2(xml) {
	var soEdicola = new SWFObject("/contents/instance1/images/gallery_edicola_parametrica.swf", "CAROUSEL EDICOLA", "948", "210", "8", "#fff");
	
	soEdicola.addParam("allowfullscreen", "true");
	soEdicola.addParam("allowScriptAcess", "true");
	soEdicola.addParam("align", "middle");
	soEdicola.addParam("swliveconnect", "true");
	soEdicola.addParam("wmode", "transparent");
	soEdicola.addParam("bgcolor", "#ffffff");
	soEdicola.addVariable("xmlUrl", xml);
	
	soEdicola.write("carouselEdicola");
}

function createTeaser(teaser) {
	//var soTeaser = new SWFObject("/contents/instance1/files/movie/"+teaser, "TEASER", "950", "300", "9", "#fff");
	
	var soTeaser = new SWFObject("/contents/instance1/images/"+teaser, "TEASER", "950", "300", "9", "#fff");
	
	soTeaser.addParam("allowfullscreen", "true");
	soTeaser.addParam("allowScriptAcess", "true");
	soTeaser.addParam("align", "middle");
	soTeaser.addParam("swliveconnect", "true");
	soTeaser.addParam("bgcolor", "#ffffff");
	soTeaser.addParam("wmode", "transparent");
	
	soTeaser.write("TeaserFotografia");
}

function createVideoPlayer(width, height, id, div, auto_play, url_video, image_url, link_url, link_label) {
	var soVideo = new SWFObject("/contents/instance1/images/video.swf", id, width, height, "8", "#fff");
	
	soVideo.addParam("allowfullscreen", "true");
	soVideo.addParam("allowScriptAcess", "true");
	soVideo.addParam("align", "middle");
	soVideo.addParam("swliveconnect", "true");
	soVideo.addVariable("auto_play", auto_play); 
	soVideo.addVariable("videoURL", url_video);
	soVideo.addVariable("imgURL", image_url);
	soVideo.addVariable("linkURL", link_url);
	soVideo.addVariable("linkLabel", link_label);
	soVideo.addParam("bgcolor", "#ffffff");
	soVideo.addParam("wmode", "transparent");
	
	soVideo.write(div);
}


/* funzione per il carousel in flash, passa al flash l'url che ritorno i dati in formato xml*/
function sendUrlToFlash(urltype) {
    var feedurl;
	var datecondition = "";
	feedtype = urltype;
	var gallery = new Array("gallery1","gallery2","gallery3","gallery4") ;

	/*più viste
	http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'&my.approved='Y')?xn_auth=no&from=0&to=10&order=my.viewCount@D
	più commentate
	http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'&my.xg_photo_commentCount%3E1&my.approved='Y')?xn_auth=no&from=0&to=10&order=my.xg_photo_commentCount@D
	più  votate
	http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'&my.approved='Y')?xn_auth=no&from=0&to=10&order=my.ratingCount@D
	più recenti
	http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'&my.approved='Y')?xn_auth=no&from=0&to=10&order=published@D*/
	
	// define ning url
	if (dateinterval!='') datecondition =  escape("&createdDate>"+dateinterval);
	
	if (urltype== 1) {
		feedurl = 	"http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'"+datecondition+"&my.approved='Y')?xn_auth=no&from=0&to=16&order=my.ratingCount@D";
		galleryToggle(gallery,urltype,"gallery");
	} else if (urltype== 2) {
		feedurl = 	"http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'&my.xg_photo_commentCount%3E1"+datecondition+"&my.approved='Y')?xn_auth=no&from=0&to=16&order=my.xg_photo_commentCount@D";
		galleryToggle(gallery,urltype,"gallery");
	} else if (urltype== 3) {
		feedurl = 	"http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'"+datecondition+"&my.approved='Y')?xn_auth=no&from=0&to=16&order=my.viewCount@D";
		galleryToggle(gallery,urltype,"gallery");
	} else {
		feedurl = 	"http://community.corsofotodigitale.it/xn/atom/1.0/content(type='Photo'"+datecondition+"&my.approved='Y')?xn_auth=no&from=0&to=16&order=published@D";
		galleryToggle(gallery,urltype,"gallery");
	}
	
	var flashMovie=getFlashMovieObject("CAROUSEL");
	flashMovie.SetVariable("xmlData", escape(feedurl));
}

function inviaCodice() {
	var codice = $('#codice').attr('value');
	$.get(webapp+'/customer-codes/checkCode.jsp',{ 'code': codice}, function(data){
		$('#rescode').html(data);
	});
}

function getLastArticle(idcat,idnode) {
	var urlacm = webapp+'/articleCategory.jsp?idArticleLevel='+idcat+'&from=1&node='+idnode+'&size=10&idXsl=1&instance='+instance+'&channel='+channel+'&ext=json&int=portlet/articlexml';
	$('#lastarticle').append('<tr><td id="loader-json" align="center" style="background: none;"><img src="/contents/instance'+instance+'/images/loader.gif" width="25"/></td></tr>');			
	$.getJSON(urlacm, function(data){
		$('#loader-json').hide();
		$('#lastarticle').hide();
		$.each(data.articoli, function(i,item){
			$('#lastarticle').append('<tr><td><a href="' + item.link + '" title="' + item.title + '">' + item.title + '</a></td></tr>');			
		})
		$('#lastarticle').fadeIn('slow');
	});
}

/*function getLastComments() {
	var urlacm = "/acm-on-line/lastComments.jsp?instance=1&channelType=1001&ext=json&channel=33";
	$.getJSON(urlacm, function(data){
		$.each(data.articoli, function(i,item){
			$('#lastcomment').append("<tr><td><a href=\""+item.link+"\" title=\"title\">"+item.title+"</a></td></tr>");
		})
	});
}*/

function calcCharLeft(MaxLength,obj) {
	var msgLen = $(obj).val().length;
	if (msgLen >= MaxLength ) {
		$('input[name="charsleft"]').val('0');
		alert('Il messaggio puo\' contenere massimo ' + MaxLength + ' caratteri.');
		var TextAreaValue = $(obj).val();
		$(obj).val(TextAreaValue.substring(0,MaxLength));
	} else {
		$('input[name="charsleft"]').val(MaxLength - msgLen);
	}
}