$(document).ready(function(){
	
	
	// init search input
	initInput("#miniSearchForm input[name=query]","").keypress(function(e){
		var ENTER_KEY = 13;
//		if(e.keyCode == ENTER_KEY){
//			location.href = $('#.searchInput a').attr('href');
//		} 
	});
	
	initInput("#newsletterForm input[name='firstName']","Name");
	initInput("#newsletterForm input[name='email']","Email");
	
	
	function initInput(id,defaultValue){
		if(defaultValue == "")
			defaultValue =  $(id)[0].value;
		
		return $(id).focus(function(){
			if(this.value==defaultValue)
				this.value='';
			$(this).css('color','#000000');
		}).blur(function(){
			if(this.value.search(/^ *$/) == 0){
				this.value=defaultValue;
				$(this).css('color','#c1c1c1')
			}
		}).val(defaultValue);
	}

	
	document.textNavigation = new function()
	{
		var $this = this;
		var dots = $("#textDots li");
		var oldCurrent = null;
		$this.texts = [];
		$this.query = function(manager, act, request, onComplete){
			$.ajax({
				url: 		'/'+ window.ABS_PATH +'api.php?dataType=json&manager='+manager+'&act='+act,
				data: 		request,
				success: 	function(r){
					onComplete(r.responce);
				},
				dataType: "json"
			});
		};
		
		var changeText = function(){
			
			//$("#previewNavigation").attr('disabled', '');
			
			if($this.current > $this.texts.length-1)
				$this.current = 0;
			
			if($this.current < 0)
				$this.current = $this.texts.length-1;
			
			$(dots[oldCurrent]).removeClass("active");
			$(dots[$this.current]).addClass("active");
			
			var texts = $this.texts[$this.current];
			fadeOut(function(){
				
				$('#infoLayer #productImage').attr("src",texts.imageSrc);
				$('#infoLayer .text h1').html(texts.headerText);
				$('#infoLayer .text .text1').html(texts.pageText);
				fadeIn(function(){
					//$("#previewNavigation").removeAttr('disabled');
				});
			});
		};
		
		var fadeIn = function(onComplete){
			$('#productImage').fadeIn('slow',function(){
				$('#infoLayer .text').fadeIn('slow',onComplete);
			});
		}

		var fadeOut = function(onComplete){
			$('#infoLayer .text').fadeOut('slow',function(){
				$('#productImage').fadeOut('slow',onComplete);
			});
		}
		
		$this.next = function(){
			oldCurrent = $this.current++;
			changeText();
			//location.href = $("#infoLayer .previewNavigation .rightButton").attr("href");
			return false;
		}
		$this.prev = function(){
			oldCurrent = $this.current--;
			changeText();
			return false;
			
			
			$('#infoLayer .text').fadeOut('slow',function(){
				location.href = $("#infoLayer .previewNavigation .leftButton").attr("href");
			});
			return false;
		};
		
//		$this.query("Products","getCategories",{},function(r){
//			$this.mainCategories = r;
//		});
			
	};
	
	// construct
	$('#productImage').fadeOut(0);
	$('#infoLayer .text').fadeOut(0,function(){
		$('#productImage').fadeIn("slow",function(){
			$('#infoLayer .text').fadeIn("slow",function(){
			});
		})
	});
	
	oHandler = $(".mydds").msDropDown();
});


