$(function(){ 
	
	/* COOKIE */
	var cookieval = $.cookie("domus"); // LEGGO IL COOKIE	
	//alert(cookieval);
	
	if(cookieval && cookieval == "permanent" || permanent && permanent == "permanent" ) {
		$.cookie("domus","permanent"); 
	}
	else if(cookieval && cookieval == "smartphone") {		
		if(canale == 4){ window.location = "http://mobi.domusacademy.it"; } // ITALIANO
		else{ window.location = "http://mobi.domusacademy.com"; } // INGLESE
	}
	else if (cookieval != "web") {
		checkagent();
	}
	/* COOKIE */
	
	$(".menu li").bind({
		mouseover: function() {
			$(this).find(".menu-second").show();
		},
		mouseout: function() {
			$(this).find(".menu-second").hide();
		}
	})
	
	/* INFINITE CAROUSEL */
	var autoscrolling = true;
	
	$('.infiniteCarousel').infiniteCarousel().mouseover(function () {
		autoscrolling = false;
	}).mouseout(function () {
		autoscrolling = true;
	});

	$('.wrapper li').mouseover(function () {
		$(this).find(".infinitetitle").show();
	}).mouseout(function () {
		$(this).find(".infinitetitle").hide();
	});

	setInterval(function () {
		if (autoscrolling) {
			$('.infiniteCarousel').trigger('next');
		}
	}, 4000);
	/* FINE CAROUSEL */
	
	// LIGHTBOX
	$(".gallery a[rel^='prettyPhoto']").prettyPhoto({
		theme:'dark_rounded',
		autoplay_slideshow: false
	});
	$(".cloned a").attr("rel","");
	
	// DATEPICKER	
	$('#datepicker').datepicker({
		regional: 'it',
		changeMonth: true,
		changeYear: true,
		yearRange: '1950:2010'
	});
	
});

/* COOKIE */
function checkagent() {
	var ua = navigator.userAgent;
	var checker = {
	  iphone: ua.match(/(iPhone|iPod|iPad)/),
	  blackberry: ua.match(/BlackBerry/),
	  android: ua.match(/Android/)
	};

	if ((checker.android) || (checker.blackberry) || (checker.iphone)) {
		if(canale == 4){ var question = confirm("Vuoi visualizzare la versione ottimizzata per Smartphone del nostro sito?"); }
		else{ var question = confirm("Would you like to view the Smartphone optimized version of our site?"); }
		
		if (question) {
			$.cookie("domus","smartphone"); // SETTO IL COOKIE A COOKIE DI SESSIONE NON SETTANDO LA DATA DI FINE ($.cookie("domus","smartphone", { expires: 100 });)
			if(canale == 4){ window.location = "http://mobi.domusacademy.it"; } // ITALIANO
			else{ window.location = "http://mobi.domusacademy.com"; } // INGLESE
		}
		else {
			$.cookie("domus", "web"); // SETTO IL COOKIE
		}
	}
}
/* COOKIE */

/* VIDEO PLAYER */
function video(contenitore,url,link_url,link_label,w,h,autoplay,videoimg) {

	var flashvars = {
		videoUrl: url,
		linkUrl: link_url,
		linkLabel: link_label,
		autoPlay: autoplay,
		videoImg: videoimg
	}
	
	var params = {
		allowfullscreen: "true",
		allowScriptAccess: "always",
		align: "middle",
		swliveconnect: "true",
		bgcolor: "#ffffff",
		wmode: "transparent"
	};
	
	var attributes = {
		id: contenitore,
		name: contenitore
	};
	
	swfobject.embedSWF(img_dir+'/video-player.swf', contenitore, w, h, '9.0.0', false, flashvars, params, attributes);
}

function popup(URL, W, H) {
	X = (screen.width - W) / 2;
	Y = (screen.height - H) / 2;
	P = "width=" + W + ",height=" + H + ",";
	P+= "top=" + Y + ",left=" + X + ",";
	P+= "scrollbars=no,status=no,resizable=no";
	POP = window.open(URL, "", P);
	POP.window.focus();
}

