
/*

	News Rotator - jQuery plug-in
	
	

	-----------------------------------------------------------------------------------------------------------------------------

*/

(function($){



$.fn.BCWebAppSort = function(options)
{
	if(!options.q_elements || notFound(options.q_elements))throw new Error('BCWebAppSort expects an argument \'q_elements\'');
	if(!options.q_field || notFound($(options.q_elements).find(options.q_field)))throw new Error('BCWebAppSort expects an argument \'q_field\'');
	
	var o = jQuery.extend({//Default values
		sortOrder: 'ascending',
		sortDataType: 'string',
		effectDuration: 70,
		effect: 'linear'
	},options);	
	
	function notFound(q)
	{
		if($(q).length == 0)return true;
		return null;
	}
	
	
	function sortAsc(a,b)//Function which sorts array in ascending order
	{
		if(o.sortDataType == 'number')
		{
			a = isNaN(a) || isNaN(b) ? a : parseInt(a);
			b = isNaN(a) || isNaN(b) ? b : parseInt(b);
		}
		
		if(a.val1 > b.val1)
		{
			return 1;	
		} else if(a.val1 < b.val1) {
			return -1;	
		} else {
			if(a.val2 && b.val2)
			{
				if(a.val2 > b.val2)
				{
					return 1;	
				} else {
					return -1;	
				}
			}
		}
	}
	
	function sortDesc(a,b)//Function which sorts array in descending order
	{
		if(o.sortDataType == 'number')
		{
			a = isNaN(a) || isNaN(b) ? a : parseInt(a);
			b = isNaN(a) || isNaN(b) ? b : parseInt(b);
		}
		
		if(a.val1 < b.val1)
		{
			return 1;	
		} else if(a.val1 > b.val1) {
			return -1;	
		} else {
			if(a.val2 && b.val2)
			{
				if(a.val2 < b.val2)
				{
					return 1;	
				} else {
					return -1;	
				}
			}
		}
	}
	

	return this.each(function(){//Loop through each parent element
		
		var parent = this;//Reference to parent
		
		var items = new Array();//Array to store details extracted from elements on page
		$(this).find(o.q_elements).each(function($key){
			items.push({
						   val1: $(this).find(o.q_field).text(),//First value to be sorted
						   pos: $key,//Position of element on page
						   val2: (!o.q_extrafield) ? null : o.q_extrafield//Extra field to be tested in case first values is the same
						                                                  //of what it is being compared to
					   });										 
		});
		
		$(this).animate({opacity:0},o.effectDuration,o.effect,function(){//Hide elements
			
			items.sort((o.sortOrder.toLowerCase().indexOf('asc') != -1) ? sortAsc : sortDesc);//sort items
			var detached = $(parent).find(o.q_elements).detach();//Remove items from page
			$(items).each(function($key,$value){//Loop through each sorted item and reinsert it to page
				var index = parseInt($value.pos);//Get index of current item
				$(detached[index]).appendTo(parent);//Attach item to parent
			});
			setTimeout(function(){$(parent).animate({opacity:1},o.effectDuration,o.effect);},100)
		});
		
		
	});
	
	
};
		  
})(jQuery);


































