﻿/**************************************************************************************************************************
* Date:         8-31-2010
* Author:       B. Kennedy
* Description:  Custom GENERIC JQuery extension methods used by the Pio web application.
***************************************************************************************************************************/
/* Get ASP.NET control's ID from Jquery, all you need to know is the HTML ID, to implement use $.clientID('txtFirstName') */
$.extend({ clientID: function(id) {     if($(id).length > 0) { return $("#" + id); }
    else { return $("[id$='" + id + "']"); }
    }
}); 

jQuery.fn.extend({
    isChildOf : function (parentId) {
    if(this.parent()[0])
    {
        if(this.parent()[0].id==parentId)
            return true;
        else
            return this.parent().isChildOf(parentId);
    }
    else
        return false;
    }
});

$.fn.extend({
    GetCursorPosition : function (){
	    if (this.length>0 && this[0].createTextRange) {
		    var r = document.selection.createRange().duplicate()
		    r.moveEnd('character', this.val().length)
		    if (r.text == '') return this.val().length
		    return this.val().lastIndexOf(r.text)
	    } else return this.selectionStart
	}
});

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

/**************************************************************************************************************************
* Date:         8-31-2010
* Author:       B. Kennedy
* Description:  Custom GENERIC JQuery methods used by the Pio web application.
***************************************************************************************************************************/
function clearIntention()
{
    $.clientID("txtAreaIntention").text("");
} 
function OpenIntentionModal() {
   $("#ModalHeader").html( "<p>Please Confirm....</p>");
   $("#IntentionModal").modal({close: false, closeClass : "closeModal", persist: true}); 
}
function OpenBusModal() {
   $("#ModalHeader").html( "<p>Please Confirm....</p>");
   $("#BusModal").modal({close: false, closeClass : "closeModal", persist: true}); 
}
$.fn.clearForm = function() {
  // iterate each matching form
  return this.each(function() {
    // iterate the elements within the form
    $(':input', this).each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
  });
};

(function($){
	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr[0];
	}	
})(jQuery);
