﻿// dependencies:
/*
Library.js - Net2
    - GetResourceText()
    - updateDiv()
    - ReplaceVB()
    - addOnloadEvent();
WebWindow.Js - Net2.WebControls
    - MsgWindow
JQuery-1.3.2
*/

var Portal = {
    UI: {
        Alert: function(t, m, icon, cb) {
            if (MsgWindow && MsgWindow.alert) {
                return MsgWindow.alert(m, t, icon, cb);
            } else {
                alert(m);
            }
            return null;
        }
        , Construction: {
            Show: function(autoFadeOut) {
                $("#underconstruction").removeClass('hide');
                $("#underconstruction").show();
                if (autoFadeOut)
                    setTimeout('Portal.UI.Construction.Hide();', 5000);
                return false;
            }
            , Hide: function() {
                $("#underconstruction").fadeOut(500);
            }
        }
        , Loader: {
            Show: function() {
                var divLoader = el('loading');
                centerEl(divLoader, false);
                $("#loading").removeClass('hide');
                $("#loading").show();
            }
            , Hide: function() {
                $("#loading").fadeOut(500);
            }
        }
        , Request: {
            Start: function(e) {
                var msg = '';
                try {
                    msg = GetResourceText('ProcessingMessage');
                } catch (error) {
                    msg = 'Processing your request... please wait';
                }
                if (MsgWindow && MsgWindow.requestStart) {
                    MsgWindow.requestStart(msg);
                }
                return true;
            }
            , End: function(e) {
                if (MsgWindow && MsgWindow.requestEnd) {
                    MsgWindow.requestEnd();
                }
                var title = '';
                try {
                    title = GetResourceText('AlertMessageTitle');
                } catch (error) {
                    title = 'Application Alert';
                }

                if (e && e.Message) { return Portal.UI.Alert(title, e.Message); }
            }
        }
    }
    , ContactUs: {
        SubjectDropDown: null
        , ContactFormDiv: null
        , Show: function(subject) {
            this.ContactFormDiv = el('ContactFormContainer');
            toggleEl(this.ContactFormDiv.id);
            // center div and get a modal background -- put these methods in common library
            if (this.ContactFormDiv.style.display == 'none') { return; }
            // center and Modal it
            centerEl(this.ContactFormDiv, true);
            // set value
            if (this.SubjectDropDown) { DropDowns.SetValue(this.SubjectDropDown.id, subject); }
        }
        , Cancel: function() {
            ModalDimmer.Hide(this.ContactFormDiv);
            hideEl('ContactFormContainer');
            return false;
        }
        , Send_Init: function(e) {
            return Portal.UI.Request.Start(e);
        }
        , Send_Callback: function(e) {
            var w = Portal.UI.Alert(e.Title, e.Message);
            setInputValue(e.TxtMessageId, '');
            w.onclose = function() {
                Portal.ContactUs.Cancel();
                return true;
            }
        }
    }
    , Search: {
        Keyword: function() {
            var kw = document.getElementById('header_keyword').value;
            location.replace('/module/content/search/#search?{kw:"' + kw + '"}');
            return false;
        }
        , KeyPress: function(e, fn) {
            e = (e) ? e : event;
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                if (fn) { return fn(e); }
                return false;
            } else
                return true;
        }
        , Watermark: {
            Text: 'Keyword Search ( Optional )'
            , Init: function(id, text) {
                var txt = text ? text : Portal.Search.Watermark.Text;
                var txtId = '#' + id;
                $(txtId).attr('wmtext', txt);
                if ($(txtId).val() == '') {
                    $(txtId).val(txt);
                }
                $(txtId).click(function() {
                    if ($(this).val() == $(this).attr('wmtext')) {
                        $(this).val('');
                        $(this).css('color', '#43413E');
                    }
                });
                $(txtId).blur(function() {
                    if ($(this).val() == '') {
                        $(this).val($(this).attr('wmtext'));
                        $(this).css('color', '#666');
                    }
                });
            }
        }
    }
}

var SendToMyFriend = {
    Open: function() {
        var options = { modal: false, width: 840, height: 720, resize: true, dragenabled: true };
        var url = '/module/sas/sendtoafriend.aspx?url=' + this.encode(document.URL) + '&name=' + this.encode(document.title);
        var win = MsgWindow.open('send2friend', 'iframe', url, 'Send to a Friend', options);
    }
    , Close: function() {
        CloseWebWindow();
    }
    , Send_Init: function(e) {
        Portal.UI.Request.Start(e);
        return true;
    }
    , Send_Complete: function(e) {
        Portal.UI.Request.End(e);
        var w = Portal.UI.Alert(e.Title, e.Message);
        w.onclose = function() {
            SendToMyFriend.Close();
            return true;
        }
    }
    , TextBox_Keyup: function(e, txtbox, labelId, subjectLabelId) {
        if (txtbox) {
            if (txtbox.value != '' && txtbox.value.length > 0) {
                setHtml(labelId, txtbox.value);
                if (subjectLabelId) { setHtml(subjectLabelId, txtbox.value); }
            } else {
                var label = labelId.indexOf('Email') != -1 ? "(Your Email)" : "(Your Name)";
                setHtml(labelId, label);
                if (subjectLabelId) { setHtml(subjectLabelId, label); }
            }
        }
    }
    , encode: function(p) { return encodeURIComponent ? encodeURIComponent(p) : escape(p); }
}

/* Rich text Box */
var RichDropDown = {
    HiddenId: null,
    LabelId: null,
    DivId: null,
    Init: function(hiddenId, labelId, divId) {
        this.HiddenId = hiddenId;
        this.LabelId = labelId;
        this.DivId = divId;
    }
    , Select: function(Id, Name) {
        if (this.LabelId == null || this.HiddenId == null) {
            alert('Dropdown has not been initialized');
        }
        setInputValue(this.HiddenId, Id);
        updateDiv(this.LabelId, Name);
        this.Toggle();
    }
    , RadTreeNode_Click: function(node) {
        if (node)
            this.Select(node.Value, node.Text);
    }
    , Toggle: function() { toggleEl(this.DivId); }
};

/* Set Selected Classes */
var Elements = {
    InitToolbars: function() {
        var divElements = document.getElementsByTagName('div');
        var divToolIdx = 0;
        for (var e = 0; e < divElements.length; e++) {
            if (divElements[e].className == 'tool') {
                if (divToolIdx == 0)
                    Elements.Select(divElements[e]);
                divElements[e].savedevents = { onclick: divElements[e].onclick };
                divElements[e].onclick = function() {
                    Elements.Select(this);
                    if (this.savedevents && this.savedevents.onclick)
                        return this.savedevents.onclick();
                    return false;
                }
                divToolIdx++;
            }
        }
    }
    , Select: function(htmlElement, cssClass) {
        cssClass = cssClass ? cssClass : 'selected';

        var divSiblings = htmlElement.parentNode.childNodes;
        for (var c = 0; c < divSiblings.length; c++) {
            if (divSiblings[c].nodeType == 1) {
                var className = divSiblings[c].className;
                divSiblings[c].className = ReplaceVB(className, cssClass, '');
            }
        }
        htmlElement.className += ' selected';
    }
}
// initialize the tool bars
if (typeof (addOnloadEvent) != 'undefined')
    addOnloadEvent(Elements.InitToolbars);

var PageBreak = {
    Insert: function(e) {
    e.focus();
    e.selection.setContent("<p style='page-break-before: always; color: green'><-- Page Break --></p>");
    }
}