
var HansonDodge = {
    init: function () {
        HansonDodge.Dropdowns.init();
        HansonDodge.Modals.init();
        HansonDodge.Background.init();
    }
};

HansonDodge.Modals = {
    modals: null,
    init: function () {
        HansonDodge.Modals.modals = jQuery(".modal");
        HansonDodge.Modals.run();
    },
    run: function () {
        HansonDodge.Modals.modals.click(function (e) {
            e.preventDefault();
            var self = jQuery(this);
            var width = self.attr("modal-width") == undefined ? "416" : self.attr("modal-width");
            var height = self.attr("modal-height") == undefined ? "75%" : self.attr("modal-height");
            var scrolling = self.attr('modal-scrolling') == undefined ? "auto" : self.attr('modal-scrolling');

            jQuery.modal("<iframe src='" + jQuery(this).attr("href") + "' height='100%' width='" + width + "' style='border:0' frameborder='0' allowtransparency='true' scrolling='"+scrolling+"'>", {
                closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
                opacity: 80,
                overlayClose: true,
                autoResize: false,
                position: ["5%", ],
                containerCss: {
                    height: height,
                    width: width
                },
                onClose: function (dialog) {
                    jQuery.modal.close();
                    //avoid IE8 bug with closing iframe disabling @font-face by reloading css
                    //http://kenneth.kufluk.com/blog/2010/02/losing-face-with-ie8/
                    if (jQuery.browser.msie) {
                        jQuery('#css-global')[0].href = $('#css-global')[0].href;
                    }
                }
            });

            return false;
        });
    }
};

HansonDodge.Dropdowns = {
    subnav: null,
    init: function () {
        var self = HansonDodge.Dropdowns;
        self.subnav = jQuery('#pageSubmenu');
        if (self.subnav.length) {
            var menuBtns = self.subnav.children('ul').children('li').hover(
				function () { jQuery(this).addClass('over'); },
				function () { jQuery(this).removeClass('over'); }
			);

            menuBtns.each(
				function (index1) {
				    var dropDown = jQuery(this).find('div.submenu');
				    if (dropDown.length) {
				        dropDown.css('display', 'block');
				        var lists = dropDown.find('ul');
				        var maxHeight = 0;
				        lists.each(function (index2) {
				            var currentHeight = lists.eq(index2).height();
				            if (currentHeight > maxHeight) maxHeight = currentHeight;
				        });
				        lists.css('height', maxHeight);
				        dropDown.css('display', '');
				    }
				}
			);
        }
    }
};

HansonDodge.Background = {
    buttons: null,
    init: function () {
        if (document.getElementById('backgroundSwitcheroo')) {
            var currentCookieValue = readCookie('currentBackgroundIndex');
            var buttons = HansonDodge.Background.buttons = jQuery('#backgroundSwitcheroo li a');
            HansonDodge.Background.currentIndex = (currentCookieValue === 'null' || currentCookieValue >= HansonDodge.Background.buttons.length) ? 0 : currentCookieValue;
            if (buttons.length) {
                HansonDodge.Background.setCurrentBackground(buttons.eq(HansonDodge.Background.currentIndex));
                buttons.click(HansonDodge.Background.onBtnClick);
            }
        }
    },
    removeCurrentBtnHighlight: function () {
        HansonDodge.Background.buttons.removeClass('current');
    },
    setCurrentBackground: function (btn, special) {
        jQuery('#backstretch').remove();
        for (var i = 0; i < HansonDodge.Background.buttons.length; i++) {
            if (HansonDodge.Background.buttons.get(i) == btn.get(0)) {
                HansonDodge.Background.currentIndex = i;
                break;
            }
        }
        createCookie('currentBackgroundIndex', HansonDodge.Background.currentIndex);
        HansonDodge.Background.removeCurrentBtnHighlight();
        var src = btn.attr('href');
        if (special) src = HansonDodge.Background.specialBackground;
        btn.addClass('current');
        jQuery.backstretch(src);
    },
    setSpecialBackground: function () {
        jQuery('#backstretch-wrap').remove();
        HansonDodge.Background.removeCurrentBtnHighlight();
        jQuery.backstretch(HansonDodge.Background.specialBackground);
    },
    onBtnClick: function (e) {
        var self = jQuery(this);
        HansonDodge.Background.setCurrentBackground(self, e.shiftKey);
        return false;
    }
};

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

jQuery(HansonDodge.init);


