$(document).ready(function(){
	// ======================================================
	//					altezza div
	// ======================================================	
	
	/*hl = parseFloat($('#content #left').height());
	hc = parseFloat($('#content #center').height());
	hr = parseFloat($('#content #right').height());
	
	var massimo = Math.max(hl,hc,hr);
	
	$('#content #left').css('height',massimo);
	$('#content #center').css('height',massimo);
	$('#content #right').css('height',massimo);*/
	
	$('#menu li.menu-current').append('<div>'+ $('#menu li.menu-current').html() +'</div>');
	$('#menu li.menu-parent').append('<div>'+ $('#menu li.menu-parent').html() +'</div>');

	// ======================================================
	//					menu content left
	// ======================================================
	
	/*$("ul.menu-0>li:has(ul)>a").click(function(){
		$('li.menu-out-0 ul.menu-1:visible').slideUp(300).removeClass('open');
		$(this).parent().find('ul.menu-1:hidden').slideDown(300).addClass('open');
		
		return false;
	});*/

	// ======================================================
	//					mostra dettagli prodotto
	// ======================================================
	
	$('.open_text').toggle(function(){
		$(this).addClass('close');
		$('.text').slideDown(300);
	},function(){
		$(this).removeClass('close');
		$('.text').slideUp(300);
	});

	// ======================================================
	//					estendi menu right
	// ======================================================
	
	$('.title_allinone.close').live('click',function(){
		$(this).removeClass('close').next('.menu_allinone').slideDown(300,function(){
			$(this).removeClass('close')
		});
	});

	$('.title_allinone:not(.close)').live('click',function(){
		$(this).addClass('close').next('.menu_allinone').slideUp(300,function(){
			$(this).addClass('close')
		});
	});
	

	// ======================================================
	//					scroll gallery
	// ======================================================
	
	var lunghezza = 0;
	$('ul.sc_menu li').each(function(){
		lunghezza = lunghezza + 78;
	});
	$('ul.sc_menu').width(lunghezza);
		
	$(function(){
		var div = $('div.sc_menu'),
		ul = $('ul.sc_menu'),
		ulPadding = 0;
		var divWidth = div.width();
		div.css({overflow: 'hidden'});
		var lastLi = ul.find('li:last-child');
		div.mousemove(function(e){
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
			var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth - 100;
			div.scrollLeft(left);
		});
	});
	
	// ======================================================
	//					navigazione scheda prod
	// ======================================================
	
	$('#schede .scheda .testo a').click(function(){
		$(this).parent().parent().triggerHandler('click');
		return false;
	});
	$('.scheda').click(function(){
	
		var url = $("input[name='url']").val();
		var section_id = $("input[name='section_id']").val();
		var scheda_id = $(this).attr('rel');
				
		$('#componenti').load(url + 'view_componente.php',{'section_id':section_id,'scheda_id':scheda_id},function(){
			$('#prodotto').slideUp(400);
			$(this).slideDown(400);
		});
	});
	
	$('#componenti .back').live('click',function(){
		$('#prodotto').slideDown(400);
		$('#componenti').slideUp(400,function() {
			$(this).html("");
		});
	});
	
	$('span.espandi_cod:not(.close)').live('click',function(){
		$(this).addClass('close').parent().find('.hide_cod').slideDown(300);
	});
	$('span.espandi_cod.close').live('click',function(){
		$(this).removeClass('close').parent().find('.hide_cod').slideUp(300);
	});
	
	// zoom su img prodotti
	

	$(".zoom_img").live('click mouseleave', function(event) {
		if (event.type == 'click') {
			div = $(this);
			width = $(this).find('img').width();
			height = $(this).find('img').height();
			$(this).not('.open').addClass('open').find('img').clone().addClass("hover").removeClass('cursor').appendTo(div).stop().css({
				marginTop: '-'+height+'px',
				marginLeft: '-'+width+'px',
				top: '50%',
				left: '50%'
			}).fadeIn(300).parent().parent().addClass('top');
		} else {
			if ($(this).hasClass('open')) {
				$(this).removeClass('open').find('img.hover').fadeOut(300,function(){
					$(this).remove();
				}).parent().parent().removeClass('top');
			}
		}	
	});
	
	$('img.hover').live('click',function(){
		$(this).fadeOut(300,function(){
			$(this).remove().parent().removeClass('open');
		}).parent().parent().removeClass('top');
	});
	
	// Login Iges
	
	$('.ico_iges.private').live('click',function() {
		show_popup_login();
	});
		
	$('#login_from_popup').live('submit',function() {
		//clear_error();
		/*val = serialize_form('#login_from_popup');	
		ajax = call_ajax(val);
		
		if (ajax.report) show_response(ajax.msg.send);
		else report_errori(ajax.msg);
		
		return false;*/
	});
	
});


// ======================================================================
// Show popup login

function show_popup_login() {
	
	$popup = $('<div id="popup">').addClass('login');			
	$dark = $('<div id="dark">');	
	$('body').prepend($popup).prepend($dark);		
	var windowHeight = $(window).height();		
	var windowWidth = $(window).width();	
	$dark.css({
		'width': windowWidth,
		'height': windowHeight		
	}).fadeTo(400,0.8);
	
	$close=$('<button>').addClass('close');
	$popup.load('http://www.italianaferramenta.it/more/login.php',function() {
	
		var popupHeight = $popup.height();
		var popupWidth = $popup.width();
		
		$popup.css({
			'margin-top': -popupHeight/2,
			'margin-left': -popupWidth/2		
		}).fadeIn(400).append($close);
		
	});
	
	$('#popup .close').live('click',function() {
		close_popup();	
	});
}

function close_popup() {
	
	if ($('input[name="non_richiedere"]').is(':checked')) $.cookie("termoisolanti_newsletter", 1, {expires: 30});
	
	$popup = $('#popup');			
	$dark = $('#dark');
	
	$dark.fadeOut(200,function() {
		$(this).remove();
	});	
	$popup.fadeOut(200,function() {
		$(this).remove();
	});
	
	if ($popup.find('iframe').contents().find('input[name="reload"]').val() == 'true') location.reload();
}

// ======================================================================
// Funzione serializza form

function serialize_form(form) {
	var array = $(form).serializeArray();
	return serialize_object(array);
}

// ======================================================================
// Funzione serializza array

function serialize_object(array) {
	var object = {};
	$.each(array, function() {
		if (object[this.name]) {
			if (!object[this.name].push) {
				object[this.name] = [object[this.name]];
			}
			object[this.name].push(this.value || '');
		} else {
			object[this.name] = this.value || '';
		}
	});
	return object;
}

// ======================================================================
// Funzione ajax

function call_ajax(val,url) {

	var val = {"val":val};
	
	console.log(val + url);
	return;
	
	var response = $.ajax({
		type: "POST",
		url: url,	
		data: val,
		dataType: 'json',
		async: false,
		beforeSend: function() {
			$('#loader').show();
		},
		success: function(data){
			$('#loader').hide();
		}
	}).responseText;
	
	return $.parseJSON(response);
}

