var _timeout;
var _brandsCount;
var _selectedBg;
var _selectedDetail;
var _duration = 600;
var _durationFast = 300;
var _durationSlow= 1000;
var _selectedBrandIndex = -1;
var _selectedDetailIndex = -1;

function InitBrandsSlider() {
    //IE png tranparency fix
    if (jQuery.browser.msie) {
        $('img[src$=.png][class=replace]').each(function() {
            var div = $("<div></div>");
            div.css("height", 88);
            div.css("width", 180);
            div.addClass($(this).attr("class"));
            div.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + this.src + ",sizingMethod='scale')");
            $(this).replaceWith(div);
        });
        $('#homeBrandsDetail div').each(function() {
            var image = $(this).css('background-image').replace(/^url|[\(\)]/g, '');
            image = image.replace('"', ''); image = image.replace('"', '');
            $(this).css("background", "none");
            $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + image + ",sizingMethod='scale')alpha(opacity=0)");
        });
    }
    var _selectedDetail;
    //counts the brands
    _brandsCount = $("#homeBrands a").length;
    //set first selected bg image
    _selectedBg = $("#homeBrandsImageSliderList").find("li:first");

    $("#contentHomeTop").hover(function() {
        StopSlideBrands();
    }, function() {
        StartSlideBrands();
    });
    
    //starts the brands slider
    SlideBrands();

    $("#homeBrands a").hover(function() {
        var newLogoIndex = $("#homeBrands a").index(this);
        var selectedBrand = $(this).attr("class");
        if (newLogoIndex != _selectedBrandIndex) {
            //hide the selected logo hoverimage 
            if (_selectedBrandIndex > -1)
                HideBrand(_selectedBrandIndex);
            _selectedBrandIndex = newLogoIndex;
            //if exists, hide the current detail
            HideCurrentDetail();
            //Show the new brand
            ShowBrand(_selectedBrandIndex, selectedBrand);
            //set selectedDetailIndex for situations when hovering faster then ShowBrand-fade-animation duration
            _selectedDetailIndex = selectedBrand
        } else {
            //show detail, when brand is selected and detail not
            if (_selectedDetailIndex != selectedBrand) {
                _selectedDetailIndex = selectedBrand;
                ShowDetail(selectedBrand);
            }
        }
    }, function() {
    });

}
function SlideBrands() {
    //if exists, hide the current detail
    HideCurrentDetail();
    //hide the selected brand
    if (_selectedBrandIndex > -1)
        HideBrand(_selectedBrandIndex);
    //get next brand index
    if ((_selectedBrandIndex + 1) < _brandsCount) {
        _selectedBrandIndex = _selectedBrandIndex + 1;
    } else {
        _selectedBrandIndex = 0;
    }
    //show the brand items
    ShowBrand(_selectedBrandIndex, null);
    //start new slide after 3 seconds
    _timeout = setTimeout("SlideBrands()", 3000);
}
function ShowBrand(brand, sdetail) {
    var logoA = $("#homeBrands a")[brand];
    //get the new bg image
    var newBg = $("#homeBrandsImageSliderList").find("li." + $(logoA).attr("class"));
    //get the hoverLogo
    if (jQuery.browser.msie) {
        var logoHover = $(logoA).find("div:last");
        //get the defaultLogo
        var defaultLogo = $(logoA).find("div:first");
    }
    else {
        var logoHover = $(logoA).find("img:last");
        //get the defaultLogo
        var defaultLogo = $(logoA).find("img:first");
    }
    //set style to hoverLogo
    $(logoHover).css("opacity", 0);
    $(logoHover).css("position", "absolute");
    $(logoHover).css("top", $(defaultLogo).position().top);
    $(logoHover).css("left", 0);
    $(logoHover).css("width", ($(defaultLogo).width() + 2) + "px");
    //animate the hoverLogo and show a new image
    logoHover.stop().fadeTo(_duration, 1, function() {
        if (newBg != _selectedBg) {
            //show detail
            if (sdetail != null)
                ShowDetail(sdetail);
            //show the new bg image
            newBg.css("filter", "alpha(opacity=0)"); // IE fix
            _selectedBg.animate({ "opacity": 0 }, { queue: false, duration: _durationSlow });
            newBg.animate({ "opacity": 1 }, { queue: false, duration: _durationSlow });
            _selectedBg = newBg;
        }
    });
    //hide the default logo
    defaultLogo.stop().fadeTo(_duration, 0);
}
function HideBrand(brand) {
    var logoA = $("#homeBrands a")[brand];
    if (jQuery.browser.msie) {
        var defaultLogo = $(logoA).find("div:first");
        var logoHover = $(logoA).find("div:last");
    }
    else {
        var defaultLogo = $(logoA).find("img:first");
        var logoHover = $(logoA).find("img:last");
    }
    logoHover.stop().fadeTo(_duration, 0);
    defaultLogo.stop().fadeTo(_duration, 1);
}
function ShowDetail(sBrand) {
    //show
    var detail = $("#homeBrandsDetail").find("div." + sBrand);
    detail.css("opacity", 0);
    detail.css("display", "block");
    detail.stop().animate({ "opacity": 1 }, { queue: false, duration: (_duration) });
    _selectedDetail = detail;
    
}
function HideCurrentDetail() {
    _selectedDetailIndex = -1;
    if (_selectedDetail != null) {
        var _currentSelectedDetail = _selectedDetail;
        _selectedDetail.stop().fadeOut(_duration);
    }
    
}
function StopSlideBrands() {
    clearTimeout(_timeout);
}
function StartSlideBrands() {
    HideCurrentDetail();
    _timeout = setTimeout("SlideBrands()", 3000);
}

function InitPreOwnedCarousel() {
    $("#contentHomePreOwned #preOwnedCarousel").hover(function() {
        var description = $(this).find(".description"); //$("#contentHomePreOwned .description");
        $(description).animate({ "top": "0px" }, { queue: false, duration: 500 });
    }, function() {
       var description = $(this).find(".description"); //$("#contentHomePreOwned .description");
       $(description).animate({ "top": "50px" }, { queue: false, duration: 500 });
    });    

    jQuery('#preOwnedCarousel').jcarousel({
        scroll: 1,
        initCallback: preOwnedcarousel_initCallback
    });
    //buttons are not visible onload
    $("#carouselButtonPrevious").css("visibility", "visible");
    $("#carouselButtonNext").css("visibility", "visible");
    //position of the listitem is absolute onload en relative when page is loaded
    $("#contentHomePreOwned #preOwnedCarousel ul li").css("position", "relative");    
}
function preOwnedcarousel_initCallback(carousel) {
    jQuery('#carouselButtonNext').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#carouselButtonPrevious').bind('click', function() {
        carousel.prev();
        return false;
    });
};
