// Инициализация событий...
jQuery(function(){
	jQuery('#bigmap div.towns').each(function(){
		var id = this.id;
		jQuery('div.close a', jQuery(this)).click(function(){
			jQuery('#' + id).removeClass('show');
			return false;
		});
		jQuery('a.town', jQuery(this))
		.click(function(){
			jQuery('#bigmap div.show').removeClass('show');
			jQuery('#' + id).addClass('show');
			return false;
		})
		.mouseover(function(){
			setTimeout(function(){
				jQuery('#' + id).addClass('showlabel');
			}, 200);
		})
		.mouseout(function(){
			setTimeout(function(){
				jQuery('#' + id).removeClass('showlabel');
			}, 500);
		});
	});
	jQuery(document).bind('keypress', function(event){
		var code=event.charCode || event.keyCode;
		if (code && code == '27') {
			jQuery('#jquery-overlay').fadeOut();
			jQuery('#jquery-input').hide();
			jQuery('#new_reg').hide();
		}
	});
});

// Обработчик событий админ-интерфейса
function act(action) {
	var img = jQuery('#img_bigmap');
	if (img.length == 0) return false;

	img.unbind('click');
	img.css('cursor','');

	var new_reg_div = jQuery('div#new_reg');
	if (new_reg_div.length > 0) new_reg_div.hide();
	
	switch(action) {
		case 'add' : {
			img.css('cursor','crosshair');
			img.click(function(e){
				var ev_x = (window.event) ? window.event.offsetX : e.layerX;
				var ev_y = (window.event) ? window.event.offsetY : e.layerY;
		
				if (ev_x > 2) ev_x -= 2;
				if (ev_y > 2) ev_y -= 2;
		
				if (new_reg_div.length == 0) {
					var html = '<div id="new_reg" class="towns show" style="position: absolute; top: ' + ev_y + 'px; left: ' + ev_x + 'px;">\
									<a href="javascript:void(0);" class="town">&nbsp;</a>\
								</div>';
					jQuery('#img_bigmap').before(html);
				} else {
					new_reg_div.css({top : ev_y, left : ev_x}).show();
				}
		
				jQuery('div#incontent input[name="coords"]').val(ev_x + ':' + ev_y);
				jQuery('div#incontent input[name="act"]').val('add');
				
				// TODO: добавление точки
				service_html(jQuery('div#incontent').html());				
			});
			break;
		}
		case 'edit' : {
			// редактирование точки
			jQuery('div.towns').each(function(){
				var id = this.id;
				jQuery('a.town', this).unbind('click').click(function() {
					if (typeof dat_arr[id] == 'undefined') return false;

					var ev_x = dat_arr[id].coords[0];
					var ev_y = dat_arr[id].coords[1];

					service_html(jQuery('div#incontent').html());

					jQuery('#jquery-input input[name="id"]').val(id);
					jQuery('#jquery-input input[name="coords"]').val(ev_x + ':' + ev_y);
					jQuery('#jquery-input input[name="act"]').val('edit');
					jQuery('#jquery-input input[name="town"]').val(dat_arr[id].name);
					jQuery('#jquery-input input[name="submit"]').val("Сохранить");
					jQuery('#jquery-input textarea[name="town_descr"]').val(dat_arr[id].descr);

					return false;
				});
			});
			break;
		}
		case 'remove' : {
			// удаление точки
			jQuery('div.towns').each(function(){
				var id = this.id;
				jQuery('a.town', this).unbind('click').click(function() {
					if (typeof dat_arr[id] == 'undefined') return false;

					var ev_x = dat_arr[id].coords[0];
					var ev_y = dat_arr[id].coords[1];

					service_html(jQuery('div#incontent').html());

					jQuery('#jquery-input label').remove();
					jQuery('#jquery-input input[name="id"]').val(id);
					jQuery('#jquery-input input[name="act"]').val('remove');
					jQuery('#jquery-input input[name="close"]').before('<label class="input" style="text-align: center; margin-top: 50px;"><img src="/images/Attention.gif" width="37" height="33" alt="Attention" /><br><span style="color:#fff;">Вы действительно желаете удалить "' + dat_arr[id].name + '"?</span></label>');
					jQuery('#jquery-input input[name="submit"]').val("Удалить");

					return false;
				});
			});
			break;
		}
		default: break;
	}
	return false;
}

function service_html(html) {
	jQuery('#jquery-overlay').remove();
	jQuery('#jquery-input').remove();
	
	jQuery('body').append('<div id="jquery-overlay" style="display:none;"></div><div id="jquery-input" style="display:none;">' + html + '</div>');
	// Get pag dimension
	var arrPageSizes = ___getPageSize();
	jQuery('#jquery-overlay').css({
		background:	'#4d4d4d',
		position:	'absolute',
		top:		0,
		opacity:	0.3,
		zIndex:		1000,
		width: 		arrPageSizes[0],
		height:		arrPageSizes[1]
	}).fadeIn();
	
	// Get page scroll
	var arrPageScroll = ___getPageScroll();
	// Calculate top and left offset for the jquery-lightbox div object and show it
	jQuery('#jquery-input').css({
		position:	'absolute',
		zIndex:		1001,
		top:		arrPageScroll[1] + (arrPageSizes[3] / 3),
		width: 		arrPageSizes[0]
	}).show();

	jQuery('#jquery-input input[name="close"]').click(function(){
		jQuery('#jquery-overlay').fadeOut();
		jQuery('#jquery-input').hide();
		jQuery('#new_reg').hide();
	});

	jQuery(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		jQuery('#jquery-overlay').css({
			width: arrPageSizes[0],
			height: arrPageSizes[1]
		});
		// Get page scroll
		var arrPageScroll = ___getPageScroll();
		// Calculate top and left offset for the jquery-lightbox div object and show it
		jQuery('#jquery-input').css({
			width: arrPageSizes[0],
			top: arrPageScroll[1] + (arrPageSizes[3] / 3)
		});
	});
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
