/*
 * Making search nice and easy ;)
 *
 * @author Tomasz  Zielonka
 * 
 */

//-----modyfikacja jQuery fadeTo dla ie6
jQuery.fn.fadeTo = function(speed,to,callback) {
	return this.animate({opacity: to}, speed, function() {
		if (to == 1 && jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};
//-----

jQuery(function($) {
	var sVal=new Array();
	$(".helpSearch").each(function() {
		$this=$(this);
		var id=$this.attr('id');
		sVal[id]=$this.find(':selected').text();

		$this.sSelect();
		if(sVal[id] != null) {
			$this.getSetSSValue(sVal[id]);
			$this.change(function() {
				var q=new Array();
				if($("#cat").val()) q.push("cat="+$("#cat").val());
				//if($("#promotion").val()) q.push("promotion="+$("#promotion").val());

				$(".helpSearch").not($(this).nextAll(".helpSearch")).each(function() {
					if($(this).val()) q.push($(this).attr('id')+"="+$(this).val());
				});

				var url="/getDict.php?"+q.join("&");

				setSelects($(this).nextAll(".helpSearch"), url);

				$('input#name').flushCache();
				$('input#producer').flushCache();
			});
		}
	});

	$("input#name").autocomplete("/getSuggest.php", {
		minChars: 3,
		extraParams: {
			t: 'n',
			producer: function() { return ($("#producer").val()?$("#producer").val():''); },
			name: function() { return ($("#name").val()?$("#name").val():''); },
			diameter: function() { return ($("#diameter").val()?$("#diameter").val():''); },
			size: function() { return ($("#size").val()?$("#size").val():''); },
			sindex: function() { return ($("#sindex").val()?$("#sindex").val():''); },
			lindex: function() { return ($("#lindex").val()?$("#lindex").val():''); },
			ratio: function() { return ($("#ratio").val()?$("#ratio").val():''); }
		},
		scroll: true,
		scrollHeight: 200,
		width: 328
	});

	if(!$("input#producer").is("[readonly=readonly]")) {
		$("input#producer").autocomplete("/getSuggest.php", {
			minChars: 3,
			extraParams: {
				t: 'p',
				producer: function() { return ($("#producer").val()?$("#producer").val():''); },
				name: function() { return ($("#name").val()?$("#name").val():''); },
				diameter: function() { return ($("#diameter").val()?$("#diameter").val():''); },
				size: function() { return ($("#size").val()?$("#size").val():''); },
				sindex: function() { return ($("#sindex").val()?$("#sindex").val():''); },
				lindex: function() { return ($("#lindex").val()?$("#lindex").val():''); },
				ratio: function() { return ($("#ratio").val()?$("#ratio").val():''); }
			},
			scroll: true,
			scrollHeight: 200,
			width: 328
		});
	}

	$("select#marka").sSelect();
	$("select#model").sSelect();
	
	$("select#marka").change(function() {
		newUrl='/getCars.php?'+($("#marka").getSetSSValue()?'marka='+$("#marka").getSetSSValue():'');
		//alert(newUrl);
		setSelects($("#model"), newUrl);
		$('input#name').flushCache();
		$('input#producer').flushCache();
	});

	$("select#model").change(function() {
		if($("#marka").getSetSSValue()!='' && $("#model").getSetSSValue()!='') {
			$('#cars').submit();
		}
	});
	//$("input#producer").suggest("/getSuggest.php");

});

function setSelects($obj, url) {
	var fcs=new Array();

	//alert(url);
	$obj.each(function(i, item) {
		var eid=$(item).attr('id');

		//change unbind
		fcs[eid]=$('#nls_'+eid).change;
		$('#nls_'+eid).unbind();
		$('#nls_'+eid).addClass('load');
		$('#nls_'+eid).fadeTo("fast", 0.5);

	});

	$.getScript(url, function() {
		$obj.each(function(i, item) {
			var eid=$(item).attr('id');
			//if(!obj.val()) {
				if(select[eid]) {
					$('#'+eid).setOptionsFromArray(select[eid], $('#'+eid).children(':selected').val());
				}
				//else

				//change bind again
				$('#nls_'+eid).change(fcs[eid]);
				$('#nls_'+eid).fadeTo("fast", 1.0);
				$('#nls_'+eid).removeClass('load');
			//}
		});
	});
}