function show_photo(id) {
	prepare_modal_layout(260, 180, '<div id="photo_popup"></div>');
	get_photo(id, 'photo_popup');
};
	
function get_photo(id, container_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				var width_old = $('#modal_window').width();
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				var left = $('#modal_window').css('marginLeft').replace(/px/, '');
				var height = $('#modal_window .photo_popup_container img').height();
				var width = $('#modal_window .photo_popup_container img').width();
				var width_delta = Math.floor((width - width_old) / 2);
				left = parseInt(left) - parseInt(width_delta);
				$(div).css({'display' : 'none'});
				$('#modal_window').animate({'height' : (parseInt(height) + 40) + 'px', 'width' : (parseInt(width) + 40) + 'px', 'marginLeft' : left + 'px'}, 200, function() {
					$(div).css({'display' : 'block'});
				});
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/photo.popup.php?id=" + id, true);
	xmlhttp.send(null);
}


function show_login_data (email) {
	$('dl.helpers .account').remove();
	$('dl.helpers').append('<dt class="account" id="menu_profile"><a href="#" onclick="show_profile_form(); return false;">' + email + '</a></dt>');
	$('dl.helpers').append('<dt class="account" id="menu_logout"><a href="#" onclick="logout(); return false;">Выйти</a></dt>');
}

function hide_login_data () {
	$('dl.helpers .account').remove();
	$('dl.helpers').append('<dt class="account" id="menu_register"><a href="#" onclick="show_register_form(); return false;">Регистрация</a></dt>');
	$('dl.helpers').append('<dt class="account" id="menu_login"><a href="#" onclick="show_login_form(); return false;">Вход</a></dt>');
}

function logout () {
	clear_cookie('user_cookie');
	window.location.reload();
	//hide_login_data();
}



function show_register_form() {
	prepare_modal_layout(350, 470, '<div id="register_popup"></div>');
	get_register_form('register_popup');
};

function get_register_form(container_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				init_register_form();
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/register.popup.form.php", true);
	xmlhttp.send(null);
}

function init_register_form() {
	var register_popup_options = {
		url: '/scripts_ajax/register.popup.execute.php',
		type: 'POST',
		beforeSubmit: function() {
			return process_register_form();
		},
		success: function(response, status, xhr, $form) {
			div = document.getElementById("register_popup")
			div.innerHTML = response;
			var height = $('#modal_window .register_form').height();
			$(div).css({'display' : 'none'});
			$('#modal_window').animate({'height' : (height + 50) + 'px'}, 200, function() {
				$(div).css({'display' : 'block'});
			});
			init_register_form();
		}
	};
	$('form#register_form').ajaxForm(register_popup_options);
}

