var Timeout = null;
var imageCounter = 0;

function loadImage() {
	imageCounter++;
	if (imageCounter == 9) {
		if (!$.browser.msie || $.browser.version.substr(0,1)>6) {
			BindImageChange();
			Timeout = setTimeout("ChangeHeaderImage('next')", 5000);
		}
	}
}

$(document).ready(function(){
	
	$('div.advert').click(function(){
		window.location.href = "/bass.php/proteus.php";
	});
	$('#steel').click(function(){
		window.location.href = "/catalog_izdelij_iz_nerzhaveyushej_stali";
	});

	$('.images a').lightBox({
		fixedNavigation:true
	});
	
	$('.field input, .textarea textarea').focus(function(){
		if (($(this).val() == 'Имя, Фамилия') || ($(this).val() == 'Телефон') || ($(this).val() == 'E-mail')) {
			$(this).val('');	
		}
		$(this).parent().addClass('focus');
	}).blur(function(){
		$(this).parent().removeClass('focus');
	});
	$('.submit').click(function() {
		$('.error').removeClass('error');
		var error = Array();
		try {
			name = $('#name').val();
			phone = $('#phone').val();
			mail = $('#mail').val();
			text = $('#text').val();
			if ((name == 'Имя, Фамилия') || (name == '')) error[0] = '#name';
			if ((phone == 'Телефон') || (phone == '')) error[1] = '#phone';
			if ((mail == 'E-mail') || (mail == '') || (!mail.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/))) error[2] = '#mail';
			if (text == '') error[3] = '#text';
			if (error.length != 0) {
				throw(error); 
			}
			$('#form').fadeOut(700, function() {
				$('#message').fadeIn(700);
			});
			$.ajax({
				type: "POST",
				url: "kont.php",
				data: {
					"name":name,
					"phone":phone,
					"mail":mail,
					"text":text
				}
			});
		} catch(e) {
			$(e).each(function(i,id) {
				if (id != undefined) 
					$(id).parent().addClass('error');
			});
		}
	});
});

function BindImageChange() {
	$('.arrows a.next').bind('click',function() {ChangeHeaderImage('next');});
	$('.arrows a.prev').bind('click',function() {ChangeHeaderImage('prev');});
}

function ChangeHeaderImage(side) {
	clearTimeout(Timeout);
	$('.arrows a.next').unbind('click');
	$('.arrows a.prev').unbind('click');
	if (side == 'next') {
		if ($('.show').next().attr('class') == 'arrows') {
			$('.slide_img').children().first().addClass('class_tmp');
		} else {
			$('.show').next().addClass('class_tmp');	
		}
	} else {
		if ($('.show').prev().attr('class') == undefined) {
			$('.slide_img img').last().parent().parent().prev().addClass('class_tmp');
		} else {
			$('.show').prev().addClass('class_tmp');	
		}
	}
	$('.show').animate({opacity:0.1},500, function() {
		$('.slide_img img').addClass('hide').removeClass('show');
		$('.class_tmp').addClass('show');
		$('.show').removeClass('class_tmp').removeClass('hide');
		$('.show').css('opacity',0.1);
		$('.show').animate({opacity:1},500, function() {
			BindImageChange();
			Timeout = setTimeout("ChangeHeaderImage('next')", 5000);
		});
		$('.arrows').animate({opacity:1},400);
	});
	$('.arrows').animate({opacity:0.05},400);
}

