/**
* @desc PS WordPress Template Generator
* @author Per Soderlind - soderlind.no
*/

jQuery(document).ready(function(){	
	
	jQuery("#template_info :input").each( function(i) {
			// code from http://plugins.jquery.com/project/saveToCookie
			switch (jQuery(this).attr("type")) {
				case "checkbox":
					// Get "checked" status and set name field
					var isChecked = jQuery(this).attr('checked');
					var name = 'form-' + jQuery(this).attr('id');

					//if this item has been cookied, restore it
					if(jQuery.cookie( name ) == "true") {
			        	jQuery(this).attr( 'checked', jQuery.cookie( name ) );
			      	}
					//assign a change function to the item to cookie it
					jQuery(this).change(
			        	function(){
			          		jQuery.cookie( name, jQuery(this).attr('checked'), { path: '/', expires: 365 });
			        	}
					);
				break; // case "checkbox":
				
				case "radio":
					var isSelected = jQuery(this).attr('selected');
					var name = 'form-' + jQuery(this).attr('id');
					var value = jQuery(this).val();

					// Unlike the others, we have to test against the value here.
					// After we make sure the cookie exists.
					if(jQuery.cookie( name )) {
						if (jQuery.cookie ( name ) == value) {
							jQuery(this).attr( 'checked', 'checked' );
						}
					}
					// Assign a change function to the item to cookie it
					jQuery(this).change(
						function(){
							jQuery.cookie( name, jQuery(this).val(), { path: '/', expires: 365 });
						}
					);			      
				break; //case "radio":
				
				case "submit":
				break;
				
				default:
					//if this item has been cookied, restore it
					var name = 'form-' + jQuery(this).attr('id');

					if(jQuery.cookie( name )) {
						jQuery(this).val( jQuery.cookie(name) );
					}
					//assign a change function to the item to cookie it
					jQuery(this).change(
						function() {
							jQuery.cookie(name, jQuery(this).val(), { path: '/', expires: 365 });
						}
					);
			} // switch
    	} // function()
    ); // jQuery("#template_info :input").each(

	jQuery(document).ajaxStart(function() {
		//jQuery("#template_info").slideUp("slow"); 
	}).ajaxStop(function() {
		SyntaxHighlighter.highlight();
		jQuery("#template_info").slideUp("slow"); 
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	var v = jQuery("#template_info").validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#result"
			});
		}
	});
	
	
}); // jQuery(document).ready(function(){
