﻿/// <reference name="MicrosoftAjax.js" />

var BaseUI =
{
    Background: null,
    ListToolTip: null,
    ListToolTipX: 0,
    ListToolTipY: 0,
    ListToolTipText: '',
    MouseX: 0,
    MouseY: 0,
    Callout: null,
    Initialize: function () {
        BaseUI.Callout = $get('callout')

        if (BaseUI.Callout != null) {
            BaseUI.Callout.DataPanel = $get('calloutdata')
            BaseUI.Callout.Box = $get('calloutbox')
            BaseUI.Callout.Arrow = $get('calloutarrow')
            document.body.appendChild(BaseUI.Callout)
        }

        BaseUI.Background = document.createElement('div')
        BaseUI.SetClass(BaseUI.Background, 'overlay')
        document.body.appendChild(BaseUI.Background)

        if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version == 6)) {
            BaseUI.Background.style.position = 'absolute'
            BaseUI.Background.style.height = '100%'
            BaseUI.Background.style.width = '100%'

            $addHandlers(window, { "resize": BaseUI.WindowResize,
                "scroll": BaseUI.WindowResize
            }, this)

            BaseUI.WindowResize()
        }

        BaseUI.SetupRowHilighting()

        $addHandler(document, 'mousemove', BaseUI.MouseMove)

        $addHandler(document.body, 'keydown', function (e) {
            switch (e.keyCode) {
                case Sys.UI.Key.enter:
                    if (e.target != null) {
                        if (e.target.tagName != null) {
                            if (e.target.tagName.toLowerCase() == 'textarea') {
                                return true;
                            }
                        }
                    }
                    e.preventDefault()
                    return false
            }
            return true
        })
        
    },
    ShowCallout: function () {
        var viewportwidth;
        var viewportheight;

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
        if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth
            viewportheight = window.innerHeight
        }
        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
        else if ((document.documentElement != null) && (document.documentElement.clientWidth != null) && (document.documentElement.clientWidth != 0)) {
            viewportwidth = document.documentElement.clientWidth
            viewportheight = document.documentElement.clientHeight
        }

        BaseUI.Expand(BaseUI.Callout)

        BaseUI.Callout.Arrow.style.marginTop = '-7px'

        var theBounds = Sys.UI.DomElement.getBounds(BaseUI.Callout)
        var theScrollY = BaseUI.ScrollY()

        if (theBounds.y + theBounds.height > viewportheight + BaseUI.ScrollY()) {
            var theMarginTop = (-((theBounds.y + theBounds.height) - (viewportheight + theScrollY)))

            if (-theMarginTop + 50 > theBounds.height) theMarginTop += 50

            BaseUI.Callout.style.top = (theBounds.y + theMarginTop) + 'px'

            BaseUI.Callout.Arrow.style.marginTop = (-theMarginTop - 7) + 'px'
        }
    },
    HideCallout: function () {
        BaseUI.Collapse(BaseUI.Callout)
    },
    AddMask: function (el, msk, styleclass, maskstyleclass) {
        el.Mask = msk
        el.StyleClass = styleclass
        el.MaskStyleClass = maskstyleclass

        el.GetValue = function () {
            return (this.value == this.Mask ? '' : this.value)
        }

        if (el.value == '') {
            el.value = el.Mask
            BaseUI.SetClass(el, el.MaskStyleClass)
        }
        else {
            BaseUI.SetClass(el, el.StyleClass)
        }

        $addHandler(el, 'focus', function () {
            if (this.value == this.Mask) {
                this.value = ''
                BaseUI.SetClass(this, this.StyleClass)
            }
        })
        $addHandler(el, 'blur', function () {
            if (this.value.trim() == '') {
                this.value = this.Mask
                BaseUI.SetClass(this, this.MaskStyleClass)
            }
        })
    },
    WindowResize: function () {
        BaseUI.Background.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop + "px"
        BaseUI.Background.style.width = document.documentElement.clientWidth + document.documentElement.scrollLeft + "px"
    },
    ShowEmailPanel: function () {
        if (TheTravelShares != null) {
            if (TheTravelShares.length > 0) {
                var ts = TheTravelShares[0]

                switch (ts.TSEL.getAttribute("orientation")) {
                    case "topright":
                        ts.TSMenu.style.top = (-ts.TSMenu.offsetHeight - 1) + 'px';
                        ts.TSMenu.style.left = '1px'
                        break;
                    case "topleft":
                        ts.TSMenu.style.top = (-ts.TSMenu.offsetHeight - 1) + 'px';
                        ts.TSMenu.style.left = (-(ts.TSMenu.offsetWidth - ts.TSEL.offsetWidth) - 1) + 'px'
                        break;
                    case "bottomright":
                        ts.TSMenu.style.top = (ts.TSEL.offsetHeight + 1) + 'px';
                        ts.TSMenu.style.left = '1px'
                        break;
                    case "bottomleft":
                        ts.TSMenu.style.top = (ts.TSEL.offsetHeight + 1) + 'px';
                        ts.TSMenu.style.left = (-(ts.TSMenu.offsetWidth - ts.TSEL.offsetWidth) - 1) + 'px'
                        break;
                    default:
                        ts.TSMenu.style.top = (-ts.TSMenu.offsetHeight - 2) + 'px';
                        ts.TSMenu.style.left = '1px'
                        break;
                }

                ts.TSMenu.style.visibility = 'visible';
                ts.EmailPNL.style.visibility = 'inherit';
                ts.TitleEL.innerHTML = 'Email a Friend'
            }
        }
    },
    SetClass: function (el, className) {
        if (el) {
            try {
                if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version < 8)) {
                    el.className = className
                }
                else {
                    el.setAttribute("class", className)
                }
            }
            catch (e) { }
        }
    },
    GetClass: function (el) {
        if (el) {
            if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version < 8)) {
                return el.className
            }
            else {
                return el.getAttribute("class")
            }
        }
    },
    RemoveAllChildren: function (el) {
        if (el) {
            while (el.childNodes.length > 0) el.removeChild(el.childNodes[0])
        }
    },
    Expand: function (el) {
        if (el) {
            el.style.visibility = 'visible'
            el.style.display = 'block'
        }
    },
    Collapse: function (el) {
        if (el) {
            el.style.visibility = 'hidden'
            el.style.display = 'none'
        }
    },
    Show: function (el) {
        if (el) {
            el.style.visibility = 'visible'
        }
    },
    Hide: function (el) {
        if (el) {
            el.style.visibility = 'hidden'
        }
    },
    AddOption: function (sel, text, value, tag) {
        var theOption = document.createElement('option')

        theOption.text = text
        theOption.value = value
        theOption.Tag = tag

        sel.options.add(theOption, -1);
    },
    GetSelectedOption: function (sel) {
        if (sel.selectedIndex >= 0) {
            return sel.options[sel.selectedIndex]
        }

        return null
    },
    MakeRequest: function (url, verb, data, func, obj) {
        try {
            var TheRequest = new Sys.Net.WebRequest()

            TheRequest.set_url(url)
            TheRequest.set_httpVerb(verb)

            if (data) {
                TheRequest.set_body(data)
                TheRequest.get_headers()["Content-Length"] = data.length;
            }

            TheRequest.set_userContext(new RequestParams(func, obj))
            TheRequest.add_completed(BaseUI.MakeRequestComplete)
            TheRequest.invoke()

            return TheRequest
        }
        catch (error) {
            if (func) func('Communication Error')
        }
    },
    MakeRequestComplete: function (executer, eventArgs) {
        var TheParams = executer.get_webRequest().get_userContext()

        if (executer.get_responseAvailable()) {
            TheParams.responseData = executer.get_responseData().replace(/\u2028/g, '')
        }
        else {
            TheParams.responseData = null
        }

        if (TheParams.func) TheParams.func(TheParams)
    },
    GetCookie: function (check_name) {
        var a_all_cookies = document.cookie.split(';')
        var a_temp_cookie = ''
        var cookie_name = ''
        var cookie_value = ''
        var b_cookie_found = false

        for (i = 0; i < a_all_cookies.length; i++) {
            a_temp_cookie = a_all_cookies[i].split('=')
            cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '')

            if (cookie_name == check_name) {
                b_cookie_found = true;
                if (a_temp_cookie.length > 1) {
                    cookie_value = decodeURIComponent(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''))
                }
                return cookie_value
                break
            }
            a_temp_cookie = null
            cookie_name = ''
        }

        if (!b_cookie_found) return null
    },
    SetCookie: function (cookieName, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();

        if (nDays == null || nDays == 0) nDays = 1;

        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + encodeURIComponent(cookieValue) + ";path=/;expires=" + expire.toGMTString();
    },
    SetupRowHilighting: function () {
        for (var i = 0; i < 400; i++) {
            var row = $get('row' + i)

            if (row != null) {
                if (i == 0) {
                    BaseUI.CreateListToolTip()
                }

                row.StyleClass = BaseUI.GetClass(row)

                $addHandler(row, 'mouseover', function () { BaseUI.RowMouseOver(this) })
                $addHandler(row, 'mouseout', function () { BaseUI.RowMouseOut(this) })

                var rowinfo = $get('row' + i + 'info')

                if (rowinfo != null) {
                    row.RowInfo = rowinfo
                    row.RowInfo.Visible = false

                    $addHandler(row, 'click', function () {
                        if (this.RowInfo.Visible) {
                            BaseUI.Collapse(this.RowInfo)
                            this.RowInfo.Visible = false
                        }
                        else {
                            BaseUI.Expand(this.RowInfo)
                            this.RowInfo.Visible = true
                        }
                    })
                }
            }
        }
    },
    RowMouseOver: function (row) {
        BaseUI.SetClass(row, 'row-over');
        BaseUI.ListToolTip.CurrentElement = row
        BaseUI.ShowListToolTip()
    },
    RowMouseOut: function (row) {
        BaseUI.SetClass(row, row.StyleClass);
    },
    CreateListToolTip: function () {
        var theListToolTip = document.createElement('div')
        var theStrong = document.createElement('strong')

        BaseUI.SetClass(theListToolTip, "tooltip")

        theStrong.innerHTML = BaseUI.ListToolTipText

        BaseUI.ListToolTip = theListToolTip

        BaseUI.HideListToolTip()

        theListToolTip.appendChild(theStrong)
        document.body.appendChild(theListToolTip)
    },
    ShowListToolTip: function () {
        var theBounds = Sys.UI.DomElement.getBounds(BaseUI.ListToolTip.CurrentElement)

        BaseUI.ListToolTip.style.top = (theBounds.y - 40) + 'px'
        BaseUI.ListToolTip.style.left = (theBounds.x + 300) + 'px'

        BaseUI.Expand(BaseUI.ListToolTip)
    },
    HideListToolTip: function () {
        BaseUI.Collapse(BaseUI.ListToolTip)
    },
    ScrollX: function () {
        var theScrollX = document.body.scrollLeft;

        if (theScrollX == 0) {
            if (window.pageYOffset) theScrollX = window.pageXOffset;
            else theScrollX = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;

            if (theScrollX == 0) theScrollX = (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : 0;
        }

        return theScrollX
    },
    ScrollY: function () {
        var theScrollY = document.body.scrollTop;

        if (theScrollY == 0) {
            if (window.pageYOffset) theScrollY = window.pageYOffset;
            else theScrollY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

            if (theScrollY == 0) theScrollY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : 0;
        }

        return theScrollY
    },
    MouseMove: function (e) {
        try {
            var x = e.clientX + BaseUI.ScrollX()
            var y = e.clientY + BaseUI.ScrollY()

            BaseUI.MouseX = x
            BaseUI.MouseY = y

            if (BaseUI.Callout != null) {
                if (BaseUI.Callout.style.visibility.toLowerCase() != 'hidden') {
                    if (BaseUI.Callout.CurrentElement == null) return

                    theIsInBounds = false
                    theBounds1 = Sys.UI.DomElement.getBounds(BaseUI.Callout)
                    theBounds2 = Sys.UI.DomElement.getBounds(BaseUI.Callout.CurrentElement)

                    if ((x >= theBounds1.x) && (x <= theBounds1.x + theBounds1.width + 5) &&
                        (y >= theBounds1.y) && (y <= theBounds1.y + theBounds1.height + 5)) {
                        theIsInBounds = true
                    }

                    if (!theIsInBounds) {
                        if ((x >= theBounds2.x) && (x <= theBounds2.x + theBounds2.width + 15) &&
                            (y >= theBounds2.y) && (y <= theBounds2.y + theBounds2.height)) {
                            theIsInBounds = true
                        }
                    }

                    if (!theIsInBounds) {
                        BaseUI.HideCallout()
                    }
                }
            }

            if (BaseUI.ListToolTip != null) {
                if (BaseUI.ListToolTip.style.visibility.toLowerCase() != 'hidden') {
                    if (BaseUI.ListToolTip.CurrentElement == null) return

                    var theIsInBounds = false
                    var theBounds = Sys.UI.DomElement.getBounds(BaseUI.ListToolTip.CurrentElement)

                    if ((x >= theBounds.x) && (x <= theBounds.x + theBounds.width) &&
                        (y >= theBounds.y) && (y <= theBounds.y + theBounds.height)) {
                        theIsInBounds = true
                    }

                    if (!theIsInBounds) BaseUI.HideListToolTip()
                }
            }
        }
        catch (e) { }
    }
}

$sc = BaseUI.SetCookie

function RequestParams(afunc, obj) {
    this.func = afunc
    this.responseData = null
    this.obj = obj
    this.Error = null
}



