﻿// document.forms[0].onsubmit = function() { return false; }
var ControlContainer = null;
var PageControlContainer = null;
var ContentItemTypeEnum = { Upload: 1, Bookmark: 2, Rss: 3, News: 4, Blog: 5 };

function openControlWindow(id, path, title, options) {
    var url = ControlContainer + '?c=' + path;
    return MsgWindow.open(id, 'iframe', url, title, options);
}

function loadPageContainer(mod, args) { var url = PageControlContainer + '?c=' + mod + '&' + args; location.replace(url); }

//Global Ajax Init, creates a modal window with a loading window
function AjaxRequest_Init(obj) { MsgWindow.requestStart(GetResourceText('ProcessingMessage')); return true; }
// Globa function to stop request window
function AjaxRequest_End(obj) { MsgWindow.requestEnd(); if (obj.Message) { return alt(GetResourceText('AlertMessageTitle'), obj.Message); } }
// global function for alerting users
function alt(t, m, icon, cb) { return MsgWindow.alert(m, t, icon, cb); }
// auto size the current window
function ModuleWindow_Resize() { WebWindow_Resize(); }

// need to apply IE 6 Fix on nav
function FixIE6NavItems() { NavItems.ApplyOverClass('nav'); NavItems.ApplyOverClass('regnav'); }
function Timeout() { location.replace(TimeoutUrl); }
function Timeout_Warning(timeleft) { var w = MsgWindow.alert('Your session will timeout in ' + timeleft + ' minutes.  Click "Ok" to renew your session.', 'Timeout Warning'); w.oncommand = function(obj) { if (obj.command == 'Ok') { RefreshSession(); }     this.close(true); } }

Namespace.Manager.Register('eKampus.Navigator');
Namespace.Manager.Register('eKampus.Contact');
Namespace.Manager.Register('eKampus.Search');
Namespace.Manager.Register('eKampus.Home.Video.Player.Settings');


eKampus.Home.Video.Player.Settings.videosxml = '/videos.xml';
eKampus.Home.Video.Player.Settings.headline = '';
eKampus.Home.Video.Player.Settings.filepath = '';

eKampus.Home.Video.Player.Settings.init = function(xml, header, filepath) {
    eKampus.Home.Video.Player.Settings.videosxml = xml;
    eKampus.Home.Video.Player.Settings.headline = header;
    eKampus.Home.Video.Player.Settings.filepath = filepath;
}
eKampus.Home.Video.Player.Start = function(divid, swf) {
    var flashvars = eKampus.Home.Video.Player.Settings;
    var params = { wmode: "opaque" };
    var attributes = {};
    var swfFile = eKampus.Home.Video.Player.Settings.filepath + swf;
    var swfInstallFile = eKampus.Home.Video.Player.Settings.filepath + "expressInstall.swf";
    // swfobject is from swfobject.js file
    swfobject.embedSWF(swfFile, divid, "100%", "600", "9.0.0", swfInstallFile, flashvars, params, attributes);
}

eKampus.Search.Url = '/public/search/results.aspx';
eKampus.Search.Keyword = function(txtbox) { location.replace(eKampus.Search.Url + '?k=' + txtbox.value); return false; }
eKampus.Search.Keydown = function(wmt) {
    eKampus.Search.Keyword(wmt);
    return false;
}
eKampus.Search.Do = function(e) {
    if (e && e.target && e.target.value) {
        eKampus.Search.Execute({ Keyword: e.target.value });
    }
    return false;
}
eKampus.Search.Init = function(e) {
    e.LoaderDivId = 'SearchResultsContainer';
    return true;
}
eKampus.Search.SiteSearch_Init = function(e) {
    e.ShowLoader('divSearchResource');
    e.ShowLoader('divSearchForum');
    e.ShowLoader('divSearchBlog');
    e.ShowLoader('divSearchWebsite');
    e.ShowLoader('divSearchOutcome');
    e.ShowLoader('divSearchWebpageContent');
    return true;
}
eKampus.Search.SiteSearch_Keydown = function(e) { eKampus.Search.Execute(); return false; }

eKampus.Search.Callback = function(e) { }
eKampus.Navigator.NavigateUrl = function(c) { return eKampus.Navigator.GetUrl({ UniqueCode: c }); }
eKampus.Navigator.Callback = function(obj) {
    if (obj && obj.NavigateUrl)
        location.replace(obj.NavigateUrl);
}

eKampus.Contact.Send_Callback = function(obj) {
    setInputValue(obj.txtMessageId, '');
    AjaxRequest_End(obj);
}
eKampus.Contact.Send_Init = function(e) {
    AjaxRequest_Init(e);
    return true;
}

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); }
};

var NavItems = {
    ApplyOverClass: function(divid) {
        if (document.all && document.getElementById) {
            navRoot = document.getElementById(divid);
            if (navRoot) {
                for (i = 0; i < navRoot.childNodes.length; i++) {
                    node = navRoot.childNodes[i];
                    if (node.nodeName == "LI") {
                        node.onmouseover = function() {
                            this.className += " over";
                        }
                        node.onmouseout = function() {
                            this.className = this.className.replace(" over", "");
                        }
                    }
                }
            }
        }
    }
}

/* 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';
    }
}

var News = {
    HomePage_DayClick: function(d, u) {
        location.replace(u + '?d=' + d);
        return false;
    }
    , Story: {
        Loading: function(e) {
            e.LoaderDivId = 'NewsStory';
            return true;
        }
        , Loading_Complete: function(e) {
            return true;
        }
    }
};

// initialize the tool bars
addOnloadEvent(Elements.InitToolbars);


Namespace.Manager.Register("Account");
Account.Login = {
    Start: function(retUrl) {
        var gotoUrl = '';
        if (retUrl)
            gotoUrl = String.format('&ReturnUrl={0}', retUrl);
        var login = openControlWindow('login', String.format('account/login{0}', gotoUrl), GetResourceText('Login'), { modal: true, width: 315, height: 220, showhandle: true });
        return false;
    }
    , Callback: function(obj) {
        if (obj.Message) { AjaxRequest_End(obj); }
        if (obj.ReturnUrl) {
            var l = (parent) ? parent.location : location;
            l.replace(obj.ReturnUrl);
        }
    }
    , EnterKeyDown: function(e) {
        return Account.Login.Send();
    }
}

var ResourceText = {
    EditorUrl: ''
    , Edit: function(key, lang) {
        var src = String.format(this.EditorUrl, key, lang);
        //openWin(page, 'editor', null);
        var options = { width: 700, height: 575, modal: true, dragenabled: true, resize: true };
        var win = MsgWindow.open(key, 'iframe', src, 'Edit Text', options, 'recal');
        win.onclose = function() { window.location.reload(); return true; };
    }
    , Div_MouseOver: function(div, cssClassname) { if (div.className) { div.className = cssClassname; } }
    , Div_MouseOut: function(div, cssClassname) { if (div.className) { div.className = cssClassname; } }
}