﻿(function() {
    var originalAjaxMethod = jQuery.ajax;
    jQuery.extend({
        ajax: function(s) {
            if ($.browser.msie && s.type.toUpperCase() == 'GET') {
                if (s.data) {
                    s.data['ieGetTimeStamp'] = (new Date()).getTime().toString();
                } else {
                    s.data = {'ieGetTimestap' : (new Date()).getTime().toString() };
                }
            }
            return originalAjaxMethod.call(this, s);
        }
    });
})();

var path_to_root = (window.location.host == 'localhost') ? '/BizPlan/' : '/';


function togglePanel(panelId, collapsed) {
	var collPanel = null;
	if (typeof (panelId) == 'string') collPanel = $find(panelId);
	else collPanel = $(panelId).get(0);
	collPanel.set_Collapsed(collapsed);
}

String.prototype.startsWith = function(t) {
    return (t == this.substring(0, t.length));
}

String.prototype.endsWith = function(t) {
    return (t == this.substring(this.length - t.length));
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}

Array.prototype.remove = function (from, to) {

    //examples
    // Remove the second item from the array
    //array.remove(1);
    // Remove the second-to-last item from the array
    //array.remove(-2);
    // Remove the second and third items from the array
    //array.remove(1, 2);
    // Remove the last and second-to-last items from the array
    //array.remove(-2, -1);
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

$(document).ready(function() {
	$('.DefaultButtonContainer').bind('keypress', function(e) {
		var e = e || window.event;
		var key = e.keyCode || e.which;

		if (key == 13) {
			__doPostBack($(this).find('.DefaultButton:first').attr('id').replace(/_/g, '$'), '')
			return false;
		}
		return true;
	});
	$('.ModalPanel .button.close').click(function() { $(this).parents('.ModalPanel:first').get(0).hide();return false; });
});

var BPNotifyTimeout;
function BPNotify(html, cssJSON) {
    if ($('.BPNotification').length > 0) {
        clearTimeout(BPNotifyTimeout);
        $('.BPNotification').fadeOut('slow', function () {
            $('.BPNotification').remove();
            var notifyDiv = $('<div class="BPNotification"></div>');
            $(notifyDiv).html(html)
                .prependTo('body')
                .css('margin-left', '-' + ($(notifyDiv).outerWidth() / 2) + 'px')
                .fadeIn('slow', function () {
                    BPNotifyTimeout = setTimeout("$('.BPNotification').fadeOut('slow', function() { $('.BPNotification').remove() });", 3000);
                });
        });
    } else {
        var notifyDiv = $('<div class="BPNotification"></div>');
        $(notifyDiv).html(html)
        .prependTo('body')
        .css('margin-left', '-' + ($(notifyDiv).outerWidth() / 2) + 'px')
        .fadeIn('slow', function () {
            BPNotifyTimeout = setTimeout("$('.BPNotification').fadeOut('slow', function() { $('.BPNotification').remove(); });", 3000);
        });
    }
}

function ClearBPNotify() {
    if (BPNotifyTimeout) {
        clearTimeout(BPNotifyTimeout);
    }

    $('.BPNotification').fadeOut('slow', function () { $('.BPNotification').remove(); });
}
