/**
 * This file needs to be cleaned up and better documented.
 */

function Breakout(){
	if (top.location != location) {
		top.location.href = document.location.href ;
	}
}

/**
 * Convenience function to set cookies
 */
function setCookie(name, value, expires, path, domain, secure) {   
    var val = escape(value);
    document.cookie= name + "=" + val +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

/**
 * Convenience function to get cookies
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function NewPopup(SomeURL, WindowName, WindowWidth, WindowHeight){
    var PopupWindow;
    PopupWindow  = window.open(
        SomeURL,
        WindowName,
        'width='+WindowWidth+',height='+WindowHeight+',top=65,left=150,scrollbars=yes,resizable=yes'
    );
    PopupWindow.focus();
}

function VariablePopup(SomeURL, WindowHeight, WindowWidth){
    NewPopup(SomeURL, 'Popup', WindowWidth, WindowHeight);
}

function ShowPopup(SomeURL){
    VariablePopup(SomeURL, 350, 500);
}

function ClosePopup() {
   document.all['Popup'].close();
}

function ChangeParent(SomeURL){
    if(window.opener != null){
        window.parent.opener.location = SomeURL;
    }
    else{
        window.location = SomeURL;
    }
}

function CalendarPopup(PG_FormName, PG_FieldName){
    var SomeURL = '/global/php/popupcal.php?FormName=' + PG_FormName + '&FieldName=' + PG_FieldName;
    var CalendarWindow;
    CalendarWindow  = window.open(
        SomeURL,
        'CalendarPopup',
        'width=260,height=300,top=65,left=150,scrollbars=yes,resizable=yes'
    );
    CalendarWindow.focus();
}

function ChangeLanguage(Language){
    document.cookie = ('GLBLanguage=' + Language + '; path=/; domain=.mcgill.ca; expires=');
    window.location.reload();
}

function ChangeAdminLang(toLang) {
    if (!toLang) {
        currentLang = getCookie('WPSAdminLang');
        if (!currentLang) currentLang = 'en';
        if (currentLang == 'fr') {
            toLang = 'en';
        } else {
            toLang = 'fr';
        }
    }
    setCookie('WPSAdminLang', toLang, null, '/admin/', '.mcgill.ca', true);
    window.location.reload();
}

function GetAdminLang() {
    lang = getCookie("WPSAdminLang");
    if (lang != 'en' && lang != 'fr') lang = "en";
    return lang;
}


function ChangeTextSize(NewSize){
    document.cookie = ('GLBTextSize=' + NewSize + '; path=/; expires=');
    window.location.reload();
}

function EditPeople(){
    document.cookie = ('EditPeople=EditPeople; path=/; domain=.mcgill.ca; expires=');
    window.location.reload();
}    

function AdminDirectory(DirName){
    document.cookie = ('AdminFocus=' + DirName + '; path=/; domain=.mcgill.ca; expires=');
    if(location.hostname != ''){
        var MyHostname = location.hostname;
    }
    else{
        var MyHostname = 'home.mcgill.ca';
    }
    window.location = 'https://'+location.hostname+'/admin/site/';
}

function GetModuleName(SomeString){
    Start = SomeString.indexOf('$');
    End = SomeString.indexOf('-');
    ModName = SomeString.substring(Start + 1, End);  
    return ModName;
}    

function TurnOn(ImageName){
    if(document.getElementById){
        document.getElementById(ImageName).src = eval(ImageName + '_On.src');
    }
}

function TurnOff(ImageName){
    if(document.getElementById){
        document.getElementById(ImageName).src = eval(ImageName + '_Off.src');
    }
}

/**
 * Used by AccordionMenu blocks.
 */
function AccordionMenu(object) {
    if (object.parentNode.parentNode.className == 'app_block app_askus') {
        linknodes = object.parentNode.getElementsByTagName("dl");
    }
    else {
        linknodes = object.parentNode.getElementsByTagName("ul");
    }
    // iterate through all submenus and collapse open ones
    for (i=0; i<linknodes.length; i++) {
        linknode = linknodes[i];
        titlenode = linknode.parentNode;
        if (linknode.className == "child") {
            // if the parent node is us, activate it
            if (titlenode == object) {
                linknode.className = "child active";
                titlenode.className = "active";
            }
        }
        else {        
            // deactivate it
            linknode.className = "child";
            titlenode.className = "";
            // if the click was on the same node, return
            if (titlenode == object) {
                return;
            }
        }
    }
}

/**
 * Used by the new-design CMS hovers.
 */
function ToggleCMSHover(object, show) {
    cmsDiv = object.getElementsByTagName('div')[0];
    if (show) {
        object.className += " outlined";
        cmsDiv.className += " displayed";
    }
    else {
        object.className = object.className.replace(/ outlined/, "");
        cmsDiv.className = cmsDiv.className.replace(/ displayed/, "");
    }
}

/**
 * Used to trigger the new-design CMS action.
 */
function PerformCMSAction(object, action) {
    cmsForm = object.parentNode;
    cmsForm.CMSAction.value = action;
    cmsForm.submit();
}

/**
 * Used to trigger keystroke-based editing of page areas.
 */
function ShortcutEdit(event, object) {
    if (event.shiftKey) {
        cmsForm = object.getElementsByTagName("form")[0];
        cmsForm.CMSAction.value = "edit";
        cmsForm.submit();
    }
}

function ItemsComboTabToggle(object, label) {
    tabNodes = object.parentNode.getElementsByTagName("span");
    blockNodes = object.parentNode.parentNode.parentNode.getElementsByTagName("div");
    for (i=0; i<tabNodes.length; i+=2) {
        tabNode = tabNodes[i];
        if (tabNode.id.indexOf("app_items_tab_") != 0) continue;
        currentLabel = tabNode.id.substring(14);
        if (currentLabel == label) {
            tabNode.className = "app_tab_" + label + " " + label + "_on";
        }
        else {
            if (i == tabNodes.length-2) {
                lastTab = " last_tab";
            }
            else {
                lastTab = "";
            }
            tabNode.className = "app_tab_" + currentLabel + lastTab;
        }
    }
    for (i=0; i<blockNodes.length; i++) {
        blockNode = blockNodes[i];
        if (blockNode.id.indexOf("app_items_body_") != 0) continue;
        currentLabel = blockNode.id.substring(15);
        if (currentLabel == label) {
            blockNode.className = label + "_wrapper";
        }
        else {
            blockNode.className = currentLabel + "_wrapper base_hide";
        }
    }
}

function PrintPreview() {
    styleDefault = document.getElementById('stylesheet_default');
    stylePrint = document.getElementById('stylesheet_print');
    currentStyleHref = styleDefault.href;
    otherStyleHref = stylePrint.href;
    styleDefault.href = otherStyleHref;
    stylePrint.href = currentStyleHref;
}

var ImAPopup = 0;

