var options = {};
var Images = ['img/loading.gif','img/sfumatura_header.png','img/header/home.jpg'];
var Length = Images.length;
var bgPosition;
var smaller = 0;
var pagenow = '';

function initMenu() {
	$('#nav ul').hide();
	$('#nav li a').click(
		function() {
			var checkElement = $(this).next();
			var selectElement = $(this).next();
			var thisElement = $(this).attr('id')+'Ul';
			
			if( $(this).attr('class') == 'nav' ) {
				$('.nav').css('background-image', 'url(img/menu/blank.gif)');
				$(this).css('background-image', 'url(img/menu/bg.png)');
				$(this).css('background-repeat', 'no-repeat'); 
				$(this).css('background-position', 'center top'); 
			}

			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				$('.nav').css('background-image', 'url(img/menu/blank.gif)');
				$('#'+thisElement+' li a').first().css('background-image', 'url(img/menu/bg.png)');
				$('#'+thisElement+' li a').first().css('background-repeat', 'no-repeat'); 
				$('#'+thisElement+' li a').first().css('background-position', 'center top'); 
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('.nav').css('background-image', 'url(img/menu/blank.gif)');
				$('#nav ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				$('#'+thisElement+' li a').first().css('background-image', 'url(img/menu/bg.png)');
				$('#'+thisElement+' li a').first().css('background-repeat', 'no-repeat'); 
				$('#'+thisElement+' li a').first().css('background-position', 'center top'); 
			}
		return false;
	});
	$('#sections li a').click(
		function() {
		$('#nav ul').slideUp('normal');
		$('.nav').css('background-image', 'url(img/menu/blank.gif)');
		$(this).css('background-image', 'url(img/menu/_bg.png)');
		$(this).css('background-repeat', 'no-repeat'); 
		$(this).css('background-position', 'center top'); 
		return false;
	});
}
function allblack(url) {
	$('#allblack div').width($(window).width() - ($(window).width()/5));
	$('#allblack div').height($(window).height() - ($(window).height()/5));
	$('#allblack').show();
	$('#allblack div').load(url);
	$('#allblack').click(function() {
		$('#allblack').hide();
	});
}

function goLink(where){
	form_open = 0
	if ( where == 'visita' || where == 'corporate' ) {
		$('#nav ul').slideUp('normal');
		$('.nav').css('background-image', 'url(img/menu/blank.gif)');
	}
	
	if (where == 'contattix') {
		where = 'contatti';
		$('#nav ul').slideUp('normal');
		$('.nav').css('background-image', 'url(img/menu/blank.gif)');
		$('#contatti').css('background-image', 'url(img/menu/_bg.png)');
		$('#contatti').css('background-repeat', 'no-repeat'); 
		$('#contatti').css('background-position', 'center top'); 
	}
	
	
	window.location.hash = '#pagina_'+where;
	pagenow = 'pagina_'+where;
	
	if (smaller == 1) {
		$('#header').animate({ height: '470px' });
		$('#top_page').animate({ height: '368px', 'padding-top':'119px' });
		smaller = 0;
	}
	
	$('.header').fadeTo(500,0.3, function () {
	
	$('#main').fadeOut(500);
		$(new Image()).load(function() {
			$('.header').fadeTo(800,0, function () {
				$('#main').load('_'+where+'.php', function () {
					$('#main').fadeIn(200, function () {
						$('#header').load('header.php?img='+where, function() {
							$('.header').fadeIn(900);
						});
					});
				});
			});
		}).attr('src','img/header/'+where+'.jpg');
	});
}

function goNews(where,id){
	$('.header').fadeTo(500,0.3, function () {
	$('#main').fadeOut(500);
		$(new Image()).load(function() {
			$('.header').fadeTo(800,0, function () {
				$('#main').load('_'+where+'.php?id='+id, function () {
					$('#main').fadeIn(200, function () {
						$('#header').load('header.php?img='+where, function() {
							$('.header').fadeIn(900);
							$('#header').animate({ height: '150px' });
							$('#top_page').animate({ height: '150px', 'padding-top':'0px' });
							smaller = 1;
						});
					});
				});
			});
		}).attr('src','img/header/'+where+'.jpg');
	});
}
function goxNews(where,id){
	$('#main').fadeOut(0);
	$('#main').load('_'+where+'.php?page='+id, function () {
				$('#main').fadeIn(200, function () {
				$('#header').load('header.php?img='+where, function() {
				$('.header').fadeIn(900);
			});
		});
	});
}


function changePhoto(id){
	$('#photos').fadeTo(500,0.3, function () {
		$(new Image()).load(function() {
			$('#photos').fadeTo(800,0, function () {
				$('#photos').attr('src','img/'+id+'.jpg').fadeTo(500,1);
			});
		}).attr('src','img/'+id+'.jpg');
	});
}

function Photo(){
	//$('.page_img').fadeOut(500);
	$('#photo').toggle();
	$('#photothumb').toggle();
	if ( $('#video').is(':visible')) {
		$('#video').toggle();
	}
}
function Video(){
	$('#video').toggle();
	//$('.page_img').fadeIn(1000);
	//$('#photo').toggle();
	//$('#photothumb').toggle();
}

$(window).resize(function() {
	bgPosition = (1730 - $('#header').width()) / 2;
	$('.header').css('background-repeat', 'no-repeat'); 
	$('.header').css('background-position', '-'+bgPosition+'px center'); 
});


var form_open = 0;
function openForm(){
	if (form_open==0) {
		$('#form_slide').show('blind',options,500);
		form_open = 1;
	}
}

function send_residenze() {
	$('#button').hide();
	$.post("lib/residenze.php", { email: $('#email').val(), email2: $('#email2').val(), nome: $('#nome').val(), cognome: $('#cognome').val(), privacyok: $('#privacyok').is(':checked'), affitto: $('#affitto').is(':checked'), acquisto: $('#acquisto').is(':checked') },
		function(data){
			if (data == 'true') {
				$('#titinfo').hide();
				$('#form_slide').html('Richiesta inviata correttamente,<br /> ricever&agrave; un\'email contenente maggiori informazioni.');
			}else{
				alert(data);
				$('#button').show();
			}
	});
}

function send_visita() {
	$('#button').hide();
	$.post("lib/visite.php", { email: $('#email').val(), email2: $('#email2').val(), nome: $('#nome').val(), cognome: $('#cognome').val(), privacyok: $('#privacyok').is(':checked'), visita: $('#visita').is(':checked'), degustazione: $('#degustazione').is(':checked'), telefono: $('#telefono').val(), citta: $('#citta').val(), data_visita: $('#data_visita').val(), visitatori: $('#visitatori').val() },
		function(data){
			if (data == 'true') {
				$('#r_share').html('Richiesta inviata correttamente,<br /> verr&agrave; ricontattato tramite email da un nostro responsabile.');
			}else{
				alert(data);
				$('#button').show();
			}
	});
}



jQuery(document).ready(function() {
								
    $.history.init(function(hash){
        if(hash == "") {
        } else {
            if (hash != pagenow) {
				goLink(hash.replace('pagina_', ''));
				window.location = hash.replace('pagina_', '') +'.php';
			}
        }
    },
    { unescape: ",/" });

});



