/**
 * @author josed
 */
$('<link rel="stylesheet" href="lib/styles/js-enabled.css" type="text/css"  />').appendTo($('head'));
var isIE6 = false;
var isIE7 = false;

var awe = {
    mainMenu: {
        init: function() {
            $("#menu ul li ul").css({ display: "none" });
            $("#menu ul li").hoverIntent(this.openItem, this.closeItem);
            $("#menu a.active").parent().addClass("active");
            //	$("#menu a").attr("target","");
            $("#menu ul li#nav-shop a").attr('target', '_blank');
        },
        openItem: function() {
            $(this).find('ul:first:hidden').css({ visibility: "visible", display: "none" }).slideDown(200);
            $(this).addClass('rollover');
        },
        closeItem: function() {
            $(this).find('ul:first').slideUp(200);
            $(this).removeClass('rollover');
        }
    },
    masthead: {
        opened: false,
        sliderHeight: 380, //Default Height of Masthead
        init: function() { //Not needed because this should only be called from flash. Only here for testing
            $("#masthead-container a").each(function() {
                $(this).click(function(event) {
                    event.preventDefault();
                    awe.masthead.toggleSlider();
                });
            });
        },
        toggleSlider: function(open_height) {
            if (!awe.masthead.opened) {
                if (!open_height) {
                    open_height = $("#masthead").height();
                }
                $("#wrap").animate({ "top": open_height + "px" }, { duration: "fast" });
                //$("#footerfootball").animate({ "bottom": -50 - open_height + "px" }, { duration: "fast" });
                awe.masthead.opened = true;
            } else {
                $("#wrap").animate({ "top": awe.masthead.sliderHeight + "px" }, { duration: "fast" });
                awe.masthead.opened = false;
            }
        }
    },
    dropdownmenu: {
        init: function() {
            // $("html:not(.dropdown *)").bind('click', awe.dropdownmenu.clickAnywhereClose);
            // $(".dropdownBox a").bind('click', awe.dropdownmenu.clickInnerLinkClose);
            $(".filter a").click(function(event) { awe.dropdownmenu.open(event, this.id); });
        },
        clickAnywhereClose: function(event) {
            $(".dropdownBox").slideUp();
        },
        close: function(event, id) {
            event.preventDefault();
            $("#" + id + "-list").slideToggle();
            return false;
        },
        open: function(event, id) {
            event.preventDefault();
            $("#" + id + "-list").slideToggle();
            return false;
        }
    },
    tableRollovers: {
        init: function() {
            $(".post-table td").mouseout(awe.tableRollovers.tableMouseout).mouseover(awe.tableRollovers.tableMouseover);
        },
        tableMouseout: function() {
            $(this).parent().children("td").removeClass("table-rollover");
        },
        tableMouseover: function() {
            $(this).parent().children("td").addClass("table-rollover");
        }
    },
    previouspostsRollovers: {
        init: function() {
            $(".prevpost").mouseout(awe.previouspostsRollovers.postMouseout).mouseover(awe.previouspostsRollovers.postMouseover);
        },
        postMouseout: function() {
            $(this).removeClass("table-rollover");
        },
        postMouseover: function() {
            $(this).addClass("table-rollover");
        }
    },
    share: {
        init: function() {
            $("#share-bar ul li ul").css({ display: "none" });
            $("#share-bar ul li:first").hoverIntent({
                over: this.openItem,
                out: this.closeItem,
                timeout: 300
            });
        },
        openItem: function() {
            $(this).find('ul:first:hidden').css({ visibility: "visible", display: "none" }).slideDown(200);
            $("#share-bar").addClass('open');
            $(this).addClass('rollover');
        },
        closeItem: function() {
            $(this).find('ul:first').slideUp(100, function() {
                $("#share-bar").removeClass('open');
                $(".rollover").removeClass('rollover');
            });
        }
    },
    productGrid: {
        init: function() {

            if ($('div.select-color').length > 0) awe.productGrid.colorFilter();

            if ($('ul.product-list').length > 0) {
                $('ul.product-list li').hover(function() {
                    $(this).addClass('hover');
                },
				function() {
				    $(this).removeClass('hover');
				});
            }

            if ($('ul#gridNav').length > 0) {
                var ulWidth = $('ul#gridNav').width();
                var liWidth = 0;

                $('ul#gridNav li').each(function() {
                    liWidth += $(this).width();
                }
				);
                var liPaddings = 6;
                if ($('ul#gridNav').parents('.micoach-wrap').length > 0) liPaddings = 5
                liWidth = ulWidth - liWidth - liPaddings;
                $('<li class="shadowli" style="width: ' + liWidth + 'px;">&nbsp;</li>').appendTo($('ul#gridNav'));

            }


        },
        colorFilter: function() {

            $('div.select-color').each(function() {
                var colorName = $(this).find('label').html();
                var colorBlock = $('<span class="color-filter ' + colorName + '">' + colorName + '</span>');
                $(this).find('input, label').addClass('hidden');
                $('<span class="color-filter ' + colorName + '" title="' + colorName + '">' + colorName + '</span>').appendTo($(this));

                if ($(this).find('input').is(':checked')) $(this).find('span').toggleClass('selected');
            });

            $('span.color-filter').hover(
					function() {
					    $(this).addClass('hover');
					},
					function() {
					    $(this).removeClass('hover');
					}
				);
            $('span.color-filter').click(
					function() {
					    $(this).parent().find('input').click();


					}
				);
        },
        requestFiltering: function() {
            // change event on any input in the filter form
            // when event is fired, new page is requested

        }

    },
    ModalScreen: {
        init: function() {
            if ($('#Overlay').length > 0) return;

            //create modal screen
            if ($('#ModalScreen').length == 0) {
                $('body').append('<div id="ModalScreen"></div>');
                $('#ModalScreen').click(function() {
                    ProvNHOverlay.hide();
                });
            }
            //create iframe mask
            if ($('#ModalMask').length == 0) $('body').append('<iframe id="ModalMask" src="../images/backgrounds/fake-src.gif"></iframe>');

        },
        sizing: function() {

            var h = $(window).height() >= $('#PaginaContainer').height() ? $(window).height() : $('#PaginaContainer').height() + 30;

            if ($('#OverlayFrame').length > 0) {
                h = h >= ($('#OverlayFrame').height() + 60) ? h : ($('#OverlayFrame').height() + 60);
            }
            $('#ModalScreen').height(h);
            $('#ModalMask').height(h);

            var w = $(window).width() >= $('#PaginaContainer').width() ? $(window).width() : $('#PaginaContainer').width() + 40;
            $('#ModalScreen').width(w);
            $('#ModalMask').width(w);
        },
        show: function() {
            ProvNHModalScreen.init();
            ProvNHModalScreen.sizing();
            $('#ModalScreen').show();
            $('#ModalMask').show();
            $(window).bind("resize", function() {
                ProvNHModalScreen.sizing();
            });
        },
        hide: function() {
            $('#ModalScreen').hide();
            $('#ModalMask').hide();
            $(window).unbind("resize", function() {
                ProvNHModalScreen.sizing();
            });
        }
    },
    Overlay: {
        init: function() {

            if ($('ul.product-list').length < 1) return;


            $('div#grid').css({ position: 'relative' });
            if (!isIE6 || !isIE7) $('<div id="ProductDetail" class="ProductDetailOverlay"></div>').appendTo($('div#grid'));

            $('ul.product-list li').each(function() {
                var detailLink = $(this).find('a').attr('href');
                var tmpLink = detailLink.split('#');
                if (isIE6 || isIE7) {
                    var triggerLink = tmpLink[0];
                } else {
                    var triggerLink = tmpLink[0] + '&ajax=true#' + tmpLink[1];
                }

                $(this).attr('id', triggerLink);
            });

            $('ul.product-list li a').click(function(e) {
                //$('#ProductDetail').jqmShow();
                e.preventDefault();
            });

            if (!isIE6 || !isIE7) {
                $('#ProductDetail').jqm(
				{
				    overlay: 70,
				    ajax: '@id',
				    modal: true,
				    overlayClass: 'ProductDetailOverlay',
				    closeClass: 'close-product-details',
				    onLoad: function() {
				        //alert('onShow');
				        awe.ProductDetail.tab();
				        awe.ProductDetail.filter();
				        $('.jq-vertical-align').vAlign();
				    }
				});
                $('#ProductDetail').jqmAddTrigger($('ul.product-list li'));
            }

            if (isIE6 || isIE7) {
                $('ul.product-list li').click(function() {
                    window.location.href = $(this).attr('id');
                });
            }

        },
        show: function() {
            $('#ProductDetail').jqmShow();
        },
        hide: function() {
            $('#ProductDetail').jqmHide();
        }

    },
    ProductDetail: {
        filter: function() {
            var id = 1;
            $('.filters .filter').each(function() {
                $(this).find('.filter-content').hide();
                $(this).attr('id', id);
                id++;
            });

            $('.filter-button').bind('click', function() {
                $self = $(this).parent('.filter');
                $self.find('.filter-content').slideToggle();

                $(this).parents('.filters').find('.filter').each(function() {
                    if ($self.attr('id') != $(this).attr('id')) {
                        $(this).find('.filter-content').slideUp();
                    }
                });
            });

            $('.filter-view a').click(function() {
                $('div.product-image img').attr('src', $(this).attr('href'));
                return false;
            });


            $('.filter-color a').click(function() {
                var url = $(this).attr('href');

                var tmpLink = url.split('?');
                var triggerLink = tmpLink[0] + '?ajax=true&' + tmpLink[1];

                $.get(url, function(data) {
                    $('#ProductDetail').html($(data).find('#details-block'));

                    awe.ProductDetail.tab();
                    awe.ProductDetail.filter();
                    $('.jq-vertical-align').vAlign();

                });
                return false;
            });

        },

        tab: function() {
            // Tab content
            if ($('.tab-content')) {
                $ul = $('<ul />').addClass('tab');

                $('.tab-content div').each(function(i) {
                    $(this).attr('id', 'tab-' + i).addClass('tab');
                    $h3 = $(this).find('h3');
                    var title = $h3.html();
                    $h3.remove();

                    var content = $('<span/>').html(title);
                    $li = $('<li/>').attr('id', 'tab-' + i).html(content);
                    if ($(this).hasClass('default')) {
                        $li.addClass('active');
                    }

                    $ul.append($li);

                    if (!$(this).hasClass('default')) {
                        $(this).hide();
                    }

                });
                $('.tab-content').prepend($ul);

                $('.tab li:not(.details-item)').bind('click', function() {
                    $parent = $(this).parent();
                    $el = $(this);
                    if (!$el.hasClass('active')) {
                        $('.tab li').removeClass('active');

                        $el.addClass('active');
                        $('.tab-content div').fadeOut();
                        var rel = $el.attr('id');
                        //$('.tab-content').find($el.attr('id')).fadeOut()
                        $('.tab-content').find('#' + rel).fadeIn();

                    }
                    return false;
                });
            }
        }
    },
    ShoeFinder: {
        init: function() {

            // code to loop through list, check line length and break into two evenly-spaced lines if necessary
            var sfItems = $("#slides li");
            var sfItemsLen = sfItems.length;
            for (x = 0; x < sfItemsLen; x++) {
                var sfItemWidth = $(sfItems[x]).children("a").children("span").width();
                if (sfItemWidth > "310") {
                    $(sfItems[x]).addClass("sfMultiLine");
                }
            }

            //grab the height and calculate top value
            var item_height = $('#slides li').outerHeight();
            var top_value = item_height * (-1);

            //move the last item before first item, just in case user clicks prev button
            $('#slides li:first').before($('#slides li:last'));

            //set the default item to the correct position 
            $('#slides ul').css({ 'top': top_value - 1 });

            //when the user clicks "previous"
            $('#sfprev').click(function() {
                //get the top position         
                $('#slides ul').stop(true, true);
                var top_indent = parseInt($('#slides ul').css('top')) + item_height;
                var t = setTimeout('$(".sfActive").removeClass("sfActive").prev("li").addClass("sfActive")', 150);
                $('#slides ul').animate({ 'top': top_indent }, 300, function() {
                    $('#slides li:first').before($('#slides li:last'));
                    $('#slides ul').css({ 'top': top_value });
                });
                return false;
            });

            //when the user clicks "next"
            $('#sfnext').click(function() {
                //get the bottom position
                $('#slides ul').stop(true, true);
                var top_indent = parseInt($('#slides ul').css('top')) - item_height;
                var t = setTimeout('$(".sfActive").removeClass("sfActive").next("li").addClass("sfActive")', 150);
                $('#slides ul').animate({ 'top': top_indent }, 300, function() {
                    $('#slides li:last').after($('#slides li:first'));
                    $('#slides ul').css({ 'top': top_value });
                });
                return false;
            });

            $("#slides ul li:not(.sfActive)").live("click", function() {
                return false;
            });
        }
    },
    tabResizes: {
        init: function() {

            $('#tout-products ul li').click(function(e) {
                e.preventDefault();
                var url = $(this).find('a').attr('href');
                self.location.href = url;
            });

            var tabNavigations = $('div.div-content');
            tabNavigations.hide().filter(':first').show();

            $('div.tab-container ul#gridNav li:first').addClass('active');
            $('div.tab-container').height(387);
            $('div.tab-container').filter(".colorzone").height(320);

            $('div.tab-container ul#gridNav li a, .colorzone div.div-content a.tag-play:not(:last)').click(function(e) {
                $('div.tab-container').stop(true, true);
                e.preventDefault();
                tabNavigations.hide()
                $('div.tab-container ul li').removeClass('active');
                if ($(this).is("div.tab-container ul#gridNav li a")) {
                    $(this).parent().addClass('active');
                } else {
                    var runnertab = $(this).attr("id");
                    $('div.tab-container ul#gridNav li.'+runnertab).addClass('active');
                }
                

                var divHeight = parseInt($(this.hash).height());
                divHeight = divHeight + 36;
                $('div.tab-container').animate({ height: divHeight }, 400);
                tabNavigations.filter(this.hash).show();

                

                return false;

            });
            /*$('div.div-content a.tag-play').click(function() { 
            
            

      
            });*/
            $('div.tab-container ul#gridNav').css("position", "absolute");
            $('div.tab-container div.div-content').css("margin-top", "40px");
        }
    }
};

