function rotateFilmCover (name)
{
	first = $(name + " > .first");
	second = $(name + " > .second");
	third = $(name + " > .third");

	second.fadeOut("normal", function(){
		second.removeClass("second");
		second.addClass("first");
		second.fadeIn();
	});

	third.fadeOut("normal", function(){
		third.removeClass("third");
		third.addClass("second");
		third.fadeIn();
	});

	first.fadeOut("normal", function(){
		first.removeClass("first");
		first.addClass("third");
		first.fadeIn();
	});



}

function setHomePage (url) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if (window.sidebar) {
	if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e) {
			var strTemp = '';
			strTemp += "Permission denied";
			alert(strTemp);
		}
	}
	var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
}


function setGossipNavigation (el, over)
{
	if (over)
	{
		el.addClass('active');
	}
	else
	{
		if (!el.hasClass('perm_active')) {
			el.removeClass('active');
		}
	}

	return false;

}

function setGossipNavigationPermanent (el)
{
	var hidecontent = undefined;

	$('#home_gossip_navigation > li').each(function(){
			$(this).removeClass('perm_active');
			setGossipNavigation ($(this), false)

			content = findContentForGossipNavigaion ($(this));
			if (content.is(':visible'))
			{
				hidecontent = content;
			}
		});

	el.addClass('perm_active');
	setGossipNavigation (el, true);

	var showcontent = findContentForGossipNavigaion (el);

	hidecontent.fadeOut("slow");
	showcontent.fadeIn("slow");

	return false;
}

function findContentForGossipNavigaion (el)
{
	var elclass = $($(el).parent().get(0)).attr('class');
	var newel = $("li." + elclass + "_container");
	return newel;
/*	var content = null;
	content = $(el).next();
	return content;*/
}

$(document).ready(function(){
	$(function() {
	  $("#newslettersubmitajax").submit(function() {
		 $.ajax({
		   type: "POST",
		   url: "/gonewsletter.php",
			data: $('#newslettersubmitajax').serialize(),
			success: function(msg){
			var result = msg.charAt(0);
			if (result == '0') {
				msg = msg.substring(1);
				//var message = $("#successmessage").html();
				$('#hiddennewslettermessage').html(msg);
				return false;
			}
			else {
				var randomnumber=Math.floor(Math.random()*10000)
				$(".captchaimage").attr("src", "/newsletter_captcha.php?rand=".concat(randomnumber));
				$('#error_message').html(msg);
				return false;
			}
		   }
		 });
		 return false;
		});
	});

	setInterval(function(){rotateFilmCover ('#home_top_items')},6000);

	$('#home_gossip_navigation').children('li').mouseover(function() {
		$('#home_gossip_navigation').children('li').removeClass('active');
		var cont = $(this).attr('class') + '_container';
		$(this).addClass('active');
		$('#home_gossip_box').children('li').hide();
		$('.' + cont).show();
	});

	$('#location_1_submit').click(function() {
		var url = '/cinema/cercacinema';
		var province = $('#location_1_province').val();
		if (province.length > 0)
		{
			location.href = province;
		}
		else
		{
			location.href = '/cinema/cercacinema/';
		}

		return false;
	});


	//$('.fixzindex').bgiframe();

	$('#bookmarkthis').click(function () {
		setHomePage ('http://www.film.it');
		return false;
	});

	var browserCheck = (document.all) ? 1 : 0;
	if (!browserCheck) {
		$('#btli').hide();
		$('#btli_smap').hide();
	}

	$('.user-share, #user-share').click(function () {
		$('#share').toggle();
		return false;
	});

	/*$('#user-vota').click(function(){
		$('#rate').toggle();
		return false;
	});*/

	$('.cercacinema select').change(function(){
		var url = $(this).val();

		if (url.length > 0 && !$(this).parent().hasClass('.province_select'))
		{
			location.href = url;
		}

		if ($(this).parent().hasClass('.province_select'))
		{
			var url = $(this).val();
			var query = "?province=" + url;

			$.ajax({
			type: "GET",
			url: "/go_ccinema.php" + query,
			contentType: "application/json; charset=utf-8",
	  	  	dataType: "json",
			success: function(msg){
				$("#citta_id").empty();
				$("#citta_id").append(msg.cities);
				$("#selectcitta").text(msg.default_message).css('color', 'red');
			}
			});
		}

	});

	$('#citta_id').click( function(){
		$('#selectcitta').css('color','black');
	});


	$("a.trailer_active").bind("mouseover",function(){
		$(this).removeClass('trailer_active');
		$(this).addClass('trailer_inactive');
	}).bind("mouseleave",function(){
		$(this).removeClass('trailer_inactive');
		$(this).addClass('trailer_active');
	});

	$('ul.calendar > li > h2').click (function()
	{
		$('ul.calendar > li.calendar_text:visible').slideUp();
		$(this).parent().next().slideDown();
	});

	//add hover class to UL of scheda menu if hovering over the last li element; only if the li is not active (has class active), because the UL already has the class hover. in tpl/common/scheda_menu.tpl
	$('ul#scheda_menu li.last').not('.active').hover(
		function () {
			$(this).parent().addClass('hover');
		},
		function () {
			$(this).parent().removeClass('hover');
		}
	)
});


