﻿/// <reference path="jquery-1.3.2-vsdoc2.js"/>
// The above reference gives jQuery intellisense with Visual Studio.
// You must also:
// 1. Install VS 2008 SP1.
// 2. Install VS 2008 Patch KB958502 to Support "-vsdoc.js" Intellisense Files:
//    http://code.msdn.microsoft.com/KB958502/Release/ProjectReleases.aspx?ReleaseId=1736
// 4. Download the jQuery-vsdoc.js file
//    Save the jquery-vsdoc.js file next to your jquery.js file in your project.
// Note: Intellisense for $ will not work inside this construct:
//       function($) { })(jQuery); - Typical for plugins. The reason is here: 
//       http://docs.jquery.com/Using_jQuery_with_Other_Libraries
//       As a work around can use jQuery instead of $ and you get full intellisense.
// Nigel Snow 7 Dec 2009
//==================================================================================================
(function($) {
    /*
    * jquery.rbi_common.js
    *
    * Common jQuery functions written for RBI
    *
    * Copyright (c) 2009 Nigel Snow - RBI 
    *
    */

    $.fn.rbiPopulateSelect = function(watermark, items) {
        // populates select list from array of items given as objects: { value: 2, text: '£10 to £14.99' }
        var selectElement = this[0];

        // If the selection box is disabled then enable it
        if (this.filter('[disabled]').length == 1) {
            this.removeAttr("disabled");
        }


        selectElement.options.length = 0;
        if (items.length > 0)
            selectElement.options[0] = new Option(watermark, '');

        $.each(items, function() {
            selectElement.options[selectElement.options.length] = new Option(this.text, this.value);
        });
    };

    $.fn.rbiFindExpectedSingleItem = function(selector) {

        // Todo - set verboseMode to false in release.
        var verboseMode = false,
            itemSelected = this.find(selector);

        if (verboseMode && itemSelected.length != 1) {
            alert("rbiFindExpectedSingleItem: The jQuery selector '" + selector + "' is expected to match a single item, it has matched " + itemSelected.length + " item(s).\n\nThe jQuery functionality will not behave correctly as a result. Please fix this now.\n\nSet verboseMode to false in jquery.rbi_common.js to prevent this message being displayed on live.");
        }

        return itemSelected;
    };

    $.fn.rbiBehaviourCopyValueToTitle = function() {
        return this.blur(function() {
            if (this.value.length > 0) {
                this.title = this.value;
            }
        });
    };

    // Apply the watermark behaviour
    $.fn.rbiApplyWatermark = function(waterMarkValue) {
        // Get a consistant jQuery handle on the watermark control
        var waterMarkControl = $(this);

        // Set the initial watermark value, but only if the current value is "", otherwise
        // we clear any existing values by placing the watermark on it...
        if (waterMarkControl.val() == "") {
            // Set the value to be the watermark value
            waterMarkControl.val(waterMarkValue);

            // Add the class to show that we are watermarked
            waterMarkControl.addClass("watermarked");
        }

        return waterMarkControl.unbind().focus(function() {
            // When we are given focus clear the watermark value and styling if the control still
            // has the watermark value.
            if (waterMarkControl.val() == waterMarkValue) {
                waterMarkControl.removeClass("watermarked");
                waterMarkControl.val("");
            }
        }).blur(function() {
            // When we lose focus set the watermark value and styling if the control does
            // not have any value
            if (jQuery.trim(waterMarkControl.val()) == "") {
                waterMarkControl.val(waterMarkValue);
                waterMarkControl.addClass("watermarked");
            }
        });
    };

})(jQuery);

/*
    Common jQuery related functions
*/

function rbiFilterByProperty(arr, prop, value) {
    // returns array of elements whose 'prop' property is 'value'
    return $.grep(arr, function(item) { return item[prop] == value });
};

function rbiBrowserIsIe6() {
    // $.browser is deprecated in jQuery but they are unlikely to ever remove it. Sometimes
    // you need to know this.... If it's ever removed then the reliance on it is localised 
    // here and will be easy to put back in.
    return $.browser.msie && ($.browser.version == "6.0");
}

/*
    Common jQuery initialisers
*/

/* Div Show/Hide toggle */
$(document).ready(function() {
    $(".divToggleTrigger").click(function() {
        var divToAnimate = $(this).parent().find(".divToggleDiv").filter(":not(:animated)");

        if (divToAnimate != null) {
            var triggerSpan = $(this).find("span");
            divToAnimate.slideToggle(1000, SwitchToggleTrigger(triggerSpan));
        }
    });

    function SwitchToggleTrigger(triggerSpan) {
        if ($(triggerSpan).hasClass("up")) {
            $(triggerSpan).removeClass("up").addClass("down");
        }
        else {
            $(triggerSpan).removeClass("down").addClass("up");
        }
    }
});
