﻿// JScript File

// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = location.y + 'px';
    top.style.left = location.x + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function Right(str, n)
/***
IN: str - the string we are RIGHTing
n - the number of characters we want to return

RETVAL: n characters from the right side of the string
***/
{
    if (n <= 0)     // Invalid bound, return blank string
        return "";
    else if (n > String(str).length)   // Invalid bound, return
        return str;                     // entire string
    else { // Valid bound, return appropriate substring
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function confirmDelete(obj, msg) {
    jQuery(obj).closest('tr').addClass('hiliteDelete');

    if (confirm(msg)) {
        return true;
    } else {
        jQuery(obj).closest('tr').removeClass('hiliteDelete');
        return false;
    }
}

if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function() {
        jQuery('#jQTabs, div.jQTabs').tabs();

        jQuery('#jQAccordion').accordion({ autoHeight: false, collapsible: true});

        $('div.makeInlineDialog').each(function() {
            $(this).wrap('<div />');
            var oWrapper = $(this).parent();
            oWrapper.addClass('ui-inline-dialog ui-widget ui-widget-content ui-corner-all');
            var oTitle = $('<div />').addClass('ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix').append('<span>' + $(this).attr('title') + '</span>').addClass('ui-dialog-title');
            oWrapper.prepend(oTitle);
        });

        $('.makeButtonPane').addClass('ui-dialog-buttonpane ui-widget-content ui-helper-clearfix');
        $('input[type="submit"], input[type="button"], button, a', $('.makeButtonPane')).button().addClass('processed');
        $('a.makeAddButton').button({ icons: { primary: 'ui-icon ui-icon-plusthick'} }).addClass('processed');
        $('a.makeAddButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-plusthick' }, text: false }).addClass('processed');
        $('a.makeEditButton').button({ icons: { primary: 'ui-icon ui-icon-pencil'} }).addClass('processed');
        $('a.makeEditButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-pencil' }, text: false }).addClass('processed');
        $('a.makeNotesButton').button({ icons: { primary: 'ui-icon ui-icon-comment'} }).addClass('processed');
        $('a.makeDocBButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-document-b' }, text: false }).addClass('processed');
        $('a.makeNotesButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-comment' }, text: false }).addClass('processed');
        $('a.makeDeleteButton').button({ icons: { primary: 'ui-icon ui-icon-trash'} }).addClass('processed').click(function(e) {
            var sConfirm = $(this).attr('confirmmsg');
            var bRet = true;
            if (sConfirm != '' && sConfirm != null) {
                bRet = confirm(sConfirm);
                if (!bRet) e.preventDefault();
            }
            return bRet;
        });
        $('a.makeDeleteButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-trash' }, text: false }).addClass('processed').click(function(e) {
            var sConfirm = $(this).attr('confirmmsg');
            var bRet = true;
            if (sConfirm != '' && sConfirm != null) {
                bRet = confirm(sConfirm);
                if (!bRet) e.preventDefault();
            }
            return bRet;
        });
        $('a.makeSaveButton').button({ icons: { primary: 'ui-icon ui-icon-disk'} }).addClass('processed');
        $('a.makeSaveButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-disk' }, text: false }).addClass('processed');
        $('a.makeCancelButton').button({ icons: { primary: 'ui-icon ui-icon-arrowreturnthick-1-w'} }).addClass('processed');
        $('a.makeCancelButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-arrowreturnthick-1-w' }, text: false }).addClass('processed');
        $('a.makeReorderButton').button({ icons: { primary: 'ui-icon ui-icon-arrow-4' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em').css('cursor', 'move');
        $('a.makeMoveRightButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-e' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeMoveLeftButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-w' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeExpandButton, .makeNextButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-e'} }).addClass('processed');
        $('a.makeCollapseButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-s'} }).addClass('processed');
        $('a.makeExpandButtonIcon, .makeNextButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-e' }, text: false }).addClass('processed');
        $('a.makeCollapseButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-s' }, text: false }).addClass('processed');
        $('a.makePreviousButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-w'} }).addClass('processed');
        $('a.makePreviousButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-w' }, text: false }).addClass('processed');
        $('a.makeTinyButton').button().addClass('processed')
        $('a.makeImageButton').button().addClass('processed').css('width', '2.4em').css('height', '2.4em').css('margin-top', '0.1em').find('span.ui-button-text').css('padding', '0.5em');
        $('a.makeOpenLinkButton').button({ icons: { primary: 'ui-icon ui-icon-extlink'} }).addClass('processed');
        $('a.makeOpenLinkButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-extlink' }, text: false }).addClass('processed');
        $('a.makeResetButton').button({ icons: { primary: 'ui-icon ui-icon-arrowrefresh-1-s'} }).addClass('processed');
        $('a.makeResetButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-arrowrefresh-1-s' }, text: false }).addClass('processed');
        $('a.makeFavoriteButton').button({ icons: { primary: 'ui-icon ui-icon-star'} }).addClass('processed');
        $('a.makeFavoriteButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-star' }, text: false }).addClass('processed');
        $('a.makePreviewButton').button({ icons: { primary: 'ui-icon ui-icon-zoomin'} }).addClass('processed');
        $('a.makePreviewButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-zoomin' }, text: false }).addClass('processed');
        $('a.makePublishButton').button({ icons: { primary: 'ui-icon ui-icon-signal-diag'} }).addClass('processed');
        $('a.makeCheckButton').button({ icons: { primary: 'ui-icon ui-icon-check'} }).addClass('processed');
        $('a.makeCheckButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-check' }, text: false }).addClass('processed');
        $('a.makeGearButton').button({ icons: { primary: 'ui-icon ui-icon-gear'} }).addClass('processed');
        $('a.makeGearButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-gear' }, text: false }).addClass('processed');
        $('.makeButton:not(.processed), .jqButton:not(.processed), .jQButton:not(.processed)').button();
        $('a.emailLink, a.makeEmailButton').button({ icons: { primary: 'ui-icon ui-icon-mail-closed'} });
        $('div.ui-invertednavtabs ul.ui-tabs-nav li').each(function() {
            $(this).addClass('ui-corner-bottom ui-state-default').hover(function() { $(this).addClass('ui-state-hover'); }, function() { $(this).removeClass('ui-state-hover'); });
        });
        $('a.processed').each(function() {
            var b = this;
            if (b && typeof (b.click) == 'undefined') {
                b.click = function() {
                    var result = true;
                    if (b.onclick) result = b.onclick();
                    if (typeof (result) == 'undefined' || result) {
                        eval(b.getAttribute('href'));
                    }
                }
            }
        });

        $('input:text, input:password, textarea').focus(function() {
            $(this).addClass('focused');
        }).blur(function() {
            $(this).removeClass('focused');
        });

        $('div.validationsumm').addClass('ui-state-error ui-corner-all').css('padding', '1em').css('margin', '1em');

        $('span.validationerror').each(function() {
            $(this).html('<img src="/cmsimages/spacer.gif" height="16" width="16" title="' + $(this).attr('errormessage') + '" />');
        });

        $('span.validationerrlong').each(function() {
            $(this).html('<img src="/cmsimages/spacer.gif" height="16" width="16" title="' + $(this).attr('errormessage') + '" /> <b>' + $(this).attr('errormessage') + '</b>');
        });

        jQuery('input.pcase').blur(function() {
            var sVal = jQuery(this).val();
            var start = sVal.substr(0, 1);
            var rest = sVal.substr(1);
            jQuery(this).val(start.toUpperCase() + rest);
        });

        jQuery('input.datefld').datepicker();

        jQuery('input.csreadonly').focus(function() {
            jQuery(this).blur();
        });

        jQuery('tr.datarow, tr.altdatarow').mouseover(function() {
            jQuery(this).addClass('datarowhilite');
        }).mouseout(function() {
            jQuery(this).removeClass('datarowhilite');
        });


        jQuery('a.button-save, a.button-delete').each(function() {
            if (jQuery(this).parent().get(0).tagName.toUpperCase() != 'P' || jQuery(this).siblings().count > 0) {
                jQuery(this).wrap('<div></div>');
            }
            $(this).parent().css('margin-left', '25%');
            $(this).mouseover(function() {
                jQuery(this).addClass('ui-state-hover');
            }).mouseout(function() {
                jQuery(this).removeClass('ui-state-hover');
            });
        });

        jQuery('a.button-save').each(function() {
            jQuery(this).addClass('ui-state-default').addClass('ui-corner-all').prepend('<span class="ui-icon ui-icon-disk"/>');
        });

        jQuery('a.button-delete').each(function() {
            jQuery(this).addClass('ui-state-error').addClass('ui-corner-all').prepend('<span class="ui-icon ui-icon-closethick"/>');
        });

        jQuery('div.msg-warning').each(function() {
            var sMsg = jQuery(this).text();
            jQuery(this).hide();
            jQuery.jGrowl(sMsg, { sticky: true });
        });

        jQuery('div.msg-success').each(function() {
            var sMsg = jQuery(this).text();
            jQuery(this).hide();
            jQuery.jGrowl(sMsg);
        });
    });

    jQuery(window).load(function() {
        jQuery('#wait').hide();
    });
}

