/*
*Moves option elements from one selector to other 
*
*
*
*
*
*/

;(function($) {
$.fn.extend({moves:function(options){
var args = Array.prototype.slice.call(arguments, 1);
this.each(function(){
if(typeof options == "string")
	{
	var jSelector=$.data(this,"jselector");
		try
			{jSelector[options].apply(jSelector, args);}
		catch(e)
			{window.console.log('Error');}
	}
	else
	{
	$.data(this, "jselector", new $.jSelector(this,options));
	var aa=$.data(this,"jselector");
	}

	
}

)
}
});

$.jSelector=function(container,options){


var defaults = {
allToRight: ">>",  //text of the button, that moves everything from left select to right.
selectedToRight: ">", //text of the button, that moves selected option elements from left select to right.
selectedToLeft: "<", //text of the button, that moves selected option elements from right select to left.
allToLeft: "<<", //text of the button, that moves everything from right select to left.
findButtons :true, // looking existing buttons in the container
allButtons:true,
hideEmptyOptgroup:false,
callback:function(selectedItems, direction){}
};
this.element=container;
this.current=10;
var moveAllArray=new Array(false, false, true,true);
var isLeftToRightArray=new Array(true, false, true, false);
var classesArray=new Array("selected-to-right", "selected-to-left", "all-to-right", "all-to-left");
var textArray=new Array();


this.options=$.extend({}, defaults, options || {});

textArray[0]=this.options.selectedToRight;
textArray[1]=this.options.selectedToLeft;
textArray[2]=this.options.allToRight;
textArray[3]=this.options.allToLeft;
var findButtons=this.options.findButtons;
var self=this;
					jQuery(this.element).each(function()
									{
									var elements=$(this).find("select");
									$.removeData(self, 'jselector.first');
									$.removeData(self, 'jselector.second');
									$.data(self, 'jselector.first',elements.eq(0).clone(true));
									$.data(self, 'jselector.second',elements.eq(1).clone(true));
									$.data(self, 'jselector.optsgroup', elements.eq(0).find('optgroup'));
									$("option", elements.eq(0)).each(
																	function(){
																				if($(this).parent().get(0).tagName.toLowerCase() == 'optgroup')
																				{var optgindex=elements.eq(0).find('optgroup').index($(this).parent().get(0));
																				$.data(this, 'jselector.optgindex',optgindex);
																				}
																				else
																					$.data(this, 'jselector.optgindex',-1);
																	});
									if (!self.options.findButtons) 
										{$("button", this).remove();
											$(this).append($('<button>'+textArray[0] +'</button>'));
											$(this).append($('<button>'+textArray[1] +'</button>'));
										 if(self.options.allButtons)
											{$(this).preppend($('<button>'+textArray[2] +'</button>'));
											 $(this).append($('<button>'+textArray[3] +'</button>'));
											}
										}
									var buttons=$(this).find("button");
									$("button", this).
									each(function(index, element){
									$(element).addClass(classesArray[index]).click(function(){jSelector_move.call(self,elements[0], elements[1], moveAllArray[index], isLeftToRightArray[index]); return false});}); 
									
									});
									
};

$.jSelector.prototype={
reset: function(){
					var defaultFirst=$.data(this, 'jselector.first');
					var defaultSecond=$.data(this, 'jselector.second');
					$(this.element).find("select").eq(0).replaceWith($(defaultFirst));
					$(this.element).find("select").eq(1).replaceWith($(defaultSecond));
					
					$(this.element).moves(this.options);
					
					}

}

/*
* @method move
* @description Moves option elements from one selector to other 
* @param {Element} sel1. Left select
* @param {Element} sel22. Right select
* @param {Boolean} moveAll. If true, then moves all options from one to another. By default is false. 
* @param {Boolean} isLeftToRight. Direction of movement. If true, then moves option from left to right, otherwise from right to left. By default is true.
* */
function jSelector_move(sel1, sel2, moveAll, isLeftToRight)
{
var select1, select2;

select1=$(sel1, document);
select2=$(sel2, document);

	if (isLeftToRight == undefined){isLeftToRight=true;}

var from=isLeftToRight?select1:select2;
var to=isLeftToRight?select2:select1;

var from_collection=from.get(0).options;
var to_collection=to.get(0).options;
var self=this;
var callbackExists=this.options.callback && (typeof this.options.callback == 'function');

	if(moveAll)
		{   if($("option", from).length>0)
			  {if (callbackExists)
			  this.options.callback.call(this, $("option", from), isLeftToRight);
				$("option:selected", to).attr("selected", false);
				
				if (isLeftToRight)
				$("option", from).appendTo(to);
				else
				{
				
				$("option", from).each(
										function()
													{
													if($.data(this,'jselector.optgindex') == -1)
													{
													$(this).appendTo(to);
													}
													else
													
													$.data(self, 'jselector.optsgroup').eq($.data(this,'jselector.optgindex')).show().append($(this));
													}
										
										);}
			  }
		}
	else if ($("option:selected", from).length>0)
		{
		if (callbackExists)
		this.options.callback.call(this, $("option:selected", from), isLeftToRight);
		
		 $("option:selected", to).attr("selected", false);
		 if (isLeftToRight)
		 {
		 
		 $("option:selected", from).appendTo(to);
		 if(self.options.hideEmptyOptgroup)
		 {$("optgroup", from).each(function(){
		 	if($('option',this).length == 0) $(this).hide()
		 	
		 	})}
		 
		 }
		 else
		 {$("option:selected", from).each(
										function()
													{
													if($.data(this,'jselector.optgindex') == -1)
													{
													$(this).appendTo(to);
													}
													else
													
													$.data(self, 'jselector.optsgroup').eq($.data(this,'jselector.optgindex')).show().append($(this));
													}
										
										);
									
										}
		}

};


   
})(jQuery)