function getForCity(city_code)
{

	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
				$('#rss_result').text(msg.message);
				if (msg.result)
				{
					sp.text(++spval);
				}
		}
	});


}
function getForProvince(province_code)
{
	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
				$('#rss_result').text(msg.message);
				if (msg.result)
				{
					sp.text(++spval);
				}
		}
	});
}

function getForCinema(cinema_code)
{
	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
				$('#rss_result').text(msg.message);
				if (msg.result)
				{
					sp.text(++spval);
				}
		}
	});
}
function getForFilm(film_code)
{
	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
				$('#rss_result').text(msg.message);
				if (msg.result)
				{
					sp.text(++spval);
				}
		}
	});
}

function rssVota(rss_id, rss_channel, vota, object)
{
	var query = "?idrecord=" + rss_id + "&vota=" + vota;
	sp = object.children();
	spval = parseInt(object.children().text());
	$.ajax({
		type: "GET",
		url: "/rss_rating.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
				$('#rss_result').text(msg.message);
				if (msg.result)
				{
					sp.text(++spval);
				}
		}
	});
}

function myTest()
{
	var query = "?asdasd";
	$.ajax({
		type: "GET",
		url: "/gophotocool.php" + query,
		success: function(msg){
				alert(msg);
		}
	});

}
function getCercaForProvince(province)
{
	var query = "?province=" + province;
	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
			/*$("#selectcitta").html(msg.city_first);
			$("#selectcinema").html(msg.cinema_first);
			$("#selectfilm").html(msg.film_first);*/
			$("#citta_id").html(msg.cities);
			$("#cinema_id").html(msg.cinemas);
			$("#insala_id").html(msg.films);
		}
	});
}

function getCercaForCity(city)
{
	var query = "?city=" + city;
	$.ajax({
		type: "GET",
		url: "/go_ccinema.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){
			$("#cinema_id").html(msg.cinemas);
			$("#insala_id").html(msg.films);
			/*$("#selectcinema").html(msg.cinema_first);
			$("#selectfilm").html(msg.film_first);*/
		}
	});
}

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		countfield.value = maxlimit - field.value.length;
}


/* Photo contest part */

function finishVoting()
{
	$('#click_vote').hide();
}

function showBig(id) {
	$('#small_article_box').hide();
	$('#article_big_' + id).show();
}

function closeBig(id) {
	$('#article_big_' + id).hide();
	$('#small_article_box').show();
}

$(document).ready(function() {
	var left = $('.guidacarousel');
	var left_cnt = left.children('li').length;
	if (left_cnt > 5) {
		var left_set = 5;
	} else {
		left_set = left_cnt;
	}
	$('#left-scroller').attr('style', 'height: ' + left_set * 75 + 'px');
	var top = $('#guida-tv-hour-scroller').children(':first').children(':first');
	top.width(top.children('li').length * 250);
	var top2 = $('#guida-tv-scroll').children(':first').children(':first');
	top2.width(top.width());
	$('#guida-tv-scroll').children(':first').height(left_set * 75);
	
	var a_z_search_orig_val = $("input[name=a-z-search]").val();
	$("input[name=a-z-search]").click(function() {
		if ($("input[name=a-z-search]").val() == a_z_search_orig_val) {
			$("input[name=a-z-search]").val('')
		}
	});
	
	$("#a-z-submit").click(function() {
		var val = $("input[name=a-z-search]").val();
		var cur_url = window.location.pathname
		var arr_cur_url = cur_url.split('/');
		var loc = '/' + arr_cur_url[1] + '/' + arr_cur_url[2] + '/' + $("input[name=a-z-search]").val().replace(' ', '-');
		if (val != a_z_search_orig_val && val.length > 0) {
			window.location = loc;
		}
		return false;
	});

});

