(function( $ ){
	
	var settings = {
		orientation:'horizontal',
		resetToFirstNewFilter:false,
		onSubmit:null
	};

	var methods = {
		init : function( options ) {
			return this.each(function(){
				
				var $this = $(this);
				var data = $this.data('projectsSubnav');
							
				if ( options ) $.extend( settings, options );
								
				// If the plugin hasn't been initialized yet
				if (!data) {	
					$this.data('projectsSubnav', {
						orientation:null,
						resetToFirstNewFilter:null,
						target:null,
						functions:{
							onSubmit:null,
							onReset:null
						}
					});
					data = $this.data('projectsSubnav');
					
					data.orientation = settings.orientation;
					data.resetToFirstNewFilter = settings.resetToFirstNewFilter;
					data.functions.onSubmit = settings.onSubmit;
					data.functions.onReset = settings.onReset;
										
					$this.addClass('jq-projectsSubnav');
					$this.addClass(data.orientation);
					
					var $skin;
					var $check;
					$this.find('input.checkbox').each(function(){
						$check = $(this);
						$skin = $('<div class="checkbox-skin"><a href="#"></a></div>');
						$skin.addClass($check.attr('alt'));
						if($check.is(':checked')) $skin.addClass('checked');
						$skin.find('a').text($check.attr('title'));
						if($check.attr('alt') == 'reset'){
							$skin.find('a').html('&#215;&nbsp;Reset');
							$skin.hide();
						}
						$skin.data('id', $check.val());
						$check.after($skin).hide();
					});
					
					$('.checkbox-skin:not(.reset)').click(function(){
						var $t = $(this);
						var id = $t.data('id');
						var $s = $t.siblings('input[value="'+id+'"]');
						
						if(data.resetToFirstNewFilter){
							$this.projectsSubnav('cleanFilters');
							$t.addClass('checked');
							$s.attr('checked', 'checked');
						}else{
							if($t.hasClass('checked')){
								$t.removeClass('checked');
								$s.removeAttr('checked');
							}else{
								$t.addClass('checked');
								$s.attr('checked', 'checked');
							}
						}
						
						$this.projectsSubnav('submit');
						return false;
					});
					
					$('.checkbox-skin.reset').click(function(){
						$this.projectsSubnav('reset');
						return false;
					});
					
					if($this.find('input:checked, option:selected').length > 0) $this.find('.checkbox-skin.reset').show();
					
					var $selectSkin = $('<div class="select-skin"><div class="selected"><a href="#"></a></div><ul class="options"></ul></div>');
					var $optionsSkin = $selectSkin.find('ul.options');
					var $option;
					$this.find('select option').each(function(){
						$option = $(this);
						$skin = $('<li class="option-skin"><a href="#">' + $option.attr('title') + '</a></li>');
						$skin.addClass($option.attr('alt'));
						if($option.is(':selected')) $selectSkin.find('.selected a').text($option.attr('title'))
						$skin.data('id', $option.val());
						$optionsSkin.append($skin);
					});
					$this.find('select').after($selectSkin).hide();
					
					$selectSkin.hover(function(){
						$(this).addClass('active');
						$this.projectsSubnav('openClientList');
						return false;
					},function(){
						$(this).removeClass('active');
						$this.projectsSubnav('closeClientList');
						return false;
					}).find('.option-skin').click(function(){
						if(data.resetToFirstNewFilter){
							$this.projectsSubnav('cleanFilters');
						}
						// $this.find('select option:selected').attr('selected', '');
						// $this.find('select option[value="' + $(this).data('id') + '"]').attr('selected', 'selected');
						// $this.find('.select-skin .selected a').text($(this).text());
						// $this.projectsSubnav('closeClientList');
						$this.projectsSubnav('selectClient', $(this).data('id'), true);
						
						$this.projectsSubnav('submit');
						return false;
					});
				}
			});
		},
		openClientList:function(){
			return this.each(function(){
				$(this).find('.select-skin .options').stop(true, true);
				$(this).find('.select-skin .options').slideDown('fast');
			});
			
		},
		closeClientList:function(){
			return this.each(function(){
				$(this).find('.select-skin .options').stop(true, true);
				$(this).find('.select-skin .options').slideUp('fast');
			});		
		},
		submit:function(){
			return this.each(function(){
				var $this = $(this);
				var data = $this.data('projectsSubnav');
				
				var filters = [];
				$this.find('input:checked, option:selected').each(function(){
					if($(this).val() > 0) filters.push($(this).val());
				});

				if($this.find('input:checked').length > 0 || $this.find('option:selected').val() > 0){ 
					$this.find('.checkbox-skin.reset').show();
				}else{
					$this.find('.checkbox-skin.reset').hide();
				}
				
				if(data.functions.onSubmit) data.functions.onSubmit(filters);
			});
		},
		reset:function(){
			return this.each(function(){
				var $this = $(this);
				var data = $this.data('projectsSubnav');
				
				$this.find('input:checked, option:selected').removeAttr('checked').removeAttr('selected');
				$this.find('.checkbox-skin').removeClass('checked');
				$this.find('.select-skin .selected a').text($this.find('select option:eq(0)').attr('title'));
				if(data.functions.onReset) data.functions.onReset();
				$this.projectsSubnav('submit');
			});
		},
		cleanFilters:function(){
			return this.each(function(){
				var $this = $(this);
				
				$('#filter-form input:checkbox').removeAttr('checked');
				$('#filter-form .checkbox-skin').removeClass('checked');
				$('#filter-form select option:selected').removeAttr('selected');
				
				$this.projectsSubnav('selectClient', -1, true);
			});
		},
		selectClient:function(id, andClose){
			return this.each(function(){
				var $this = $(this);
				var txt = $this.find('select option[value="' + id + '"]').text();
				
				andClose = (andClose == false) ? false : true;
				
				$this.find('select option:selected').attr('selected', '');
				$this.find('select option[value="' + id + '"]').attr('selected', 'selected');
				$this.find('.select-skin .selected a').text(txt);
				if(andClose) $this.projectsSubnav('closeClientList');
			});
		}
	};
		
	$.fn.projectsSubnav = function( method ) {
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.projectsSubnav' );
		}    
	
	};	
})( jQuery );