/**
 * StyleSwitch
 * 
 * loads additional stylesheet for Windows platform style overrides
 * 
 */
StyleSwitch = {	
	init: function()
	{
		// if platform is windows, append windows.css to document head
		if ( navigator.appVersion.indexOf("Win")!=-1 ) 
		{
			$('<link rel="stylesheet" type="text/css" href="lib/styles/windows.css" />').appendTo($('head'));
		}
	}	
}
StyleSwitch.init();

$(document).ready(function() {
	if ( $.browser.msie && $.browser.version.substr(0,1) == 6 ) isIE6 = true;
	if ( $.browser.msie && $.browser.version.substr(0,1) == 7 ) isIE7 = true;
	
	awe.Overlay.init();	
	adjustHeights();
	awe.mainMenu.init();
	//awe.masthead.init();
	awe.dropdownmenu.init();
	awe.tableRollovers.init();
	//awe.previouspostsRollovers.init();
//	$("#tabs").tabs();
	$("a.fancybox").fancybox();
	awe.productGrid.init();

    awe.ShoeFinder.init();
    awe.ProductDetail.tab();
    awe.ProductDetail.filter();
	awe.tabResizes.init();
    $('.jq-vertical-align').vAlign();

    //Analytics
    analyticsWrapper.init();



   
   
    
    
    
    
});


function adjustHeights() {
    $('#footerfootball').attr("style", "bottom: 0px; ");
    var windowHeight = $(window).height();
    var wrapHeight = $("#wrap").height();

    if ((windowHeight - 130) > wrapHeight) {
        $('#wrap').css('height', windowHeight - 130);
    } else if ((windowHeight - 130) < wrapHeight) {
        $('#wrap').css('height', wrapHeight);
    } else {
        $('#wrap').css('min-height', wrapHeight);
    };
}
$(window).resize(function() {
    /*$('#wrap').css({
        'height': 'auto',
        'min-height': 'auto'
    });  // Eliminate the heights value
    adjustHeights();  // Set the heights once more*/
});













/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src");
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href");
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});

