
jQuery(function($){

	var form = $('.tabsearch form');

	form.zinePretifyForm();
	
});

(function($){
	
	$.fn.zinePretifyForm = function(){
		return this.each(function(){

			var form = $(this);

			/*form.find('input[type=button],input[type=submit]').each(function(){

				var originalButton = $(this),
					button = $('<span>',{
						className	: 'button',
						html		: originalButton.val()+'<span></span>'
					});

				button.insertAfter(originalButton.hide());

				button.click(function(){
					originalButton.click();
				});

			});*/
			
			form.find('input[type=checkbox]').each(function(){

				var originalCheckBox = $(this),
					checkBox = $('<span>',{
						className	: 'checkBox '+(this.checked?'checked':'')
					});
				
				checkBox.insertAfter(originalCheckBox.hide());
				
				checkBox.click(function(){
					checkBox.toggleClass('checked');
					originalCheckBox.attr('checked',checkBox.hasClass('checked'));
				});

			});
			
			form.find('input[type=radio]').each(function(){
				
				var originalRadio = $(this),
					radio = $('<span>',{
						className	: 'radio '+(this.checked?'checked':'')
					});
				
				radio.insertAfter(originalRadio.hide());
				
				radio.click(function(){
					$('input[type=radio][name='+originalRadio.attr('name')+']').each(function(){
						$(this).next().removeClass('checked');
					});
					
					radio.addClass('checked');
					originalRadio.attr('checked',true);
				});
				
			});
			
			form.find('select').each(function(i){

				var select = $(this);
			
				var selectBoxContainer = $('<span>',{
					width		: select.outerWidth(),
					className	: 'selectContainer',
					html		: '<div class="selectBox"></div><span></span>',
					css			: {zIndex : 1000-i}
				});
			
				var dropDown = $('<ul>',{className:'dropDown'});
				var selectBox = selectBoxContainer.find('.selectBox');
				
				select.find('option').each(function(i){
					var option = $(this);
					
					if(i==select.attr('selectedIndex')){
						selectBox.html(option.text());
					}
					
					var li = $('<li>',{
						html:	option.html()
					});
							
					li.click(function(){
						
						selectBox.html(option.text());
						dropDown.trigger('hide');
						
						select.val(option.val());
						return false;
					});
					
					dropDown.append(li);
				});
				
				selectBoxContainer.append(dropDown.hide());
				select.hide().after(selectBoxContainer);
				
				dropDown.bind('show',function(){
					
					if(dropDown.is(':animated')){
						return false;
					}
					
					selectBox.addClass('expanded');
					dropDown.slideDown('fast');
					
				}).bind('hide',function(){
					
					if(dropDown.is(':animated')){
						return false;
					}
					
					selectBox.removeClass('expanded');
					dropDown.slideUp('fast');
					
				}).bind('toggle',function(){
					if(selectBox.hasClass('expanded')){
						dropDown.trigger('hide');
					}
					else dropDown.trigger('show');
				});
				
				selectBoxContainer.click(function(){
					dropDown.trigger('toggle');
					return false;
				});
			
				$(document).click(function(){
					dropDown.trigger('hide');
				});
			});
			
		});
	}
	
})(jQuery);