function process_register_form() {
	var email = $("#field_email").val();
	var password = $("#field_password").val();
	var password_repeat = $("#field_password_repeat").val();
	email = email.toLowerCase();
	if (email == '') {
		alert('Вы не указали адрес электронной почты');
	} else if ((email != '') && (email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес электронной почты');
	} else if (password == '') {
		alert('Вы не указали пароль');
	} else if (password != password_repeat) {
		alert('Пароль и его повтор не совпадают');
	} else {
		return true;
	}
	return false;
}



function show_profile_form() {
	prepare_modal_layout(600, 300, '<div id="profile_popup"></div>');
	get_profile_form('profile_popup');
};

function get_profile_form(container_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				init_profile_form();
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/profile.popup.php", true);
	xmlhttp.send(null);
}

function init_profile_form() {
	var profile_popup_options = {
		url: '/scripts_ajax/profile.popup.php',
		type: 'POST',
		beforeSubmit: function() {
			return process_profile_form();
		},
		success: function(response, status, xhr, $form) {
			div = document.getElementById("profile_popup")
			div.innerHTML = response;
			var height = $('#modal_window .profile_form').height();
			$(div).css({'display' : 'none'});
			$('#modal_window').animate({'height' : (height + 50) + 'px'}, 200, function() {
				$(div).css({'display' : 'block'});
			});
			init_profile_form();
		}
	};
	$('form#profile_form').ajaxForm(profile_popup_options);
	$('form#profile_avatar_form').ajaxForm(profile_popup_options);
}

function process_profile_form() {
	return true;
}



function show_email_form() {
	prepare_modal_layout(470, 370, '<div id="email_popup"></div>');
	get_email_form('email_popup');
};

function get_email_form(container_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				init_email_form();
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/email.popup.php", true);
	xmlhttp.send(null);
}

function init_email_form() {
	var email_popup_options = {
		url: '/scripts_ajax/email.popup.php',
		type: 'POST',
		beforeSubmit: function() {
			return process_email_form();
		},
		success: function(response, status, xhr, $form) {
			div = document.getElementById("email_popup")
			div.innerHTML = response;
			var height = $('#modal_window .email_form').height();
			$(div).css({'display' : 'none'});
			$('#modal_window').animate({'height' : (height + 50) + 'px'}, 200, function() {
				$(div).css({'display' : 'block'});
			});
			init_email_form();
		}
	};
	$('form#email_form').ajaxForm(email_popup_options);
}

function process_email_form() {
	var email = $("#field_email").val();
	var name = $("#field_name").val();
	var message = $("#field_message").val();
	email = email.toLowerCase();
	if (email == '') {
		alert('Вы не указали адрес электронной почты');
	} else if ((email != '') && (email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес электронной почты');
	} else if (name == '') {
		alert('Вы не указали ваше имя');
	} else if (message == '') {
		alert('Вы не ввели свое сообщение');
	} else {
		return true;
	}
	return false;
}




function show_email_link_form(article_id) {
	prepare_modal_layout(470, 440, '<div id="email_link_popup"></div>');
	get_email_link_form('email_link_popup', article_id);
};

function get_email_link_form(container_id, article_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				init_email_link_form();
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/email.link.popup.php?article_id="+encodeURIComponent(article_id), true);
	xmlhttp.send(null);
}

function init_email_link_form() {
	var email_link_popup_options = {
		url: '/scripts_ajax/email.link.popup.php',
		type: 'POST',
		beforeSubmit: function() {
			return process_email_link_form();
		},
		success: function(response, status, xhr, $form) {
			div = document.getElementById("email_link_popup")
			div.innerHTML = response;
			var height = $('#modal_window .email_link_form').height();
			$(div).css({'display' : 'none'});
			$('#modal_window').animate({'height' : (height + 50) + 'px'}, 200, function() {
				$(div).css({'display' : 'block'});
			});
			init_email_link_form();
		}
	};
	$('form#email_link_form').ajaxForm(email_link_popup_options);
}

function process_email_link_form() {
	var to_email = $("#field_to_email").val();
	var from_email = $("#field_from_email").val();
	var from_name = $("#field_from_name").val();
	to_email = to_email.toLowerCase();
	from_email = from_email.toLowerCase();
	if (to_email == '') {
		alert('Вы не указали адрес электронной почты получателя');
	} else if ((to_email != '') && (to_email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес электронной почты получателя');
	} else if (from_email == '') {
		alert('Вы не указали адрес вашей электронной почты');
	} else if ((from_email != '') && (from_email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес вашей электронной почты');
	} else if (from_name == '') {
		alert('Вы не указали ваше имя');
	} else {
		return true;
	}
	return false;
}




function show_photo_order_form(photo_id) {
	remove_gallery();
	prepare_modal_layout(770, 490, '<div id="photo_order_popup"></div>');
	get_photo_order_form('photo_order_popup', photo_id);
};

function get_photo_order_form(container_id, photo_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				init_photo_order_form();
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/photo.order.popup.php?photo_id="+encodeURIComponent(photo_id), true);
	xmlhttp.send(null);
}

function init_photo_order_form() {
	var photo_order_popup_options = {
		url: '/scripts_ajax/photo.order.popup.php',
		type: 'POST',
		beforeSubmit: function() {
			return process_photo_order_form();
		},
		success: function(response, status, xhr, $form) {
			div = document.getElementById("photo_order_popup")
			div.innerHTML = response;
			var height = $('#modal_window .photo_order_form').height();
			$(div).css({'display' : 'none'});
			$('#modal_window').animate({'height' : (height + 50) + 'px'}, 200, function() {
				$(div).css({'display' : 'block'});
			});
			init_photo_order_form();
		}
	};
	$('form#photo_order_form').ajaxForm(photo_order_popup_options);
}

function process_photo_order_form() {
	var from_email = $("#field_from_email").val();
	var from_phone = $("#field_from_phone").val();
	var from_name = $("#field_from_name").val();
	from_email = from_email.toLowerCase();
	if (from_name == '') {
		alert('Вы не указали ваше имя');
	} else if (from_email == '' && from_phone == '') {
		alert('Необходимо указать либо адрес электронной почты, либо телефон');
	} else if ((from_email != '') && (from_email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес вашей электронной почты');
	} else {
		return true;
	}
	return false;
}




function show_login_form() {
	prepare_modal_layout(260, 180, '<div id="login_popup"></div>');
	get_login_form('login_popup');
};
	
function get_login_form(container_id) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message form data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
			}
	  	}
	}
	xmlhttp.open("GET", "/scripts_ajax/login.popup.form.php", true);
	xmlhttp.send(null);
}

function process_login_form()
{
	var email = $("#login_email").val();
	var password = $("#login_password").val();
	var container_id = "login_popup";
	email = email.toLowerCase();
	if (email == '') {
		alert('Вы не указали адрес электронной почты');
	} else if ((email != '') && (email.search(/^[a-z0-9._-]+@[a-z0-9_-]+(\.[a-z]+){1,2}$/) != 0)) {
		alert('Вы указали неверный адрес электронной почты');
	} else if (password == '') {
		alert('Вы не указали пароль');
	} else {
		submit_login_form(email, password, container_id);
	}
}

function submit_login_form(email, password, container_id)
{
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.responseText == 'false') {
				alert('FATAL ERROR! Could not get message result data.');
			} else {
				div = document.getElementById(container_id)
				div.innerHTML = xmlhttp.responseText;
				var height = $('#modal_window .login_form').height();
				$(div).css({'display' : 'none'});
				$('#modal_window').animate({'height' : (height + 40) + 'px'}, 200, function() {
					$(div).css({'display' : 'block'});
				});
			}
	  	}
	}
	var params = "email="+encodeURIComponent(email)+"&password="+encodeURIComponent(password);
	xmlhttp.open("POST", "/scripts_ajax/login.popup.execute.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlhttp.setRequestHeader("Content-length", params.length);
	//xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}



function prepare_modal_cross () {
	var modal_cross = document.createElement('img');
	modal_cross.id = 'modal_cross';
	modal_cross.src = '/graphics/modal_cross.png';
	modal_cross.style.zIndex = '9999999';
	modal_cross.style.position = 'fixed';
	modal_cross.style.zoom = '1';
	modal_cross.style.right = '0px';
	modal_cross.style.top = '0px';
	modal_cross.style.width = '50px';
	modal_cross.style.height = '50px';
	modal_cross.style.cursor = 'pointer';
	modal_cross.style.display = 'none';
		
	return modal_cross;
}


function prepare_modal_back () {
	var modal = document.createElement('div');
	modal.id = 'modal';
	modal.style.zIndex = '9999';
	modal.style.position = 'absolute';
	modal.style.zoom = '1';
	modal.style.left = '0px';
	modal.style.top = '0px';
	modal.style.width = $(window).width() + 'px';
	modal.style.height = $(document).height() + 'px';
	modal.style.opacity = '0.90';
	modal.style.filter = 'alpha(opacity = 90)';
	modal.style.backgroundColor = '#e0e0e0';
//	modal.style.webkitBoxShadow = 'inset 0px 0px 240px #777777';
//	modal.style.MozBoxShadow = 'inset 0px 0px 240px #777777';
//	modal.style.boxShadow = 'inset 0px 0px 480px #777777';
	modal.style.display = 'none';
		
	return modal;
}

function prepare_modal_window (width, height, inner_html) {
	var modal_window = document.createElement('div');

	modal_window.id = 'modal_window';
	modal_window.style.zIndex = '99999';
	modal_window.style.position = 'fixed';
	modal_window.style.left = '50%';
	modal_window.style.marginLeft = '-' + Math.floor(width / 2) + 'px';
	modal_window.style.top = 100 + 'px';
	modal_window.style.width = width + 'px';
	modal_window.style.height = height + 'px';
	modal_window.style.backgroundColor = '#fff';
	modal_window.style.webkitBoxShadow = '0px 0px 6px #999999';
	//modal_window.style.webkitBorderRadius = '10px';
	modal_window.style.MozBoxShadow = '0px 0px 6px #999999';
	//modal_window.style.MozBorderRadius = '10px';
	modal_window.style.boxShadow = '0px 0px 12px #999999';
	//modal_window.style.borderRadius = '10px';
	if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
		modal_window.style.border = '3px solid #bbbbbb';
	}
	modal_window.style.display = 'none';

	modal_window.innerHTML = inner_html;

	return modal_window;
}

