document.documentElement.className += ' js';

if (typeof window["console"] == 'undefined') {
    var console = {};
    console.log = function () {};
}

var GMAP_API_KEY;

if (window.location.hostname.match(/salomon\.local/)) {
    GMAP_API_KEY = 'ABQIAAAAwEdfwwyxirTc82HXcd6OzxRUp957JBLWrfND4pQ4R6TDIKu7KBQpjJZH7IH61xQJx0I2LLUTmeudkw';
} else if (window.location.hostname.match(/salomon2010\.local/)) {
    GMAP_API_KEY = 'ABQIAAAAwEdfwwyxirTc82HXcd6OzxTosNwuZWcQojlmNEg2sXFdYG2_hBQMq9fRzbxSWwupBDHsiaVuU_-TBw';
} else if (window.location.hostname.match(/interfacecms.com/)) {
    GMAP_API_KEY = 'ABQIAAAAwEdfwwyxirTc82HXcd6OzxT29Yal1pdyHA-N5TJSpLv3PLicLRT_P_vNhQigatOmOHg-b2jJZT-LPg';
}

var salomon = (function () {

    var overlay = (function () {
        var wrapper = $('<div id="overlay-wrapper"></div>'),
            container = $('<div id="overlay"></div>'),
            scrim = $('<div id="overlay-scrim"></div>'),
            is_open = false;

        function resize(e) {
            wrapper.css({
                top: (((document.documentElement || document.body).scrollTop || document.body.scrollTop) + Math.round(($(window).height() / 2) - (wrapper.height() / 2))) + 'px',
                left: (((document.documentElement || document.body).scrollLeft || document.body.scrollLeft) + Math.round(($(window).width() / 2) - (wrapper.width() / 2))) + 'px'
            });

            scrim.css({ height: $(document).height() + 'px' });
        }

        function open(url,callback) {
            function display(html) {
                var c = $('<div class="tgoth close">Close X</div>');

                container.html(html).append(c);
                c.click(close);

                if ($.support.opacity) {
                    wrapper.css({
                        opacity: '0',
                        display: 'block'
                    });
                } else {
                    wrapper.css({
                        visibility: 'hidden',
                        display: 'block'
                    });
                }
                wrapper.css({
                    top: (((document.documentElement || document.body).scrollTop || document.body.scrollTop) + Math.round(($(window).height() / 2) - (wrapper.height() / 2))) + 'px',
                    left: (((document.documentElement || document.body).scrollLeft || document.body.scrollLeft) + Math.round(($(window).width() / 2) - (wrapper.width() / 2))) + 'px'
                });

                Cufon.replace('#overlay .tgoth', { fontFamily: 'tgoth' });
                Cufon.replace('#overlay .modbold', { fontFamily: 'modbold' });

                if ($.support.opacity) {
                    wrapper.animate({ opacity: 1 }, 250);
                } else {
                    wrapper.css({ visibility: '' });
                }

                is_open = true;
                $(window).resize(resize);
            }

            if (flashbg.wasPlaying()) flashbg.pauseVideo(true);

            scrim.css({ height: $(document).height() + 'px' });
            if ($.support.opacity) {
                scrim.fadeIn(250);
            } else {
                scrim.show();
            }

            $.ajax({
                dataType: 'html',
                url: url,
                success: function(html) {
                    display(html);
                    if (callback) {
                        callback();
                    }
                }
            });
        }

        function close() {
            if (is_open) {
                if (document.getElementById('mediaplayer')) {
                    swfobject.removeSWF('mediaplayer');
                }

                if ($.support.opacity) {
                    wrapper.fadeOut(250);
                    scrim.fadeOut(250, function () { if (flashbg.wasPlaying()) flashbg.playVideo(); });
                } else {
                    wrapper.hide();
                    scrim.hide();
                    if (flashbg.wasPlaying()) flashbg.playVideo();
                }

                $(window).unbind('resize', resize);
                is_open = false;
            }
        }

        $(document).ready( function () {
            $(document.body).append(scrim).append(wrapper.append(container).append('<div class="bottom"></div>'));
            scrim.click(close);
        });

        return {
            open: open,
            close: close,
            isOpen: function () { return is_open; }
        };
    })();

    function common() {
        var hide_all = $('<a id="hideall" href="#" rel="close">Toggle Content</a>');

        $('#contentarea').append(hide_all);
        hide_all.click(	function () {
			      if ($(this).hasClass ("hidden")) {
				        $(this).removeClass('hidden');
				        $(this).siblings().show();
			      } else {
				        $(this).addClass('hidden');
				        $(this).siblings().hide();
			      }

            return false;
		    });

        // hide focus for nav elements in IE
        hide_all.get(0).hideFocus = true;

        $('p.share').click(function() { $(this).next('.sharebox').toggle(); return false; });
        $('.sharebox .close').click(function() { $(this).parent().toggle(); return false; });

	      Cufon('.modbold', { hover: true, fontFamily: 'modbold' });
	      Cufon('.tgoth', { hover: true, fontFamily: 'tgoth' });
	      Cufon('.mrseav', { hover: true, fontFamily: 'mrseav' });
	      Cufon.now();
    }

    var home = (function () {
        var feature;

        function resize() {
            feature.css({ top: (Math.round(($(window).height() / 2) - feature.height() / 2)) + 'px' });
        }

        var api = function () {
            feature = $('#homefeature');

            feature.css({
                position: 'absolute',
                top: (Math.round(($(window).height() / 2) - feature.height() / 2)) + 'px'
            });

            $(window).resize(resize);
            //flashbg.scrimOn();
        };

        api.unload = function () {
            $(window).unbind('resize', resize);
            //flashbg.scrimOff();
        };

        return api;
    })();

    var productDetail = (function () {
        var variation,
            feedback_chars = 140;

        function like(e) {
            var liked = $(this.parentNode);

            $.get(this.href, function (response) {
                liked.closest('.tools').children('.likes').empty().text(response + ((+response) == 1 ? ' like' : ' likes'));
                Cufon.refresh('.tgoth');
            });

            liked.addClass('liked').empty();

            return false;
        }

        function wishlist(e) {
            var wishlist = $(this.parentNode);

            $.get(this.href, function (response) { $('#wishlist-count').text(response); });
            wishlist.addClass('added');
            this.href = "http://" + (window.location.host || window.location.hostname) + "/wishlist/";
            $(this).unbind();

            return false;
        }

        function shareProduct(e) {
            $('.sharebox').toggle();
            return false;
        }

        function showFeedback(e) {
            $('#feedback').addClass('show-form');
            return false;
        }

        function cancelFeedback(e) {
          $('#feedback').removeClass('show-form');
           return false;
        }

        function feedbackPaging(e) {
            $.get(this.href, function (response) {
                $('#feedbacks').html(response);
                $('#feedback-paging a').click(feedbackPaging);
            });

            return false;
        }

        function feedback(e) {
            var form = $(this);

            e.preventDefault();

            function validate() {
                var valid = true,
                    errors = [];

                // make sure all required fields have a value
                form.find('.required').each(function () {
                    var parent = $(this.parentNode);

                    if (!this.value) {
                        errors.push('<li>' + parent.children('label').text() + ' is required.</li>');
                        parent.addClass('error');
                        valid = false;
                    } else {
                        parent.removeClass('error');
                    }
                });

                // check any special cases
                if (valid) {
                    // check email format
                    if (!(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test($('#feedback-email').val()))) {
                        errors.push('<li>Email address is invalid.</li>');
                        $('#feedback-email').parent().addClass('error');
                        valid = false;
                    }

                    // check feedback length
                    if ($('#feedback-comment').val().length > feedback_chars) {
                        errors.push('<li>Feedback must be less than ' + feedback_chars + ' characters long.</li>');
                        $('#feedback-form').addClass('over-limit');
                        valid = false;
                    }
                }

                if (!valid) {
                    form.children('.error-list').empty().append(errors.join('\r'));
                    form.addClass('has-errors');
                } else {
                    form.removeClass('has-errors');
                }

                return valid;
            }

            $('feedback-form').addClass('loading');

            if (validate()) {
                $.ajax({
                    data: form.serialize(),
                    type: 'post',
                    dateType: 'html',
                    url: form.attr('action'),
                    success: function (response) {
                        $('#feedbacks').html(response);
                        $('#feedback-paging a').click(feedbackPaging);
                        $('#feedback').removeClass('show-form');
                        form.get(0).reset();
                        $('#feedback-count span').text(feedback_chars);
                    },
                    error: function (xhr, status, error) {
                        form.children('.error-list').empty().text(xhr.responseText);
                        form.addClass('has-errors');
                    },
                    complete: function () {
                        $('feedback-form').removeClass('loading');
                    }
                });
            }
        }

        var over_limit = false;
        function characterCount(e) {
            var len = this.value.length,
                diff = feedback_chars - len;

            if (diff < 0 && !over_limit)  {
                $('#feedback-form').addClass('over-limit');
                over_limit = true;
            } else if (over_limit && diff >= 0) {
                $('#feedback-form').removeClass('over-limit');
                over_limit = false;
            }

            $('#feedback-count span').text(diff);
        }

        var api = function (path) {
            variation = path[3] || variation;

            $('.tools .like a').click(like);
            $('.tools .wishlist:not(.added) a').click(wishlist);
            $('#sharebtn').click(shareProduct);

            $('#show-feedback-form').click(showFeedback);
            $('#feedback-form .close').click(cancelFeedback);
            $('#feedback-paging a').click(feedbackPaging);

            $('#feedback-form .submit').append('<div id="feedback-count"><span>' + feedback_chars + '</span> characters left</div>');
            $('#feedback-comment').keyup(characterCount);

            $('#feedback-form').submit(feedback);
        };

        api.update = function (path) {
            var gender = path[0],
                category = path[1],
                product = path[2],
                variation = path[3] || variation;

            $('#variation-info').addClass('loading');

            $.ajax({
                dataType: 'html',
                method: 'get',
                url: '/extensions/fetch_variation/?&gender=' + gender + '&category=' + category + '&product=' + product + '&variation=' + variation,
                success: function (response) {
                    $('#variation-info').html(response);
                    $('.tools .like a').click(like);
                    $('.tools .wishlist a').click(wishlist);
                    Cufon.refresh('.tgoth');
                },
                complete: function () {
                    $('#variation-info').removeClass('loading');
                }
            });
        };

        // this is used by the product detail template to tell us what variation is being rendered
        // we need this because it is possible to enter a product page without specifying a variation in the address
        api.setVariation = function (slug) {
            variation = slug;
        };

        return api;
    })();

    var productGrid = (function () {
        var gender, category;

        function filterSetup() {
            // maintains a list of filter settings
            var filters = (function () {
                var obj = {}, api;

                // filters(key, value): add key value pair to the filter list, stepping on any values that already exist for that key
                api = function (key, value) {
                    obj[key] = [];
                    value.split(',').forEach(function (v) {
                        obj[key].push(v);
                    });
                };

                // add a value to a key list, without stepping on any previous values
                api.add = function (key, value) {
                    if (!obj[key]) obj[key] = [];

                    value.split(',').forEach(function (v) {
                        if (obj[key].indexOf(v) == -1) {
                            obj[key].push(v);
                        }
                    });
                };

                // remove a value from a key list, or the entire key if no value is provided
                api.remove = function (key, value) {
                    var index;

                    if (obj[key]) {
                        if (!value) {
                            delete obj[key];
                        } else {
                            value.split(',').forEach(function (v) {
                                index = obj[key].indexOf(v);
                                if (index != -1) {
                                    obj[key].splice(index, 1);
                                    if (obj[key].length === 0) delete obj[key];
                                }
                            });
                        }
                    }
                };

                // either add or remove a value from a key, depending on whether it exists or not
                api.toggle = function (key, value) {
                    var index;

                    if (!obj[key]) {
                        api(key, value);
                    } else {
                        value.split(',').forEach(function (v) {
                            index = obj[key].indexOf(v);
                            if (index == -1) api.add(key, v);
                            else api.remove(key, v);
                        });
                    }
                };

                api.clear = function () { obj = {}; };

                // returns a serialized version of all filter settings (also tacks on gender and category, if set)
                api.toQueryString = function () {
                    var string = [];

                    for (key in obj) {
                        if (obj.hasOwnProperty(key)) {
                            string.push(key + '=' + obj[key].join(','));
                        }
                    }

                    return '?gender=' + gender + (category ? '&category=' + category : '') +
                        (string.length ? '&' + string.join('&') : '');
                };

                return api;
            })();

            function toggleSetting(e) {
                var el = $(this),
                    filter = this.href.match(/\?(.*)$/)[1],
                    filter_type = el.closest('.filtertype').closest('li');

                el.toggleClass('selected');

                // are there any filters set?
                if (filter_type.find('.selected').length > 0) {
                    filter_type.addClass('on');
                } else {
                    filter_type.removeClass('on');
                }

                filter = filter.split('=');
                filters.toggle(filter[0], filter[1]);
                updateFilters(filters.toQueryString());

                return false;
            }

            function clearFilter(e) {
	              var el = $(this),
                    parent_node = el.closest('.filtertype'),
                    filter_type = parent_node.attr('id');

	              parent_node.find('.selected').removeClass('selected');
                $('.filters').find('li.' + filter_type).removeClass('on');

                // if this is a slider, we need to reset it
                parent_node.find('.ui-slider').each(resetSlider);

                filters.remove(filter_type);
                updateFilters(filters.toQueryString());

                return false;
            }

            function clearAllFilters(e) {
                var list = $('.filters');

                // reset sliders
                $('.ui-slider').each(resetSlider);

                filters.clear();

                updateFilters(filters.toQueryString());
	              list.find('.active,.selected,.on').removeClass('active').removeClass('selected').removeClass('on');
                $('#filter-list').css({ paddingBottom: '24px' });

                Cufon.refresh('.tgoth');

                return false;
            }

            function resetSlider() {
                var filter = $(this),
                    parent_node = this.parentNode,
                    min = filter.slider('option', 'min'),
                    max = filter.slider('option', 'max'),
                    handles = filter.find('.ui-slider-handle span');

                filter.slider('values', 0, min);
                filter.slider('values', 1, max);

                $(handles[0]).text(min);
                $(handles[1]).text(max);
                Cufon.refresh('#' + parent_node.id + ' .ui-slider-handle span');
            }

            function sliderSetup() {
                var parent_node = this,
                    range_input = $(parent_node.parentNode).children('.range-input'),
                    initial_values,
                    current_values = range_input.val().split('-'),
                    slider = $('<div></div>'),
                    handles;

                    // determine min and max values based on id
                switch (parent_node.id) {
                  case 'size-range-slider':
                        initial_values = [125, 166];
                    break;
                  case 'flex-range-slider':
                    initial_values = [1, 5];
                    break;
                }

                // create the slider
                slider.slider({
                    range: true,
                    min: initial_values[0],
                    max: initial_values[1],
                    values: [+(current_values[0]), +(current_values[1])],
                    slide: function (e, ui) {
                        var handles = slider.find('.ui-slider-handle span');

                        range_input.val(ui.values[0] + '-' + ui.values[1]);

                        $(handles[0]).text(ui.values[0]);

                        // do not show max value if it equals min
                        if (ui.values[0] != ui.values[1]) {
                            $(handles[1]).text(ui.values[1]);
                        } else {
                            $(handles[1]).text('');
                        }

                        Cufon.refresh('#' + parent_node.id + ' .ui-slider-handle span');
                    },

                    // do not use the change event, because on filter reset,
                    //   we need to programatically set values without triggering this event
                    stop: function (e, ui) {
                        var filter_type = $(this).closest('.filtertype').closest('li');

                        filter_type.addClass('on');

                        filters(range_input.attr('name'), range_input.val());
                        updateFilters(filters.toQueryString());
                    }
                });

                handles = slider.children('.ui-slider-handle');
                $(handles[0]).append('<span class="left">' + current_values[0] + '</span>');

                // do not show max value if it equals min
                if (current_values[0] != current_values[1]) {
                    $(handles[1]).append('<span class="right">' + current_values[1] + '</span>');
                } else {
                    $(handles[1]).append('<span class="right"></span>');
                }

                $(parent_node).append(slider);
                Cufon.replace('#' + parent_node.id + ' .ui-slider-handle span', { fontFamily: 'tgoth' });
            }

            function displayFilter(e) {
		            var cat = $(this).attr('href').split('#')[1],
		                filter = $('#' + cat),
                    filter_link = filter.closest('.' + cat),
                    height;

		            if (!filter_link.hasClass('active') == true) {
		                $('#filter-list > li').removeClass('active');
  		              $('.filters .filtertype').hide();

  		              filter_link.addClass('active');
                    filter.css({ visibility: 'hidden', display: 'block' });
                    height = filter.parent().height();

                    $('#filter-list').css({ paddingBottom: height + 'px' });
                    filter.css({ visibility: 'visible' });
		            } else {
                    filter.hide();
                    filter_link.removeClass('active');
                    $('#filter-list').css({ paddingBottom: '24px' });
                }

                Cufon.refresh('.tgoth');

                return false;
		        }

            // eventually this will strip old filter info from swfaddress path, and set a new path with updated info
            function updateFilters(filters) {
                router.go(SWFAddress.getValue().split('?')[0] + filters);
            }

            // save any initial filter values on page load
            var settings = SWFAddress.getValue().split('?')[1];
            if (settings) {
                ['flex', 'price', 'shape', 'mustache', 'collection', 'lacing', 'liner'].forEach(function (setting) {
                    var value = settings.match(new RegExp(setting + '=([^&]*)'));
                    if (value) {
                        filters(setting, value[1]);
                        displayFilter.call($('.' + setting + ' .category'));
                    }
                });
            }

            // top level filter toggle
	          $('.filters .category').click (displayFilter);

            // individual filter link toggle
	          $('.filterbox ul a').click(toggleSetting);

            // filter clearing
            $('.filters .rm_filtertype').click(clearFilter);
	          $('.filters .rm_all').click(clearAllFilters);

            // sliders
            $('#flex-range-slider, #size-range-slider').each(sliderSetup);

            // hide focus in IE
            $('.filters a').each(function () { this.hideFocus = true; });
        };

        function gridSetup() {
            $('.grid li .product').hover(
		            function () { $(this).addClass('over'); },
		            function () { $(this).removeClass('over'); }
            );
        }

        var api = function (path) {
            gender = path[0];
            category = (path[1] && path[1].charAt(0) != '?') ? path[1] : null;

            $('#content').append('<img id="indicator" src="/frontend/img/loader.gif" />');

            filterSetup();
            gridSetup();
        };

        api.update = function (settings) {
            settings = settings || '';

            if (!settings.match(/gender/)) {
                settings += (settings.length ? '&' : '') + 'gender=' + gender;
            }

            if (!settings.match(/category/) && category) {
                settings += (settings.length ? '&' : '') + 'category=' + category;
            }

            $('#content').addClass('loading');

            $.ajax({
                dataType: 'html',
                method: 'get',
                url: '/extensions/fetch_product_grid/?' + settings,
                success: function (response) {
                    $('#product-grid').html(response);
                    gridSetup();
                    Cufon.refresh('.modbold').refresh('.tgoth');
                },
                complete: function () {
                    $('#content').removeClass('loading');
                }
            });
        };

        return api;
    })();

    var compareProducts = (function () {
        var gender, category;

        function toggleList() {
            $(this.parentNode).children('.product-list').toggle();
        }

        function updateQuery() {
            var ids = [], query;

            $('.product-selector input').each( function () {
                if (this.value)
                    ids.push(this.value);
            });

            query = '?gender=' + gender + '&category=' + category + '&productID=' + ids.join(',');
            router.go(SWFAddress.getValue().split('?')[0] + query);
        }

        function changeItem(e) {
            var id = this.href.match(/productID=(\d+)/)[1],
                el = $(this).closest('.product-selector'),
                input = el.children('input');

            $('#product-comparison').addClass('loading');

            input.val(id);
            updateQuery();

            return false;
        }

        var api = function (path) {
            gender = path[0];
            category = path[1];

            $('.product-selector a').click(changeItem);
            $('.product-selector h3').click(toggleList);
         };

        api.update = function (settings) {
            settings = settings || '';

            if (!(/gender/.test(settings))) settings += '&gender=' + gender;
            if (!(/category/.test(settings))) settings += '&category=' + category;

            $.ajax({
                dataType: 'html',
                method: 'get',
                url: '/extensions/fetch_comparison/?' + settings,
                success: function (response) {
                    $('#product-comparison').html(response);
                    $('.product-selector a').click(changeItem);
                    $('.product-selector h3').click(toggleList);
                    Cufon.refresh('.tgoth');
                },
                complete: function () {
                    $('#product-comparison').removeClass('loading');
                }
            });
        };

        return api;
    })();

    var teamLanding = (function () {
        function setup() {
            $('#team-types a').click(swapTeams);
            $('#members li').hover(
      		      function () {
      			        $(this).children('.img').hide();
      			        $(this).children('.hover').show();
      		      },
      		      function () {
      			        $(this).children('.hover').hide();
      			        $(this).children('.img').show();
      		      }
      	    );
        }

        function swapTeams(e) {
            $('#team-types .active').removeClass('active');
            $(this).addClass('active');
        }

	      var api = function () {
            setup();
        };

        api.update = function (team) {
            $('#content').addClass('loading');

            $.get('/extensions/fetch_team/?team=' + encodeURIComponent(team), function (response) {
                $('#members').html(response);
                setup();
                Cufon.refresh('.tgoth');

                $('#content').removeClass('loading');
            });
        };

        return api;
    })();

    function teamDetail() {
        $('#media-list a').each( function (i) {
            $(this).data('index', i).click( function () {
                var el = $(this);
                mediaplayer.swapMedia(el.data('index'));
                $('#media-list .on').removeClass('on');
                el.addClass('on');

                return false;
            });
        });
    }

    function wishList() {
        $('#wishlist-items').html() == "0" ? $('#wishlist-empty').show() : $('#wishlist-empty').hide();

        function removeItem(e) {
            var wishlistItem = $(this).parents('li');

            $.get(this.href, function (response) {
                $('#wishlist-count').text(response);
                $('#wishlist-items').text(response);
		            if (response == "0")
		  	            $('#wishlist-empty').show();
		  	            $('.printmail').hide();
            });

            $(this).unbind('click');
            wishlistItem.hide();

            return false;
        }

	      $('.grid li .product').hover(
            function () {
                $(this).children('.image').hide();
                $(this).children('.hover').show();
            },
            function () {
                $(this).children('.hover').hide();
                $(this).children('.image').show();
            }
        );

        $('#wishlist .grid .remove').click(removeItem);
    }

    function blogPost() {
        function comment(e) {
            var form = $(this);
            e.preventDefault();

            function validate() {
                var valid = true,
                    errors = [];

                // make sure all required fields have a value
                form.find('.required').each(function () {
                    var parent = $(this.parentNode);

                    if (!this.value) {
                        errors.push('<li>' + parent.children('label').text() + ' is required.</li>');
                        parent.addClass('error');
                        valid = false;
                    } else {
                        parent.removeClass('error');
                    }
                });

                // check any special cases
                if (valid) {
                    // check email format
                    if (!(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test($('#comment-email').val()))) {
                        errors.push('<li>Email address is invalid.</li>');
                        $('#comment-email').parent().addClass('error');
                        valid = false;
                    }
                }

                if (!valid) {
                    form.children('.error-list').empty().append(errors.join('\r'));//.join('<br />'));
                    form.addClass('has-errors');
                } else {
                    form.removeClass('has-errors');
                }

                return valid;
            }

            function updateComments(comment) {
                var container = $('#comments dl');
                var comment_count = +($('#comment-count').text()) + 1;

                if (container.length == 0) {
                    $('#comments').html('<h4 class="tgoth">Comments</h4><dl></dl>');
                    Cufon.refresh('.tgoth');
                    container = $('#comments dl');
                }

                if (comment_count == 1) { $('#comment-tail').empty().text('Comment')}
                if (comment_count == 2) { $('#comment-tail').empty().text('Comments')}

                comment_count += '';
                $('#comment-count').empty().text(comment_count);
                Cufon.refresh('.tgoth');
                container.append('<dt>' + comment.name + ' | Posted just now</dt><dd>' + comment.comment + '</dd>');

            }

            $('#post-comment').addClass('loading');

            if (validate()) {
                $.ajax({
                    data: form.serialize(),
                    type: 'post',
                    dateType: 'json',
                    url: form.attr('action'),
                    success: function (response) {
                        updateComments(response);
                        form.get(0).reset();
                    },
                    error: function (xhr, status, error) {
                        form.children('.error-list').empty().text(xhr.responseText);
                        form.addClass('has-errors');
                    },
                    complete: function () {
                        $('#post-comment').removeClass('loading');
                    }
                });
            }
        }

        $('#post-comment').submit(comment);
    }

    $(document).ready( function () {
        common();

		    // tech overlays
        function overlayLink(e) {
            overlay.open(this.href);
            return false;
        }
        $('a[rel="technology"],a[rel="buynow"],a[rel="vid"]').live("click", overlayLink);

        // help prevent spam by injecting action on load
        $('a[rel="sharewishlist"]').live("click", function(e) {
            overlay.open(this.href, function() {
                $('#emailwishlist').submit(function(e) {
                    $.ajax({
                        url: "/extensions/share_wishlist/",
                        type: "post",
                        data: $('#emailwishlist').serialize(),
                        success: function() {
                            overlay.close();
                        }
                    });
                    return false;
                });
            });
            return false;
        });

	      // Handle external links
	      function exLink(e) {
            window.open(this.href);
            return false;
        }
	      $('a[rel="external"]').live("click", exLink);

	      // Print page
	      function printPage(e) {
	          window.print();
	          return false;
	      }
      	$('a[rel="print"]').live("click", printPage);

	      $('body').mouseup(
		        function(e) {
			          var isNavOpen  = $('#navigation').css('display')  == "block";
			          var isFootOpen = $('#languagebox').css('display') == "block" ||
				                       $('#dealersbox').css('display')  == "block";

			          if (!$(e.target).closest('.footerbox').length && !$(e.target).closest("#footer a.active").length && isFootOpen) {
				            $("#footer a.active").removeClass('active');
				            $('.footerbox').hide();
			          } else if (!$(e.target).closest('a.active.zoom').length && !isNavOpen) {
				            $('a.active.zoom').parent().removeClass('active');
				            $('a.active.zoom').removeClass('active').next().hide();
			          } else if (!$(e.target).closest('#navbtn').length && !$(e.target).closest('#navwrap').length && isNavOpen) {
				            $('#navbtn').removeClass('open');
				            $('#navigation').hide();
			          }
		        }
	      );

        // Navigation
	      $('#navbtn').click( function () {
		        $(this).toggleClass('open');
		        $('#navigation').toggle();

            return false;
	      });

        $('#navigation ul:not(.partners) a').click( function () {
		        $('#navbtn').removeClass('open');
		        $('#navigation').hide();
        });

	      $('#navigation .partners a.title').click ( function () {
            $('#navigation .partners .form').hide();

            return false;
        });

	      $('#navigation .partners a.title').toggle (
		        function () { $(this).next().fadeIn(); },
		        function () { $(this).next().fadeOut(); }
	      );

        $('#navigation form').submit(function() {
		        var form = $(this),
		        text = $(this).next();

		        form.removeClass('error');
		        $.ajax({
			          url: form.attr('action'),
			          type: 'post',
			          data: form.serialize(),
			          success: function(res) {
				            window.location = '/shopcrew/';
			          },
			          error: function(res) {
				            text.replaceWith('<p class="text"><strong>Invalid login!</strong> Try again or <a href="/shopcrew/sign-up/">Sign-up</a></p>');
				            text.addClass('error');
			          }
		        });

		        return false;
	      });

        // Shop Crew
        $('.subject .link').hover (
		        function () {
			          Cufon.replace($(this).siblings('h5'), {color: '#000'});
			          $(this).parent('.subject').addClass('active');
		        },
		        function () {
			          Cufon.replace('h5', {color: '#7b7970'});
			          $(this).parent('.subject').removeClass('active');
		        }
	      );

	      $('#sidebar .resources ul').hover (
		        function () {
			          Cufon.replace(('.knowledge #sidebar .resources h3'), {color: '#000000'});
		        },
		        function () {
			          Cufon.replace(('.knowledge #sidebar .resources h3'), {color: '#7b7970'});
		        }
	      );

	      $('.resources ul li a.zoom').click (
		        function () {
		            $(this).parent().addClass("active");
			        $(this).toggleClass("active").next().toggle();
		        }
	      );

        // Footer
        $('.footerbox .close').click( function () {
			      $('.footerbox').hide();
			      $('#footer a.active').removeClass('active');

            return false;
		    });

	      $('#footer .nav .flyout a').click( function () {
			      var box = $('#' + this.href.split('#')[1]),
			          wasVisible = box.is(':visible');

			      $('.footerbox').hide();
			      $('#footer a.active').removeClass('active');

			      if (!wasVisible) {
				        box.show();
				        $(this).addClass('active');
			      }

            return false;
		    });

        $('#languagebox ul a').click(function (e) {
            var location = this.href + '#' + SWFAddress.getValue();
            window.location = location;

            return false;
        });

        $('#dealersbox form').submit(function (e) {
            e.preventDefault();
            router.go('/dealers/?q=' + encodeURIComponent($('#dealersearch').val()));
            $('#dealersbox').hide();
            $('#dealersearch').val('');
        });

        function checkInput() {
            if ($('#dealersearch').val()) {
                $('#dealersearch-label').hide();
            } else {
                $('#dealersearch-label').show();
            }
        }

        checkInput();
        $('#dealersearch').focus(function () { $('#dealersearch-label').hide(); }).blur(checkInput);

        // populate the dealer flyout with nearest locations returned from CMS
        function nearestDealers(locations) {
            var dealer_list = $('#dealersbox .dealers'),
                listings, li, address;

            if (locations.length === 0) return;

            dealer_list.empty();
            listings = locations.slice(0, 3);

            for (var i = 0, len = listings.length; i < len; ++i) {
                address = listings[i].address.address.trim() +
                    (listings[i].address.address_two ? '<br />' + listings[i].address.address_two.trim() : '') +
                    '<br />' + listings[i].address.city.trim() + ', ' + (listings[i].address.state ? listings[i].address.state.trim() : (listings[i].address.country || '').trim()) + ' ' + listings[i].address.postal_code.trim();

                li = $('<li><h5>' + listings[i].name + '</h5><p>' + address + '</p><p class="viewmap"><a href="/dealers/?dealer_id=' + listings[i].id + '">Map</a></p>');
                dealer_list.append(li);

                li.click( function () { $('#dealersbox').hide(); });
            }

            Cufon.replace('#dealersbox .dealers h5', { fontFamily: 'tgoth' });
            dealer_list.removeClass('loading');
        }

        // attempt to fetch a list of dealers given the browser's reported lat/lng
        if (navigator.geolocation) {
            $('#dealersbox .dealers').addClass('loading');

            try {
                navigator.geolocation.getCurrentPosition( function (position) {
                    // we have a lat/lng, so query the api for locations
                    $.ajax({
                        url: '/api/dealers.json?search=(' + encodeURIComponent('loc within 50mi of ' + position.coords.latitude + ',' + position.coords.longitude) + ')',
                        dataType: 'json',
                        success: nearestDealers
                    });
                }, function () {
                    // no location info is available, so... nevermind, I guess
                    $('#dealersbox .dealers').removeClass('loading');
                });
            } catch (e) {
                $('#dealersbox .dealers').removeClass('loading');
            }
        }

        // hide focus for nav elements in IE
        $('#navbtn,#footer a').each(function () { this.hideFocus = true; });

	      // Preload Images
        (new Image()).src="/frontend/img/nav-bg.jpg";
        (new Image()).src="/frontend/img/nav-bg-shadow.png";
        (new Image()).src="/frontend/img/zoom-bg.png";
        (new Image()).src="/frontend/img/zoom-300-bg.png";
        (new Image()).src="/frontend/img/zoom-579-bg.png";
    });

    return {
        common: common,
        home: home,
        productDetail: productDetail,
        productGrid: productGrid,
        compareProducts: compareProducts,
        teamLanding: teamLanding,
        teamDetail: teamDetail,
        blogPost: blogPost,
        wishList: wishList,

        overlay: overlay
    };
})();

