﻿$(document).ready(function() {
$('.CaridianBCTEditorControl a[href^=#]').click(function() {
        location.href = this.href;
        location.reload(true);
        return true;
    });
});

/*
    **** SetRegionCookieAndRedirect
    ****
    ****    Sets a cookie named USER_SELECTED_REGION to the specified @cookieValue
    ****    and then redirects to @targetUrl.
*/
function SetRegionCookieAndRedirect(cookieValue, targetUrl)
{
    var cookie_name = "USER_SELECTED_REGION";
    $.cookie(cookie_name, cookieValue, { expires: 365, path: '/' });
    window.location = (targetUrl);
}

function getNextSeparator(element) {
    var cur = element;
    while (cur != null && cur.id != 'sep') {
        cur = cur.nextSibling;
    }
    return cur;
}

function getPrevSeparator(element) {
    var cur = element;
    while (cur != null && cur.id != 'sep') {
        cur = cur.previousSibling;
    }
    return cur;
}

function styleTabbedContentEditor(oWebTab, selectedIndex) {
    var visibleTabs = 0;
    var styledTabs = 0;

    for (var i = 0; i < oWebTab.Tabs.length; i++) {
            if (oWebTab.Tabs[i].getVisible()) {
                visibleTabs++;
            }
    }

    for (var i = 0; i < oWebTab.Tabs.length; i++) {
        var tab = oWebTab.Tabs[i];
        if (tab.getVisible()) {
            styledTabs++;

            if (i == selectedIndex) {
                switch (styledTabs) {
                    case 1:
                        tab.elemDiv.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/editor-border1.png)';
                        break;
                    case 2:
                        tab.elemDiv.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/editor-border2.png)';
                        break;
                    case 3:
                        tab.elemDiv.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/editor-border3.png)';
                        break;
                    case 4:
                        tab.elemDiv.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/editor-border4.png)';
                        break;
                    case 5:
                        tab.elemDiv.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/editor-border5.png)';
                        break;
                }
            }
            
            tab.getElement().style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/1px_tabrepeater.gif)';
            tab.getElement().style.backgroundRepeat = 'repeat-x';
            tab.getElement().style.width = '84px';
            
            var tabSeparatorMid = getNextSeparator(tab.getElement());
            if (styledTabs == visibleTabs) {
                tabSeparatorMid.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/ts-last.png)';
            }
            else if (i < selectedIndex) {
                tabSeparatorMid.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/ts-mid-left.png)';
            }
            else {
                tabSeparatorMid.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/ts-mid-right.png)';
            }
            tabSeparatorMid.style.backgroundRepeat = 'no-repeat';
            tabSeparatorMid.style.width = '18px';
        }
    }

    if (visibleTabs > 0) {
        var tabSeparatorFirst = getPrevSeparator(oWebTab.Tabs[0].getElement());
        tabSeparatorFirst.style.backgroundImage = 'url(/_layouts/images/CaridianBCT_ExtWeb/ts-first.png)';
        tabSeparatorFirst.style.backgroundRepeat = 'no-repeat';
        tabSeparatorFirst.style.width = '18px';
    }
}

function tabbedcontenteditor_Init(oWebTab) {
    if (window.location.hash) {
        var anchor = unescape(window.location.hash);
        for (var i = 0; i < oWebTab.Tabs.length; i++) {
            if (oWebTab.Tabs[i].getVisible() && oWebTab.Tabs[i].text
                && oWebTab.Tabs[i].text.toLowerCase().replace(/[^a-zA-Z]+/g, '') === anchor.substr(1).toLowerCase().replace(/[^a-zA-Z]+/g, '')) {
                oWebTab.setSelectedIndex(i);
            }
        }
    }
        
    if (oWebTab.Tabs.length > 0)
        oWebTab.Tabs[0].elemDiv.parentNode.style.paddingTop = '0px';
            
    for (var i = 0; i < oWebTab.Tabs.length; i++) {
        if (oWebTab.Tabs[i].getVisible()) {
            var oTab = oWebTab.Tabs[i];
            oTab.getElement().style.paddingTop = '4px';
            //oTab.elemDiv.style.height = "150px";
            oTab.elemDiv.style.backgroundRepeat = 'no-repeat';
            oTab.elemDiv.style.paddingLeft = '32px';
            oTab.elemDiv.style.paddingRight = '32px';
            oTab.elemDiv.style.paddingTop = '32px';
        }
    }
      
    styleTabbedContentEditor(oWebTab, oWebTab.getSelectedIndex());    
}

function tabbedcontenteditor_After(oWebTab, oTab, oEvent) {
    styleTabbedContentEditor(oWebTab, oTab.getIndex());    
}

function editorcontrol_AfterAction(oEditor, actID, oEvent, p4, p5, p6, p7, p8) {
    if (    actID == 'InsertImage' ||
            actID == 'InsertFlash' ||
            actID == 'InsertWindowsMedia' ||
            actID == 'InsertLink' ||
	        actID == 'ChangeView') {
        var txt = oEditor.getText();
        var homeURL = this.location.protocol + '//' + this.location.host;
        var re = new RegExp(homeURL, "gi");
        txt = txt.replace(re, '');
        oEditor.setText(oEditor._amp(txt));
    }
}

function regionSwitcher_Click(menuId, itemId) {
    var item = igmenu_getItemById(itemId);
    var menu = igmenu_getMenuById(menuId);
    if (item.isTopLevelItem()) {
        menu.CancelPostBack = true;
    }    
}
