$(document).ready(function() {
    //New window links
    $("#headerbottomwrap").hide();
    $("#headerbottomwrap ul").hide();
    $("a._blank").click(function() {
        window.open(this.href);
        return false;
    });
    //--- REGION CHOOSER

    //$("#chooseRegion").click(function () {
        //var regionChooserHeight = $("#regionChooser").height()+2;
        //if ($("#regionChooserWrap").height() != regionChooserHeight) {
           // $("#regionChooserWrap").animate({"height": regionChooserHeight}, 200);
        //} else {
            //$("#regionChooserWrap").animate({"height": 0}, 200);
        //}
    //});
    
    $("#chooseRegion").click(function () {
        
        if (!$("#regionChooserWrap.active").is(':visible') && !$("#regionChooserWrap.active").is(':animated')) {
            $("#regionChooserWrap").slideDown('slow');
            $("#chooseRegion").addClass('active');
            $("#regionChooserWrap").addClass('active');
        }

        if ($("#regionChooserWrap.active").is(':visible') && !$("#regionChooserWrap.active").is(':animated')) {

            $("#regionChooserWrap").slideUp('slow');
            $("#chooseRegion").removeClass('active');
            $("#regionChooserWrap").removeClass('active');
        }

        
    });

    $(document).click(function(event) { 
        if(!$(event.target).closest("#chooseRegion.active").length && !$(event.target).closest('#regionChooserWrap.active').length) {

                
                $("#regionChooserWrap").slideUp('slow');
                $("#chooseRegion").removeClass('active');
                $("#regionChooserWrap").removeClass('active');
                
        }

    });

    $("#searchDrop").click(function () {
        
        if (!$("#headerSearch.active").is(':visible') && !$("#headerSearch.active").is(':animated')) {
            $("#headerSearch").slideDown('fast');
            $("#searchDrop").addClass('active');
            $("#headerSearch").addClass('active');
        }

        if ($("#headerSearch.active").is(':visible') && !$("#headerSearch.active").is(':animated')) {

            $("#headerSearch").slideUp('fast');
            $("#searchDrop").removeClass('active');
            $("#headerSearch").removeClass('active');
        }

        
    });

    $(document).click(function(event) { 
        if(!$(event.target).closest("#headerSearch.active").length && !$(event.target).closest('#searchDrop.active').length) {

                
                $("#headerSearch").slideUp('fast');
                $("#searchDrop").removeClass('active');
                $("#headerSearch").removeClass('active');
                
        }

    });



    





    
    //--- MENU
    //var hideMenu;
    //var subMenu = $("#headerbottomwrap").hasClass("showSub") ? true : false;
    //function hideMenus () {
    //    $("#headerbottomwrap").hide();
    //    $("#headerbottomwrap ul").hide();
    //    $("#headerbottomwrap ul.cur").show();
    //    $("#headerBikes").animate({height: 0}, 200, 'swing');
    //    if (!subMenu) {
    //        $("#headerbottomwrap").animate({top: "-350px"}, 200, 'swing');
    //    }
    //    
    //}
    //$("#menu .drop").click(function () {
    //    $("#headerbottomwrap ul").hide();
    //    $("#headerbottomwrap").show();
    //    $("#sub" + $(this).attr("id")).show();
    //    if (!subMenu) {
    //        $("#headerbottomwrap").animate({top: "0"}, 200, 'swing');
    //    }
    //}).hover(function () {
    //    clearTimeout(hideMenu);
    //}, function () {
    //    hideMenu = setTimeout(hideMenus, 2500);
    //});
    //$("#headerbottomwrap ul").hover(function () {
    //    clearTimeout(hideMenu);
    //},function () {
    //    hideMenu = setTimeout(hideMenus, 2500);
    //});
    //--- BIKE MENU
    //$(".menuBikeCat").hover(function () {
    //    var curCat = $(this).attr("id");
    //    if ($("#headerBikes").height() == ($("#cats_" + curCat).height()+27)) {
    //        //It's the current one, being clicked a second time - hide it
    //        $(".bikeMenu").animate({opacity: 0}, 300);
    //        $("#headerBikes").animate({height: 0}, 300);
    //    } else {
    //        //Show
    //        $(".bikeMenu:not(#cats_" + curCat + ")").animate({opacity: 0}, 300, 'swing', function () {
    //            $(this).hide();
    //        });
    //        $("#cats_" + curCat).show().animate({opacity: 1}, 300, 'swing');
    //        $("#headerBikes").animate({height: ($("#cats_" + curCat).height()+27)}, 300);
    //    }
    //    return false;
    //});
    //$(".bikeMenu").hover(function () {
    //    clearTimeout(hideMenu);
    //}, function () {
    //    hideMenu = setTimeout(hideMenus, 2500);
    //})







    //--- SEARCH
    $("#qs").focus(function() {
        $(this).keyup();
    }).keyup(function(e) {
        if (e.keyCode == 27) {
            $(this).blur();
            $("#searchResults").hide();
            if (isIE6()) $('select').show();
            return;
        }
        var val = $(this).val();
        whiteval = val.replace(/^\s*|\s*$/g,'');
        if (val && whiteval != '') {
            $('#searchResults').show();
            if (isIE6()) $('select').hide();
            $("#searchList").load("/qs/",
                {data: val}
            );
        } else {
            $('#searchResults').hide();
            if (isIE6()) $('select').show();
        }
    });
    
    $(window).scroll(function () {
        body_image_top_fixed();
    });
    
    body_image_top_fixed();
});

