/**
*	Dynmic forms for Solution Provider Categories
*
*	@author 	Ahmad Varoqua <ahmad.varoqua@gmail.com>
*	@date		2009-02-24 21:38:33 PST
*	@job		SL > HTML > Making JS forms to handle all categories
*	@music 		Not currently listening to anything.
*	@param 		$input	[array] - Description
*	@return		none
*
*/
$(function(){	

    // On load hide the total form div
    $('#totalform div').hide();
    
    // On load hide the total form spans for "* required"
    $('#totalform div span').hide();

    // Show only the common stuff
    $('#totalform div.common').show();
    $('.common_required').show();
    
    var onClick = function() {
    	
    	// Hide everything again to start with a clean slate but show the 
    	// common items
        $('#totalform div').hide();
        $('#totalform div span').hide();
    	$('#totalform div.common').show();
    	$('#totalform div span.common_required').show();
    	
        // Remove the 'required' class from actual form elements to start with a clean slate
        $('input[type=text]').removeClass('required');
        $('.services_list').attr('validate', 'required:false');

		// Make sure all common_required elements are still required
   		$('input[type=text]').each(function (){   
   		 	    		 		
   			if ($(this).hasClass('common_required'))
   			{
   				$(this).addClass('required');
   			}    			
   		});

		// Do the same for textareas
   		$('textarea').each(function (){   
   		 	    		 		
   			if ($(this).hasClass('common_required'))
   			{
   				$(this).addClass('required');
   			}    			
   		});

        
        // Review what checkboxes were selected
    	$('form #sp_category_section input:checked').each(function(){
    		// Get the id of the checkbox and look for all items with that class 
    		// and display it
    		//$('.' + this.id).show();
    		
    		// Loop through and see if anything is required,
    		// if so, show the span with the asterix
    		//$('.' + this.id + '_required').show();
    		
    		// Make it even faster by combining the above two show() calls into one:
    		$('.' + this.id + ',' + '.' + this.id + '_required' ).show();
    		
    		
    		// Add validation to all inputs based on selected SP category
    		var temp_id = this.id
    		
    		$('input[type=text]').each(function (){   
    		 	    		 		
    			if ($(this).hasClass(temp_id + '_required'))
    			{
    				$(this).addClass('required');
    			}    			
    		});
    		
    		// Do the same for checkboxes
    		$('input[type=checkbox]').each(function (){   
    		 	    		 	
    			if ($(this).hasClass(temp_id + '_required') && $(this).hasClass('services_list'))
    			{
    				$(this).attr('validate', 'required:true, minlength:1, maxlength:3');
    			}
					
    		});
		    
		    // Do the same for textareas
    		$('textarea').each(function (){   
    		 	    		 		
    			if ($(this).hasClass(temp_id + '_required'))
    			{
    				$(this).addClass('required');
    			}
    			
    		});		    
		    	    
    	});		
    };
    
    // Check if the checkboxes are selected
    //$('form input').click(onClick);		    
    $('form input[type=checkbox]').click(onClick);
    
    // Handling preselected toggles
    onClick();  	
});



