var curr_pos = 0;

function go_first() {
	$("#spot_content").animate({"left":"0px"},400,"swing");
	$(".nav").removeClass("active");
	$("#nav_1").addClass("active");
	$(".top_title, .top_cat").hide();
	$("#pos_1, #pos_cat_1").show();
	curr_pos = 0;	
}

function go_second() {
	$("#spot_content").animate({"left":"-976px"},400,"swing");
	$(".nav").removeClass("active");
	$("#nav_2").addClass("active");
	$(".top_title, .top_cat").hide();
	$("#pos_2, #pos_cat_2").show();
	curr_pos = -976;
}

function go_third() {
	$("#spot_content").animate({"left":"-1952px"},400,"swing");
	$(".nav").removeClass("active");
	$("#nav_3").addClass("active");
	$(".top_title, .top_cat").hide();
	$("#pos_3, #pos_cat_3").show();
	curr_pos = -1952;
}

function go_next() {
	if (curr_pos==0) { go_second(); return false; }
	if (curr_pos==-976) { go_third(); return false; }
	if (curr_pos==-1952) { go_first(); return false; }
}

function go_prev() {
	if (curr_pos==0) { go_third(); return false; }
	if (curr_pos==-976) { go_first(); return false; }
	if (curr_pos==-1952) { go_second(); return false; }
}

$(document).ready(function(){
						   
	var tempo = setInterval(function () { go_next() }, 11000);
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	var isiPod = navigator.userAgent.match(/iPod/i) != null;
	var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
	
	if (isiPad||isiPod||isiPhone) {
	$("#spot_container").touchwipe({
     wipeLeft: function() { 
		 	go_next();
			clearInterval(tempo);
			tempo = setInterval(function () { go_next() }, 11000);
			},
     wipeRight: function() {
			 go_prev();
			 clearInterval(tempo);
			 tempo = setInterval(function () { go_next() }, 11000);
		 },
     min_move_x: 20,
     preventDefaultEvents: true
	});
	}

	$("#nav_1").click(function(event){
		event.preventDefault();
		go_first();
		clearInterval(tempo);
		tempo = setInterval(function () { go_next() }, 11000);
	});

	$("#nav_2").click(function(event){
		event.preventDefault();
		go_second();
		clearInterval(tempo);
		tempo = setInterval(function () { go_next() }, 11000);
	});

	$("#nav_3").click(function(event){
		event.preventDefault();
		go_third();
		clearInterval(tempo);
		tempo = setInterval(function () { go_next() }, 11000);
	});

	$(".cell a img").mouseover(function(event){
		$(this).parent().parent().css({"background-color":"#000"});
		$(this).parent().parent().find("a.reg_title").css({"color":"#ddd"});
	});
	
	$(".cell a img").mouseout(function(event){
		$(this).parent().parent().css({"background-color":"#fff"});
		$(this).parent().parent().find("a.reg_title").css({"color":"#333"});
	});

	$("#open_cats").click(function(event){
		event.preventDefault();
		$("#cat_closed").hide();
		$("#cat_open").show()
	});

	$("#close_cats").click(function(event){
		event.preventDefault();
		$("#cat_closed").show();
		$("#cat_open").hide()
	});

	$("#big_pic_container").mouseover(function(event){
		$("#striscia").show();
	});

	$("#big_pic_container").mouseleave(function(event){
		$("#striscia").hide();
	});

	$("#pix1").mouseover(function(event){
		$("#prev_pic").stop().show().animate({"left":"30px"});
	});

	$("#pix1").mouseout(function(event){
		$("#prev_pic").stop().hide().css({"left":"65px"});
	});

	$("#pix2").mouseover(function(event){
		$("#next_pic").stop().show().animate({"left":"886px"});
	});

	$("#pix2").mouseout(function(event){
		$("#next_pic").stop().hide().css({"left":"851px"});
	});
	
	$("#facebook").mouseover(function(event){
		$("#facebook").css({"background-position":"left -20px"});
	});
	
	$("#facebook").mouseout(function(event){
		$("#facebook").css({"background-position":"left 0"});
	});
	
});