//tab effects

var TabbedContent = {
    init: function() {
        $(".tab_item").on('click', function() {
            $(this).siblings(".tab_item").removeClass("cur");
            $(this).addClass("cur");
            TabbedContent.slideContent($(this));
        }, 300);
    },

    slideContent: function(obj) {
        var margin = $(obj).parent().parent().parent().parent().parent().find(".slide_content").width();
        margin = margin * ($(obj).prevAll().size());
        margin = margin * -1;
        var index = $(obj).parent().parent().parent().parent().parent().find(".tabs").index($(obj).parents(".tabs"));
        $(obj).parent().parent().parent().parent().parent().find(".tabslider:eq(" + index + ")").stop().animate({
            marginLeft: margin + "px"
        }, {
            duration: 300
        });
    }
}

var SpecContent = {
    init: function() {
        $('.spec_item').on('click', function(){  
            $(this).siblings(".spec_item").removeClass("cur");
            $(this).addClass("cur");
            SpecContent.slideContent($(this));
        }, 300);
    },

    slideContent: function(obj) {
        var margin = $(obj).parent().parent().parent().parent().parent().find(".slide_content").width();
        margin = margin * ($(obj).prevAll().size());
        margin = margin * -1;
        var index = $(obj).parent().parent().parent().parent().parent().find(".tabs").index($(obj).parents(".tabs"));
        $(obj).parent().parent().parent().parent().parent().find(".tabslider:eq(" + index + ")").stop().animate({
            marginLeft: margin + "px"
        }, {
            duration: 300
        });
    }
}

//For the Bike Archives Dropdown List
function goTo(form) {
	if (form.targets.options[form.targets.selectedIndex].value =='#') {
	} else { 
		popupwindow(form.targets.options[form.targets.selectedIndex].value,'archivebike','width=1024,height=500,scrollbars=yes,resizable=yes,toolbar=no,status=no,location=yes');
	}
}
function popupwindow(theurl,windowname,features) {
	newwindow = window.open(theurl,windowname,features);
	if (window.focus) {newwindow.focus()}
	void 0;
}


//body images which need to be fixed when they reach the top
function body_image_top_fixed() {
    $(".body_image_top_fixed").each(function () {
        if ($(this).offset()['top'] - $(window).scrollTop() < 1) {
            $(this).css({"background-attachment": "fixed"});
        } else {
            $(this).css({"background-attachment": "scroll"});
        }
    });
}


function isIE6() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion<7)
            return true;
    }
    return false;
}