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 > a').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 > li > a').mouseover(function(){
		//setGossipNavigation ($(this), true);
		/* Gian wanted this */
		setGossipNavigationPermanent ($(this));
		return false;
	});
	
	$('#home_gossip_navigation > li > a').mouseout(function(){
		setGossipNavigation ($(this), false);
		return false;
	});
	
	$('#home_gossip_navigation > li > a').click(function(){
		//setGossipNavigationPermanent ($(this));
		//return false;
	});

	$('#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').click(function () {
		$('#share').toggle();
		$('#rate').hide();
		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").html(msg.cities);
					$("#selectcitta").html('');
			}
			});	
		}
		
	});	
	
	
	$("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();
	});

	
});


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();
}