
$(document).ready(
	function() {
		
		$('.lightbox').lightBox({
			imageLoading:			'/include/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			'/include/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			'/include/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:			'/include/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:                     '/include/images/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
		});
		
		var height = $('#height-table').height();
		$('#shadow').css('height', height + 'px');
		
		window.onresize = function() {
			var left = (($('#shadow').width()) - 800) / 2;
			if (left < 0) left = 0;
			
			$('#shadow #white-paper').css('left', left);
		}
		
		// Shadows form close event
		$('#close').bind('click', function() {
			$('#shadow').css('display', 'none');
			$('#white-paper').css('display', 'none');
		});
		
		// Kotik
		var img = new Image();
		img.src = '/images/green_cat.gif';
		
		$('#kotik').bind('mouseover', function() {
			$(this).find('img').attr('src', img.src);
		}).bind('mouseout', function() {
			$(this).find('img').attr('src', '/images/green_cat_inv.gif');
		})
		
		// Brownblock
		$('div.brown-block').not('div.brown-index').bind('mouseover', function() {
			$(this).find('div.bluepng').hide();
			$(this).find('img').css('display', 'none');
			$(this).find('img.overImg').css('display', 'block');
		}).bind('mouseout', function() {
			$(this).find('div.bluepng').show();
			$(this).find('img').css('display', 'block');
			$(this).find('img.overImg').css('display', 'none');
		});
		
		// Shadow Submit
		$('#shadow form').bind('submit', function() {
			var letter = '';
			var err = 0;
			var cont = 0;
			$(this).find('input, textarea').each(function() {
				var title = $(this).attr('title');
				var value = $(this).val();
				if ((title.length != 0) && (value.length != 0)) {
					letter += title + '         ' + value + "\n";
				}
				/* Required fields check */
				var required = $(this).attr('required');
				if (required != null) {
					if ((required) && (value != "")) {
						err += 1;
					}
				}
				
			})
			$(this).find('textarea.forMail').val(letter);
			if (err < 3) {
				alert('Заполните обязательные поля: Контактное лицо, Контактный телефон и Электронная почта"');
				return false;
			}
			else {
				alert('Ваще сообщение успешно отправлено');
			}
						
		});
	var imageViewer = $('#image-viewer');
        if (imageViewer)
            try {
                imageViewer.setAsImageViewer();
            } catch (e) {
                ;
            }
	}

);

function toggleAllText() {
	$('#all_text').toggle('show');
}

function showForm(id) {
	$('#shadow').css('display', 'block');
	$('#white-paper').css('display', 'block');
	$('#shadow .shadow-content').css('display', 'none');
	$('#shadow #' + id).css('display', 'block');
}

function resetForm(id) {
	$('#shadow #' + id + ' input').not('input.imp').val('');
	$('#shadow #' + id + ' textarea').val('');
	
}

function showFotos(imageCount) {
	if (imageCount > 0) {
		$('#shadow').css('display', 'block');
		$('#absImages').css('display', 'block');
	}
}

function closeImageShow() {
	$('#shadow').css('display', 'none');
	$('#absImages').css('display', 'none');
}

function showFotosByStr(imageCount, str) {
	if (imageCount > 0) {
		$('#shadow').css('display', 'block');
		$('#absImages').css('display', 'block');
	}
	images = str.split('|||');
	code = '';
	for (i = 0; i < images.length; i++) {
		attrs = images[i].split('===');
		imageCode = '<div class="image">';
		image = new Image();
		image.src = attrs[0];
		size = attrs[1].split('x');
		imageCode += '<img id="i' + i + '" src="' + image.src + '" class="real" width="200" height="130" size="' + size[0] + 'x' + size[1] + '" alt="" />';
		imageCode += '<img alt="" src="/images/clear.gif" width="200" height="160"> </div>' + "\n";
		code += imageCode;
	}
	$('div.images').html(code);
	
	var opened = false;
	var id;
	$('div.images img.real').bind("click", function() {
		if (!opened) {
			$('div.images img.real').not(this).fadeTo("fast", 0.5);
			size = ($(this).attr('size')).split('x');
			if (size[0] > 600) {
				size[0] = 600;
			} 
			if (size[1] > 400) {
				size[1] = 400;
			}
			$(this).animate({width: size[0], height: size[1]}, 300);
			id = ($(this).attr('id')).substr(1);
			$(this).css('zIndex', 1);
			opened = true;
		} else {
			if ((($(this).attr('id')).substr(1)) == id) {
				$('div.images img.real').not(this).fadeTo("fast", 1);
				$(this).animate({width: "200", height: "130"}, 300);
				$(this).css('zIndex', 0);
				opened = false;
			}
		}
	});
}

var del = true;
function searchFocus() {
	if (del) {
		$('#query').val('');
		del = false;
	}
}

function setListeners() {
	$('div.hb').bind('mouseover', function() {
		var index = ($(this).attr('id')).substr(8);
		
		// Title
		$('#first-row h3 span').addClass('invis');		
		$('#hotTitle' + index).removeClass('invis');
		
		// Desc
		$('p.descr span').addClass('invis');		
		$('#hotDesc' + index).removeClass('invis');
		
		$(this).find('a').addClass('onHover');
		
		// Original image
		$(this).find('div.bluepng').css('visibility', 'hidden');
		$(this).find('img').css('display', 'none');
		$(this).find('img.overImg').css('display', 'inline');
		
	}).bind('mouseout', function() {
		$(this).find('a').removeClass('onHover');
		$(this).find('div.bluepng').css('visibility', 'visible')
		$(this).find('img').css('display', 'inline');
		$(this).find('img.overImg').css('display', 'none');
		
	}).bind('click', function() {
		href = $(this).find('a').attr('href');
		str = window.location.href;
		index = str.lastIndexOf('/');
		loc = (window.location.href.substr(0, index + 1)) + href;
		window.location = loc; 
	});
}

function setA(index) {
	res = $('div.equip-list div');
	if ($(res[index - 1]).hasClass('active')) 
		$(res[index - 1]).removeClass('active');
	else {
		$('div.equip-list div').removeClass('active');
		$(res[index - 1]).addClass('active');
	}
}

function showModelLetter() {
	$('#ModelLetter').toggle('show');
	$('#ModelLetterHeader').toggle('show');
}

function submitForm(form) {
	var name = $('#letter_name').val();
	var contact = $('#letter_contact').val();
	var message = $('#letter_message').val();
	
	if(name == '' || contact == '' || message == '') {
		alert('Не все поля заполнены.');		
	}
	else {
		form.submit();
	}
}