function guidatvFilter(what) {
	var filters = new Array();
	filters[0] = 'film';
	filters[1] = 'serietv';
	filters[2] = 'sport';
	filters[3] = 'musica';
	filters[4] = 'tvragazzi';

	var obj = $('#' + what + '-checkbox');
	if (obj.children('a').hasClass('checked')) {
		obj.children('a').removeClass('checked');
		$('.filter_' + what).removeClass('fnc_bg_' + what);
		return true;
	}

	for (var i = 0; i < filters.length; i++) {
		if (filters[i] == what) {
			$('#' + what + '-checkbox').children('a').addClass('checked');
			$('.filter_' + what).addClass('fnc_bg_' + what);
		} else {
			$('#' + filters[i] + '-checkbox').children('a').removeClass('checked');
			$('.filter_' + filters[i]).removeClass('fnc_bg_' + filters[i]);
		}
	}
}

function guidatv_controller(direction, step) {

	var objects = new Array();
	objects[0] = guidatv_central;
	objects[1] = guidatv_hours;
	objects[2] = guidatv_left;
	objects[3] = guidatv_line;
	$('#preloader-box').show();
	if (step != '') {
		objects[1].setMaxStep(step);
		objects[0].setMaxStep(step);
	}

	for (var i = 0; i < objects.length; i++) {
		objects[i].call(direction);
		//objects[i].debug(direction, step);
	}
	$('#preloader-box').fadeOut('slow');
}

function guidatv() {
	this.step_x = 250;
	this.step_y = 75;
	this.pos_x = 0;
	this.pos_y = 0;
	this.pos_x_max = 22;
	this.pos_y_max = 7;
	this.animation_speed = 2;
	this.working = false;

	this.call = function(direction) {
		var left = $('.guidacarousel');

		/*if (this.working) {
			return true;
		} else {
			this.working = true;
		}*/

		switch (direction) {
			case 'up' :
				this.pos_y_max = left.children('li').length - 3;
				this.up();
			break;
			case 'down' :
				this.pos_y_max = left.children('li').length - 3;
				this.down();
			break;
			case 'right' :
				this.right();
			break;
			case 'left' :
				this.left();
			break;
		}
	}

	this.work_over = function() {
		this.working = false;
	}
}

/* Guidatv hours scroller object */
var guidatv_hours = new guidatv();
guidatv_hours.goToStep = function(step) {
	var holder = $('#guida-tv-hour-scroller').children(':first').children(':first');
	this.pos_x = step;
	holder.attr('style', 'left: -' + (this.step_x*step) + 'px');
	this.work_over();
};
guidatv_hours.up = function() {};
guidatv_hours.down = function() {};
guidatv_hours.right = function() {
	var holder = $('#guida-tv-hour-scroller').children(':first').children(':first');
	var next_pos = this.pos_x + 1;

	if (next_pos < this.pos_x_max) {
		holder.animate({
			left: -(next_pos * this.step_x)
		}, this.animation_speed, function() {
		});

		this.pos_x = next_pos;
	}
	this.work_over();
};

guidatv_hours.setMaxStep = function(x_max) {
	this.pos_x_max = x_max;
};

guidatv_hours.left = function() {
	var holder = $('#guida-tv-hour-scroller').children(':first').children(':first');
	var next_pos = this.pos_x - 1;

	if (next_pos >= 0) {
		holder.animate({
			left: -(next_pos * this.step_x)
		}, this.animation_speed, function() {
		});
		this.pos_x = next_pos;
	}
	this.work_over();
};
guidatv_hours.debug = function(arga, argb) { alert('guida hours: arga -' + arga + ', argb -' + argb ); }
/**/

/* Guidatv hours scroller object */
var guidatv_central = new guidatv();
guidatv_central.goToStep = function(step) {
	var holder = $('#guida-tv-scroll').children(':first').children(':first');
	this.pos_x = step;
	holder.attr('style', 'left: -' + (this.step_x*step) + 'px');
	this.work_over();
};
guidatv_central.up = function() {};
guidatv_central.down = function() {};
guidatv_central.right = function() {
	var holder = $('#guida-tv-scroll').children(':first').children(':first');
	var next_pos = this.pos_x + 1;
	if (next_pos < this.pos_x_max) {
		holder.animate({
			left: -(next_pos * this.step_x)
		}, this.animation_speed, function() {

		});
		this.pos_x = next_pos;
	}
	this.work_over();
};
guidatv_central.setMaxStep = function(x_max) {
	this.pos_x_max = x_max;
};

