$(document).ready(function(){
	//	menu a sinistra
	$('#left_menu').children('li').each(function(i){
		var $this = $(this),
			o = $this.offset();
		$this.children('ul').hide().css({
			position: 'absolute',
			top: (18 * i) + 'px',
			left: '145px'
		});
	}).hover(function(){
		$(this).css('backgroundColor', '#f6ff00').children('ul').show();
	}, function(){
		$(this).css('backgroundColor', '#fff').children('ul').hide();
	});
	
	//	 share this
	var $st = $('#share_this');
	if ($st.length > 0) {
		var element = document.getElementById('share_this');
		var object = SHARETHIS.addEntry({title:'share',	summary: 'Sharing is good for the soul.'}, {button:false});
		object.attachButton(element);
		$st.click(function(){if (jQuery.browser.mozilla) $(this).blur();});
	}
	
	//	product thumbnails
	$('#product_thumbnails img:first').css('opacity', '0.3');
	$('#product_thumbnails img').click(function(){
		$('#product_thumbnails img').css('opacity', '1');
		var $ig = $('#immagine_grande'),
			$this = $(this),
			t = $this.attr('src').split('/').slice(-1),
			n = $ig.attr('src').split('/').slice(-1),
			d = $this.next('.didascalia_imm_h').html();
		$ig.attr('src', '/catalog/original/' + t);
		$this.css('opacity', '0.3');
		$('#dida').html(d);
	});
	$('#immagine_grande_cont').hover(function(){
		var dida = $('#dida'),
			h = + $('#dida').outerHeight();
		dida.css({top: (450 - h).toString() + 'px'});
	}, function(){
		var dida = $('#dida');
		dida.css({top:'450px'});
	});
	
	
	//	prodotto select size & color
	var $selects = $('#sel_size, #sel_color'),
		$avail = $('#avail'),
		$noavail = $('#no_avail');
	$selects.change(function(){
		if ($selects.length == 2) {
			var type = this.id.split('_')[1],
				other = type == 'color' ? 'size' : 'color',
				o = {},
				$this = $(this),
				$other = $('#sel_' + other);
			o.pid = $('#product_id').val();
			if (type == 'color') o.col = this.value;
				else o.sz = this.value;
			if (this.value != '') {
				$.post('/js/ajax/get_product_' + other + 'list.php', o, function(data){
					if (data.length > 0) {
						if ($other.val() != '' && $.inArray($other.val(), data) == -1) {
							$avail.hide();
							$noavail.show();
						} else if (($other.val() != '' && $.inArray($other.val(), data) != -1) || ($other.val() == '') ) {
							$avail.show();
							$noavail.hide();
						}
					} else {
						$avail.hide();
						$noavail.show();
					}
				}, 'json');
			}
		} else if ($selects.length == 1) {
			var type = this.id.split('_')[1],
				o = {};
			o.pid = $('#product_id').val();
			o.type = type;
			o.dato = this.value;
			if (this.value != '') {
				$.post('/js/ajax/get_product_avail.php', o, function(data){
					var num = parseInt(data);
					if (num > 0) {
						$avail.show();
						$noavail.hide();
					} else {
						$avail.hide();
						$noavail.show();
					}
				});
			}
		}
	});
	
	//	prodotto BUY
	$('#form_carrello').submit(function(e){
		e.preventDefault();
		var $sels = $('#sel_size'),
			$selc = $('#sel_color');
		if ( ($sels.length == 1 && $sels.val() == '') || ($selc.length == 1 && $selc.val() == '') ) {
			var $pdc = $('#product_detail_container'),
				wi = $pdc.width(),
				he = $pdc.height(),
				o = $pdc.offset();
			$('<div id="oscure2"></div>').css({
				position: 'absolute',
				top: o.top,
				left: o.left,
				height: he + 'px',
				width: wi + 'px',
				'backgroundColor': '#000',
				display: 'none',
				opacity: '0.6'
			}).appendTo('body');
			$('<div id="alert2"><div id="head">Hanazuki.com</div><div id="close">x</div><div id="testo">Please select size and color.</div><div id="ok">Ok</div></div>').appendTo('body');
			var $al = $('#alert2'),
				$alH = $al.height();
			$al.css({"position": "absolute", "top": (o.top + he/2 - $alH/2) + "px", "left": (o.left + wi/2 - 125) + "px", 'opacity': '1.0', "display": "none"});
			$("#oscure2,#alert2").fadeIn(150);
			$('#close, #ok', '#alert2').click(function(){
				$('#oscure2,#alert2').remove();
			});
			return false;
		} else $(this).unbind().submit();
	});
	
	/*/	checkout
	$('#world, #world_l, #europe, #europe_l').hide();
	$('#da_nazione').change(function(){
		var n = $(this).val(),
			o = {};
		o.peso = $('#cart_peso').val();
		o.nazione = n;
		if (n != '') {
			$.post('/js/ajax/get_shipping.php', o, function(data){
				$('#sh_span').text(data);
				var ship = parseFloat(data),
					gt = parseFloat($('#cart_tot').text());
				gt += ship;
				gt = gt.toString();
				if (gt.length == 2) gt += '.00';
				if (gt.split('.')[1].length == 1) gt += '0';
				$('#grand_tot').text(gt);
			});
		} else {
			$('#sh_span').text('_.__');
			$('#grand_tot').text($('#cart_tot').text());
		}
		if (n == 'mondo') {
			$('#europe, #europe_l').hide();
			$('#world, #world_l').show();
		} else if (n == 'europa') {
			$('#world, #world_l').hide();
			$('#europe, #europe_l').show();
		} else if (n == 'olanda' || n == '') {
			$('#world, #world_l, #europe, #europe_l').hide();
		}
	});*/
	
	//	checkout - modified by mary @date 12/07/2009
	$('#nl, #eu01, #eu02, #noneu01, #noneu02').hide();
	$('#da_nazione').change(function(){
		var n = $(this).val(),
			o = {};
		o.peso = $('#cart_peso').val();
		o.nazione = n;
		if (n != '') {
			$.post('/js/ajax/get_shipping.php', o, function(data){
				$('#sh_span').text(data);
				var ship = parseFloat(data),
					gt = parseFloat($('#cart_tot').text());
				gt = gt + ship;
				gt = gt.toString();
				if (gt.indexOf('.') == -1) gt += '.00';
				var decimali = gt.split('.')[1];
				if (decimali.length == 1) gt += '0';
				else if (decimali.length > 2) {
					var secondaCifra = decimali[1],
						numero = parseInt(secondaCifra) + 1;
					decimali = decimali[0] + numero.toString();
					gt = gt.split('.')[0] + '.' + decimali;
				}
				$('#grand_tot').text(gt);
			});
		} else {
			$('#sh_span').text('_.__');
			$('#grand_tot').text($('#cart_tot').text());
		}
		if (n == 'nl' || n == '') {
			$('#eu01, #eu02, #noneu01, #noneu02').hide();
			$('#nl').show();
		} else if (n == 'eu01') {
			$('#nl, #eu02, #noneu01, #noneu02').hide();
			$('#eu01').show();
		} else if (n == 'eu02') {
			$('#nl, #eu01, #noneu01, #noneu02').hide();
			$('#eu02').show();
		} else if (n == 'noneu01') {
			$('#nl, #eu01, #eu02, #noneu02').hide();
			$('#noneu01').show();
		} else if (n == 'noneu02') {
			$('#nl, #eu01, #eu02, #noneu01').hide();
			$('#noneu02').show();
		}
	});
	
	$('#form_confirm').submit(function(e){
		e.preventDefault();
		if ( $('#da_nome').val() == '' || $('#da_indirizzo').val() == '' || $('#da_citta').val() == '' || $('#da_cap').val() == '' || $('#da_nazione').val() == '' || $('#da_email').val() == '' || $('#da_tel').val() == '') {
			var $pdc = $('#middle_cart'),
				wi = $pdc.outerWidth(),
				he = $pdc.outerHeight(),
				o = $pdc.offset();
			$('<div id="oscure2"></div>').css({
				position: 'absolute',
				top: o.top,
				left: o.left,
				height: he + 'px',
				width: wi + 'px',
				'backgroundColor': '#000',
				display: 'none',
				opacity: '0.6'
			}).appendTo('body');
			$('<div id="alert2"><div id="head">Hanazuki.com</div><div id="close">x</div><div id="testo">Please complete the form with all required data.</div><div id="ok">Ok</div></div>').appendTo('body');
			var $al = $('#alert2'),
				$alH = $al.height();
			$al.css({"position": "absolute", "top": (o.top + he/2 - $alH/2) + "px", "left": (o.left + wi/2 - 125) + "px", 'opacity': '1.0', "display": "none"});
			$("#oscure2,#alert2").fadeIn(150);
			$('#close, #ok', '#alert2').click(function(){
				$('#oscure2,#alert2').remove();
			});
			return false;
		} else $(this).unbind().submit();
	});
});