function remove_modal_layout () {
	if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
		$('#modal_window').animate({'top': '120px'}, 200, function () {
			$('#modal').animate({'opacity': '0.0'}, 300, function () {
			    $('#modal').remove();
			});
			$('#modal_window').remove();
		});
	} else {
		$('#modal_window').animate({'opacity': '0.0', 'top': '120px'}, 200, function () {
			$('#modal').animate({'opacity': '0.0'}, 300, function () {
			    $('#modal').remove();
			});
			$('#modal_window').remove();
		});
	}
}

function prepare_modal_layout (width, height, inner_html) {
	var _body = document.getElementsByTagName('body')[0];
		
	var modal = prepare_modal_back();
	var modal_window = prepare_modal_window(width, height, inner_html);

	_body.appendChild(modal_window);
	_body.appendChild(modal);
		
	$(modal).click(function(){
		remove_modal_layout();
	});
		
	$(modal).show();
	$(modal).css({'opacity': '0.0'});
	$(modal).animate({'opacity': '0.85'}, 200, function () {
		$(modal_window).show();
		if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
			$(modal_window).css({'top': '80px'});
			$(modal_window).animate({'top': '100px'}, 300);
		} else {
			$(modal_window).css({'opacity': '0.0', 'top': '80px'});
			$(modal_window).animate({'opacity': '1.0', 'top': '100px'}, 300);
		}
	});
}

