var searchboxHome = {
		
	newContainerID: null,
	usedContainerID: null,
	currentProcessing: null,
	
	init: function(containerIDNew, containerIDUsed)
	{
		searchboxHome.newContainerID = containerIDNew;
		searchboxHome.usedContainerID = containerIDUsed;
		
	},
	
	reload: function(type) {
		searchboxHome.currentProcessing = type;
		
		attrs = new Array();
		
		// parsing request string 
		requestString = document.location.search;
		if(requestString.indexOf('?')==0)
		    requestString = requestString.substring(1);
		requestParams = requestString.split("&",3)
		for(i=0; i< requestParams.length; i++) {
		    kv = requestParams[i].split('=');
		    if(kv[0]!='' && kv[1]!='') {
		        attrs[kv[0]] = kv[1];
		    }
		}
		//parsing form elements
		els = searchboxHome._getFormElements();
		
		els.each(function() {
		   el = jQuery(this);
		
		   if(el.attr('type')=='checkbox') {
			   attrs[el.attr('name')] = el.is(':checked')?el.val():"";
		   }
		   else {
			   if(el.hasClass("custom-placeholder") && el.attr('alt')==el.val()){
				  el.val(''); 
			   }
			   attrs[el.attr('name')] = el.val();
		   }
		   
		})
		// binding the url
		url = document.location.pathname+searchboxHomeUrl;
		
		attrs['t'] = searchboxHome.currentProcessing;
		
		
		for(key in attrs) {
		    url += '&'+key+'='+escape(attrs[key]);
		}
		
		//console.log(url);
		
		searchboxHome._request(url);
	},
	
	_getContainerID: function() {
		
		return searchboxHome.currentProcessing == 'n' ? searchboxHome.newContainerID : searchboxHome.usedContainerID; 
	},
	
	_getFormElements: function() {
		
		containerId = searchboxHome._getContainerID();
		
		return jQuery("#"+containerId+" form input,#"+containerId+" form select");
		
	},
	
	_request: function(url) {
		//els = searchbox_home_new._getFormElements().attr('disabled','true');
		searchboxHome._blockForm();
		jQuery.ajaxSetup({ cache: false });
		jQuery.get(url, searchboxHome._requestComplete);
	},
	
	_blockForm: function() {
		containerId = searchboxHome._getContainerID();
		jQuery("#"+containerId).block({css: {border: 0,background: 'none', padding: '5px', width: '45px', marginTop: '-15px'}, message: "<img src='/public/images/loading-animation.gif' />"});
	},
	_unblockForm: function() {
		//jQuery("#"+searchbox_home_new.containerID).unblock();
	},
	
	_requestComplete: function(data) {
		containerId = searchboxHome._getContainerID();
		dom = jQuery(data).find('#'+containerId);
		
		searchboxHome._unblockForm();
		//searchbox_home_new._getFormElements().removeAttr('disabled');
		jQuery('#'+containerId).html(dom.html());
		
		jQuery('#'+containerId).find('input.custom-placeholder').each(function() {
			inputPlaceholder(this);
		});
	}
}
jQuery(function() {
	searchboxHome.init("mod-searchbox-home-new", "mod-searchbox-home-used");
})
