﻿// custom form plugin

(function($){
	$.fn.form = function(options) {
    
		var defaults = {
			container: 'select-container',
			preselectText: 'Please Select',
			speed: 250,
			ajaxSubmit: false
		};
	  
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			obj = $(this);
			var selectTitle = obj.attr('name');
			var isMultiple = obj.attr('multiple');
			
			// create the new "select box", populate from orig select, hide the orig
			obj.hide().before('<div class="' + options.container + '" id="' + selectTitle + '"><p>' + options.preselectText + '</p><ul></ul></div>');
			obj.children('option').each(function(){
				if($(this).val() != ''){
					$('#' + selectTitle + ' ul').append('<li>' + $(this).text() + '</li>');
				}
			});
			var ulHeight = $('#' + selectTitle + ' ul').height();
			$('#' + selectTitle + ' ul').append('<span></span>');
			$('#' + selectTitle + ' ul').css({'height':ulHeight});
			
			// bind the open/close animation
			$('#' + selectTitle + ' p').toggle(
				function(){
					$('body').click();
					$('~ul', this).slideDown(options.speed).addClass('open');
						$('body').click(function(){
							if($('#' + selectTitle + ' ul').hasClass('open')){
								$('#' + selectTitle + ' p').click();
								$('body').unbind();
							}
						});
				},
				function(){
					$('~ul', this).slideUp(options.speed).removeClass('open');
				}
			);
			
			if(!isMultiple){
				// bind the option select click
				$('#' + selectTitle + ' li').click(function(){
					$(this).parents('#' + selectTitle).next().find('option:contains("' + $(this).text() + '")').attr({selected:'selected'});
					$(this).parents('#' + selectTitle).children('p').text($(this).text()).click();
					return false;
				});
			}
			
			if(isMultiple){
				// bind the option select click
				$('#' + selectTitle + ' li').click(function(){
					var isSelected = $(this).hasClass('selected');
					var numSelected = 0;
					if(!isSelected){
						$(this).addClass('selected').parents('#' + selectTitle).next().find('option:contains("' + $(this).text() + '")').attr({selected:'selected'});
						$(this).parents('#' + selectTitle).next().children('option').each(function(){
							$(this).attr('selected') ? numSelected++ : numSelected = numSelected;
						});
					} else {
						$(this).removeClass('selected').parents('div').next().find('option:contains("' + $(this).text() + '")').attr({selected:''});
						$(this).parents('#' + selectTitle).next().children('option').each(function(){
							$(this).attr('selected') ? numSelected++ : numSelected = numSelected;
						});
					}
					numSelected != 0 ? $(this).parents('#' + selectTitle).children('p').text(numSelected + ' selected') : $(this).parents('#' + selectTitle).children('p').text(options.preselectText);
					return false;
				});
			}

			if(options.ajaxSubmit){
				$('#' + selectTitle + ' li').click(function(){
					$(this).parents('#' + selectTitle).next().find('option:contains("' + $(this).text() + '")').attr({selected:'selected'});
					var queryString = obj.attr('name');
					var catID = obj.find('option:contains("' + $(this).text() + '")').val();
					var targetElement = $(this).parents('form').next().children('ul').attr('class');
					$(this).parents('form').next().attr({id:'ajax-target'}).empty().append('<img src="themes/sportgenic/images/ajax-loader.gif"/>');
					$('#ajax-target').load('' + window.location + '?' + queryString + '=' + catID + ' .' + targetElement + '',function(){
						smwExpandCollapse();						
					});
					return false;
				});
			}
				
		}); // end return this.each(function)
	};
})(jQuery);

