function moveObj(obj) {
	var id = $(obj).attr('class');
	$('.menuarea li').removeClass('current');
	$(obj).parent('li').addClass('current');
	$('#external').scrollTo($('#'+id), 800, {axis:'xy'});
};

function fadeImg(obj) {
	var imgSrc = $(obj).attr('src').replace('_tmb','');
	var imgAlt = $(obj).attr('alt');
	var tar = $(obj).parents('.preview').next().find('div.loader').addClass('loading').html('');
	$(document).find('.desc select').remove(); // fix x topcoat
	var imgObj = new Image();
	$(imgObj).load(function() {
			$(this).hide();
			tar.removeClass('loading').append(this);
			$(this).fadeIn(600);
	}).error(function(){ $(this).attr('src', '/placeholder.jpg'); }).attr('src', imgSrc).attr('alt', imgAlt);
	
	$(obj).parents('.preview').next().find('.desc').fadeOut(300,
			function(){
				$(this).html($(obj).next().html()).slideDown(1000);
			}
	);
};

function togglemenu(obj) {
	$('#menulist > li > a.current').removeClass('current');
	$(obj).addClass('current');
	$('#menulist li ul').each(function(){
		$(this).slideUp(200).prev().attr('title', $('#menulist').attr('show'));
	});
	var list = $(obj).next();
	if (list.css('display')=='none') {
			list.slideDown(400);
			$(obj).attr('title', $('#menulist').attr('hide'));
	} else {
			list.slideUp(400);
			$(obj).attr('title', $('#menulist').attr('show'));
	}
	return false;
};

/**
 * Gestione upload file excel per caricare i prodotti
 */
function uploadFileExcel(tform, turi) {
	$.ajaxUpload({
		uploadform: tform,
		url: turi,
		secureuri: false,
		dataType: 'xml',
		success: function(xml) {
			alert($("message", xml).text());
			window.location.reload();
		},
		error: function (msg, status, e) {
			alert(e);
		}
	})
};

function view(data) {
	$('#TB_ajaxContent').html(data);
};

function loginView() {
	if ($('#TB_ajaxContent').length) {
		$('#TB_ajaxContent').load('action-ecommerce?action=login-view&fix='+parseInt(Math.random()*99999));
	} else {
		$('#popupwindow').load('action-ecommerce?action=login-view&fix='+parseInt(Math.random()*99999), function() {
			tb_show('CARRELLO','#TB_inline?height=500&width=805&inlineId=popupwindow',false);
		});		
	}
};

function disconnect() {
	$('#TB_ajaxContent').load('action-ecommerce?action=login-disconnect&fix='+parseInt(Math.random()*99999));
};

function basketView() {
	if ($('#TB_ajaxContent').length) {
		$('#TB_ajaxContent').load('action-ecommerce?action=basket-view&fix='+parseInt(Math.random()*99999));
	} else {
		$('#popupwindow').load('action-ecommerce?action=basket-view&fix='+parseInt(Math.random()*99999), function() {
			tb_show('CARRELLO','#TB_inline?height=500&width=805&inlineId=popupwindow',false);
		});
	}
};

function basketAdd(code, catalog) {
	if (catalog == 'sagni')
		code = code + '-' + $(document).find('.full select').val(); // adding topcoat
	if ($('#TB_ajaxContent').length) {
		$('#TB_ajaxContent').load('action-ecommerce?action=basket-add&catalog=' + catalog + '&code=' + code + '&fix=' + parseInt(Math.random()*99999));
	} else {
		$('#popupwindow').load('action-ecommerce?action=basket-add&catalog=' + catalog + '&code=' + code + '&fix=' + parseInt(Math.random()*99999), function() {
			tb_show('CARRELLO','#TB_inline?height=500&width=805&inlineId=popupwindow',false);
		});
	}
};

function basketUpdate(code) {
	$('#TB_ajaxContent').load('action-ecommerce?action=basket-update&code=' + code + '&quantity=' + $('#p'+code).val() + '&fix=' + parseInt(Math.random()*99999));
};

function basketRemove(code) {
	$('#TB_ajaxContent').load('action-ecommerce?action=basket-remove&code=' + code + '&fix=' + parseInt(Math.random()*99999));
};

function paymentView() {
	$('#TB_ajaxContent').load('action-ecommerce?action=payment-view&fix=' + parseInt(Math.random()*99999));
};

function deliveryView() {
	$('#TB_ajaxContent').load('action-ecommerce?action=delivery-view&fix=' + parseInt(Math.random()*99999));
};

function summaryView() {
	$('#TB_ajaxContent').load('action-ecommerce?action=summary-view&fix=' + parseInt(Math.random()*99999));
};

function summaryConfirm() {
	$('#TB_ajaxContent').load('action-ecommerce?action=summary-confirm&fix=' + parseInt(Math.random()*99999));
};

