document.observe("dom:loaded", function() {
	setLayout();
	formatMenu();
	animateTabs();
});

function formatMenu(){
	if($('nav')){
		$('nav').select('span').each(function(voice){
			//voice.update(voice.innerHTML.capitalize());
			if(voice.innerHTML=='Offerte'){
			voice.up().up().setStyle({'background':'#BFFF4F'});}
		});
		/*var i;span=new Element('span');
		var span2=new Element('span');
		
		$('nav').select('ul.level0').each(function(ul){
			ul.select('li.level1').each(function(li){
				/*if(i>=20)
					span.insert(li);
				else span2.insert(li);
				console.log(span);
				console.log(span2);
				i++;
				});
				//ul.insert(span2);
				//ul.insert(span);
				
			});*/
	}
	if($('narrow-by-list')){
		$('narrow-by-list').select('a').each(function(voice){
			//voice.update(voice.innerHTML.capitalize());
		});
	}
	if($('narrow-by-list2')){
		$('narrow-by-list2').select('a').each(function(voice){
			//voice.update(voice.innerHTML.capitalize());
		});
	}
}
function disableSelection(target){
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default"
}
function animateTabs(){
	if($$(".product-view .product-shop .tab_content")!=''){
		disableSelection(document.getElementById('tab_container'));
		$('tab_container').observe('contextmenu',function(event){Event.stop(event);})
		$$("ul.tabs li").each(function(li){
			li.observe('click', function(event){
				Event.stop(event);
				$$("ul.tabs li").each(function(element){
					if(element.hasClassName('active')){
						element.removeClassName('active');
						var tmp=element.select('a').toString().split('#')[1];
						$(tmp).hide();
					}
				});
				$(this).addClassName("active");
				var activeTab = $(this).select('a').toString().split('#')[1];
				$(activeTab).show();
			});
		});
	}
}

