$(document).ready(function() {
    counter('counter');
	zoomImage();
	toggleParagraph();
	hlOffice();
	formValidation();
});


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


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 hlOffice(){
	var point = '.point',
		officeA = '.office a';

	$(point+', '+officeA).hover(function(){
		ind = $(officeA).index(this)!=-1? $(officeA).index(this):$(point).index(this);
		$(point+':eq('+ind+'), '+officeA+':eq('+ind+')').addClass('hl');
	}, function(){
		$(point+':eq('+ind+'), '+officeA+':eq('+ind+')').removeClass('hl');
	});
}


function formValidation(){
	//форма заказа машинки
	if($('#techOrder').size()>0){
		validation('req', 'btt');
		
		$('#btt:not(.disabled)').live('click', function(){
			var params = {action:'order'},
				fields = $('#techOrder').find('.field');

			fields.each(function(){
				params[$(this).attr('name')] = $(this).val();
			});
			
			$.ajax({
				type: "POST",
				url: "/action.php",
				data: params,
				success: function(){
					formSentCallBack();
				}
			});
			
		});
	}
	
	
	function formSentCallBack(){
		var $ordForm = $('#techOrder form, p.note'),
			$thanks = $('.thanks');
		
		$ordForm.animate({opacity: '0'}, 'fast', function(){
			$('form .clean').val('');
			validation('req', 'btt');
			$thanks.fadeIn('fast', function(){
				$(this).delay(2000).fadeOut('fast', function(){
					$ordForm.animate({opacity: '100'}, 'fast');
				});
			});
		});
	}
	

	function validation(fldClass, bttId){
		var reqFields = $('input.'+fldClass);
		
		checkFields();
		reqFields.keyup(function(){
			checkFields();
		});
		
		function checkFields(){
			var button = $('#'+bttId);
			var reqFieldsNum = reqFields.size();
			var filledNum = 0;

			reqFields.each(function(){
				if($(this).val() != '')
					filledNum = filledNum + 1;
			});
			
			if(filledNum == reqFieldsNum)
				button.attr('disabled', '').removeClass('disabled');
			else
				button.attr('disabled', 'true').addClass('disabled');
		}
		
	}
}