guidatv_central.left = function() {
	var holder = $('#guida-tv-scroll').children(':first').children(':first');
	var next_pos = this.pos_x - 1;

	if (next_pos >= 0) {
		holder.animate({
			left: -(next_pos * this.step_x)
		}, this.animation_speed, function() {
		});
		this.pos_x = next_pos;
	}
	this.work_over();
};
guidatv_central.up = function() {
	var holder = $('#guida-tv-scroll').children(':first').children(':first');
	var next_pos = this.pos_y + 1;

	if (next_pos < this.pos_y_max) {
		holder.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};
guidatv_central.down = function() {
	var holder = $('#guida-tv-scroll').children(':first').children(':first');
	var next_pos = this.pos_y - 1;

	if (next_pos >= 0) {
		holder.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};
guidatv_central.debug = function(arga, argb) { alert('guida central: arga -' + arga + ', argb -' + argb ); }
/**/

/* Guidatv left scroller object */
var guidatv_left = new guidatv();
guidatv_left.left = function() {};
guidatv_left.right = function() {};
guidatv_left.up = function() {
	var holder = $('.guidacarousel');
	var next_pos = this.pos_y + 1;

	if (next_pos < this.pos_y_max) {
		holder.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};
guidatv_left.down = function() {
	var holder = $('.guidacarousel');
	var next_pos = this.pos_y - 1;

	if (next_pos >= 0) {
		holder.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};

/* Guidatv blueline scroller object */
var guidatv_line = new guidatv();
guidatv_line.left = function() {};
guidatv_line.right = function() {};
guidatv_line.up = function() {
	var holder = $('.line1');
	var holder2 = $('.line2');
	var next_pos = this.pos_y + 1;

	if (next_pos < this.pos_y_max) {
		holder.animate({
			top: next_pos * this.step_y
		}, this.animation_speed, function() {
		});
		holder2.animate({
			top: next_pos * this.step_y
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};
guidatv_line.down = function() {
	var holder = $('.line1');
	var holder2 = $('.line2');
	var next_pos = this.pos_y - 1;

	if (next_pos >= 0) {
		holder.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		holder2.animate({
			top: -(next_pos * this.step_y)
		}, this.animation_speed, function() {
		});
		this.pos_y = next_pos;
	}
	this.work_over();
};
guidatv_line.debug = function(arga, argb) { alert('guida left: arga -' + arga + ', argb -' + argb ); }
/**/

function showEpisode(num) {
	$('.epiz').each(function(){
		$(this).hide();
	});

	$('#episode_' + num).show();
}

$(document).ready(function(){
	$("#cerca_trailer").autocomplete('/search-trailer-ajax/?ajax&search_trailer=true');
	$("#cerca_trailer").result(function(event, data, formatted) {
		$.ajax({
		url: '/search-trailer-ajax/?ajax&search_trailer=true&idrecord=' + data[1],
		success: function(url) {
			if (url.length > 1) {
				window.location = url;
			}
		}
		});
	});
});

 $(document).ready(function(){
	$('#usa_box').hide();
	$('#ita_box').show();
	/*$('.ita_click').click(function(e)
			{
					 e.preventDefault();
						$('#usa_box').hide();
							$('#ita_box').show();
			});
		$('.usa_click').click(function(e)
			{
				 e.preventDefault();
						$('#ita_box').hide();
							$('#usa_box').show();
			});
*/

	var startSliding = {
		currentTimeout: 0,
		
		from: function (no) {
			var currentlyAt = no,
				step_ms = 7000;
			
			var slideNext = function () {
				
				//hide all others
				$('.rightScroll li').show();
				$('.mainCover ul:first li').hide();
				
				//show current
				var nextAt = currentlyAt + 1;
				$('.rightScroll li:nth-child('+ nextAt  +')').hide();
				$( $('.mainCover ul:first li').get(currentlyAt) ).fadeIn();
				var count = $('.mainCover .rightScroll li').length;
				//increment
				currentlyAt = (currentlyAt + 1) % count;
				startSliding.currentTimeout = setTimeout(slideNext, step_ms);
			};
		
			clearTimeout( this.currentTimeout );
			slideNext();
		}
	};

	$('.rightScroll').children('li').each(function(key) {
		$(this).click(function() {
			startSliding.from(key);
		});
	});
	
	startSliding.from(0);
 });
 
 function goTo(url){
 	 window.location = url; 
 }
 
 function goToNew(url){
	 window.open(url, '_blank');
 }

 $(document).ready(function(){
 	$(".main-item").each(function() {
 		$(this).mouseover(function() {
 			$('#main-nav').children('ul').children('li').each(function(){
 				if ($(this).children('div').length != 0){
 					$(this).children('div').css('display', 'none');
 				}
 			});
 			
 			if($(this).children('div').length != 0){
 				$(this).children('div').css('display', 'block');
 				if ($("div.breakingnews-box-out").length != 0)
 					$("#main-nav").css('height', '49px');
 			}
 		})
 		$(this).mouseout(function() {
 			$(this).children('div').css('display', 'none');
 			if($("div.breakingnews-box-out").length != 0){
 				$("#main-nav").css('height', 'auto');
 			}
 		})
 	});
  });
