jQuery = jQuery.noConflict();

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
				if (options.hours == null) options.hours = 24;
                date.setTime(date.getTime() + (options.expires * options.hours * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


jQuery(function() {

		var modCatList 	= jQuery('#exsearch #category');
		var modVenList	= jQuery('#exsearch #aqs_vendor_list');
		var modModList	= jQuery('#exsearch #aqs_model_list');
		
		var CookieNameSearch = 'exmodsearch';
		var options = { path: '/', expires: 1, hours: 3 };
		var storeparams;
	
		var dinCat;
		var dinVen;
		var dinMod = 0;		
		
		modModList.attr('disabled','disabled');

		// Загрузка Вендоров при смене категории
		modCatList.change(function(){
			modVenList.attr('disabled','disabled');
			if (jQuery(this).val() != 0)
			{
				jQuery.get('/ajaxsearch?xajax=true&mode=vendors&cat='+jQuery(this).val(), 
						function(data){
							if(data){
								modVenList.html( data );
							}
				});
			}
			modVenList.removeAttr('disabled');
		});
		
		// Загрузка Моделей при смене вендора
		modVenList.change(function(){
			modModList.attr('disabled','disabled');
			if (jQuery(this).val() != 0)
			{
				jQuery.get('/ajaxsearch?xajax=true&mode=models&venid='+jQuery(this).val(), 
						function(data){
							if(data){
								modModList.html( data );
								if (dinMod !== 0) 
									modModList.val(dinMod);
								modModList.removeAttr('disabled');
							}
				});
			} else {
				modModList.html( '<option value="0">Любая модель</option>' );
				modModList.attr('disabled','disabled');
			}
		});		
		

		
	var paramscookie = jQuery.cookie(CookieNameSearch);
	if (paramscookie)
	{
		var data = paramscookie.split('&');
		var extract = new Array();
		jQuery.each(data, function(){
			var extract = this.split("=");
			if (extract[1] !== null) {
				jQuery('#exsearch input[name='+extract[0]+']').val(extract[1]);
				jQuery('#exsearch select[name='+extract[0]+']').find('option[value='+extract[1]+']').attr('selected','true');
				
				if (extract[0] == 'v')
				{
					dinVen = extract[1];
					jQuery('#aqs_vendor_list').trigger('change');
				}
				else if (extract[0] == 'm')
				{
					dinMod = extract[1];
				}
				else if (extract[0] == 'category')
					dinCat = extract[1];
			}
		});
		//jQuery('#aqs_vendor_list').trigger('change');
			jQuery('#exsearch select[name=m]').val(dinMod);
	}		
});