// Hooks for flash background
var flashbg = (function () {
    var videos,
        hide_timer,
        obj = null,
        was_playing = true;

    function scrimOn() {
	      if (obj) obj.scrimOn();
	      else window.setTimeout(scrimOn, 200);
    }

    function scrimOff() {
	      if (obj) obj.scrimOff();
	      else window.setTimeout(scrimOff, 200);
    }

    function pauseVideo(hide_controls) {
        if (obj) {
            obj.pauseVideo();

            if (hide_controls) {
                $('#play-pause').hide();
            } else {
                $('#play-pause').removeClass('pause').addClass('play').text('Play');
            }
        }
	      else window.setTimeout(pauseVideo, 25);
    }

    function playVideo() {
	      if (obj) {
            $('#play-pause').removeClass('play').addClass('pause').text('Pause').show();
            obj.playVideo();
        }
	      else window.setTimeout(playVideo, 25);
    }

    function swapVideo(index) {
	      if (obj) obj.swapVideo(index);
    }

    function videoChanged(index) {
        var controls = $('#bgcntrl li');

        if (controls.length) {
            controls.removeClass('active');
            $(controls[index]).addClass('active');
        } else {
            // we are experiencing a timing issue on page load
            window.setTimeout(function () { videoChanged(index); }, 100);
        }
    }

    function hideVideo() {
        if (obj) {
            obj.hideVideo();
            hide_timer = window.setTimeout(function () {
                $('#flash_bg').css({ visibility: 'hidden' });
                hide_timer = null;
            }, 1000);
        } else {
            window.setTimeout(hideVideo, 25);
        }
    }

    function showVideo() {
        if (hide_timer) {
            window.clearTimeout(hide_timer);
            hide_timer = null;
        }

        if (obj) {
            $('#flash_bg').css({ visibility: '' });
            obj.showVideo();
        } else {
            window.setTimeout(showVideo, 25);
        }
    }

    function createControls(videos) {
        var container = $('#bgcntrl'),
            video_listings = $('<ul></ul>'),
            play_pause = $('<div id="play-pause" class="pause">Pause</div>'),
            li;

        for (var i = 0, len = videos.length; i < len; ++i) {
            li = $('<li' + (i == 0 ? ' class="active"' : '') + '><span class="preview">' + videos[i].title + '</span></li>');
            li.data('index', i);
            video_listings.append(li);
        }

        container.append(video_listings);
        container.append(play_pause);

        $('#bgcntrl li').click(function () {
            var video = $(this);

            swapVideo(video.data('index'));
            $('#bgcntrl .active').removeClass('active');
            video.addClass('active');
            $('#play-pause').removeClass('play').addClass('pause').text('Pause');
        });

        play_pause.click(function () {
            var control = $(this);

            if (control.hasClass('pause')) {
                was_playing = false;
                pauseVideo();
            } else {
                was_playing = true;
                playVideo();
            }
        });

        Cufon.replace('#bgcntrl li', { fontFamily: 'tgoth' });
    }

    var api = function (videos) {
        function externalInterfaceReady() {
            var test = document.getElementById("flashbg");

            if (test && typeof test.eIR == "function" && test.eIR() === true) {
                obj = test;
                obj.setVideos(videos);
                createControls(videos);
            } else {
                window.setTimeout(externalInterfaceReady, 100);
            }
        }

        externalInterfaceReady();
    };

    api.scrimOn = scrimOn;
    api.scrimOff = scrimOff;
    api.pauseVideo = pauseVideo;
    api.playVideo = playVideo;
    api.swapVideo = swapVideo;
    api.videoChanged = videoChanged;
    api.hideVideo = hideVideo;
    api.showVideo = showVideo;
    api.wasPlaying = function () { return was_playing; };

    return api;
})();