function countryManager() {
	if ($('#country').val()=='US') {
		$('#province').each(function(){this.disabled='disabled';}).parent().slideUp();
		$('#USState').each(function(){this.removeAttribute('disabled');}).parent().slideDown('fast', function(){this.removeAttribute('style');});
	} else {
		$('#USState').each(function(){this.disabled='disabled';}).parent().slideUp();
		$('#province').val('').each(function(){this.removeAttribute('disabled');}).parent().slideDown('fast', function(){this.removeAttribute('style');});
	}
};

function orderlist() {
	$('#TB_ajaxContent').load('action-ecommerce?action=order-list&fix=' + parseInt(Math.random()*99999));
};

function orderView(num) {
	$('#TB_ajaxContent').load('action-ecommerce?action=order-view&num=' + num + '&fix=' + parseInt(Math.random()*99999));
};

function mngDeliveryForm(deliveryMode) {
	$("form[id=asynch] :input[name!=deliveryMode]").not(':hidden').each(function(){
		if (deliveryMode.value=='ex-works') {
			this.disabled = 'disabled';
		} else {
			this.removeAttribute('disabled');
		}
	});
};

function fixedMenu(def, paddingTop) {
	var nav = $(def);
	// forse potrebbe essere utile
	//nav.css('left', nav.offset().left + 'px');
	nav.css({'position': 'absolute'});
	var topmost_point = nav.offset().top;
	var REAL_TOP = topmost_point - paddingTop;
	var the_window = $(window);
	var NAV_IS_FIXED = (nav.css('position') == 'fixed');
	the_window.scroll(function () {
        if (the_window.scrollTop() > REAL_TOP) {
            if ($.browser.msie && $.browser.version == "6.0") {
                nav.css('top', the_window.scrollTop() + paddingTop);
            } else if (!NAV_IS_FIXED) {
        	    nav.css({
        	        top: paddingTop,
        	        position: 'fixed'
        	    });
        	    NAV_IS_FIXED = true;
        	}
        } else {
            if (NAV_IS_FIXED) {
                nav.css({
                    position: 'absolute',
                    top: topmost_point
                });
                NAV_IS_FIXED = false;
            }
        }
	});
};

$(document).ready(function() {
	
	// select default menu group
	var catalogo = $('#menulist ul');
	if (catalogo.length && location.hash.length>1) {
		catalogo.find('li.current').removeClass('current');
		catalogo.find('.l' + location.hash.substring(1)).parent().addClass('current');
		var curr = catalogo.find('li.current a');
		// carico prima le immagini della sezione corrente
		$('#' + curr.attr('class') + ' img').each(function(){
			$(this).attr('src',$(this).attr('title'));
		});
		// delay per ie7
		window.setTimeout(function(){
			moveObj(curr[0]);
		}, 2000);
	}
	catalogo.find('li.current').parent().show().prev().each(function(){ $(this).attr('title', $('#menulist').attr('hide')) });
	
	// carico tutte le altre immagini
	$('#external img').each(function(){
		$(this).attr('src', $(this).attr('title')).removeAttr('title');
	});

	// sagni: ahover sulle thumbnails
	$('ul.preview li').each(function(){$(this).ahover();});
	
	// luxury outlet e international gift: zoom photo
	$('#product-list .photo a').lightBox();
	
	// luxury outlet e international gift: edit
	$('#category-it').bind('change', function(){
		$('#category-en')[0].selectedIndex = this.selectedIndex;
	});

	$('#category-en').bind('change', function(){
		$('#category-it')[0].selectedIndex = this.selectedIndex;
	});
	
	$(':input[name^=category-promo-]').click(function(){
		var t = $(':input[name^=category-promo-]').length;
		var p = $(':input[name^=category-promo-]').index(this);
		var o = (p<t/2)?t/2+p:p-t/2;
		if (this.checked) {
			$(':input[name^=category-promo-]:eq('+o+')').attr('checked','checked');
		} else {
			$(':input[name^=category-promo-]:eq('+o+')').removeAttr('checked');
		}
	});
	
	// international gift: public menus
	if ($('meta[name=copyright]').attr('content').indexOf('INTERNATIONAL GIFT')>-1) {

		// ombra on top
		$('#main').before("<div id='top'></div>");
				
		// ie navigation-menu fix
		if ($.browser.msie) {
			$(window).resize(function(){
				$('#navigation-bar').css('left', $('#container').offset().left).show();
			}).resize();
		}
		
		// fixed menus over page scroll
		if ($.browser.version!='6.0')
			fixedMenu('#navigation-bar, .istit', -200);
		
		// catalog menu
		if ($('#intern').length) {
			$('#navigation-bar .box li:gt(0)').hide();
			$('#navigation-bar .box li:eq(0) a').css('width', '200px');
			var notes = (location.pathname.indexOf('/en/')>-1)?"<div id='notes'>The prices do not include VAT and transport costs</div>":"<div id='notes'>I prezzi sono al netto dell' IVA e delle spese di trasporto</div>";
			$('#navigation-bar .box').append($('#menulist')).append(notes);
			$('.menuarea').hide();
		}
		
		// catalog scroll
		$('#product-list .scroll').each(function() {
			if($(this).children().size() > 4) {
				$(this).css('overflow-y', 'scroll');
			}
		});
	}

});