﻿// Create Instance

if (typeof Smith == 'undefined') Smith = {};
Smith.base = {};
Smith.base.Timer = {};
// Implement Class
$.extend(Smith.base, {
    init: function(SiteRoot, PageName) {
            this.SiteRoot = SiteRoot;
        
        $(window).resize(function() {
            Smith.base.PositionModal();
        }).scroll(function() {
            Smith.base.PositionModal();
            // Reposition Alert Message so that user sees it when scrolls down on page.
            $("#bottomMessage").animate({top:$(window).scrollTop() + 'px'},{queue: false, duration:350});
            $("#pageAlert").animate({top:$(window).scrollTop() + 'px'},{queue: false, duration:350});
        });

        $('#ModalOK').click(function() { Smith.base.CloseModal();Smith });
        // Allow the user to close the modal by clicking the
        // background
        $('#modalBackground').click(function() { Smith.base.CloseModal() });
        // also by clicking Escape
        $(document).keyup(function(event) {
            if (event.keyCode == 27 && $('#modalForeground').css('display') == 'block')
                Smith.base.CloseModal();
        });
        $("a").click(function(){Smith.base.showBottomMessage('Please wait while the content continues to load...');});
        
        // Hide frames and drop downs if using IE6 when user mouses over menu. Make it appear again two seconds
        // after mousing out of the menu.
        if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 && parseInt(jQuery.browser.version) > 4) {
            $("#MainMenu").mouseover(function(){
                $("iframe").css("display","none");
                $("select").css("display","none");
                clearTimeout(Smith.base.Timer);
            }).mouseout(function(){
                Smith.base.Timer = setTimeout("$('iframe').css('display','inline');$('select').css('display','inline');",2000);
            });
        }
    },
    hoverImage: function(elem) {
        $(elem).children("img").each(function() {
            $(this).css("-moz-opacity", ".85").css("filter", "alpha(opacity=85)").css("opacity", "0.85");
        });
        $(elem).children("img").each(function() {
            $(this).mouseover(function() {
                $(this).css("-moz-opacity", "1.0").css("filter", "alpha(opacity=100)").css("opacity", "1.0");
            });
            $(this).mouseout(function() {
                $(this).css("-moz-opacity", ".85").css("filter", "alpha(opacity=85)").css("opacity", "0.85");
            });
        });
    },
    replaceAmp: function() {
        // Replace all ampersands in <a> tag "href" values with "{amp}"
        $("a[href]").each(function() {
            var href = $(this).attr("href")
            if (href.indexOf("&") != -1) {
                href = href.replace(/&/g, "{amp}");
                $(this).attr("href", href);
            }
        });
    },
    replaceDollar: function() {
        // Replace all ampersands in <a> tag "href" values with "{amp}"
        $("a[href]").each(function() {
            var href = $(this).attr("href")
            if (href.indexOf("$") != -1) {
                href = href.replace(/\$/g, Smith.base.SiteRoot + "$");
                $(this).attr("href", href);
            }
        });
    },
    replaceTilda: function() {
        // Replace all ampersands in <a> tag "href" values with "{amp}"
        $("a[href]").each(function() {
            var href = $(this).attr("href")
            if (href.indexOf("/~") != -1) {
                href = href.replace(/\/~/g, "");
                $(this).attr("href", href);
            }
            if (href.indexOf("~") != -1) {
                href = href.replace(/~/g, "");
                $(this).attr("href", href);
            }
        });
    },
    transformDsLinks: function(pageId) {
        // Replace all ampersands in <a> tag "href" values with "{amp}"
        $("a[href]").each(function() {
            var href = $(this).attr("href")
            var cssClass = $(this).attr("class")
            if (href.indexOf("dsDisplay.aspx") != -1 && cssClass == "AvailLink") {
                href = href + '&page=' + pageId;
                $(this).attr("href", href);
            }
        });
    },
    switchClass: function(element, newClass) {
        /// <summary>Changes the class of the specified element</summary>
        /// <history>
        ///         [ajohnson] 2009/01/07
        /// </history>
        document.getElementById(element).className = newClass;
    },
    ajaxCall: function(url, data, success) {
        $.ajax({ type: 'POST',
            url: url,
            data: data,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: success
        });
    },
    Fade: function(elementID, start, end, ms) {
        /// <summary>Animation effect to fade an element in or out.<summary>
        /// <param name="elementID" type="string">The name of the element to fade.</param>
        /// <param name="start" type="Number">The starting opacity from 0-100 (0: transparent, 100: opaque)</param>
        /// <param name="end" type="Number">The ending opacity from 0-100 (0: transparent, 100: opaque)</param>
        /// <param name="ms" type="Number">The number of milliseconds it takes for the element to fade</param>

        var speed = Math.round(ms / 100);
        var timer = 0;

        // determine direction for blending, if start and end are the same, do nothing
        if (start > end) {
            for (i = start; i >= end; i--) {
                setTimeout("Smith.base.FadeAnimate('" + elementID + "','" + i + "')", (timer * speed));
                timer++;
            }
        } else if (start < end) {
            for (i = start; i <= end; i++) {
                setTimeout("Smith.base.FadeAnimate('" + elementID + "','" + i + "')", (timer * speed));
                timer++;
            }
        }
    },
    FadeAnimate: function(elementID, opacity) {
    /// <summary> Called  by Fade to set the opacity of an element. Internal Use Only</summary>
    /// <param name="elementID" type="String">The name of the element.</param>
    /// <param name="opacity" type="Number">The opacity to set the element to. </param>

    var element = document.getElementById(elementID);
    // IE fix
    element.style.zoom = 1;

    element.style.opacity = (opacity / 100);
    element.style.MozOpacity = (opacity / 100);
    element.style.KhtmlOpacity = (opacity / 100);
    element.style.filter = 'alpha(opacity=' + opacity + ')';
    },
    ShowModal: function(headerText, Message) {
        /// <summary> A utility to show the modal Popup dialog with a header and a message. </summary>
        /// <param name="headerText">The Header text</param>
        /// <param name="Message">The message to display.</param>    

        // IE Fix for the Search Box
        // $get('MenuBar').style.zIndex = 0;

        $('#ModalHeaderInfo').html(headerText);
        $('#ModalLabel').html(Message);
        Smith.base.OpenModal();
    },
    ModalOK: function() {
        /// <summary> Called when the user clicks on the OK Button of the modal dialog. Internal Use Only. </summary>

        // IE Fix for the Search Box
        //$get('MenuBar').style.zIndex = 100;
        Smith.base.CloseModal();
    },
    OpenModal: function() {
        /// <summary> Opens the Modal Dialog. Internal Use Only. </summary>

        //$get('drawerContainer').style.zIndex = 0;

        $('#modalBackground').css('opacity', 0).show().animate({ opacity: 0.7 }, 500);
        $('#modalForeground').fadeIn('slow');
        $('#modalBackground').css("z-index", "200");

        this.PositionModal();


    },
    CloseModal: function() {
        /// <summary> Closes the Modal Dialog. Internal Use Only. </summary>

        //$get('drawerContainer').style.zIndex = 1000;

        $('#modalForeground').fadeOut('slow');
        $('#modalBackground').fadeOut('slow');
        $('#modalBackground').css('z-index', 0);

        //$('#MenuBar').css('z-index', 100);

    },
    GetWindowDimensions: function() {
        var dimensions = {
            width: 0,
            height: 0
        };

        if (window.innerWidth) {
            dimensions.width += window.innerWidth;
            dimensions.height += window.innerHeight
        }
        else {
            dimensions.width += document.body.offsetWidth;
            dimensions.height += document.body.offsetHeight
        }
        if (document.documentElement) {
            dimensions.width += document.documentElement.scrollLeft;
            dimensions.height += document.documentElement.scrollTop;
        }
        else {
            dimensions.width += document.body.scrollLeft;
            dimensions.height += document.body.scrollTop;
        }
        return dimensions;

    },
    showBottomMessage: function(message)
    {
        $("#bottomMessage").css("width",$(document).width() + 'px').html(message);
        setTimeout("Smith.base.fadeBottomMessageIn()",5000);
        setTimeout("Smith.base.fadeBottomMessageOut()",15000);
        
    },
    fadeBottomMessageIn:function()
    {
        $("#bottomMessage").slideDown('fast');
        $("#pageAlert").slideDown('fast');
    },
    fadeBottomMessageOut:function()
    {
        $("#bottomMessage").slideUp('slow');
        $("#pageAlert").slideUp('fast');
    },    
    PositionModal: function() {
        /// <summary> Called when user scrolls or resizes window to ensure the modal box stays centered on the screen. Internal Use Only. </summary>

        var w = 0;
        var h = 10;

        if (window.innerWidth) {
            w += window.innerWidth;
            h += window.innerHeight
        }
        else {
            w += document.body.offsetWidth;
            h += document.body.offsetHeight
        }
        if (document.documentElement) {
            w += document.documentElement.scrollLeft;
            h += document.documentElement.scrollTop;
        }
        else {
            w += document.body.scrollLeft;
            h += document.body.scrollTop;
        }

        if (self.pageYOffset) {
            dialogH = self.pageYOffset;
        }
        else
            if (document.documentElement && document.documentElement.scrollTop) {
            dialogH = document.documentElement.scrollTop;
        }
        else
            if (document.body) {
            dialogH = document.body.scrollTop;
        }
        dialogH = dialogH + 200;

        var dialogW;
        dialogW = w / 2;
        dialogW = dialogW - 150;

        $('#modalForeground').css('left', dialogW + 'px');
        $('#modalForeground').css('top', dialogH + 'px');
        $('#modalBackground').css('width', $(document).width() + 'px');
        $('#modalBackground').css('height', $(document).height() + 'px');
    },
    clearSelectedNav:function()
    {
        $(".navSelected").removeClass().addClass('navLink');    
    }
   
});