var mediaplayer = (function () {
    var obj;

    var api = function (id, media) {
        function externalInterfaceReady() {
            var test = document.getElementById(id);

            if (test && typeof test.eIR == "function" && test.eIR() === true) {
                obj = test;
                obj.setMedia(media);
            } else {
                window.setTimeout(externalInterfaceReady, 1000);
            }
        }

        externalInterfaceReady();
    };

    api.swapMedia = function (index) {
        if (obj) obj.swapMedia(index);
    };

    // api for interfacing with events triggered by flash object
    api.isPlaying = function () {
        if (flashbg.wasPlaying() && !salomon.overlay.isOpen()) {
            flashbg.pauseVideo();
        }
    };

    api.isPaused = function () {
        if (flashbg.wasPlaying() && !salomon.overlay.isOpen()) {
            flashbg.playVideo();
        }
    };

    api.isDone = function () {
        if (flashbg.wasPlaying() && !salomon.overlay.isOpen()) {
            flashbg.playVideo();
        }
    };

    return api;
})();

// helper functions

// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);

        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this && this[from] === elt)
                return from;
        }

        return -1;
    };
}

// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fun /*, thisp*/)
    {
        var len = this.length >>> 0;
        if (typeof fun != "function")
            throw new TypeError();

        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
            if (i in this)
                fun.call(thisp, this[i], i, this);
        }
    };
}

String.prototype.trim = function() {
    var str = this.replace(/^\s+/, ""),
        end = str.length - 1,
        ws = /\s/;

    while (ws.test(str.charAt(end))) {
        end--;
    }

    return str.slice(0, end + 1);
};