/**
*	Works on data tables (table.data). Triggers on Mouseover colour changes for all rows
*	Can be prevented by assigning a class of nohover to the row
*/
$('.data tr').livequery(function(){
	if($(this).is('.nohover')) {
		return;
	}

    // use the helper function hover to bind a mouseover and mouseout event
	$(this) 
        .hover(function() { 
            $(this).addClass('hover'); 
        }, function() { 
            $(this).removeClass('hover'); 
        }); 
	}, function() { 
	    // unbind the mouseover and mouseout events 
	    $(this) 
	        .unbind('mouseover') 
	        .unbind('mouseout'); 
	});



/**
*	Works on data tables (table.data). Triggers on Mouseover colour changes for rows
*	other than the <th> tags
*/
$('.data td').livequery('click', function(){  
	if(!$(this).is('.noclick')) {
		window.location.href = $(this).parent('tr').attr('id');
	}
});


/**
* Set up the tabbed interface and stores the last visited tab in a cookie for 14 days
* if the #tabbed surrounding div tag has the class total-recall
* i.e. <div id="tabbed" class="total-recall"><ul></ul><div></div></div>
*
*/
$('#tabbed > ul').livequery(function() {
	if($(this).parent('#tabbed').hasClass('total-recall')) {
		$(this).tabs({ cookie: { expires: 14 } });	
	} else {
		$(this).tabs();
	}	
});



// SPECIFIC METHODS

var RAINBOW = new Object();


/**
*	Method to add a class to any specified element on start of an Ajax request
*	and to remove the class after the request completes.
*	@param string the DOM element to attach the class to (can be any valid jquery path) - required
*	@param string optional default class name is busy but if className is specified then will be used instead
*	Method adapted from http://www.thinkingphp.org/2007/06/27/simple-global-ajax-activity-indicator-with-jquery/
*/
RAINBOW.ajaxActive = function(attachTo, className)
{
	// a DOM element is required
    if(attachTo != undefined) {
		var element = $(attachTo);
	} else {
		return;
	}

	// if a class name has been passed in then use it
	// else use the default
    if(undefined != className) {
		var className = className
	} else {
		var className = 'busy'
	}

    $(document).ajaxSend(function() {
        element
			.removeClass(className)
			.addClass(className);
    });

	$(document).ajaxStop(function() {
		element.removeClass(className)
	});
};

/**
 *
 *	@param string
 *	@param string URL for Ajax call to fetch element
 *	@param object the calling element (new element is inserted before this element)
 *	@param string the container in which the newly created element lives. OPTIONAL.
 	If created the method will call sortableRefresh on the container
 *	@erturn null
 */
RAINBOW.genericInsertRow = function(identity, url, element, container) {
	// calculate the increment number of the new row
	var count = $('.' + identity).size() + 1;

	// place an ajax call to fetch the new row
	$.get
	(
		url + 'count:' + count
		, function(data){
			$(data).insertBefore(element);
		}
	);
};


/**
 *	iterates through each idetified elements (if any)
 *	and resequence name of each found field to their
 *	position in the revised version of the form
 *	@param string
 *	@erturn null
 */
RAINBOW.resequenceFields = function(identity) {
	$('.' + identity).each(
		function(i) {
			var match = '[0-9+]';
			var replace = i+1;
			var inputs = $(this).find('input');
			var selects = $(this).find('select');
			var bullets = $(this).find('td.order');

			$(inputs).each
			(
				function(){
					$(this).attr('name', RAINBOW.preg_replace(match, replace, $(this).attr('name')))
				}
			)

			$(selects).each
			(
				function(){
					$(this).attr('name', RAINBOW.preg_replace(match, replace, $(this).attr('name')))
				}
			)

			$(bullets).each
			(
				function(){
					$(this).html(replace)
				}
			)
	});
};

/**
 *	Javascript equivalent(ish) to PHP preg_replace method
 *
 */
RAINBOW.preg_replace = function(pattern, pattern_replace, my_string)
{
	var new_string = String (my_string);
	var reg_exp = RegExp(pattern, 'g');
	return new_string.replace (reg_exp, pattern_replace);
};