/**
*	Tooltip functionality
*
*	@author 	Ahmad Varoqua <ahmad.varoqua@gmail.com>
*	@date		2009-02-25 21:59:18 PST
*	@job		SL > HTML > Making JS forms to handle all categories   0.75
*	@music 		Not currently listening to anything.
*	@param 		$input	[array] - Description
*	@return		none
*
*/
$(function(){		
	$("a").tooltip({showURL: false});
});			
    
    
/**
*	Add more clients
*
*	@author 	Ahmad Varoqua <ahmad.varoqua@gmail.com>
*	@date		2009-02-25 22:00:26 PST
*	@job		SL > HTML > Making JS forms to handle all categories   0.75
*	@music 		Not currently listening to anything.
*	@param 		$input	[array] - Description
*	@return		none
*
*/			
$(function(){
    $('.add_more').live('click', function(){
    	var originalNode = $(this).parent().parent().find('.add_more_clients_clone:last');
    	var key = originalNode.data('key') || 0;
    	var clonedNode = originalNode.clone();
    	
    	clonedNode = clonedNode.insertAfter(originalNode);
    	clonedNode.data('key', key - 1);
    	
    	clonedNode.find('input[type=text]').attr('value', '').each(function (){
    						
    		var x = $(this).attr('name').replace('solution_provider[project][', '').replace('][first_name]', '').replace('][last_name]', '').replace('][email]', '').split('[rating][');
    		var project_id = x[0];
    		var rating_id = x[1];
    		var rating_id_new = Number(x[1]) + 1;
    		$(this).attr('name', $(this).attr('name').replace(/\[rating\]\[[0-9]/, '[rating][' + rating_id_new));
    		
    		// Update the id too					
    		$(this).attr('id', $(this).attr('id').replace(/rating_[0-9]/, 'rating_' + rating_id_new));
    	});
 	});
});			


/**
*	Add more projects
*
*	@author 	Ahmad Varoqua <ahmad.varoqua@gmail.com>
*	@date		2009-02-26 12:32:19 PST
*	@job		SL > JS > Cleaning up clone and removing required span
*	@music 		Not currently listening to anything.
*	@param 		$input	[array] - Description
*	@return		none
*
*/	
$(function(){
    $('.add_more_projects_clone').hide();
    $('#add_more_projects').click(function(){
    	
    	// Get the last project and it's index
    	var lastProject = $('#projects_section').find('.project:last');
    	var lastProjectIndex = lastProject.find('.project_index:last').attr('value');
    	
    	// Now create a clone
    	var clonedProject = $('.add_more_projects_clone').show().clone();
    	clonedProject.addClass('full_service specialist inhouse_developer teen_grid project').removeClass('add_more_projects_clone').appendTo('#projects_section');
    	
    	// Increment the project index for the clone
    	var new_project_index = Number(lastProjectIndex) + 1;
    	clonedProject.find('.project_index').attr('name', $(this).attr('name').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index))
    	clonedProject.find('.project_index').attr('value', new_project_index);

    	// Update the form field names, start with <input type="text"/>
    	clonedProject.find('input[type=text]').each(function (){
    		$(this).attr('name', $(this).attr('name').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index));					
    		
    		// Update the id too
    		$(this).attr('id', $(this).attr('id').replace(/clone_project_[0-9]/, 'solution_provider_project_' + new_project_index));
    		
    		$(this).addClass('required');
    	});
    	    	
    	// Remove required from client first name, last name, email
    	clonedProject.find('.not_required').each(function (){    		
    		$(this).removeClass('required');
    	});

    	// Update the textarea too
    	clonedProject.find('textarea').each(function (){
    		$(this).attr('name', $(this).attr('name').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index));					

    		// Update the id too
    		$(this).attr('id', $(this).attr('id').replace(/clone_project_[0-9]/, 'solution_provider_project_' + new_project_index));
    		
    		$(this).addClass('required');
    	});				
    	
    	// Now modify select
    	clonedProject.find('select').each(function (){
    		$(this).attr('name', $(this).attr('name').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index));					

    		// Update the id too
    		$(this).attr('id', $(this).attr('id').replace(/clone_project_[0-9]/, 'solution_provider_project_' + new_project_index));
    	});				
    	
    	// Update the form field names, start with <input type="text"/>
    	clonedProject.find('input[type=hidden]').each(function (){
    		$(this).attr('name', $(this).attr('name').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index));					

    		// Update the id too
    		$(this).attr('id', $(this).attr('id').replace(/clone_project_[0-9]/, 'solution_provider_project_' + new_project_index));
    	});				
    			
    	// Update the form label "for" attribute
    	clonedProject.find('label').each(function (){
    		$(this).attr('for', $(this).attr('for').replace(/clone\[project\]\[[0-9]/, 'solution_provider[project][' + new_project_index));					
    	});							
    				
    	$('.add_more_projects_clone').hide();				
 	});
});			


/**
*	JS validation for project data
*
*	@author 	Ahmad Varoqua <ahmad.varoqua@gmail.com>
*	@date		2009-03-31 16:09:52 PDT
*	@job		SL > Dev > Add JS validation for new projects
*	@music 		Not currently listening to anything.
*	@param 		$input	[array] - Description
*	@return		none
*
*/
$(function(){
    $("#testform").validate(); 	
});

$.metadata.setType("attr", "validate");