var windowEx = function(obj){
	if ($('#info_' + obj.id).length) {
		var html = $('#info_' + obj.id).html(); 
		var plane = $('<div id="plane"/>').appendTo('body').css({
			opacity: 0,
			background: '#fff'
		}).animate({
			opacity: 0.3
		});
		var cont = $('<div id="cont"></div>').appendTo('body')
		var wind = $('<div id="wind"><div class="t"></div><a href="javascript:closeWindowEx()" title="Закрыть" class="close"><img alt="Закрыть" src="/static/themes/belogorie/images/window.close.png"/></a><div class="cont">' + html + '</div><div class="b"></div></div>').appendTo(cont);
		cont.click(function(e){
			if (e.target.id == 'cont') closeWindowEx();
		});
		var h = wind.children('.cont').height();
		wind.children('.cont').css({
			height: 0
		}).animate({
			width: '538px'
		}, '1000', function(){
			$(this).animate({
				height: h
			})
		});
	}
}

var closeWindowEx = function(){
	$('#header').appendTo('#main');
	$('#cont, #plane').remove();
}

$.fn.mapNavigator = function(options){
	
	function sign(num){
		return num != 0 ? Math.round(num/Math.abs(num)) : 0;
	}
	
	var self = this;
	this.self = self;
	var settings = $.extend({
		speed: 100,
		pix: 3,
		mode: 'extra'
	}, options);
	
	for (var i in settings) self[i] = settings[i];
	
	self.doc = $('#main');
	self.map = self.find('.map').eq(0);
	
	var flag = true;
	function moveTo(coords){
		flag = false;
		self.stop(true, true);
		self.animate(coords, {
			duration: self.speed,
			easing : 'easeOutQuad', 
			complete: function(){
				flag = true;
			}
		});
	}
	
	//self.map.css({display: 'block', visibility: 'visible', height:'auto', width:'auto'});
	
	//console.log(self.doc.width() + ' ' + self.map.width());
	
	self.css({
		left: Math.round((self.doc.width()-2000)/2),
		top: 0,
		marginTop:0,
		marginLeft:0
	});
	
	var thread = false;
	self.fadeIn(2000, function(){
		if (self.mode=='extra') {
			$(document).mousemove(function(e){
				var mousestop = function(e){
					if (flag) {
						var docH = self.doc.height();
						var imgH = self.map.height();
						var docW = self.doc.width();
						var imgW = self.map.width();
						var x = e.clientX;
						var y = e.clientY;
						var px = x/docW;
						var py = y/docH;
						var mintop = -(imgH - docH);
						var minleft = -(imgW - docW);
						moveTo({
							top: Math.round(mintop*(py)),
							left: Math.round(minleft*(px))
						})
						$('#info').html('px: '+Math.round(px*100) + '<br/> py: ' + Math.round(py*100) + '<br/> top: ' +Math.round(mintop*(1-py)) +'<br/> left: ' + Math.round(minleft*(1-px)));
					}
				}
				return (function(){
					if (thread) clearTimeout(thread);
					thread = setTimeout(function (){
						mousestop(e)
					}, 0);
				})();
			});
		}
		
		if (self.mode=='simple') {
			
			function move(hor, begin, grow, ises) {
				
				var anim = {};
				
				if (hor) {
					if (begin) {
						grow = grow ? 0 : self.doc.width()-self.map.width() + 'px';
					} else {
						minleft = self.doc.width()-self.map.width();
						var d = self.offset().left;
						grow = grow ? d+20 > 0 ? '0px' : '+=20px' : d-20 < minleft ? minleft+'px' : '-=20px';
					}
					anim = {left: grow};
				}else {
					if (begin) {
						grow = grow ? 0 : self.doc.height()-self.map.height() + 'px';
						
					} else {
						mintop = self.doc.height()-self.map.height();
						var d = self.offset().top;
						grow = grow ? d+20 > 0 ? '0px' : '+=20px' : d-20 < mintop ? mintop+'px' : '-=20px';
					}
					anim = {top: grow};
				}
				
				if (begin) {
					self.animate(anim, {
						duration: 1300,
						easing : 'easeInOutSine',
						complete: function(){
							ises.stop(true,false).animate({opacity: 0.6});
						}
					});
				} else {
					self.stop(true, false);
					self.animate(anim,{
						duration: 200,
						easing : 'easeOutQuad'
					});
				}
				
			}
			
			self.doc.append('<div class="l"></div><div class="t"></div><div class="r"></div><div class="b"></div>');
			self.doc.find('.b').hover(function(){
				if (self.doc.height()-self.map.height() != self.offset().top)
				$('.ib').stop(true, true).animate({opacity: 1});
				move(false,true, false, $('.ib'));
			}, function(){
				$('.ib').stop(true, true).animate({opacity: 0.6});
				move(false,false, false, $('.ib'));
			});
			self.doc.find('.t').hover(function(){
				if (0 != self.offset().top)
				$('.itr, .itl').stop(true, true).animate({opacity: 1});
				move(false, true, true, $('.itr, .itl'));
			}, function(){
				$('.itr, .itl').stop(true, true).animate({opacity: 0.6});
				move(false,false, true, $('.itr, .itl'));
			});
			self.doc.find('.r').hover(function(){
				if (self.doc.width()-self.map.width() != self.offset().left)
				$('.ir').stop(true, true).animate({opacity: 1});
				move(true,true, false, $('.ir'))
			}, function(){
				$('.ir').stop(true, true).animate({opacity: 0.6});
				move(true,false, false, $('.ir'));
			});
			self.doc.find('.l').hover(function(){
				if (0 != self.offset().left)
				$('.il').stop(true, true).animate({opacity: 1});
				move(true, true, true, $('.il'));
			}, function(){
				$('.il').stop(true, true).animate({opacity: 0.6});
				move(true,false, true, $('.il'));
			});
		}
		
		$(window).resize(function(){
			var minleft = self.doc.width()-self.map.width();
			var mintop = self.doc.height()-self.map.height();
			if (self.offset().left < minleft) self.css({left: minleft});
			if (self.offset().top < mintop) self.css({left: mintop});
		});
	});
};


