﻿/****************************\
* Calendar Related Functions *
\****************************/
function createDatePicker(id)
{
    $('#' + id).css('margin-right', '3px');
    $('#' + id).datepicker(
    {
        showOn: 'button',
        buttonImage: '/images/calendar/calendar.jpg',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        currentText: 'Go To Today',
        closeText: 'Close',
        yearRange: '-1:+10',
        onSelect: function(dateText) {
            document.all ?
                $(this).get(0).fireEvent("onchange")
        : $(this).change();
        }
    });
}

function createCustomDatePicker(id, oOpts)
{
    var oCalOps = $.extend(
        {},
        {
            showOn: 'button',
            buttonImage: '/images/calendar/calendar.jpg',
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            currentText: 'Go To Today',
            closeText: 'Close',
            yearRange: '-1:+10',
            onSelect: function (dateText)
            {
                document.all ?
                    $(this).get(0).fireEvent("onchange")
                    : $(this).change();
            }
        },
        oOpts);

    $('#' + id).css('margin-right', '3px');
    $('#' + id).datepicker(oCalOps);
}

function createDatePickers()
{
    $('.date').css('margin-right', '3px');
    $('.date').datepicker(
    {
        showOn: 'button',
        buttonImage: '/images/calendar/calendar.jpg',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        currentText: 'Go To Today',
        closeText: 'Close',
        yearRange: '-1:+10',
		onSelect: function(dateText) {
			document.all ? $(this).get(0).fireEvent("onchange") : $(this).change();
		}
    });
}
/********************************\
* End Calendar Related Functions *
\********************************/



// custom show/hide function to show images inpage... so they do not take up as much space
function imgMouseOver(id)
{
    // used in the function below
    var iImgWidth = 200;

    $(window).load(function()
    {
        // make sure we even need to do anything with the image (it could be small enough that it will be "sized up")
        if ($('#' + id).attr('width') > iImgWidth)
        {
            function imageGrow(e)
            {
                if ($('#' + id).data('bFullSize'))
                {
                    imageShrink(e);
                }
                else
                {
                    $('#' + id).data('bFullSize', true);
                    $('#' + id).animate({ width: $('#' + id).data('width') }, 500);
                }
            }
            function imageShrink(e)
            {
                $('#' + id).animate({ width: iImgWidth + 'px' }, 500, 'linear', function()
                {
                    $('#' + id).data('bFullSize', false);
                });
            }

            // save the width of the image
            $('#' + id).data('width', ($('#' + id).attr('width') < 650) ? $('#' + id).attr('width') : 650);

            // set the mouseover action to display full size
            $('#' + id).click(imageGrow);
            // set the mouseout action to shrink
            $('#' + id).mouseleave(imageShrink);
            // set the fullsize flag to false
            $('#' + id).data('bFullSize', false);

            // init image to 100px, add hand cursor, and make relative
            $('#' + id).css({ 'position': 'absolute', 'cursor': 'hand', 'cursor': 'pointer', 'width': '' + iImgWidth + 'px', 'top': '0', 'left': '0', 'z-index': '900' });

            // make the image parent a relative positioned element and put a place holder under the image with the same height and width
            $('#' + id).parent().css('position', 'relative');
            $('#' + id).before('<div style="width:' + iImgWidth + 'px; height:' + $('#' + id).height() + 'px;">&nbsp;</div>');
        }
    });
}

function createImageMouseovers() {
    $('img.mouseover').each(function() {
        imgMouseOver($(this).attr('id'));
    });
}

// Select/Deselect All Checkboxes
// container: jquery selector of item wrapping checkboxes
// checked: true/false to select/deselect all
function selectAll(container, checked)
{
    $("'" + container + " input:checkbox'").each(function() {
        this.checked = checked;
    });
    return false;
}

// Alternate Rows on tr tags (used in maintenance system)
function altRows() 
{
    $(".formDisplay").each(function(index)
    {
        trToStripe = $("> tbody > tr:not('.header'):not('.ignore'):visible", $(this));
        trToStripe.filter(":even").removeClass("altRow");
        trToStripe.filter(":odd").addClass("altRow");
    });
}

function createOptLists() {
    // hide pickers if clicked anywhere else
    $(document).click(function () {
        $('.optList').hide();
    });
    // add iframes to all .defaultPicker elements to cover dropdowns in IE6
    if ($.browser.msie && $.browser.version <= 6) {
        $('.defaultPicker').append('<iframe class="optList" scrolling="no" frameborder="0"></iframe>');
        $('iframe.optList').height($('iframe.optList').siblings('div.optList').outerHeight());
        $('iframe.optList').width($('iframe.optList').siblings('div.optList').outerWidth());
        $('iframe.optList').css('z-index', $('iframe.optList').siblings('div.optList').css('z-index') - 1);
    }
    // add click event to button
    $('.defaultPicker').click(function (e) {
        // prevent bubble
        e.stopImmediatePropagation();
        $('.optList', this).toggle();
    });
    // add hover feedback on options
    $('.optList div').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });
    // add click action on options
    $('.optList div').click(function (e) {
        // prevent bubble
        e.stopImmediatePropagation();

        // get the handles we need
        var picker = $(this).parent().parent();     // the .defaultPicker div itself
        var txt = $('#' + $('>span', picker).html().trim());  // the span just inside the .defaultPicker
        var val = $('span', this).html();            // the span in the option div

        // set the value in the text field
        $(txt).val(val).focus();

        // hide the picker
        $('.optList', picker).hide();
    });
}

// global function to keep highlighting effects consistant across pages
function highlight(oElement, iDuration)
{
    // default duration to 2 sec if it was not supplied
    iDuration = iDuration ? iDuration : 2000;

    $(oElement).effect('highlight', { color: '#6EBD57' }, iDuration);
}

// create querystring object
if (!document.queryString) document.queryString = function() {
    if (this.URL.indexOf("?") != -1) {
        var b = this.URL.substring(this.URL.indexOf("?") + 1).toLowerCase().split("&"), f = {}, x, y;
        for (x in b) f[(y = b[x].split("="))[0]] = y[1];
        return f;
    } else {
        return null;
    }
}
