﻿// 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];
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

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 });

        $('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').css('position', 'relative');
        $('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.makeRemoveButton').button({ icons: { primary: 'ui-icon ui-icon-minusthick'} }).addClass('processed');
        $('a.makeRemoveButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-minusthick' }, 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.makeTinyAddButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-plus' }, text: false }).addClass('processed');
        $('a.makeTinyRemoveButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-minus' }, text: false }).addClass('processed');
        $('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.makePrintButton').button({ icons: { primary: 'ui-icon ui-icon-print'} }).addClass('processed').click(function(e) {
            e.preventDefault();
            window.print();
        });
        $('a.makePrintButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-print' }, text: false }).addClass('processed').click(function(e) {
            e.preventDefault();
            window.print();
        });
        $('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'));
                    }
                }
            }
        });

        $('img.hoverChange').each(function() {
            $(this).attr('orig', $(this).attr('src'));
            var oImg = new Image();
            oImg.src = $(this).attr('rel');
        });

        $('img.hoverChange').hover(function() {
            var sChange = $(this).attr('rel');
            if (sChange != '') {
                $(this).attr('src', sChange);
            }
        }, function() {
            var sChange = $(this).attr('orig');
            if (sChange != '') {
                $(this).attr('src', sChange);
            }
        });

        $('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.validationerror.showOnload').each(function() {
            $(this).css('visibility', 'visible')
        });

        $('span.validationerrlong').each(function() {
            $(this).html('<img src="/cmsimages/spacer.gif" height="16" width="16" title="' + $(this).attr('errormessage') + '" /> <b>' + $(this).attr('errormessage') + '</b>');
        });

        $('input.pcase').blur(function() {
            var sVal = $(this).val();
            var start = sVal.substr(0, 1);
            var rest = sVal.substr(1);
            $(this).val(start.toUpperCase() + rest);
        });

        $('input.datefld').datepicker(); //.addClass('dateHeader');
        //jQuery('div.ui-datepicker-title').addClass('dateHeader');

        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);
        });

        $('map').ImageMapToolTips();
    });

    jQuery(window).load(function() {
        jQuery('#wait').hide();

        $("a[rel^='prettyphoto'],a[rel^='prettyPhoto'],a[rel^='lightbox']").prettyPhoto();
    });
}

(function($) {
    // plugin definition
    $.fn.pageTabs = function(options) {
        var selector = $(this).selector;
        var opts = $.extend({}, $.fn.pageTabs.defaults, options);

        return this.each(function() {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            $(this).addClass('ui-tabs ui-widget ui-widget-content ui-corner-all');
            $('ul:first', $(this)).addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
            $('ul:first>li', $(this)).addClass('ui-state-default ui-corner-top').hover(function() {
                if (!$(this).hasClass('ui-tabs-selected')) { $(this).addClass('ui-state-hover'); }
            }, function() {
                $(this).removeClass('ui-state-hover');
            });
            $('div:first', $(this)).addClass('ui-tabs-panel ui-widget-content ui-corner-bottom');
            if (o.selected >= 0) {
                $('ul:first>li:eq(' + o.selected + ')', $(this)).addClass('ui-tabs-selected ui-state-active');
            }
        });
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.pageTabs.defaults = {
        selected: 0
    };
})(jQuery);

(function($) {
    $.fn.ImageMapToolTips = function(options) {
        var selector = $(this).selector;
        var opts = $.extend({}, $.fn.ImageMapToolTips.defaults, options);

        return this.each(function() {
            var $this = $(this);
            if (this.nodeName.toLowerCase() == 'map') {
                var sId = $(this).attr('id');
                if (sId == '' || sId == null) {
                    sId = $(this).attr('name');
                }
                var oImg = $('img[usemap="#' + sId + '"]');
                if (oImg) {
                    oImg.wrap('<div></div>');
                    var oTTWrapper = oImg.closest('div');
                    oTTWrapper.css('position', 'relative');

                    // setup our area for showing the tip
                    var oTip = $('<div class="imTTWrapper ui-widget ui-widget-content ui-corner-all" style="padding:0.1em;"><div class="imTTHeader ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-state-default"><span style="padding:0.25em;" class="ui-dialog-title"></span></div><div class="imTTBody"><div style="padding:0.5em;"></div></div></div>').appendTo(oTTWrapper);
                    oTip.css({
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        display: 'none'
                    });

                    $('area', $(this)).hover(function() {
                        // mouseover
                        var coords = $(this).attr('coords').split(',');
                        var x1 = parseInt(coords[0], 10);
                        var y1 = parseInt(coords[1], 10);
                        var x2 = parseInt(coords[2], 10);
                        var y2 = parseInt(coords[3], 10);
                        var iLinkW = x2 - x1;
                        var iLinkH = y2 - y1;
                        var sTitle = $(this).attr('title');
                        var sBody = $(this).attr('body');
                        var sDir = $(this).attr('direction');
                        var tW = parseInt($(this).attr('width'), 10);
                        if (sTitle != '' && sBody != '') {
                            $('div.imTTHeader span', oTip).html(sTitle);
                            $('div.imTTBody div', oTip).html(sBody);
                            var iW = opts.width;
                            if (tW > 0) {
                                iW = tW;
                            }
                            var sD = opts.direction;
                            if (sDir != '' && sDir != null) {
                                sD = sDir;
                            }
                            var iL = 0;
                            var iT = 0;
                            oTip.css({ display: 'block' });
                            var iTipHeight = oTip.height();
                            switch (sD.toLowerCase()) {
                                case 's':
                                    iT = (y2 + 5);
                                    iL = (x1 + iLinkW / 2) - iW / 2;
                                    break;
                                case 'e':
                                    // show tip to the right
                                    iT = (y1 + iLinkH / 2) - iTipHeight / 2;
                                    iL = (x1 + 5 + iLinkW);
                                    break;
                                case 'w':
                                    // show tip to the left
                                    iT = (y1 + iLinkH / 2) - iTipHeight / 2;
                                    iL = (x1 - 5 - iW);
                                    break;
                                default:
                                    // north
                                    iT = (y1 - iTipHeight - 5);
                                    iL = (x1 + iLinkW / 2) - iW / 2;
                            }
                            oTip.css({ top: iT, left: iL, width: iW });
                        }
                    }, function() {
                        // mouseout
                        oTip.hide();
                    });

                }
            }
        });
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.ImageMapToolTips.defaults = {
        direction: 'n',
        width: 300
    };
})(jQuery);
