$(document).ready(function ()
{
    //feedback animation
    setTimeout("$('.fxFadeOut').fadeTo('slow', 0.01, function() { $(this).slideUp('slow', function() { $(this).remove(); }); });", 5000);

    //Twistie Support
    $(".appFxTwistie").find("tr:first").addClass("noSelect");
    $(".appFxTwistie").find("tr:eq(1)").hide();
    $(".appFxTwistie").find("td:eq(0)").addClass('appFxTwistieIconClosed');
    $(".appFxTwistie").find("tr:eq(0)").click(function (event)
    {
        event.stopImmediatePropagation();
        if ($(this).next("tr").is(":hidden"))
        {
            $(this).find("td:eq(0)").removeClass('appFxTwistieIconClosed').addClass('appFxTwistieIconOpened');
            $(this).next("tr").show();
        } else
        {
            $(this).next("tr").hide();
            $(this).find("td:eq(0)").removeClass('appFxTwistieIconOpened').addClass('appFxTwistieIconClosed');
            $(this).addClass("");
        }
    });

    //Legacy Twistie Support
    $("td[class^=Twistie]").parent().parent().parent().find("tr:eq(1)").hide();
    $("td[class^=Twistie]").addClass("appFxTwistieIconClosed");
    $("td[class^=Twistie]").parent().addClass("noSelect");
    $("td[class^=Twistie]").parent().click(function (event)
    {
        event.stopImmediatePropagation();
        if ($(this).parent().parent().find("tr:eq(1)").is(":hidden"))
        {
            $(this).find("td:eq(0)").removeClass('appFxTwistieIconClosed').addClass('appFxTwistieIconOpened');
            $(this).parent().parent().find("tr:eq(1)").show();
        } else
        {
            $(this).find("td:eq(0)").removeClass('appFxTwistieIconOpened').addClass('appFxTwistieIconClosed');
            $(this).parent().parent().find("tr:eq(1)").hide();
        }
    });

    //deslection
    $(function () { $.extend($.fn.disableTextSelect = function () { $(this).css("cursor", "pointer"); return this.each(function () { if ($.browser.mozilla) { $(this).css('MozUserSelect', 'none'); } else if ($.browser.msie) { $(this).bind('selectstart', function () { return false; }); } else { $(this).mousedown(function () { return false; }); } }); }); $('.noSelect').disableTextSelect(); });
        

    //future development 
    /*
    var app_width = $('#appContainer').width();
    var app_width_menu = $('#appLayoutMenu').width();
    var app_width_body = $('#appLayoutBody').width();
    var app_width_call = $('#appLayoutCallouts').width();

    var avail_width = $('#appWorkspaceAreaPage').width();
    var max_width = 0;

    alert('app_width=' + app_width.toString() + 'px.; app_width_menu=' + app_width_menu.toString() + 'px.; app_width_body=' + app_width_body.toString() + 'px.; app_width_call=' + app_width_call.toString() + '; appWorkspaceAreaPage=' + avail_width.toString());

    $('.appComponentContainer table').each(function () {
    var this_width = $(this).width();

    alert("width=" + $(this).width().toString() + "outerWidth=" + $(this).outerWidth().toString());

    if (this_width > max_width) {
    max_width = this_width;
    }
    });

    if (max_width > avail_width) {
    alert('Max available width exceeded by ' + (max_width - avail_width).toString() + 'px.;');

    app_width_body += (max_width - avail_width) + (2 * 12); //the 24px is for the padding-left and padding-right on class appComponentBody on a div
    app_width = (2 * (app_width - app_width_menu) + app_width_body + app_width_call);

    alert('app_width=' + app_width.toString() + 'px.; app_width_menu=' + app_width_menu.toString() + 'px.; app_width_body=' + app_width_body.toString() + 'px.; app_width_call=' + app_width_call.toString() + '; appWorkspaceAreaPage=' + avail_width.toString());


    //alert('New app width = ' + app_width.toString());
    var strStyle1 = 'width: ' + app_width.toString() + 'px;border: green 1px solid;'
    var strStyle2 = 'width: ' + app_width_body.toString() + 'px;border: red 1px solid;'
    var strStyle3 = 'width: ' + app_width_call.toString() + 'px;border: blue 1px solid;'

    $('#appContainer').attr('style', strStyle1);
    $('#appLayoutBody').attr('style', strStyle2);
    $('#appLayoutCallouts').attr('style', strStyle3);
    }
    */

});


/* RADEditor Support */
function SetEditorStyle(editor) { var style = editor.get_contentArea(); style.className = "appRadEditor"; }


/*page viewing permission*/



/* Component Support */

