last_z_index = 20;
ajust_number = 2;
sep_size = 10;
font_size = 12;
cells_padding_v = 2;
cells_padding_h = 5;
moving_id = '';
year = 0;
selected_day = '';
image = '';
language = 'english';
need_reload = 0;
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 ajust_calendar(){
	var i;
	var element;
	var max_width = 0;
	var max_height = 0;
	var left_margin = 30;
	var top_margin = 30;
	var year_title_width = 0;
	var year_title_height = 0;
	var image_width = 0;
	var image_height = 0;
	for (i = 1; i <=12; i++){
		element = document.getElementById("mon-"+i);
		if (element){
			if (element.offsetWidth > max_width) max_width = element.offsetWidth;
			if (element.offsetHeight > max_height) max_height = element.offsetHeight;
		}
	}
	element = document.getElementById("year-title");
	if (element){
		year_title_width = element.offsetWidth;
		year_title_height = element.offsetHeight;
	}
	element = document.getElementById("calendar_image");
	if (element){
		image_width = element.offsetWidth;
		image_height = element.offsetHeight;
	}
	if (ajust_number == 1){
		$("#year-title").css({left: ((left_margin + (4 * max_width + 3 * sep_size) / 2) - year_title_width / 2) + 'px', top: top_margin + 'px'});
		$("#mon-1").css({left: left_margin + 'px', top: top_margin + year_title_height + 'px'});
		$("#mon-2").css({left: (left_margin + max_width + sep_size) + 'px', top: top_margin + year_title_height + 'px'});
		$("#mon-3").css({left: (left_margin + (max_width + sep_size) * 2) + 'px', top: top_margin + year_title_height + 'px'});
		$("#mon-4").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: top_margin + year_title_height + 'px'});
		
		$("#mon-5").css({left: left_margin + 'px', top: (top_margin + year_title_height + max_height + sep_size) + 'px'});
		$("#mon-6").css({left: (left_margin + max_width + sep_size) + 'px', top: (top_margin + year_title_height + max_height + sep_size) + 'px'});
		$("#mon-7").css({left: (left_margin + (max_width + sep_size) * 2) + 'px', top: (top_margin + year_title_height + max_height + sep_size) + 'px'});
		$("#mon-8").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: (top_margin + year_title_height + max_height + sep_size) + 'px'});
		
		$("#mon-9").css({left: left_margin + 'px', top: (top_margin + year_title_height + (max_height + sep_size) * 2) + 'px'});
		$("#mon-10").css({left: (left_margin + max_width + sep_size) + 'px', top: (top_margin + year_title_height + (max_height + sep_size) * 2) + 'px'});
		$("#mon-11").css({left: (left_margin + (max_width + sep_size) * 2) + 'px', top: (top_margin + year_title_height + (max_height + sep_size) * 2) + 'px'});
		$("#mon-12").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: (top_margin + year_title_height + (max_height + sep_size) * 2) + 'px'});
	}else if (ajust_number == 2){
		$("#year-title").css({left: ((left_margin + (4 * max_width + 3 * sep_size) / 2) - year_title_width / 2) + 'px', top: (top_margin + max_height + sep_size) + 'px'});
		$("#calendar_image").css({left: ((left_margin + (4 * max_width + 3 * sep_size) / 2) - image_width / 2) + 'px', top: (top_margin + max_height + sep_size + year_title_height) + 'px'});
		
		$("#mon-1").css({left: left_margin + 'px', top: top_margin + 'px'});
		$("#mon-2").css({left: (left_margin + max_width + sep_size) + 'px', top: top_margin + 'px'});
		$("#mon-3").css({left: (left_margin + (max_width + sep_size) * 2) + 'px', top: top_margin + 'px'});
		$("#mon-4").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: top_margin + 'px'});
		
		$("#mon-5").css({left: left_margin + 'px', top: (top_margin + max_height + sep_size) + 'px'});
		$("#mon-6").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: (top_margin + max_height + sep_size) + 'px'});
		
		$("#mon-7").css({left: left_margin + 'px', top: (top_margin + (max_height + sep_size) * 2) + 'px'});
		$("#mon-8").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: (top_margin + (max_height + sep_size) * 2) + 'px'});
		
		$("#mon-9").css({left: left_margin + 'px', top: (top_margin + (max_height + sep_size) * 3) + 'px'});
		$("#mon-10").css({left: (left_margin + max_width + sep_size) + 'px', top: (top_margin + (max_height + sep_size) * 3) + 'px'});
		$("#mon-11").css({left: (left_margin + (max_width + sep_size) * 2) + 'px', top: (top_margin + (max_height + sep_size) * 3) + 'px'});
		$("#mon-12").css({left: (left_margin + (max_width + sep_size) * 3) + 'px', top: (top_margin + (max_height + sep_size) * 3) + 'px'});
	}
	check_calendar_size();
}
function decrease_font(){
	var new_font_size = Math.round(parseFloat(font_size) * 0.9);
	if ((font_size - new_font_size) < 1){
		font_size--;
	}else{
		font_size = new_font_size;
	}
	if (font_size < 5){
		font_size = 5;
	}
	set_font_size(font_size, 1);
}
function increase_font(){
	var new_font_size = Math.round(parseFloat(font_size) * 1.1);
	if ((new_font_size - font_size) < 1){
		font_size++;
	}else{
		font_size = new_font_size;
	}
	set_font_size(font_size, 1);
}
function increase_cells(){
	var new_cells_padding_v = Math.round(cells_padding_v * 1.25);
	if ((new_cells_padding_v - cells_padding_v) < 1){
		cells_padding_v++;
	}else{
		cells_padding_v = new_cells_padding_v;
	}
	var new_cells_padding_h = Math.round(cells_padding_h * 1.25);
	if ((new_cells_padding_h - cells_padding_h) < 1){
		cells_padding_h++;
	}else{
		cells_padding_h = new_cells_padding_h;
	}
	set_padding(cells_padding_v, cells_padding_h, 1);
}
function decrease_cells(){
	var old_cells_padding_v = cells_padding_v;
	var new_cells_padding_v = Math.round(cells_padding_v * 0.75);
	if ((cells_padding_v - new_cells_padding_v) < 1){
		cells_padding_v--;
	}else{
		cells_padding_v = new_cells_padding_v;
	}
	var old_cells_padding_h = cells_padding_h;
	var new_cells_padding_h = Math.round(cells_padding_h * 0.75);
	if ((cells_padding_h - new_cells_padding_h) < 1){
		cells_padding_h--;
	}else{
		cells_padding_h = new_cells_padding_h;
	}
	if (cells_padding_v < 0 || cells_padding_h < 0){
		cells_padding_v = old_cells_padding_v;
		cells_padding_h = old_cells_padding_h;
	}
	set_padding(cells_padding_v, cells_padding_h, 1);
}
function set_font_size(new_size, need_save){
	font_size = new_size;
	$("table.mon > tbody > tr > td").css({'font-size': font_size + 'px'});
	check_calendar_size();
	if (need_save == 1) save_data();
}
function set_padding(v_padding, h_padding, need_save){
	cells_padding_v = v_padding;
	cells_padding_h = h_padding;
	$("table.mon > tbody > tr > td").css({'padding': v_padding + 'px ' + h_padding + 'px ' + v_padding + 'px ' + h_padding + 'px'});
	check_calendar_size();
	if (need_save == 1) save_data();
}
function ajust_sizes(){
	var i;
	var element;
	var max_width = 0;
	var max_height = 0;
	for (i = 1; i <= 12; i++){
		element = document.getElementById("mon-"+i);
		if (element){
			if (element.offsetWidth > max_width) max_width = element.offsetWidth;
			if (element.offsetHeight > max_height) max_height = element.offsetHeight;
		}
	}
	for (i = 1; i <= 12; i++){
		element = document.getElementById("mon-"+i);
		if (element){
			$("#mon-"+i).css({'width': max_width + 'px', 'height': max_height + 'px'});
		}
	}
	check_calendar_size();
}
function check_calendar_size(){
	var i;
	var element;
	var element_right;
	var element_bottom;
	var max_right = 0;
	var max_bottom = 0;
	for (i = 1; i <= 12; i++){
		element = document.getElementById("mon-"+i);
		if (element){
			element_right = element.offsetLeft + element.offsetWidth;
			element_bottom = element.offsetTop + element.offsetHeight;
			if (element_right > max_right) max_right = element_right;
			if (element_bottom > max_bottom) max_bottom = element_bottom;
		}
	}
	max_right += sep_size;
	max_bottom += sep_size;
	$("#calendar").css({'width': max_right + 'px', 'height': max_bottom + 'px'});
	$("#calendar_container").css({'width': (max_right+40) + 'px', 'height': (max_bottom+40) + 'px'});
}
function d(element){ // Установка/снятие пометки с дня
	selected_day = '';
	if ($(element).hasClass('sd')){
		$(".sd").removeClass('sd').addClass('d');
	}else{
		$(".sd").removeClass('sd').addClass('d');
		$(element).removeClass('d').addClass('sd');
		selected_day = element.id;
	}
	save_data();
}
function mark_day(){
	$("#" + selected_day).removeClass('d').addClass('sd');
}
$(document).ready(
	function() {
		$("#js-warning").css({'display': 'none'});
	}
);
function set_events(){
	jQuery(".moving").easydrag();
	jQuery(".moving").ondrag(
	    function(e, element){
	    	if (moving_id != element.id){
		        var left_col_zindex;
				last_z_index++;
				left_col_zindex = last_z_index + 1;
				$("#left-column").css({zIndex: left_col_zindex});
				$(element).css({zIndex: last_z_index});
				moving_id = element.id;
			}
        }
    );
	jQuery(".moving").ondrop(
    function(e, element){
    	moving_id = '';
        check_calendar_size();
        save_data();
        }
    );
    jQuery("#examples-list").mouseleave(
    function(e, element){
        	hide(this);
        }
    );
}
function print_version(){
	$(".no-print").fadeOut(2000);
	$(".no-print").fadeIn(10000);
}
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 show_examples_list(sender){
	var examples_list = document.getElementById('examples-list');
	if (examples_list){
		examples_list.style.minWidth = sender.offsetWidth;
		$(examples_list).fadeIn(300);
		examples_list.style.top = get_position(sender).y - 5;
		examples_list.style.left = get_position(sender).x + sender.offsetWidth/2 - examples_list.offsetWidth/2;
	}
}
function hide(element){
	$(element).fadeOut(300);
}
function save_data(){
	//if (need_reload) alert('This page must be reloaded to continue');
	var positions = '';
	$(".moving").each(
		function(){
			positions += this.id + ':' + this.offsetLeft + ':' + this.offsetTop + ':' + this.offsetWidth + ':' + this.offsetHeight + ':' + $(this).css('zIndex') + '; ';
		}
	);
	xajax_save_data(positions, font_size, cells_padding_v, cells_padding_h, year, selected_day, image, language, need_reload);
}
function reset(){
	$(".mon").css({"width": "auto", "height": "auto"});
	xajax_reset_data(year, selected_day, image, language, need_reload);
}
function set_image(image_url){
	image = image_url;
	if (image != ''){
		$("#calendar_image").css({"width": "auto", "height": "auto"});
	}
	show_image();
	save_data();
}
function show_image(){
	if (image != ''){
		$("#calendar_image > img").attr({"src": image});
		$("#i").attr({"value": image});
		ajust_number = 2;
		$("#calendar_image").fadeIn(300);
		$("#remove_image").fadeIn(300);
	}else{
		$("#i").value = '';
		$("#calendar_image").fadeOut(300);
		$("#remove_image").fadeOut(300);
	}
}
function set_language(new_language){
	language = new_language;
	save_data();
	xajax_get_calendar(year, language, image, 0);
}
function set_year(new_year){
	year = new_year;
	$("#calendar_year").html('Calendar ' + year);
	save_data();
	xajax_get_calendar(year, language, image, 0);
}
function on_load(){
	xajax_get_calendar(year, language, image, 1);
}
