function tarracarpet_over(obj)
{
	var length=obj.src.length;
	var index_ext=0;
	while (obj.src.indexOf('.',index_ext+1)!=-1)
	 {
		index_ext=obj.src.indexOf('.',index_ext+1)
	 }
	var extension=obj.src.substring( index_ext , length);
	obj.src=(obj.src.substring(0,index_ext)+"_over"+extension);
}
function tarracarpet_out(obj)
{
	var length=obj.src.length;
	var index_ext=0;
	while (obj.src.indexOf('.',index_ext+1)!=-1)
	 {
	 index_ext=obj.src.indexOf('.',index_ext+1)
	 }
	var extension=obj.src.substring( index_ext , length);
	var idx_ov=0;
	if (obj.src.substring(index_ext-5,index_ext) == "_over") {
		obj.src=(obj.src.substring(0,index_ext-5)+extension);
	}
}

var tarracarpet_keepVisibleVal = true;
var t = null;

function tarracarpet_keepVisible() {
	tarracarpet_keepVisibleVal = true;
        tarracarpet_showProductsDelay();
}

function tarracarpet_noKeepVisible() {
	tarracarpet_keepVisibleVal = false;
	tarracarpet_showProductsDelay();
}

function tarracarpet_showProductsDelay() {
	if (t != null)	clearTimeout(t);
	t = setTimeout('tarracarpet_showProducts()', 300);
}

function tarracarpet_showProducts () {
	if (document.getElementById('top-category-nav').style.display == 'block' && !tarracarpet_keepVisibleVal) {
		document.getElementById('top-category-nav').style.display = 'none';
	} else {
		 document.getElementById('top-category-nav').style.display = 'block';
	}
}

var tarracarpet_keepVisibleValCart = true;
var tCart = null;

function tarracarpet_keepVisibleCart() {
	tarracarpet_keepVisibleValCart = true;	
}
function tarracarpet_noKeepVisibleCart() {
	tarracarpet_keepVisibleValCart = false;
	tarracarpet_showCartDelay();
}

function tarracarpet_showCartDelay() {
	if (tCart != null)	clearTimeout(tCart);
	tCart = setTimeout('tarracarpet_showCart()',300);
}

function tarracarpet_showCart () {
	$$('.header .mini-cart').each(function (el) {
		if (el.style.display == 'block' && !tarracarpet_keepVisibleValCart) {
			el.style.display = 'none';
		} else {
			el.style.display = 'block';
		}
		
	});	
}

var tarracarpet_bannerFrontResized = false;

function slide_resizer(){
	mainWidth = $("main").getWidth(); 
	carWidth = Math.min($$("li.slide").length * 133, mainWidth - 40 - ((mainWidth - 30) % 136));
	$("carousel-wrapper").setStyle({width: carWidth + 'px'});
	margin = ($("carousel-inner").getWidth() - carWidth) / 2;
	$("carousel-wrapper").setStyle({margin: '0 ' + margin + 'px'});	
	nrSlides = parseInt(carWidth / 130);	
	slider.options.visibleSlides = nrSlides;
}	

function tarracarpet_resizer(){
	var total = 0;
	$$('.middle .side-col').each(function (el) {
		total += el.offsetWidth;
	});
	var width = 0;
	$$('.middle-container').each(function (el) {
		width = el.offsetWidth;
	});
	window.window_width=width;
	
	
	
	if (width < 1180) {
		
		$$('.front-banner .slideshow-container').each(function(el) {
			el.style.marginRight = '0px';
			tarracarpet_bannerFrontResized = true;
		});
		$$('.front-banner .col-right').each(function(el) {
			el.style.marginTop = '400px';
			tarracarpet_bannerFrontResized = true;
		});
		$$('.col-3-layout .col-right').each(function(el) {
			el.style.marginTop = '20px';//'428px';//de ce 428, nu-i prea mult?
			tarracarpet_bannerFrontResized = true;
		});
	} else if (tarracarpet_bannerFrontResized == true) {
		$$('.front-banner .col-right').each(function(el) {
			el.style.marginTop = '0';
			tarracarpet_bannerFrontResized = false;
		});
		$$('.col-3-layout .col-right').each(function(el) {
			el.style.marginTop = '0';
			tarracarpet_bannerFrontResized = false;
		});
		$$('.front-banner .slideshow-container').each(function(el) {
			el.style.marginRight = '210px';
			tarracarpet_bannerFrontResized = false;
		});
	}
	width = width - total - 2 * 40;
	
	$$('.col-main').each(function (el) {
		el.style.width = width + 'px';
	});
	$$('.product-img-box>a>img').each(function (el) {
		el.style.width = (width / 3.61) + 'px';
	});
	
	var slideshow_width=0;
	$$('.front-banner .slideshow-container').each(function(el) {
		console.log("w="+el.offsetWidth);
		slideshow_width = el.offsetWidth;
	});
	$$('.front-banner .slideshow-container #slideshow').each(function(el) {
		console.log(Math.round((slideshow_width-el.offsetWidth)/2)+"px");
		el.style.marginLeft=Math.round((slideshow_width-el.offsetWidth)/2)+"px";
	});
	
	$$('p.category-description a img').each(function(el){
		var w=el.getAttribute("width");
		var h=el.getAttribute("height");
		//alert("w="+w+"; h="+h);
		if (!isNaN(parseInt(w)) && !isNaN(parseInt(h))){
			el.setAttribute("style","max-width:"+w+"px;max-height:"+h+"px;");
			if (window.window_width >= 1280){
				el.setAttribute("width","auto");
				el.setAttribute("height","auto");
			}else{
				el.setAttribute("width","100%");
				el.setAttribute("height","auto");
			}
		}
	});
	

	
	
}

window.onresize = function() {
	tarracarpet_resizer();
	slide_resizer();
};


document.observe("dom:loaded", function() {	
	tarracarpet_resizer();
	$$('.top-menu-links .last').each(function (el) {        
		el.observe ('mouseover', function(event){
    		tarracarpet_showCartDelay();
        tarracarpet_keepVisibleCart();
    	});
        el.observe ('mouseout', function(event){
    		tarracarpet_showCartDelay();
        tarracarpet_noKeepVisibleCart();
    	});
    }); 
	$$('.header .mini-cart').each(function (el) {        
		el.observe ('mouseover', function(event){
			tarracarpet_keepVisibleCart();
		});
		el.observe ('mouseout', function(event){
			tarracarpet_noKeepVisibleCart();
		});
	});
/*
  $$('.left-category-nav li').each(function (el) {
		el.observe ('mouseover', function(event){
				$(el).select('ul').each(function (el) {
					$(el).setStyle({
  					display: 'block'
					});
				});
    	});
    el.observe ('mouseout', function(event){
				$(el).select('ul').each(function (el) {				
					$(el).setStyle({
  					display: 'none'
					});
				});
    	});
	});
	*/
	
  new Horinaja('slideshow',0.6,3,true);
  slide_resizer();
	});

