

var obj;
var showingCat ;
function showCat( catID ) {


    showingCat = catID
    if (window.XMLHttpRequest) {
        obj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }

    try {
        obj.open("GET", "/brand_by_cat.php?cat="+catID, true);
        obj.onreadystatechange = onStateChange;
        obj.send(null);
    } catch (e) {
        alert("Errore: "+e);
    }
}

function onStateChange() {
        if (obj.readyState == 4) {
                if (obj.status == 200) {
                        brands = eval(obj.responseText);
                        switchBrands(brands)
                } else {
                        alert("Errore: "+obj.statusText);
                }
        }
}

function switchBrands( brands ) {
    var i


    for(i=0; i<currBrandHigh.length; i++) {
        document.getElementById('brand_'+currBrandHigh[i]).className="";
    }
    //document.getElementById('cat_'+currCatHigh).className="";
    currBrandHigh = new Array();

    //alert('cat_'+showingCat);
    if ( typeof(brands) == 'object' ) {
        if (brands.length) {
            for(i=0; i<brands.length; i++) {
                //alert('brand_'+brands[i]);
                document.getElementById('brand_'+brands[i]).className="on";
            }
        }
    }

    //document.getElementById('cat_'+showingCat).className="down";
    currBrandHigh = brands
    currCatHigh=showingCat;
}

var fx;
var originalSwitcherHeight;

function emptyCall(){};
function categoryLoaded()
{
    var originalHeight;
    var brands = eval($('hidden').innerHTML);
    switchBrands(brands);
    if($('marchi-container').getStyle('opacity') == 0)
    {
        originalHeight = $('content-switcher').getCoordinates().height;
        fx.details.start(1,0).chain(function(){
            $('marchi-container').setStyle('display', 'block');
            $('brand-container').setStyle('display', 'none');
            fx.switcher.start(originalHeight,originalSwitcherHeight);
            fx.brands.start(0,1);
            $('content-background').removeClass('with_brand');
        });
    }
}

function loadCategoriesOld(category)
{

    if(window.location.href.contains('scheda'))
    {
        if(window.location.href.contains('/it/'))
        {
            window.location.href = '/it/brand/w.code/'+category+'/#show'+category;
        }
        else
        {
            window.location.href = '/en/brand/w.code/'+category+'/#show'+category;
        }
    }
    else
    {
        new Ajax('/brand_by_cat.php?cat='+category, {
    		method: 'get',
    		update: $('hidden'),
    		onComplete: categoryLoaded
    	}).request();
    }
}
function loadCategories(category)
{
    var temp;

    document.getElementById("1").setAttribute("class", "menCollection");
    document.getElementById("2").setAttribute("class", "womenCollection");
    document.getElementById("3").setAttribute("class", "accessories");
    document.getElementById("4").setAttribute("class", "menShoes");
    document.getElementById("5").setAttribute("class", "art");
    document.getElementById("6").setAttribute("class", "music");
    document.getElementById("7").setAttribute("class", "others");
    document.getElementById("8").setAttribute("class", "womenShoes");
    
    temp = document.getElementById(category).getAttribute("class");
    temp = temp+"Down";
    
    document.getElementById(category).setAttribute("class", temp);

    if(window.location.href.contains('scheda'))
    {
        if(window.location.href.contains('/it/'))
        {
            window.location.href = '/it/brand/w.code/'+category+'/#show'+category;
        }
        else
        {
            window.location.href = '/en/brand/w.code/'+category+'/#show'+category;
        }
    }
    else
    {
        new Ajax('/brand_by_cat.php?cat='+category, {
    		method: 'get',
    		update: $('hidden'),
    		onComplete: categoryLoaded
    	}).request();
    }
}
function brandLoaded()
{
    var originalHeight;
    var tdSize;
	Lightbox.anchors = [];
	$$('a').each(function(el){
		if (el.rel && el.rel.test(/^lightbox/i)){
			el.onclick = Lightbox.click.pass(el, Lightbox);
			Lightbox.anchors.push(el);
		}
	}, Lightbox);

    if($('brand-container').getStyle('opacity') == 0)
    {
        originalHeight = $('content-switcher').getCoordinates().height;
        fx.brands.start(1,0).chain(function(){
            $('content-switcher').setStyle('height',originalHeight);
            $('marchi-container').setStyle('display', 'none');
            $('brand-container').setStyle('height', '0');
            $('brand-container').setStyle('display', 'block');
            tdSize = ($('product').getCoordinates().width - (4*($$('#product td').length+1)))/$$('#product td').length;
            $$('#product td').each(function(item,i){
                item.style.width = tdSize
            });
            $$('#product th').each(function(item,i){
                item.style.width = tdSize
            });
            fx.switcher.start(originalHeight,$('loaded-brand').getCoordinates().height);
            fx.details.start(0,1);
            $('content-background').addClass('with_brand');
        });
    }
}
window.addEvent('domready', function(){
    if($('marchi-container') != null)
    {
        originalSwitcherHeight = $('content-switcher').getCoordinates().height;
        $('marchi-container').setStyle('opacity',0);
        $$('a').each(function(item, index){
            if(item.id.contains('brand_'))
            {
                var brandPath = item.href;
                item.addEvent('click', function(){
                    new Ajax(brandPath+'/w.ajax/1', {
                		method: 'get',
                		update: $('brand-container'),
                		onComplete: brandLoaded
                	}).request();
                });
                item.href = 'javascript:emptyCall();';
            }
        });
        fx = {
        	brands: $('marchi-container').effect('opacity', {duration: 1000}).start(0,1),
            details: $('brand-container').effect('opacity', {duration: 1000}).hide(),
            switcher: $('content-switcher').effect('height', {duration: 1500})
    	};    
    }
    else
    {
        var tdSize;
        tdSize = ($('product').getCoordinates().width - (4*($$('#product td').length+1)))/$$('#product td').length;
        $$('#product td').each(function(item,i){
            item.style.width = tdSize
        });
        $$('#product th').each(function(item,i){
            item.style.width = tdSize
        });
    }
});

