if(typeof console === "undefined") {
    console = { log: function() { } };
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3)
        if ((x=MM_findObj(a[i]))!=null){
            document.MM_sr[j++]=x;
            if(!x.oSrc) x.oSrc=x.src;
            x.src=a[i+2];
        }
}

function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image;
                d.MM_p[j++].src=a[i];
            }
        }
}

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        var embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}


$(document).ready(function(){

	fix_flash(); 

    /*
     * Category Filter toggles
     */
    $(".slide_toggle").click(function(){
        var selector = $(this).attr("id");
        $(".slide_toggle ~ ." + selector).slideToggle("slow");
        $(this).toggleClass("opened");
        $(this).toggleClass("closed");
    });

    $(".filter_cbox_wrapper").each(function(){
       
       var height = $(this).height();
       $(this).data('full_height', height);

       var num_cboxes = $(this).find("input");


       var height = 140;
       var numberOfCheckboxes = 6;
       if ($(this).attr('id') == 'size_filter_container'){
           var height = 220
           var numberOfCheckboxes = 10;
       }
       if(num_cboxes.length > numberOfCheckboxes){
            $(this).animate({
                height: height
            }, 0);
       }
    });

    $(".more_filters").click(function(){
        $(this).fadeOut("fast");
        var target = $(this).parent();
        target.animate({
            height: target.data("full_height")+'px'
        }, 1000);
    });


    $(".filter_cbox").click(function(){
        $("#filters_form").submit();
    });

    $(".more_filters").css({"display":"block"});
    /*
     * End Category Filter toggles
     */

     /*
      * Bastian and Rugger landing page tabs
      */

      $(".tab_button").click(function(){
         var id = $(this).attr("id");

         $(".tab_button").each(function(){
            if($(this).hasClass("active")){
                 $(this).toggleClass("active");
            }
         });
         $(this).toggleClass("active");


         if($(this).attr("id") == "video"){
            $("#video_wrapper").css({"display":"block"});
         }else{
             $("#video_wrapper").css({"display":"none"});
         }
         $(".tab_content").fadeOut();
         $("#tab_"+id).fadeIn();

      });

      $(".close_tabs").click(function(){
            $(".tab_content").each(function(){
                if($(this).attr("id") == "tab_video"){
                    $("#video_wrapper").css({"display":"none"});
                }
                $(this).fadeOut();
            });

            $("#tab_default").fadeIn();
            $(".tab_button").each(function(){
            if($(this).hasClass("active")){
				$(this).toggleClass("active");
			}
		});
	});
	/*
	 * End Tabs
	 */


    /*
     * Carousel on rugger page
     */
	if($('#rugger_carousel').length){
		$('#rugger_carousel').cycle({
			fx:     'fade',
			timeout: 8000,
			speed: 1200,
			next:   '.scrollingHotSpotRight',
			prev:   '.scrollingHotSpotLeft'
		});
	}

    /*
     * Carousel on mb page
     */
	if($('#mb_carousel').length){
		$('#mb_carousel').cycle({
			fx:     'fade',
			speed:  'slow',
			timeout: 5000,
			next:   '.scrollingHotSpotRight',
			prev:   '.scrollingHotSpotLeft'
		});
	}

    $(".pr_").toggle(
        function() {
            $("#pr_table"+$(this).attr('id')).css("display","block");
            $("#pr_indicator"+$(this).attr('id')).text('-');
        },
        function() {
            $("#pr_table"+$(this).attr('id')).css("display","none");
            $("#pr_indicator"+$(this).attr('id')).text('+');
        }
        );
	
     $('.image_to_replace_on_hover').each(function() {
        $(this).mouseenter(function() {
            $(this).attr("src", $(this).attr("src").replace(/off/, 'over'));
        });
        $(this).mouseleave(function() {
            if ( !$(this).hasClass('active') ) {
              $(this).attr("src", $(this).attr("src").replace(/over/, 'off'));
           }
        });
    });

    $('.image_to_replace_on_hover').click(function() {
        $('.image_to_replace_on_hover').removeClass('active');
        $('.image_to_replace_on_hover').each(function() {
            $(this).attr("src", $(this).attr("src").replace(/over/, 'off'));
        });
        $(this).addClass('active');
        $(this).attr("src", $(this).attr("src").replace(/off/, 'over'));
    });
	
    var prodmain = $("#prod_panel_1").height();
    prodmain = prodmain+50;
    $("#prod_tab_container").css("height", prodmain+"px")

    $("#prod_tab_1").click(function(){

        var prod1 = $("#prod_panel_1").height();
        prod1 = prod1+50;
        $("#prod_tab_container").css("height", prod1+"px");

    });
    $("#prod_tab_2").click(function(){
        $("#prod_tab_container").css("height", "151px");
    });
    $("#prod_tab_3").click(function(){
        $("#prod_tab_container").css("height", "220px");
    });
    $("#prod_tab_4").click(function(){
        var prod4 = $("#prod_panel_4").height();
        prod4 = prod4+50;
        $("#prod_tab_container").css("height", prod4+"px");
    });
    $("#prod_tab_5").click(function(){
        $("#prod_tab_container").css("height", "209px");
    });

    $('#nav_search').click(handle_nav_search_click);

    function handle_nav_search_click(){
        if ($('#search_h').is(':visible')) {
            $('#search_form').trigger('submit');
        } else {
            $('#search_h').fadeIn();
            $('#search_input').val('Search');
            $('#search_input').select();
        }
    }

    $('#search_form').submit(function() {
        var search_term = $('#search_input').val();

        if (search_term == '') {
            $('#search_input').val('Please enter search term')
            $('#search_input').select();
            return false;
        } else if (search_term.toLowerCase() == 'search') {
            $('#search_input').val('Please enter search term');
            $('#search_input').select();
            return false;
        } else if (search_term.toLowerCase() == 'please enter search term') {
            $('#search_input').select();
            return false;
        } else {
            return true;
        }
    });

    $('#search_input').click(function() {
        var search_term = $('#search_input').val();

        if (search_term.toLowerCase() == 'search' || search_term.toLowerCase() == 'please enter search term') {
            $('#search_input').val('');
            $('#search_input').focus();
        }
    });

	// This functionality is used on both the product page and the category page.
    $('#recent_button').toggle(function(){
        if ($.browser.msie && $.browser.version.substr(0,1)<7) {
            /*Do nothing*/
        } else {
            $('#hide_recent').slideUp();
        }
    },function(){
        $('#hide_recent').slideDown();
    });

	// This functionality is only used on the product page, but since it is the same as above, I put it here.
	$('#related_button').toggle(function(){
        if ($.browser.msie && $.browser.version.substr(0,1)<7) {
            /*Do nothing*/
        } else {
            $('#hide_related').slideUp();
        }
    },function(){
        $('#hide_related').slideDown();
    });

    $('.size_click_box').click(function(){
        if($(this).hasClass('size_notavailable')){
            return false;
        }
        var selected_option = $(this).attr("id");
        var selected_id = "#"+selected_option;
        if ($(this).hasClass('size_outofstock')) {
            /*Do nothing - product out of stock*/
        } else {
            /*Previous code
             *$('.size_click_box').css("background-color", "white");
            $(selected_id).css("background-color", "#e9e7e8");*/
            $('.size_click_box').removeClass('size_selected');
            $(selected_id).addClass('size_selected');
            var selected_option =selected_option.substr(10, 2)
            var selected_option = "#select_option_"+selected_option;
            $(selected_option).attr("selected","selected");
        }
    });


	/**
	 * Apply cycle plugin for main banner only if there is more than 1 image
	 */
    
	if ( $('#swf_panel .big_banner_cycle').children().length > 1 ) {
        if ($('#michael-bastian').length != 0) {
            var timeout_length = 6000;
        } else {
            var timeout_length = 2000;
        }
		$('#swf_panel')
			.after('<div class="banner_carousel_pagin">')
			.cycle({
			fx     : 'fade',
			speed  : 2000,
            timeout: timeout_length,
			pager  : '.banner_carousel_pagin',
			pause  : 1
		});
        

        $('.banner_carousel_pagin').css('visibility','visible');

		$('.banner_carousel_pagin a').click(function() {
			$('#swf_panel').cycle('pause');

			var all_anchors = $(this).parent().find('a');

			$(this).addClass('pagin_button_' + (all_anchors.index($(this))+1) + '_hover');

			all_anchors.not('.activeSlide').each(function(){
				$(this).removeClass('pagin_button_' + (all_anchors.index($(this))+1) + '_hover');
			});
		});

		var counter = 0;
		$('.banner_carousel_pagin a').each(function() {
			counter++;
			$(this).addClass('pagin_button_' + counter).html('');
		});
	}
	
	// Search Box in default_top.inc.php
	// Clear the search box when a user focuses on it
	$("#search_input").focus(function() {
		$(this).text = "";
	});
	
	// AW11 Slideshow
	if($("#aw11_slide").length){
		$("#aw11_slide").cycle({
			fx:      'fade',
			timeout: 4000,
			speed:   1500,
			pager:   '#aw11_slide_nav_dots',
			next:    '#aw11_slide_nav_next',
			prev:    '#aw11_slide_nav_prev'
		});
	}
	
	$("#nav1").hoverIntent({
		over: function(){
			$('.main_nav_overlay').hide();
			$('#nav_overlay_mens').fadeIn(100);
		},
		timeout: 100,
		sensitivity:6,
		interval:80,
		out: function(){
			$('#nav_overlay_mens').fadeOut(100);
		}
	});
	$("#nav3").hoverIntent({
		over: function(){
			$('.main_nav_overlay').hide();
			$('#nav_overlay_womens').fadeIn(100);
		},
		timeout: 100,
		sensitivity: 6,
		interval:80,
		out: function(){
			$('#nav_overlay_womens').fadeOut(100);
		}
	});
});