function setLayout(){
	window.onresize=function() {
		setLayout();
		animateTabs();
	}
	var colMain,search,quickLinks,width=f_clientWidth(),leftPrice,ribbon;
	//alert(width);
		if(width<=1152){
			if(width>1024){width=width;} else {width=991;};
			if($$('.product-view')!=''){colMain=Math.round(width-250)} else {colMain=Math.round((width*75)/100)}
			search=Math.round(width/4);
			quickLinks=Math.round(width-20);
			//if($('news-ribbon')!=null) {ribbon=((width>1009)?630:575);}
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			if(($('banner')!=null)){$('banner').select('img')[0].setStyle({'width':'85%'});}
			if($$('.col-right')!='') $$('.col-right')[0].hide();
		}else if(width>=1200 && width<1280){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*64)/100)}
			search=Math.round(width/(2.4));
			quickLinks=Math.round(width-20);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			if($('banner')!=null) $('banner').select('img')[0].setStyle({'width':'100%'});
			if($$('.col-right')!='') $$('.col-right')[0].show();
			//if($('news-ribbon')!=null) {ribbon=608;}
		} else if(width>=1280 && width<=1370){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*64)/100)}
			quickLinks=Math.round(width-20);
			search=Math.round(width/(2.4));
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			if($$('.col-right')!='') $$('.col-right')[0].show();
			//if($('news-ribbon')!=null) {ribbon=608;}
		} else if(width>=1400 && width<=1440){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*64)/100)}
			quickLinks=Math.round(width-20);
			search=Math.round(width/2);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			if($$('.col-right')!='') $$('.col-right')[0].show();
			//if($('news-ribbon')!=null) {ribbon=659;}
		}else if(width>1600 && width<=1680){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*69)/100)}
			search=Math.round(width/2);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			quickLinks=Math.round(width-20);
			//if($('news-ribbon')!=null) {ribbon=Math.round((width-(width*42)/100));}
		}else if(width>1680 && width<=1920){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*69)/100)}
			search=Math.round(width/2);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			quickLinks=Math.round(width-20);
			//if($('news-ribbon')!=null) {ribbon=Math.round((width-(width*44.8)/100));}
		}else if(width>=1920 && width<=2560){
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*69)/100)}
			search=Math.round(width-(width*40)/100);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			quickLinks=Math.round(width-20);
			//if($('news-ribbon')!=null) {ribbon=Math.round((width-(width*44.8)/100));}
		}else {
			if($$('.product-view')!=''){colMain=Math.round(width-250)}
			else {colMain=Math.round((width*69)/100)}
			search=Math.round(width/2);
			if($('price')!=null) {leftPrice=Math.round(width-(width*24)/100)}
			quickLinks=Math.round(width-20);
			//if($('news-ribbon')!=null) {ribbon=Math.round((width-(width*24)/100));}
		}
		//------------------------------------------------------------
		$$('div.page')[0].setStyle({'width':Math.round(width)+'px'});
		$$('div.col-main')[0].setStyle({'width':colMain+'px'});
		$$('div.quick-access')[0].setStyle({'width':quickLinks+'px'});
		if($('price')!=null) {$('price').setStyle({'left':leftPrice+'px'})}
		//if($('news-ribbon')!=null) {$('news-ribbon').setStyle({'margin':'-241px 0 0 '+ribbon+'px'})}
		$('search').setStyle({
			'width':search+'px',
			'float':'left',
			'margin':'0 '+'0 '+'0 '+30+'px'
		});
		//------------------------------------------------------------
		//posizionamento dei bottoni shoppingcart e wishlist
		var topCart=$$('.logo img')[0].offsetHeight;
		var cart=$$('a.top-link-cart')[0]
		var wish=$$('a.top-link-wishlist')[0]
		
		$$('.form-search')[0].insert(wish);
		$$('.form-search')[0].insert(cart);
		//------------------------------------------------------------
		//aggiornamento del numero di prodotti presente nello shoppingcart
		var cartText=cart.select('span')[0].innerHTML.split('(')[0];
		var cartProducts;
		
		if(parseInt(cart.select('span')[0].innerHTML.split('(')[1])){
			cartProducts=parseInt(cart.select('span')[0].innerHTML.split('(')[1]);
		}else {cartProducts='';} 
		
		var cartBadge = new Element('div').addClassName('badge');
		var cartBadgeleft=  new Element('div').addClassName('badge-left');
		var cartBadgeright= new Element('div').addClassName('badge-right');
		var cartBadgetext=  new Element('div').addClassName('badge-text').update(cartProducts);
		
		if(!$('cart-badge') && cartProducts!='' ){//se ci sono prodotti e non c' il badge procedo
			cartBadge.insert(cartBadgeleft);
			cartBadge.insert(cartBadgetext);
			cartBadge.insert(cartBadgeright);
			cart.insert(cartBadge);
		}
		//aggiorno il testo ripulito dalle quantit prodotti
		cart.select('span')[0].update(cartText);
		//------------------------------------------------------------
		//aggiornamento del numero di prodotti presente nella whishlist
		var wishText=wish.select('span')[0].innerHTML.split('(')[0];
		var wishProducts;
		
		if(parseInt(wish.select('span')[0].innerHTML.split('(')[1])){
			wishProducts=parseInt(wish.select('span')[0].innerHTML.split('(')[1]);
		}else {wishProducts='';} 
		
		var wishBadge = new Element('div', {'class': 'badge'});
		var wishBadgeleft=  new Element('div', {'class': 'badge-left'});
		var wishBadgeright= new Element('div', {'class': 'badge-right'});
		var wishBadgetext=  new Element('div', {'class': 'badge-text'}).update(wishProducts);
		if(!$('whish-badge') && wishProducts!='' ){//se ci sono prodotti e non c' il badge procedo
			wishBadge.insert(wishBadgeleft);
			wishBadge.insert(wishBadgetext);
			wishBadge.insert(wishBadgeright);
			wish.insert(wishBadge);
		}
		//aggiorno il testo ripulito dalle quantit prodotti
		wish.select('span')[0].update(wishText);
		//------------------------------------------------------------
	}
	
	//----------------------------------------------------------------
	//funzioni per determinare le dimensioni del monitor
	function f_clientWidth() {
		return f_filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);
	}
	function f_clientHeight() {
		return f_filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0
		);
	}
	function f_scrollLeft() {
		return f_filterResults (
			window.pageXOffset ? window.pageXOffset : 0,
			document.documentElement ? document.documentElement.scrollLeft : 0,
			document.body ? document.body.scrollLeft : 0
		);
	}
	function f_scrollTop() {
		return f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}
	function f_filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
