/*
 * UPDATED: 11.26.08
 *
 * jNice
 * by Sean Mooney (sean@whitespace-creative.com) 
 *
 * To Use: place in the head 
 *  <link href="inc/style/jNice.css" rel="stylesheet" type="text/css" />
 *  <script type="text/javascript" src="inc/js/jquery.jNice.js"></script>
 *
 * And apply the jNice class to the form you want to style
 *
 * To Do: Add textareas, Add File upload
 *
 ******************************************** */
(function($){	
	$.fn.jNice = function(options){
		var self = this;
		var safari = $.browser.safari; /* We need to check for safari to fix the input:text problem */
		/* each form */
		return this.each(function(){
			$('input:submit, input:reset, input:button', this).each(ButtonAdd);
//			$('button').focus(function(){ $(this).addClass('jNiceFocus')}).blur(function(){ $(this).removeClass('jNiceFocus')});
//			$(this).bind('reset',function(){var action = function(){ Reset(this); }; window.setTimeout(action, 10); });
		});		

	};/* End the Plugin */

	var Reset = function(form){
		var sel;
		$('.jNiceSelectWrapper select', form).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; $('ul', $(this).parent()).each(function(){$('a:eq('+ sel +')', this).click();});});
		$('a.jNiceCheckbox, a.jNiceRadio', form).removeClass('jNiceChecked');
		$('input:checkbox, input:radio', form).each(function(){if(this.checked){$('a', $(this).parent()).addClass('jNiceChecked');}});
	};


	var ButtonAdd = function(){
		var value = $(this).attr('value');
		
		var onclick='';
		for (var i = 0; i < this.attributes.length; i++) {
			if (this.attributes[i].nodeName=='onclick')
            onclick = ' onclick="'+this.attributes[i].nodeValue+'"';
        }        
		$(this).replaceWith('<button'+onclick+' onmouseover="this.className=this.className+\' button-active\';" onmouseout="this.className=this.className.replace(/\s*button-active/i, \'\')" id="'+ this.id +'" name="'+ this.name +'" type="'+ this.type +'" class="'+ this.className +'" value="'+ value +'"><span class="f"><span class="s"><nobr>'+ value +'</nobr></span></span></button>');
	};


	
	/* Utilities */
	$.jNice = {
			SelectAdd : function(element, index){ 	SelectAdd(element, index); },
			SelectRemove : function(element){ SelectRemove(element); },
			SelectUpdate : function(element){ SelectUpdate(element); }
	};/* End Utilities */

	/* Automatically apply to any forms with class jNice */
	$(function(){$('.page-holder-r form').jNice();	});
	
})(jQuery);
