var view_mode;
var need_loading;
var selected_text;
var alert_script = '';
var tooltip_element;

$(document).ready(
	function() {
		if(screen.width >= 1280){
			document.body.style.backgroundPosition = "center 0px";
			document.body.style.marginTop = "38px";
		}
	}
);
function get_position(obj) {
	var x = y = 0;
	while(obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {x:x, y:y};
}
function get_mouse_pos(event){
	var x = y = 0;
	if (document.attachEvent != null){ // Internet Explorer & Opera
	    x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	    y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}else if (!document.attachEvent && document.addEventListener){ // Gecko
	    x = event.clientX + window.scrollX;
	    y = event.clientY + window.scrollY;
	}else{
	    // Do nothing
	}
	return {x:x, y:y};
}
function to_center_window(element_id) {
	var element = document.getElementById(element_id);
	element.style.top = document.body.clientHeight/2 + document.body.scrollTop - element.offsetHeight/2;
	element.style.left = document.body.clientWidth/2 + document.body.scrollLeft - element.offsetWidth/2;
}
function to_center_parent(element_id, parent_id) {
	var element = document.getElementById(element_id);
	var parent = document.getElementById(parent_id);
	if (element && parent){
		element.style.top = parent.clientHeight/2 + get_position(parent).y - element.offsetHeight/2;
		element.style.left = parent.clientWidth/2 + get_position(parent).x - element.offsetWidth/2;
	}
}
function to_left_parent(element_id, parent_id) {
	var element = document.getElementById(element_id);
	var parent = document.getElementById(parent_id);
	if (element && parent){
		element.style.top = parent.clientHeight/2 + get_position(parent).y - element.offsetHeight/2;
		element.style.left = get_position(parent).x;
	}
}
function to_right_parent(element_id, parent_id) {
	var element = document.getElementById(element_id);
	var parent = document.getElementById(parent_id);
	if (element && parent){
		element.style.top = parent.clientHeight/2 + get_position(parent).y - element.offsetHeight/2;
		element.style.left = get_position(parent).x + parent.clientWidth - element.offsetWidth;
	}
}
if ('undefined' != typeof xajax){
	xajax.callback.global.onRequest = function(){
		need_loading = true;
		setTimeout("show_loading()", 400);
	}
	xajax.callback.global.beforeResponseProcessing = function(){
		need_loading = false;
		hide_loading();
		tooltip_element = document.getElementById('tooltip');
		if (tooltip_element){
			tooltip_element.style.display = 'none';
		}
	}
}
function show_loading(){
	if (need_loading){
		load_label = document.getElementById('load-label');
		if(load_label){
			load_label.style.display = 'block';
			$(load_label).css('z-index', next_z_index + 10);
			to_center_parent('load-label', 'main-window');
		}
	}
}
function hide_loading(){
	load_label = document.getElementById('load-label');
	if(load_label){
		load_label.style.display='none';
	}
}
function hide_small_window(){
	small_window = document.getElementById('small-window');
	if(small_window){
		hide_popup_menu();
		small_window.style.display='none';
		small_window.innerHTML='';
	}
}
function hide_clear_window(element_id){ // Скрытие и полная очистка HTML-элемента
	element = document.getElementById(element_id);
	if (element){
		element.style.display='none';
		element.innerHTML='';
	}
	return false;
}
function hide_window(element_id){ // Скрытие HTML-элемента
	element = document.getElementById(element_id);
	if (element){
		element.style.display='none';
	}
	return false;
}
function get_cookie(name){
	var cookie = document.cookie.split("; ");
	for (var i = 0; i < cookie.length; i++)
	{
		var crumb = cookie[i].split("=");
		if (name == crumb[0]) return unescape(crumb[1]);
	}
	return null;
}
function set_cookie(name, value, expires, path, domain, secure){
 	var cookie_text = 	   escape(name) + '=' + escape(value);
	cookie_text +=	 	   (expires ? '; EXPIRES=' + expires.toGMTString() : '');
	cookie_text += 	   (path ? '; PATH=' + path : '');
	cookie_text += 	   (domain ? '; DOMAIN=' + domain : '');
	cookie_text += 	   (secure ? '; SECURE' : '');
	
	document.cookie = cookie_text;
}
function show_content_menu(menu_content, sender, menu_width){
	var popup_menu = document.getElementById('popup-menu');
	var popup_menu_content = document.getElementById('popup-menu-content');
	if (popup_menu && popup_menu_content){
		popup_menu_content.innerHTML = menu_content;
		popup_menu.style.width = menu_width;
		popup_menu.style.left = get_position(sender).x;
		popup_menu.style.top = get_position(sender).y + sender.offsetHeight;
		$(popup_menu).fadeIn(300);
		popup_menu.style.left = get_position(sender).x + sender.offsetWidth/2 - popup_menu.offsetWidth/2;
		$(popup_menu).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function show_square_window(content){
	var popup_menu = document.getElementById('popup-menu');
	if (popup_menu){
		var all_content = '<div style="text-align: center; width: 400px; height: 400px; background-image: url(' + interface_images_url + 'window400x400.png)" class="brown-n-n-n-11-t">';
		all_content += '<div style="text-align: right; padding: 7px 7px 0 0"><img style="cursor: pointer" onclick="hide_content_menu()" src="' + interface_images_url + 'close.png"></div>';
		all_content += content;
		all_content += '</div>';
		popup_menu.innerHTML = all_content;
		$(popup_menu).fadeIn(300);
		to_center_parent('popup-menu', 'main-window');
		$(popup_menu).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function hide_content_menu(){
	hide_popup_menu();
}
function hide_popup_menu(){
	var popup_menu = document.getElementById('popup-menu');
	if (popup_menu){
		popup_menu.style.border='solid 1px #FFC000';
		popup_menu.style.background='#45442F';
		popup_menu.innerHTML = '<div style="text-align: right; margin: -5px 0 -10px"><span style="font-size: 20px; font-weight: bold; color: red; cursor: pointer" onclick="hide_popup_menu()">&times;</span></div><div style="padding: 7px 5px 5px" id="popup-menu-content"></div>';
		popup_menu.style.width = 'auto';
		popup_menu.style.height = 'auto';
		popup_menu.style.display = 'none';
	}
}
function hide_left_info_window(){
	var left_info_window = document.getElementById('left-info-window');
	if (left_info_window){
		left_info_window.innerHTML = '';
		left_info_window.style.display = 'none';
	}
}
function hide_right_info_window(){
	var right_info_window = document.getElementById('right-info-window');
	if (right_info_window){
		right_info_window.innerHTML = '';
		right_info_window.style.display = 'none';
	}
}
function hide_center_info_window(){
	var center_info_window = document.getElementById('center-info-window');
	if (center_info_window){
		center_info_window.innerHTML = '';
		center_info_window.style.display = 'none';
	}
}
function show_small_window(){
	small_window = document.getElementById('small-window');
	if(small_window){
		$(small_window).fadeIn(300);
		$(small_window).css('z-index', next_z_index);
		next_z_index++;
		hide_popup_menu();
		to_center_parent('small-window', 'main-window');
		$("#tooltip").hide();
	}
}

function show_popup_menu(){
	var popup_menu = document.getElementById('popup-menu');
	if (popup_menu){
		$(popup_menu).fadeIn(300);
		$(popup_menu).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function show_left_info_window(){
	var left_info_window = document.getElementById('left-info-window');
	if (left_info_window){
		left_info_window.style.border='solid 1px #FFC000';
		left_info_window.style.background='#45442F';
		left_info_window.style.width='auto';
		left_info_window.style.height = 'auto';
		$(left_info_window).fadeIn(300);
		$(left_info_window).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function show_right_info_window(){
	var right_info_window = document.getElementById('right-info-window');
	if (right_info_window){
		right_info_window.style.border='solid 1px #FFC000';
		right_info_window.style.background='#45442F';
		right_info_window.style.width='auto';
		right_info_window.style.height = 'auto';
		$(right_info_window).fadeIn(300);
		$(right_info_window).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function show_center_info_window(){
	var center_info_window = document.getElementById('center-info-window');
	if (center_info_window){
		center_info_window.style.border='solid 1px #FFC000';
		center_info_window.style.background='#45442F';
		center_info_window.style.width='auto';
		center_info_window.style.height = 'auto';
		$(center_info_window).fadeIn(300);
		$(center_info_window).css('z-index', next_z_index);
		next_z_index++;
		$("#tooltip").hide();
	}
}
function show_post_window(){
	var post_window = document.getElementById('post-window');
	if (post_window){
		$(post_window).fadeIn(300);
		$(post_window).css('z-index', next_z_index);
		next_z_index++;
		to_center_parent('post-window', 'main-window');
		hide_popup_menu();
		$("#tooltip").hide();
	}
}
function show_archive_window(){
	var archive_window = document.getElementById('archive-window');
	if (archive_window){
		$(archive_window).fadeIn(300);
		$(archive_window).css('z-index', next_z_index);
		next_z_index++;
		to_center_parent('archive-window', 'main-window');
		hide_popup_menu();
		$("#tooltip").hide();
	}
}
function show_message_window(){
	var message_window = document.getElementById('message-window');
	if (message_window){
		$(message_window).fadeIn(300);
		$(message_window).css('z-index', next_z_index);
		next_z_index++;
		to_center_parent('message-window', 'main-window');
		hide_popup_menu();
		$("#tooltip").hide();
	}
}
function show_orders_window(){
	var orders_window = document.getElementById('orders-window');
	if (orders_window){
		$(orders_window).fadeIn(300);
		$(orders_window).css('z-index', next_z_index);
		next_z_index++;
		to_center_parent('orders-window', 'main-window');
		hide_popup_menu();
		$("#tooltip").hide();
	}
}
function minutes_to_str(in_minutes){
	var hours = Math.round((in_minutes-30)/60);
	var minutes = in_minutes - (hours * 60);
	hours = '' + ((hours > 9) ? hours : '0' + hours);
	minutes = '' + ((minutes > 9) ? minutes : '0' + minutes);
	return hours + ':' + minutes;
}
function seconds_to_str(in_seconds){
	var minutes = Math.round((in_seconds-30)/60);
	var seconds = in_seconds - (minutes * 60);
	
	var hours = Math.round((minutes-30)/60);
	minutes = minutes - (hours * 60);
	
	hours = '' + ((hours > 9) ? hours : '0' + hours);
	minutes = '' + ((minutes > 9) ? minutes : '0' + minutes);
	seconds = '' + ((seconds > 9) ? seconds : '0' + seconds);
	return hours + ':' + minutes + ':' + seconds;
}
function get_timestamp(){
	var d = new Date();
	var timestamp = Math.floor(d.GetTime()/1000);
	return timestamp;
}
function get_selected_text(){
	selected_text = '';
	if (document.getSelection){
		selected_text = document.getSelection();
	}else{
		selected_text = document.selection.createRange().text;
	}
}
function text_fill(span_id, max_width, max_height){ // Увеличение размера текста до пределов
	var cur_size = 10;
	var height, width;
	var need_continue = true;
	var element = document.getElementById(span_id);
	if (element){
		while (need_continue){
			element.style.fontSize = cur_size + 'px';
			height = element.offsetHeight;
			width = element.offsetWidth;
			if (width >= max_width || height >= max_height){
				need_continue = false;
				element.style.fontSize = (--cur_size) + 'px';
			}
			cur_size++;
		}
	}
}
function alert2(message){
	var element = document.createElement("DIV");
	
	//element.style.border = '1px solid red';
	element.style.position = 'absolute';
	element.style.textAlign = 'center';
	element.style.width = '644px';
	element.style.height = '323px';
	element.style.color = 'white';
	element.style.visibility = 'hidden';
	element.style.backgroundImage = 'url('+interface_images_url+'alfa_bg.png)';
	//element.style.backgroundColor = body_background_color;
	$(element).css('z-index', next_z_index + 5);
	
	var content = '';
	content += '<div style="border: 1px solid transparent; width: 644px; height: 323px; background-image: url('+interface_images_url+'alert.gif)">';
	content += '<div style="margin: 30px; width: 584px; height: 263px; overflow: auto">';
	content += '<table border="0" sellspacing="0" cellpading="0"><tr><td style="width: 584px; height: 259px; text-align: center; vertical-align: middle; font-family: Arial, Sans Serif; font-size: 10pt; color: white">';
	content += message;
	content += '<br><br><button onclick="document.body.removeChild(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);call_alert_script();">Ага!</button>';
	content += '</td></tr></table>';
	content += '</div>';
	content += '</div>';
	element.innerHTML = content;
	
	document.body.appendChild(element);
	// Выравнивание:
	var parent = document.getElementById('main-window');
	if (parent == null) parent = document.body;
	element.style.top = parent.clientHeight/2 + parent.scrollTop + get_position(parent).y - element.offsetHeight/2;
	element.style.left = parent.clientWidth/2 + parent.scrollLeft + get_position(parent).x - element.offsetWidth/2;
	/*element.style.top = parent.clientHeight/2 + get_position(parent).y - element.offsetHeight/2;
	element.style.left = parent.clientWidth/2 + get_position(parent).x - element.offsetWidth/2;*/
	element.style.visibility = 'visible';
}
function call_alert_script(){
	if (alert_script != ''){
		eval(alert_script);
		alert_script = '';
	}
}
function hide_load_screen(){
	$('#big-load-screen').fadeOut(2000);
}
function show_image_window(image_url){
	var element = document.createElement("DIV");
	
	element.style.border = '2px solid #625233';
	element.style.position = 'absolute';
	element.style.textAlign = 'center';
	element.style.color = 'white';
	element.style.padding = '5px';
	element.style.visibility = 'hidden';
	element.style.backgroundImage = 'url('+interface_images_url+'alfa_bg.png)';
	$(element).css('z-index', next_z_index + 5);
	
	var content = '';
	content += '<img src="' + image_url + '">';
	content += '<br><br><button onclick="document.body.removeChild(this.parentNode);">Закрыть</button>';
	element.innerHTML = content;
	
	document.body.appendChild(element);
	// Выравнивание:
	var parent = document.getElementById('main-window');
	if (parent == null) parent = document.body;
	element.style.top = parent.clientHeight/2 + get_position(parent).y - element.offsetHeight/2;
	element.style.left = parent.clientWidth/2 + get_position(parent).x - element.offsetWidth/2;
	element.style.visibility = 'visible';
}
function get_scroll_pos(element_id){
	var scroll_pos = 0;
	var element = document.getElementById(element_id);
	if (element){
		scroll_pos = element.scrollTop;
	}
	return scroll_pos;
}
function set_scroll_pos(element_id, scroll_pos){
	var element = document.getElementById(element_id);
	if (element){
		element.scrollTop = scroll_pos;
	}
}
function show_tips(){
	$("span.tips").tooltip({track: true, delay: 1000, opacity: 0.85, top: 5});
	$("p.tips").tooltip({track: true, delay: 1000, opacity: 0.85, top: 5});
}
function show_cakes_wmr(cake_wmr_cost, cake_count, showing_id){
	var showing_element = document.getElementById(showing_id);
	if (showing_element){
		var cake_cost = Math.ceil(cake_wmr_cost * cake_count);
		if (cake_cost > 0){
			
		}else{
			cake_cost = 0;
		}
		showing_element.innerHTML = cake_cost;
	}
}
function show_modal(message,can_close){
	var element = document.createElement("DIV");
	
	element.id = 'modal_screen';
	element.style.border = 'none';
	element.style.position = 'absolute';
	element.style.textAlign = 'center';
	element.style.top = 0;
	element.style.left = 0;
	element.style.width = document.body.clientWidth;
	element.style.height = document.body.clientHeight;
	element.style.color = 'white';
	element.style.visibility = 'hidden';
	element.style.backgroundImage = 'url('+interface_images_url+'1x1.gif)';
	$(element).css('z-index', next_z_index + 5);
	
	var content = '';
	content += '<div id="modal_window" style="position: absolute; left: 50%; top: 50%; background-image: url('+interface_images_url+'alfa_bg.png)"><div style="border: 1px solid transparent; width: 644px; height: 323px; background-image: url('+interface_images_url+'alert.gif)">';
	content += '<div style="margin: 30px; width: 584px; height: 263px; overflow: auto">';
	content += '<table border="0" sellspacing="0" cellpading="0"><tr><td style="width: 584px; height: 259px; text-align: center; vertical-align: middle; font-family: Arial, Sans Serif; font-size: 10pt; color: white">';
	content += message;
	if (can_close){
		content += '<br><br><button onclick="document.body.removeChild(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);call_alert_script();">Ага!</button>';
	}
	content += '</td></tr></table>';
	content += '</div>';
	content += '</div></div>';
	element.innerHTML = content;
	
	document.body.appendChild(element);
	element.style.visibility = 'visible';
	to_center_parent('modal_window', 'modal_screen');
}
function show_holiday_cakes(day_cost, days_count, showing_id){
	var showing_element = document.getElementById(showing_id);
	if (showing_element){
		var holiday_cakes = Math.ceil(day_cost * days_count);
		if (holiday_cakes > 0){
			
		}else{
			holiday_cakes = 0;
		}
		showing_element.innerHTML = holiday_cakes;
	}
}
