/**
* American Express Avenue
*/

(function($) {

    var Amex = {
        initialize: function() {
            // check for IE6
            window.isIE6 = /msie 6/i.test(navigator.userAgent) ? true : false;

            // tell css js is there
            $("body").addClass("JS-active");

            // init category submenus
            var refine = $('#refine ul.category').simpleMenu({
                menuType: 'div.sub'
            });

            var refineOffers = $('#search ul.links').simpleMenu({
                menuType: 'div.sub'
            });

            // init hints
            $('input:text[title]').hintValues();

            // clickable boxes
            $('.clickable').clickableBox();


            // hoverable elements
          	$('#result-table tr').hover(function() {
				var row = $(this), 
					prev = row.prev(), 
					next = row.next();
				
				row.addClass('hover');
				if(row.hasClass('new')) {
					next.addClass('hover');
				} else {
					if(prev.hasClass('new')) {
						prev.addClass('hover');
					}
				}
            }, function() {
                var row = $(this), 
					prev = row.prev(), 
					next = row.next();
				
				row.removeClass('hover');
				if(row.hasClass('new')) {
					next.removeClass('hover');
				} else {
					if(prev.hasClass('new')) {
						prev.removeClass('hover');
					}
				}
            });

            // replace submit buttons
            $('input[type=submit]').replaceInputs({
                template: '<a href="#" class="$className"><span>$value</span></a>',
                simulateInputClick: true, // simulates a click on the original input
                replacedClass: 'replaced'
            });

            // autocomplete
            $('#sloc').autocomplete({
                path: '/AutoComplete',
                //	path: '../dotnet/src/AmexAvenues/Content/xml/autocomplete.xml',
                submit: true
            });

            // gallery
            $('.gallery a').gallery();

            $('.filter').filterInit();

            //$('.pageSizeOptions').addSumbitBehavior();
            $('.pageSizeOptions').addGetOnSelect();

            $('.onlineSortOptions').addGetOnSelect();

            // equalize boxes
            $('.equalize').equalizeBoxes({
                boxes: '.equal'
            });

            // share href of banner with online shop button
            //$('.button-online-merchant').attr('href', $('#banner a').attr('href'));

        }
    };

    /**
    * Hints
    */
    $.fn.hintValues = function(settings) {
        this.each(function() {
            $(this).blur(function() {
                if (!this.value || this.value == this.title) {
                    this.value = this.title;
                    $(this).addClass('blurred');
                }
            });
            $(this).focus(function() {
                if (this.value == this.title) {
                    this.value = '';
                    $(this).removeClass('blurred');
                }
            });
            $(this).triggerHandler('blur');
        });
    };

    /**
    * Clickable boxes
    */
    $.fn.clickableBox = function(settings) {
        this.click(function(e) {
            var target = e.target;
            if (!/^a$/.test(target.nodeName)) {
                var link = $(this).find('a').eq(0);
                var href = link.attr('href');
                if (href) {
                    window.location = href;
                    e.preventDefault();
                }
            }
        });
    };

    /**
    * Hoverable
    */
    $.fn.hoverable = function(settings) {
        this.hover(function() {
            $(this).addClass(settings.className);
        }, function() {
            $(this).removeClass(settings.className);
        });
    }

    /** 
    * FilterInit
    */
    $.fn.filterInit = function(settings) {
        this.find('.filterGlobal').click(SwapLinkValues);
        this.find('.filterLocal').click(SwapLinkValues);
    }

    /**
    * AddSubmitBehavior
    *
    $.fn.addSumbitBehavior = function(settings) {
    this.change(function() {
    $('body').css('cursor', 'wait');
    var searchForm = $('#searchForm');
    searchForm.find('#pageSize').attr('value', $(this).val());
    searchForm.submit();
    });
    }
    */

    /**
    * addGetOnSelect
    */
    $.fn.addGetOnSelect = function(settings) {
        this.change(function() {
            $('body').css('cursor', 'wait');
            var link = $(this).val();
            location.href = link;
        });
    }

    /**
    * equalizeBoxes
    */
    $.fn.equalizeBoxes = function(settings) {
        var setHeight = function(obj) {
            var h = 0;
            obj.each(function() { h = Math.max(h, $(this).get(0).offsetHeight); });
            obj.each(function() {
                var p = parseInt($(this).css("paddingTop")) + parseInt($(this).css("paddingBottom"));
                $(this).css({ minHeight: (h - p) + "px" });
                // compensate for stubborn IE6
                if (window.isIE6) {
                    $(this).css({ height: (h - p) + "px" });
                }
            });
            // tickle DOM, for some browser
            $("body").toggleClass("tickled");
        };

        this.each(function() {
            setHeight($(settings.boxes, this));
        });

        return this;
    };

    /**
    * Helpers
    */
    Function.prototype.bind = function(scope) {
        var method = this;
        return function() {
            return method.apply(scope, arguments);
        }
    };

    function SwapLinkValues() {
        $('.globalLocalLink').each(function(e) {
            var temp = $(this).attr('href');
            var hidden = $("#hid_" + this.id);
            $(this).attr('href', hidden.attr('value'));
            hidden.attr('value', temp);
        });
    }

    /**
    * SIFR
    */
    function initSIFR() {
        var webRoot = "/";

        sIFR.useDomContentLoaded = true;
        sIFR.useStyleCheck = true;
        sIFR.delayCSS = true;

        sIFR.replaceElement(named({
            sSelector: "h1 .sifr",
            sFlashSrc: webRoot + "Content/swf/agenda.swf",
            sColor: "#0068c3",
            sLinkColor: "#0068c3",
            sHoverColor: "#0068c3",
            sWmode: "transparent",
            sCase: "upper"
        }));
        sIFR.replaceElement(named({
            sSelector: "h3 .sifr",
            sFlashSrc: webRoot + "Content/swf/agenda.swf",
            sColor: "#0068c3",
            sLinkColor: "#0068c3",
            sHoverColor: "#0068c3",
            sWmode: "transparent",
            sCase: "upper"
        }));

    }

    /**
    * Clickable boxes
    */
    $.fn.gallery = function(settings) {
        this.click(function(e) {
            var preview = $(".preview");
            preview.attr("src", $(this).attr("href"));
            e.preventDefault();
        });
    };
    /**
    * Init
    */
    $(function() {
        Amex.initialize();
    });

})(jQuery)