function getElementsByClassName(oElm, strTagName, strClassName) {
    var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName); var arrReturnElements = new Array(); strClassName = strClassName.replace(/\-/g, "\\-"); var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)"); var oElement; var obj; for (var i = 0; i < arrElements.length; i++) { oElement = arrElements[i]; if (oRegExp.test(oElement.getAttribute("className"))) { arrReturnElements.push(oElement); obj = oElement.parentNode.parentNode.parentNode; obj.rows[1].style.visibility = "hidden"; obj.rows[1].style.display = "none"; } }
    return (arrReturnElements);
}
function showContent2(ShowName, HideName, ContentName) { var divContent = document.getElementById(ContentName); var divHide = document.getElementById(HideName); var divShow = document.getElementById(ShowName); divContent.style.display = "block"; divContent.style.visibility = "visible"; divShow.style.display = "none"; divShow.style.visibility = "hidden"; divHide.style.display = "block"; divHide.style.visibility = "visible"; }
function hideContent2(ShowName, HideName, ContentName) { var divContent = document.getElementById(ContentName); var divHide = document.getElementById(HideName); var divShow = document.getElementById(ShowName); divContent.style.display = "none"; divContent.style.visibility = "hidden"; divShow.style.display = "block"; divShow.style.visibility = "visible"; divHide.style.display = "none"; divHide.style.visibility = "hidden"; }
function showContentWithCookie(ShowName, HideName, ContentName, PageID, ComponentID) { var divContent = document.getElementById(ContentName); var divHide = document.getElementById(HideName); var divShow = document.getElementById(ShowName); divContent.style.display = "block"; divContent.style.visibility = "visible"; divShow.style.display = "none"; divShow.style.visibility = "hidden"; divHide.style.display = "block"; divHide.style.visibility = "visible"; createCookie(ComponentID, "1", 365) }
function hideContentWithCookie(ShowName, HideName, ContentName, PageID, ComponentID) { var divContent = document.getElementById(ContentName); var divHide = document.getElementById(HideName); var divShow = document.getElementById(ShowName); divContent.style.display = "none"; divContent.style.visibility = "hidden"; divShow.style.display = "block"; divShow.style.visibility = "visible"; divHide.style.display = "none"; divHide.style.visibility = "hidden"; createCookie(ComponentID, "0", 365) }
function ToggleDivContent(ContentName) {
    var divContent = document.getElementById(ContentName); if (divContent.style.display == 'none') { divContent.style.display = "block"; divContent.style.visibility = "visible"; }
    else { divContent.style.display = "none"; divContent.style.visibility = "hidden"; } 
}
function createCookie(name, value, days) {
    var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); }
    else { expires = ""; }
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); }
    return null;
}
function eraseCookie(name) { createCookie(name, "", -1); }
function InterceptAnchorLinks() { if (event.srcElement.href != null) { if (event.srcElement.href.indexOf("#") > -1) { AnchorsAway(); document.location = event.srcElement.href; event.returnValue = false; } } }
function AnchorsAway() { for (var i = 0; i < AnchorArray.length; i++) { showContent2(AnchorArray[i][0], AnchorArray[i][1], AnchorArray[i][2]); } }

/* RADEditor Support */

function SetEditorStyle(editor) { var style = editor.get_contentArea(); style.className = "appRadEditor"; }
RadEditorCustomFilter = function () { }
RadEditorCustomFilter.prototype = { getHtmlContent: function (content) { return content } }
//RadEditorCustomFilter.registerClass('RadEditorCustomFilter', Telerik.Web.UI.Editor.Filter);
function OnEditorClientCommandExecuting(editor, args) {
    if (args.get_name() == "TranslateTool") {
        var language = args.get_value(); var tool = editor.getToolByName("TranslateTool"); tool.get_element().getElementsByTagName("IMG")[0].src = "/images/icons/flags/" + language + ".png"; var hasSelection = true; var html = editor.getSelectionHtml().trim(); if (!html) { html = editor.get_html(true); hasSelection = false; }
        google.language.translate(html, "", language, function (result) {
            if (!result.error) {
                currentSelectedHtml = result.translation; if (hasSelection) { editor.pasteHtml(currentSelectedHtml); }
                else { editor.set_html(currentSelectedHtml); } 
            } 
        }); args.set_cancel(true);
    } 
}

/* support for ListBox */

