/*
 * Search module
 * Singleton
 */
 
KM.Search = function() {
		
	var minChars = 1;
	var delay = 300;
	var delayStarted = false;	
	var delayTO = false;
	var str = '';
	var data = false;
	var tip = false;
	var tipVisible = false;
	
	function privateYo() {
		alert('privateYo!');
	};	
		
	return {
		publicYo: function() {
			alert('publicYo!');
		},
		parseMovie: function(d) {
			return  {
				id: d[0],
				name: d[1],
				origname: d[2],
				year: d[3],
				alias: d[4]
			}
		},
		keyPress: function(e) {
			//console.info($('#is').val());
			if(delayTO) {
				clearTimeout(delayTO);
			}
			if($('#is').val().length >= minChars) {
				delayTO = setTimeout(KM.Search.check, delay);
			} else {
				KM.Search.closeTip();
			}
		},
		blur: function() {
			KM.Search.closeTip();
		},
		focus: function() {
			KM.Search.openTip();
		},
		closeTip: function() {
			if(tipVisible) {
				$('#tip').slideUp();
				tipVisible = false;
			}
		},
		openTip: function() {
			if($('#is').val().length >= minChars) {
				clearTimeout(delayTO);
				KM.Search.check();
			} else {
				KM.Search.closeTip();
			}
		},
		check: function() {
			//console.info($('#is').val());
			
			str = $('#is').val();
			//alert(str);
			
			$.getJSON('/controller', {
					cmd: 'search',
					str: str				
				}, function(o) {							
					if(o.res == 'error' || !o.data) {
						alert('Error: ' + o.reason);										
					} else {
						data = o.data;
						KM.Search.parseData();
					}
			
			})
		},
		parseData: function() {
			
			if(!tip) {
				tip = $('<div id="tip"></div>').css({
					display: 'none',
					'z-index': '1200'
				}).appendTo("body");
			}
			
			tip.css('left', $('#is').offset().left-3);
			
			html = 'по вашему запросу ничего не найдено...';
			
			if(data && data.length > 0) {
				html = '';
				for(var i=0; i < data.length; i++) {
					var m = KM.Search.parseMovie(data[i]);
					html += '<div class="sr" alias="'+m.alias+'" nm="'+m.name+'">'+m.name+' <span class="sro">'+m.origname+'</span><span class="sry">'+m.year+'</span></div>';
				}
				if(data.length > 9) {
					html += ' &nbsp; ...';
				}
			} 
			
			tip.html(html);
			
			//tip.fadeIn();
			tip.slideDown();
			tipVisible = true;
			
			$('.sr').mouseover(function() {
				$(this).css({
					'border-left': '8px solid #c68787',
					'border-right': '8px solid #c68787',
					'margin-left': '-2px',
					'padding-left': '2px',
					'background-color': '#222222'
				});
			});
			$('.sr').mouseout(function() {
				$(this).css({
					'border-left': '0',
					'border-right': '0',
					'margin-left': '8px',
					'padding-left': '0px',
					'background-color': '#000000'
				});
			});
			$('.sr').click(function() {
				var al = $(this).attr('alias');
				//console.info($(this).attr('alias'));
				
				$('#is').val($(this).attr('nm'));
				
				$('#tip').slideUp('normal', function() {
					$('#is').animate({
						marginLeft: '700px',
						width: 0
					}, 500, '', function() {
						document.location.href = '/movies/'+al;
					});
					
				});
				
			});
			
			
		}
	}
	
}();

