$(document).ready(function() {
	counter('counter');
	zoomImage();
	toggleParagraph();
	ask();
	search();
	//fixGoodsHeight();
	
	if($('body').hasClass('home'))
		new Slider($('#slider'));
});


function fixGoodsHeight() {
	var max_height = 0;
	var good_link_blocks = $('#good li a');
	good_link_blocks.each(
		function() {
			if(max_height < $(this).height())
				max_height = $(this).height();
		}
	).css('height',max_height+5);
}


function zoomImage(){
	$("a.imgZoom").fancybox({
		'zoomOpacity' : true,
		'overlayShow' : true,
		'zoomSpeedIn' : 'fast',
		'zoomSpeedOut' : 'fast',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'		
	}); 
}



function search(){
	var textInput = $('#search .text');
	textInput.basicVal = textInput.val();
	textInput.basicColor = textInput.css('color');

	
	textInput.focus(function(){
		$(this).val('').css({'color':'black'});
	}).blur(function(x){
		return function(){
			$(this).val(x.basicVal).css({'color':x.basicColor});
		};
	}(textInput));
}


function ask(){
	$("#ask a").fancybox({
		'zoomOpacity' : true,
		'overlayShow' : true,
		'zoomSpeedIn' : 'fast',
		'zoomSpeedOut' : 'fast',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'
	});
	
	
	var requiredList = $("#askForm").find('.required'),
		buttonSubmit = $("#askForm .submit"),
		buttonClose = $("#askForm .close");
		
	
	
	$(requiredList).each(function(){
		$(this).keyup(function(){
			for(var i=0; i<requiredList.length; i++){
				if($(requiredList[i]).val() == ''){
					buttonSubmit.attr('disabled', 'true').addClass('disabled');
					return;
				}
			}
			buttonSubmit.attr('disabled', '').removeClass('disabled');
		});
	})
	
	
	
	buttonSubmit.bind("click", function(){
		$.fancybox.close();
		var form = $(this).parents('form'),
			pArr = form.serializeArray();
			
		pArr.push({'name':'action', 'value':'quest'});
		
		$.ajax({
			type   : "POST",
			cache  : false,
			url	   : "action.php",
			data   : pArr,
			success: function() {
				$.fancybox.close();
				form[0].reset();
				buttonSubmit.attr('disabled', 'true').addClass('disabled');
			}
		});
	});
	
	
	buttonClose.bind('click', function(){$.fancybox.close()})
}



function toggleParagraph(){
	$(".pOut.min .pContent").hide(); 

	$(".pOut.min h3").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	$(".pOut.min h3").click(function(){
		$(this).next(".pOut.min .pContent").slideToggle("fast");
	});
};



function counter(counterContId){
	var strHTML = 
		'<a href="http://www.liveinternet.ru/click" '+
		'target=_blank><img src="http://counter.yadro.ru/hit?t14.1;r'+
		escape(document.referrer)+((typeof(screen)=='undefined')?'':
		';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?
		screen.colorDepth:screen.pixelDepth))+';'+Math.random()+
		'" alt="liveinternet.ru: iieacaii ?enei oeoia ca 24 ?ana, iinaoeoaeae ca 24 ?ana e ca naaiai\y" '+
		'border=0 width=88 height=31></a>';
		
	if(document.getElementById(counterContId)){
		document.getElementById(counterContId).innerHTML = strHTML;
	}
}



function Slider(jqWrap){
	var ul = jqWrap.find('ul'),
		curOffset = 0,
		singleOffset = jqWrap.find('li').width(),
		maxOffset = ul.width() - singleOffset,
		leftBtt = new Button('left'),
		rightBtt = new Button('right'),
		bttArr = [leftBtt, rightBtt];
	
	
	function Button(side){
		this._side = side;
		this.jqObj = jqWrap.find('.'+this._side);
		this.offset = (this._side == 'left') ? -singleOffset : singleOffset;
		this.active = true;
	}
	Button.prototype.setActivity = function(tmpOffset){
		this.active = function(self){
			if((self._side == 'left' && tmpOffset == 0) || (self._side == 'right' && tmpOffset == maxOffset)){
				self.jqObj.addClass('inactive');
				return false;
			}
			self.jqObj.removeClass('inactive');
			return true;
		}(this);
	}
	
	
	$(bttArr).each(function(){
		this.setActivity(curOffset);
		this.jqObj.click(function(btt){
			return function(){
				if(btt.active){
					curOffset += btt.offset;
					$(bttArr).each(function(){this.setActivity(curOffset);})
					ul.animate({right: curOffset + 'px'}, 100);
				}
			}
		}(this));
	})	
}


