/*
 * Search widget
 */
 
(function () {
	//constructor
	LAMP.Search = function() {
		
		// public fields
		
		var modelDisabled = true;
		var curBrand;
		var curBrandName;
		var curLamp;
		var curLampName;
		
		var selBrandOptions = {  
		    url:'/plugins/projectorlamps/controller.php?cmd=ksearch&data=brands',
		    fillin: true,
		    delay: 0,
		    width: 148,
		    type: 'json',
	       	onSelect:function(){ 
	       		if(typeof pageTracker != 'undefined') {pageTracker._trackPageview('/ajax/selectbrand')};
		        //alert(toAlias(this.ac.val()));
		        
		        //$("#selmodel").autocomplete(selModelOptions); 
		        
		        if(modelDisabled) {
		        	//$("#selmodeltd").html("<b>Hello!</b>");
		        }
		        
		        $("#selmodeltd").html('<input type="text" id="selmodel"/>');
		        
		        selModelOptions.url = '/plugins/projectorlamps/controller.php?cmd=ksearch&data=models&brand=' + toAlias(this.ac.val());
		        $("#selmodel").autocomplete(selModelOptions);
		        
		        $('#buytable').hide();
		        
		        curBrandName = this.ac.val();
		        curBrand = toAlias(this.ac.val());
		        curLamp = null;
		        curLampName = null;
		        
		        /*
		        if($("#selmodelfake").css('display') == 'block') {
					$("#selmodelfake").hide();
					$("#selmodel").show();
					//$("#selmodel").autocomplete(selModelOptions).show();
		        } else {
		        	
		        }
		        
		        selModelOptions.url = 'ksearch.php?data=models&brand=' + toAlias(this.ac.val()),
		        alert(selModelOptions.url);
		        
		        
		        $("#selmodel").autocomplete(selModelOptions);
		        */
		        
		        
		        //$("#selmodelfake").css({'display':'none'});
		        //$("#selmodelfake").css('display','none');
		        //$("#selmodelfake").hide();
		        
		        //$("#selmodel").css({'display':'block'});
		    },	    
		    onSuggest:function(){ 
	           this.ac.css({'background-image': 'url("/i/progress.gif")'})
	       	},
			onDisplay:function(list){  
	        	this.ac.css({'background-image':'none'});
	       		if(!list)  
	    	   		this.ul.append("<div style='line-height:100px;text-decoration:underline;text-align:center;'>[Empty list...]</div>");
		    },
		    onError:function(XMLHttpRequest, textStatus, errorThrown){ 
	            this.ac.val(textStatus); 
	            this.ac.attr({disabled:"disabled"}).css({'background-color':'#d0d0d0','background-image':'none'}); 
	            this.ul.hide(); 
	            this.img.unbind("click"); 
	        }
		};
		
		var selModelOptions = {  
		    url:'/plugins/projectorlamps/controller.php?cmd=ksearch&data=models&brand=3m',
		    fillin: true,
		    delay: 100,
		    width: 148,
		    type: 'json',
		    onSelect:function(){
		    	//alert(toAlias(this.ac.val()));
		    	if(typeof pageTracker != 'undefined') {pageTracker._trackPageview('/ajax/selectmodel')};
		    	
		    	curLampName = this.ac.val();
		    	curLamp = toAlias(this.ac.val());
		    	
		    	//$('#buytable').fadeIn('slow');
		    	checkPrice(curBrand,curLamp,curBrandName,curLampName);
		    },
		    onSuggest:function(){ 
	           this.ac.css({'background-image': 'url("/i/progress.gif")'})
	       	},
			onDisplay:function(list){  
	        	this.ac.css({'background-image':'none'});
	       		if(!list)  
	    	   		this.ul.append("<div style='line-height:100px;text-decoration:underline;text-align:center;'>[Empty list...]</div>");
		    },
		    onError:function(XMLHttpRequest, textStatus, errorThrown){ 
	            this.ac.val(textStatus); 
	            this.ac.attr({disabled:"disabled"}).css({'background-color':'#d0d0d0','background-image':'none'}); 
	            this.ul.hide(); 
	            this.img.unbind("click"); 
	        }
		};
		
		// init
		
		$("#selbrand").autocomplete(selBrandOptions);
		//$("#selmodel").autocomplete(selModelOptions);
		
		// private fields
		
		var self = this;
		
		// private methods
		//alert('created');
		
	};
	
	// public methods
	
	var checkPrice = function(brand,lamp,brandName,lampName) {
		//alert('checkPrice');
		
		progressShow();
		
		$.getJSON('/plugins/projectorlamps/controller.php?cmd=checkprice&brand='+brand+'&lamp='+lamp, function(o) {		
			progressHide();				
			if(o.res == 'error') {
				alert('Error: ' + o.reason);				
			} else {			
			
				$('#pricetd').html('Цена: <b>' + formatPrice(o.price) + '</b> р.');
				if(o.avail > 0) {
					$('#availtd').show();
				} else {
					$('#availtd').hide();
				}
				
				//$('#buytable').fadeIn('slow');
				$('#buytable').hide();
				$('#buytable').slideDown('slow');
				$('#buylink').unbind('click');				
				$('#buylink').click(function() {
					//alert('Лампа '+brandName+' / '+lampName+' добавлена в корзину!');
					//alert('Вы можете купить лампу, отправив заявку через форму "Написать нам"');
					
					cart.addItemByAlias(lamp, brand);
					
					return false;
				});
				
			}			
		});
		
		
	};

})();
