/*
* jQuery-1.4.4 replaceDropdown by FirstView (http://firstviewmedia.com).
* This lets replace a select tag with options with a custom dropdown
* Use of a css file is neccessary.
*/
/*
Eg. :

$( '.jsUlDropDownReplace' ).replaceDropdown({
	easing:'easeInOutExpo',
	animSpeedIn:400,
	animSpeedOut:400
});
*/
_ddReplaceCount = 0;
(function($){
	$.fn.replaceDropdown = function(opts) {
	
		var defaults = {
			title:'Please make a selection',		// HTML of dropdown anchor //
			actLikeRealSelect:false,				// Put's the initial value first. //
			val:'',									// Initial val of dropdown - very well could be overwritten. //
			wrapperClass:'_jsDropdownWrapper',		// Wrapper div class. //
			containerClass:'_jsDropdownContainer',	// Container ul class. //
			initClass:'_jsInitDropdown',			// Init a class. //
			animate:true,							// Slide down & up. //
			easing:'',								// Slide down & up. //
			animSpeedIn:300,						// Slide in speed. //
			animSpeedOut:200,						// Slide out speed. //
			submitIfSingle:true						// Submits the form of it's the only form element in the form. //
		};
		var opts									= $.extend(defaults, opts);
		
		return this.each(function( i ){
			
			// Define vars. //
			var $this								= $(this);
			var $form								= $this.parent();
			var ddName								= $this.attr('name');
			var ddId								= $(this).attr('id');
			var numSiblings							= $this.siblings().size();
			var defaultTitle						= $this.data('customTitle') ? $this.data('customTitle') : opts.title;
			
			_ddReplaceCount++;
			
			// Use the current info to build new HTML & define newThis. //
			$this.after('<div class="'+opts.wrapperClass+' _jsWrapperNumber_'+_ddReplaceCount+'"><a href="javascript:;" class="'+opts.initClass+'">'+defaultTitle+'</a><ul class="'+opts.containerClass+'"></ul><input id="'+ddId+'" name="'+ddName+'" type="hidden" value="" /></div>');
			var $newThis = $('.'+opts.wrapperClass+'._jsWrapperNumber_'+_ddReplaceCount);
			$this.children().each( function () {
				$(this).relToData();
				var val = $(this).attr('val');
				var disabledClass = $(this).data('disabled') ? 'disabled':'';
				if( !val ) val = $(this).val();
				var html = $(this).html();
				
				if( (opts.actLikeRealSelect && $(this).attr('selected')) || $(this).data('selected') ){
					//defaultTitle = html;
					opts.val = val;
				}else{
					//defaultTitle = $this.data('customTitle');
				}
				
				$newThis.find('.'+opts.containerClass).append('<li class="'+disabledClass+'">'+html+'</li>')
					.find('li').last().data('val', val);
			});
			
			// Remove old HTML & define other objects. //
			$this.remove();
			var $ddContainer						= $newThis.find('.'+opts.containerClass);
			var $ddInitDd							= $newThis.find('.'+opts.initClass);
			var $ddLi								= $ddContainer.find('li');
			
			$('#'+ddId).val(opts.val);
			$ddInitDd.html(defaultTitle);
			
			
			// Bind Interactivity. //
			$ddContainer.hide();
			$ddInitDd.toggle(function( e ){
				e.preventDefault();
				if( $newThis.hasClass('disabled') ){
					$ddInitDd.click();
					return false;
				}
				$ddInitDd.data('oldTitle', $(this).html()).data('open', true).data('bodyClick', false);
				$ddInitDd.addClass('selected').html(defaultTitle)
				if( opts.animate ) $ddContainer.slideDown(opts.animSpeedIn, opts.easing);
				else $ddContainer.show();
			}, function ( event ) {
				event.preventDefault();
				if( $newThis.hasClass('disabled') ) return false;
				if( opts.animate ){
					$ddInitDd.data('open', false).data('bodyClick', true);
					$ddContainer.slideUp(opts.animSpeedOut, opts.easing, function(){
						$ddInitDd.removeClass('selected');
						console.log("$ddInitDd.data('oldTitle') : ", $ddInitDd.data('oldTitle'));
					});
				}
				else{
					$ddInitDd.removeClass('selected').data('open', false).data('bodyClick', true);
					$ddContainer.hide();
				}
				$ddInitDd.html($ddInitDd.data( 'oldTitle' ));
			});
		
			$ddLi.click(function( event ) {
				if( $(this).hasClass('disabled') ) return false;
				var title = $(this).html();
				var val = $(this).data('val');
				$('#'+ddId).val(val).change();
				$ddInitDd.data( 'oldTitle', title ).html(title).click();
				if( opts.animate ){
					$ddContainer.slideUp(opts.animSpeedOut, opts.easing, function(){
						$ddInitDd.removeClass('selected');
						if( !numSiblings && opts.submitIfSingle ) $form.submit();
					});
				}else{
					$ddContainer.hide();
					if( !numSiblings && opts.submitIfSingle ) $form.submit();
				}
			});
		
			$( 'html' ).click( function () {
				if( $ddInitDd.data('open') && !$ddInitDd.data('bodyClick') ){
					$ddInitDd.click().html($ddInitDd.data( 'oldTitle' ));
				}
				$ddInitDd.data('bodyClick', false);
			});
			
			return this;
		});
	};
})(jQuery);

