/*
 * jquery.bs_advancedseach.js
 * 
 * This jquery plug in is to handle the advanced search options on the bridgestone site
 * depends on jquery.cookie plugin
 * 
 */
;( function( $ ) {
	
	var FORMNAME 	= "advancedsearch";
	var _INITD	 	= false;
	
	var onEvent_setCookie = function(){
		var value 	= "";
		var id		= "";
		var fred	= this;
		switch( this.type.toLowerCase()  )
		{
			case 'radio':
				value 	= $(this).val();
				id		= $(this).attr("name");
			break;
			case 'checkbox' :
				id		= $(this).attr("id");
				value 	= $(this).attr("checked");
			break;
			default:
				value 	= $( this ).val();
				id		= $(this).attr("id");
			break;
		}
		var fullID = $.bs_advancedsearch.PREFIX +  id;
		$.cookie( fullID , value );
		$.cookie( 'atf_page' , 1 );//serach critera has changed, so reset to page 2
		$("#page").val( "1" );
	};
	var onEvent_clearSelects = function(){
		
		//clear the select cookies
		$.cookie('atf_sector', null);
		$.cookie('atf_size', null);
		$.cookie('atf_tread', null);
	}
	
	$.extend
	({
		bs_advancedsearch	: {
				initForm	: function()
				{
// , form[name="+FORMNAME+"] select
					if( !_INITD )
					{
						
						$.bs_advancedsearch.bindInputs();
						
						$.bs_advancedsearch.bindLinks();
						
						_INITD = true;
	
					}

				}
			/* set up all the form elements */
			,	bindInputs	: function ()
				{
					$( "form[name="+FORMNAME+"] input").click( onEvent_setCookie );
					$( "form[name="+FORMNAME+"] input").click( onEvent_clearSelects );
				}
			,	bindSelects	: function()
				{
//					$( "form[name="+FORMNAME+"] select" ).change( onEvent_setCookie );
				}
			,	bindLinks : function()
				{
					$( ".pagination-link" ).click(
						function()
						{
							
							$.cookie( 'atf_page' , $( this ).attr("pageno") );
							$.cookie( "atf_doSearch" , "true" );
							
						}
					);
				}
			/* EXPOSE SOME VARS */
			, 	PREFIX		: "atf_"
		}
	});


})( jQuery );
