var ie6=null;
var lt_h = 1510; //layout background height
var lt_w = 1950; //layout background width

var closelabel = { it: 'Chiudi', de: 'Schließen', en: 'Close', es: 'Cerrar', fr: 'Fermer'}


$(document).ready(function() {
	((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? (ie6=true) : (ie6=false);
	setLayout();
	
	$(window).resize(function(){
		setLayout()	
	});
});



var prods = {
	gender: null,
	dataset: null,
	curPage: null,
	curItem: null,
	elstoshow: 6,
	counter: 0,
	timer: 200,
	setGender: function() {
		xmlfile = 'xml/'+prods.gender+'/'+prods.gender+'_'+lang+'.xml';
		prods.thumbpath = 'img/products/'+prods.gender+'/thumbs/';
		prods.imgpath = 'img/products/'+prods.gender+'/big/';
		loadXML(xmlfile);
	},
	changeGender: function() {
		$('#images li, #thumbshadow, #info, #total, #selected, #product img, #storelink, #baloon').fadeOut('fast');
		$('#links span').remove();
		prods.elstoshow = 6;
		prods.counter = 0;
		prods.stopBrowse();
		setTimeout( "prods.setGender()", 800);
	},
	init: function(firstinit){
		var $imgul=$('#images');
		var $linkul=$('#links');
		$imgul.html('');
		var num=0;
		prods.dataset.each(function(i){
			if(i<prods.elstoshow&&i>prods.counter-1){
				$imgul.append('<li class="prod'+(num+1)+'" id="img'+i+'">');
				var thumbP=new Image();
				$('#img'+i).append((thumbP));
				$(thumbP).css({display: 'none'});	
				$(thumbP).attr({src:prods.thumbpath+$(this).attr('img')});
				num++;
				}
			});
		prods.showProds();
		if(firstinit) {
			setTimeout( "prods.openPage()", prods.timer*6 );
		}
		$('#total').text(prods.dataset.length);
			
		//debug
		$('#elstoshow').text(prods.elstoshow);
		$('#counter').text(prods.counter);
		},
	openPage: function() {
		$('#lft, #rght, #selected, #total, #product').fadeIn('slow');
	},
	removeProds: function(){
		prods.stopBrowse();
		$('#thumbshadow, #baloon').fadeOut('fast');
		$('#links span').remove();
		$('#links a').removeClass('selected');
		$('#images li').each(function(i) {
			setTimeout( "$('#images li img').eq("+i+").fadeOut('fast')", prods.timer*i);
			if(i == $('#images li').length - 1) {
				p = setTimeout( "prods.init()", prods.timer*i );
			}
		});
	},
	showProds: function(){
		$('#images li').each(function(i) {
			setTimeout( "$('#images li img').eq("+i+").fadeIn('fast')", prods.timer*i);
			if(i == $('#images li').length - 1) {
				setTimeout( "$('#thumbshadow').fadeIn('slow')", prods.timer*i );
				setTimeout( "prods.loadProduct("+prods.counter+", true)", prods.timer*i );
			}
			});
	},
	listen: function() {
		//listen to clicks on thumbails
		$('#links a')
			.each(function() {
				$(this)
					.click(function(e) {
						if($(this).hasClass('selected')) return;
						$('#links a').removeClass('selected');
						$(this).addClass('selected');
						e.preventDefault(e);
						var listItem = $('#links a');
						var selected = ($(listItem).index($(this))) + prods.counter;
						$('#clicked').text(selected);
						
						$('#links span').remove();
						$(this).append('<span>');
						
						prods.loadProduct(selected);
					})
					.hover(function() {
						var itemClass = $(this).parent().attr('class');
						$('#images li.'+itemClass).prepend('<p>');
					}, function() {
						var itemClass = $(this).parent().attr('class');
						$('#images li.'+itemClass+' p').remove();
					});
				});
		
		//listens to click on arrows		
		$('#rght').click(function(){
			if(prods.elstoshow>=prods.dataset.length) return;
			prods.counter=prods.counter+6;
			prods.elstoshow=prods.elstoshow+6;
			prods.removeProds();
		});
		$('#lft').click(function(){
			if(prods.counter<=0) return;
			prods.counter=prods.counter-6;
			prods.elstoshow=prods.elstoshow-6;
			prods.removeProds();
		});
		
		$('#info').click(function() {
			$('#baloon').fadeIn('fast');
		});
	},
	stopBrowse: function() {
		$('#lft, #rght, #links a, #info').unbind();
	},
	loadProduct: function(num, firstItem) {
		
		//hide current item info
		$('#storelink, #info, #baloon').fadeOut('fast');
		
		var item = prods.dataset.eq(num);
		var src = item.attr('img');
		var href = item.attr('href');
		var name = $(item).find('productName').text();
		var description = $(item).find('productDesc').text();
		
		$('#productCode').fadeOut('slow');
		
		$('#product img').fadeOut(500, function() {
			$(this).remove();
			
			var pic = new Image();
			
			$(pic).load(function(){
				//append and show item image
				$('#product')
					.append(pic)
					.find('img')
					.wrap('<a href="'+href+'" target="_blank" />')
					.fadeIn(500, function() {
						if(firstItem) {
							prods.listen(); //reactivate browsing
							
							$('#links a').eq(0)
								.addClass('selected')
								.append('<span>');
							}
					});
					
				//show item info
				$('#storelink')
					.attr('href', href)
					.fadeIn('fast');
					
				if(name.length > 0) {
					infohtml = '<h2>'+name+'</h2>';
					infohtml += '<p>'+description+'</p>';
					infohtml += '<a id="closebaloon" href="javascript:prods.closeBaloon()">' + closelabel[lang] + '</a>';
					$('#baloon').html(infohtml);
					
					$('#info').fadeIn('fast');
				}
				
				//update counter
				$('#selected').text(num+1);
					
				});
			
			$(pic).attr({src: prods.imgpath + src});
			$(pic).css({height: h_wp, width: (494*h_wp)/914});
		});
	},
	closeBaloon: function() {
		$('#baloon').fadeOut('fast');
	}
}


function setLayout(){
	h_wp=$(window).height()-29;
	w_wp=$(window).width();
	m_top = ((lt_h - h_wp) / 2);
	m_left = ((lt_w - w_wp) / 2);

	$('#layout').height(h_wp);
	$('.bg').css({top: -m_top, left: -m_left });
	$('#shape1').css({top: 813 - m_top, left: 492 - m_left });
	$('#shape2').css({top: 804 - m_top, left: 800 - m_left });
	$('#shape3').css({top: 627 - m_top, left: -m_left });
	$('#shape4').css({top: 465 - m_top, left: 931 - m_left });
	$('#ct_shape1').css({top: 587 - m_top, left: 448 - m_left });
	$('#headline').css({top: 542 - m_top, left: 569 - m_left });
	$('#ct_headline').css({top: 481 - m_top, left: 444 - m_left });
	$('#ct_shape2').css({top: 749 - m_top, left: -m_left });
	$('#prods').css({top: 743 - m_top, left: 564 - m_left });
	$('#product').css({left: 1023 - m_left, width: (494*h_wp)/914 });
	$('#product img').css({height: h_wp, width: (494*h_wp)/914});
	$('#menu_hp').css({top: 852 - m_top, left: 1060 - m_left });
	$('#menu_cat').css({top: 718 - m_top, left: 451 - m_left });
	$('#footer').css({top:h_wp, visibility:'visible'});
};

function listenHp() {
	$('#menu_hp a')
		.unbind()
		.click(function() {
			prods.gender = $(this).attr('rel');
			$('#menu_cat a.'+prods.gender).parent().addClass('active');
			$('#hp').fadeOut('slow', function() {
				$('#hp > *').hide();
				$('#hp').show();
				startCatalogue();
			});
		});
}

function listenCatalogue() {
	$('#menu_cat a').click(function() {
		if($(this).parent().hasClass('active')) return;
		
		prods.gender = $(this).attr('rel');
		$('#menu_cat li').removeClass('active');
		$(this).parent().addClass('active');
		prods.changeGender();
	});
	
	$('#ct_headline').click(function() {
		prods.stopBrowse();
		$('#cat').fadeOut('slow', function() {
			$('#menu_cat li').removeClass('active');
			$('#links span').remove();
			$('#cat > *, #thumbshadow, #info, #total, #selected, #product, #storelink, #baloon').hide();
			$('#cat').show();
			startHp();
		});
	});
}



function MM_showHideLayers() { 
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}


function loadXML(xmlfile) {
	$.ajax({
        type: "GET",
        url: xmlfile,
        dataType: "xml",
        success: function(xmlData) {
			prods.dataset =  $("item", xmlData);
			},
		complete : function(xmlData) {
			prods.init(true);
			},
		error:function(){
			alert('Error loading xml data. Please, reload this page and try again.')
			}	
	});
}

function startHp() {
	$('#bg_hp').fadeIn('slow', function() {
		$('#shape1').fadeIn('slow', function() {
			$('#shape2').fadeIn('slow', function() {
				$('#shape3').fadeIn('fast', function() {
					$('#shape4').fadeIn('slow', function() {
						$('#headline').slideDown('slow', function() {
							$('#menu_hp').fadeIn('slow');
								listenHp();
						});
					});
				});
			});
		});
	});
}

function startCatalogue() {
	$('#bg_cat').fadeIn('slow', function() {
		$('#ct_shape1').fadeIn('slow', function() {
			$('#ct_headline').fadeIn('slow', function() {
				$('#ct_shape2').fadeIn('slow', function() {
					$('#menu_cat').fadeIn('slow', function() {
						$('#prods').show();
						if(ie6) { $('#thumbmask').pngFix();}
						$('#thumbmask').show();
						prods.setGender();
						listenCatalogue();
					});
				});
			});
		});
	});
}

$(window).load(function() {
	startHp();
});
	
	
/** UTILS **/
/** jQuery-Plugin "pngFix"
**/
eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'([237-9n-zA-Z]|1\\w)'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(s(m){3.fn.pngFix=s(c){c=3.extend({P:\'blank.gif\'},c);8 e=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 5.5")!=-1);8 f=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 6.0")!=-1);p(3.browser.msie&&(e||f)){3(2).B("img[n$=.C]").D(s(){3(2).7(\'q\',3(2).q());3(2).7(\'r\',3(2).r());8 a=\'\';8 b=\'\';8 g=(3(2).7(\'E\'))?\'E="\'+3(2).7(\'E\')+\'" \':\'\';8 h=(3(2).7(\'F\'))?\'F="\'+3(2).7(\'F\')+\'" \':\'\';8 i=(3(2).7(\'G\'))?\'G="\'+3(2).7(\'G\')+\'" \':\'\';8 j=(3(2).7(\'H\'))?\'H="\'+3(2).7(\'H\')+\'" \':\'\';8 k=(3(2).7(\'V\'))?\'float:\'+3(2).7(\'V\')+\';\':\'\';8 d=(3(2).parent().7(\'href\'))?\'cursor:hand;\':\'\';p(2.9.v){a+=\'v:\'+2.9.v+\';\';2.9.v=\'\'}p(2.9.w){a+=\'w:\'+2.9.w+\';\';2.9.w=\'\'}p(2.9.x){a+=\'x:\'+2.9.x+\';\';2.9.x=\'\'}8 l=(2.9.cssText);b+=\'<y \'+g+h+i+j;b+=\'9="W:X;white-space:pre-line;Y:Z-10;I:transparent;\'+k+d;b+=\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;\';b+=\'J:K:L.t.M(n=\\\'\'+3(2).7(\'n\')+\'\\\', N=\\\'O\\\');\';b+=l+\'"></y>\';p(a!=\'\'){b=\'<y 9="W:X;Y:Z-10;\'+a+d+\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;">\'+b+\'</y>\'}3(2).hide();3(2).after(b)});3(2).B("*").D(s(){8 a=3(2).11(\'I-12\');p(a.A(".C")!=-1){8 b=a.13(\'url("\')[1].13(\'")\')[0];3(2).11(\'I-12\',\'none\');3(2).14(0).15.J="K:L.t.M(n=\'"+b+"\',N=\'O\')"}});3(2).B("input[n$=.C]").D(s(){8 a=3(2).7(\'n\');3(2).14(0).15.J=\'K:L.t.M(n=\\\'\'+a+\'\\\', N=\\\'O\\\');\';3(2).7(\'n\',c.P)})}return 3}})(3);',[],68,'||this|jQuery||||attr|var|style||||||||||||||src|navigator|if|width|height|function|Microsoft|appVersion|border|padding|margin|span|px|indexOf|find|png|each|id|class|title|alt|background|filter|progid|DXImageTransform|AlphaImageLoader|sizingMethod|scale|blankgif|appName|Internet|Explorer|parseInt|MSIE|align|position|relative|display|inline|block|css|image|split|get|runtimeStyle'.split('|'),0,{}));
