/* JavaScript File Area Digital CMS	(Area Digital 4.2)				*/
/* ad.dependent.js 													*/
/* Modified July 04th 2010								 			*/
/* http://www.areadigital.org/		 								*/

(function($){
	
	$.fn.dependent = function(settings){
		
		// merge default settings with dynamic settings
		$param = $.extend({}, $.fn.dependent.defaults, settings);
		
		this.each(function() {
			
			$this = $(this);
			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 ) {
				$title = $($parent).find('option:selected').attr('title');
				$($child).find('option[value=0]').attr('class', $title);
				$($child).find('option[class!='+$title+']').remove();
			}
			
			_parentChange($parent, $child, $group, $holder_cls, $child_cls);
		});
			
		return this;
	};
	
	 
	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');
		}
						
		$($child).find('option[value]').each(function($i) {
						
			$value = $(this).attr('value');
			$class = $(this).attr('class');
			$title = $(this).attr('title');
			$text  = $(this).text();
			
			if ($i == 0) {
				$($holder_cls+$child_cls).append('<option value="'+$value+'">'+$text+'</option>\n');
			}
			else {
				$($holder_cls+$child_cls).append('<option value="'+$value+'" class="'+$class+'" title="'+$title+'">'+$text+'</option>\n');
			}
		});
	}
	
	function _parentChange($parent, $child, $group, $holder_cls, $child_cls) {
				
		$($parent).bind('change', function(){
										   			
			// remove all the child's options
			$($child).find('option[value!=]').remove();
			$title = $(this).find('option:selected').attr('title');
			
			if ($param.group != '') {
				
				$index = $($group).index($(this))+1;
				$($group+':gt('+ $index +')').find('option[value!=]').remove();
				
				$($group+':gt('+ $index + ')').each(function($i){
					
					var $id = $(this);
					$($holder_cls + '.' + $(this).attr('id')).find('option').each(function() {
						
						var $value = $(this).attr('value');
						var $class = $(this).attr('class');
						var $title = $(this).attr('title');
						var $text  = $(this).attr('text');
						
						$($id).append('<option value="'+$value+'" class="'+$class+'" title="'+$title+'">'+$text+'</option>');
					});
				});
			}
			
			if($title == '') {
				
				var $option = $($child_cls).find('option[value=0]');
				$($child).prepend('<option value="'+$option.attr('value')+'">'+$option.text()+'</option>\n');
				
				$($holder_cls + $child_cls).find('option[value!=0]').each(function() {
																		  
					var $value = $(this).attr('value');
					var $class = $(this).attr('class');
					var $title = $(this).attr('title');
					var $text  = $(this).attr('text');
																			  
					$($child).append('<option value="'+$value+'" class="'+$class+'" title="'+$title+'">'+$text+'</option>');
				});
			}
			else {
				
				var $option = $($child_cls).find('option[value=0]');
				$($child).prepend('<option value="'+$option.attr('value')+'" class="'+$title+'">'+$option.text()+'</option>\n');
			
				$($holder_cls + $child_cls).find('option[class='+$title+']').each(function() {
																		  
					var $value = $(this).attr('value');
					var $class = $(this).attr('class');
					var $title = $(this).attr('title');
					var $text  = $(this).attr('text');
																			  
					$($child).append('<option value="'+$value+'" class="'+$class+'" title="'+$title+'">'+$text+'</option>');
				});
			}
		});
	}
	
	$.fn.dependent.defaults = {	
		parent:			'parent_id'
	};
	
})(jQuery);

$(document).ready(function(){
						   
// ------------------------------------------ Dependent Selector - Function to show the correct selections ----------------------------------------------------

	$('#wine-region').dependent({parent: 'wine-country', group: 'wine-selector'});
	$('#wine-producer').dependent({parent: 'wine-region', group: 'wine-selector'});
	$('#wine-type').dependent({parent: 'wine-producer', group: 'wine-selector'});
	
// ------------------------------------------------------------------------------------------------------------------------------------------------------------

});