function MoveListboxItem(srcListbox, destListbox) {
    if (srcListbox.selectedIndex == -1) { alert('Please select at least one record.'); return; }
    var selcnt = 0; for (i = 0; i < srcListbox.options.length; i++) { if (srcListbox.options[i].selected) { var optionText = srcListbox.options[i].text; var optionVal = srcListbox.options[i].value; var optionIndex = destListbox.options.length; var objOption = new Option(optionText, optionVal); objOption.style.color = srcListbox.options[i].style.color; if (optionVal != 0) { destListbox.options[optionIndex] = objOption; selcnt = selcnt + 1; } } }
    for (s = 0; s < selcnt; s++) { for (i = 0; i < srcListbox.options.length; i++) { if (srcListbox.options[i].selected) { srcListbox.options[i] = null; break; } } }
    if (destListbox.options.length > 1) { for (i = 0; i < destListbox.options.length; i++) { if (destListbox.options[i].value == 0) { destListbox.options[i] = null; break; } } } 
}
function ListboxItemUp(lstbox) {
    if (lstbox.selectedIndex == -1) { alert('Please select one record to move up'); return; }
    var arrOpt = new Array()
    if (lstbox.selectedIndex > 0) {
        for (i = 0; i < lstbox.options.length; i++) {
            if (i == lstbox.selectedIndex - 1) { arrOpt[i] = lstbox.options[i + 1] }
            else if (i == lstbox.selectedIndex) { arrOpt[i] = lstbox.options[i - 1] }
            else
                arrOpt[i] = lstbox.options[i];
        }
        for (i = 0; i < lstbox.options.length; i++) { lstbox.options[i] = null; }
        for (i = 0; i < arrOpt.length; i++) { lstbox.options[i] = arrOpt[i]; } 
    } 
}
function ListboxItemDown(lstbox) {
    if (lstbox.selectedIndex == -1) { alert('Please select one record to move up'); return; }
    var arrOpt = new Array()
    if (lstbox.selectedIndex < lstbox.options.length - 1) {
        for (i = 0; i < lstbox.options.length; i++) {
            if (i == lstbox.selectedIndex) { arrOpt[i] = lstbox.options[i + 1] }
            else if (i == lstbox.selectedIndex + 1) { arrOpt[i] = lstbox.options[lstbox.selectedIndex] }
            else
                arrOpt[i] = lstbox.options[i];
        }
        for (i = 0; i < lstbox.options.length; i++) { lstbox.options[i] = null; }
        for (i = 0; i < arrOpt.length; i++) { lstbox.options[i] = arrOpt[i]; } 
    } 
}
function StoreListboxValue(lstbox, hiddenfield) { hiddenfield.value = ''; for (i = 0; i < lstbox.options.length; i++) { hiddenfield.value = hiddenfield.value + lstbox.options[i].value + ',' } }
function MoveListboxItem2(srcListbox, destListbox) {
    if (srcListbox.selectedIndex == -1) { alert('Please select at least one record.'); return; }
    var selcnt = 0; for (i = 0; i < srcListbox.options.length; i++) {
        if (srcListbox.options[i].selected) {
            var optionText = srcListbox.options[i].text; var optionVal = srcListbox.options[i].value; var optionIndex = destListbox.options.length; var objOption = new Option(optionText, optionVal); objOption.style.color = srcListbox.options[i].style.color; if (optionVal != 0) {
                var isexists = 0; for (j = 0; j < destListbox.options.length; j++) { if (destListbox.options[j].value == optionVal) { isexists = 1; break; } }
                if (isexists == 0) { destListbox.options[optionIndex] = objOption; selcnt = selcnt + 1; } 
            } 
        } 
    } 
}
function RemoveListboxItem(destListbox) {
    var selcnt = 0; for (i = 0; i < destListbox.options.length; i++) { if (destListbox.options[i].selected) { selcnt = selcnt + 1; } }
    for (s = 0; s < selcnt; s++) { for (i = 0; i < destListbox.options.length; i++) { if (destListbox.options[i].selected) { destListbox.options[i] = null; break; } } }
}

//this function supports RadConfrim
// OnClientClick="return blockConfirm('Confirm question goes here.', event, 330, 100,'','dialog title');"
window.blockConfirm = function (text, mozEvent, oWidth, oHeight, callerObj, oTitle)
{
    var ev = mozEvent ? mozEvent : window.event; ev.cancelBubble = true; ev.returnValue = false; if (ev.stopPropagation) ev.stopPropagation(); if (ev.preventDefault) ev.preventDefault(); var callerObj = ev.srcElement ? ev.srcElement : ev.target; if (callerObj)
    {
        var callBackFn = function (arg)
        {
            if (arg)
            {
                callerObj["onclick"] = ""; if (callerObj.click) callerObj.click(); else if (callerObj.tagName == "A")
                {
                    try{ eval(callerObj.href) }
                    catch (e) { } 
                } 
            } 
        }
        radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle);
    }
    return false;
}
