/********************************************************************
 * jQuery Dependent Select plug-in									*
 *																	*
 * @version		2.5													*
 * @copyright	(c) Bau Alexandru 2009 								*
 * @author 		Bau Alexandru										*
 * @email		bau.alexandru@gmail.com								*
 *																	*
 * @depends		jQuery									            *
 * 																	*
 * 																	*
 * Do not delete or modify this header!								*
 *																	*
 * 																	*
 * Plugin call example:												*
 * 																	*
 * jQuery(function($){												*
 *																	*
 *		SINGLE CHILD												*
 *		$('#child_id').dependent({							        *
 *			parent:	'parent_id',									*
 *			group:	'common_class'									*
 *		});															*
 *																	*
 *		MULTIPLE CHILDS												*
 *		$('#child_id').dependent({							        *
 *			parent:	'parent_id' 									*
 *		});															*
 *																	*
 *	});																*
 *																	*
 ********************************************************************/

(function($){	// create closure

	/**
	 * Plug-in initialization
	 * @param	object	plug-in options
	 * @return object this
	 */
	$.fn.dependent = function(settings){
		// merge default settings with dynamic settings
		$param = $.extend({}, $.fn.dependent.defaults, settings);

		this.each(function(){														// for each element
			$this = $(this);														// current element object

			var $parent = '#'+$param.parent;

			var $child = $this;
			var $child_id = $($child).attr('id');
			var $child_cls = '.'+$child_id;

			if ($param.group != ''){
				var $group = '.'+$param.group;
			}

			var $index = 0;
			var $holder  = 'dpslctholder';
			var $holder_cls	= '.'+$holder;

			_createHolder($holder, $holder_cls, $child, $child_id, $child_cls);

			// check if parent allready has an option selected
			if( $($parent).val() != 0 ) {

				$class = $($parent).find('option:selected').attr('class');
				$($child).find('option[rel!='+$class+']').remove();
				// BDS - removed: no default options needed
				// $($child).prepend('<option value="">-- select --</option>');
			} else {
				// remove the child's options and add a default option
				$($child).find('option').remove();
				// BDS - removed:
				// $($child).append('<option value="">-- select --</option>');
			}

      _fixSelectmenu($child);
			_parentChange($parent, $child, $group, $holder_cls, $child_cls);

		});

		return this;
	};

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/*********************************
	 * BEGIN PLUG-IN PRIVATE METHODS *
	 *********************************/

	/**
	 * Private function description
	 */

	function _createHolder($holder, $holder_cls, $child, $child_id, $child_cls){

		// create a select to hold the options from all this child
		var $is_created = $($holder_cls+' '+$child_id).size();

		if( $is_created == 0 ){
			$('body').append('\n\n<select class="'+$holder+' '+$child_id+'" style="display:none">\n</select>\n');
		}

		// add options to the holder
		$($child).find('option[value!=]').each(function(){

			$value = $(this).attr('value');
			$class = $(this).attr('class');
      $rel   = $(this).attr('rel');
      //BDS_CHANGE add custom attributes
      $add_attributes = '';
			if($param.add_attributes != null)
			{
			  for(var i=0; i < $param.add_attributes.length; i++)
			  {
			    $add_attributes += $param.add_attributes[i] + '="' + $(this).attr($param.add_attributes[i]) + '" ';
			  }
			}
			$text  = $(this).text();

			$($holder_cls+$child_cls).append('<option value="'+$value+'" class="'+$class+'" rel="'+$rel+'" ' + $add_attributes +' >'+$text+'</option>\n');
		});
	}

	function _parentChange($parent, $child, $group, $holder_cls, $child_cls){

		// on change event
		$($parent).bind('change', function(){

			// remove all the child's options
			$($child).find('option[value!=]').remove();

			$index = $($group).index($(this));

			// set all the selects from the group to the default option
			if( $param.group != '' ){
				$($group+':gt('+ $index +')').find('option[value!=]').remove();
			}

			$class = $(this).find('option:selected').attr('class');

			// add options to the child mask from the holder
			$blInit = true;
			$($holder_cls+$child_cls).find('option[rel='+$class+']').each(function(){

				$value = $(this).attr('value');
				$class = $(this).attr('class');
				$rel   = $(this).attr('rel');
				$text  = $(this).text();
        //BDS_CHANGE add custom attributes
        $add_attributes = '';
				if($param.add_attributes != null)
				{
				  for(var i=0; i < $param.add_attributes.length; i++)
				  {
				    $add_attributes += $param.add_attributes[i] + '="' + $(this).attr($param.add_attributes[i]) + '" ';
				  }
				}
				$($child).append('<option value="'+$value+'" class="'+$class+'" rel="'+$rel+'" '+ $add_attributes + ((!$blInit) ? ' selected="selected"' : '') + '>'+$text+'</option>');
				$blInit = true;
			});

			$($child).triggerHandler('change');
      _fixSelectmenu($child);
		});

	}

  function _fixSelectmenu($child) {
    if ($($child).is('.inputSelect')) $($child).selectmenu("destroy");
    if ($($child).find('option').length > 0) {
      $($child).show();
      if ($($child).is('.inputSelect')) $($child).selectmenu({
        style:'dropdown',
        maxHeight: 500
      });
    }
    else {
      $($child).hide();
    }
  }

	/********************************
	 * /END PLUG-IN PRIVATE METHODS *
	 ********************************/

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/************************************
	 * BEGIN PLUG-IN DEFAULT PARAMETERS *
	 ************************************/

	$.fn.dependent.defaults = {
		parent:		'parent_id',
		add_attributes: null
	};

	/***********************************
	 * /END PLUG-IN DEFAULT PARAMETERS *
	 ***********************************/

})(jQuery);		// end closure

