var catcher = 0; //map-page handler

function point_hover() {
	$('.p-left').mouseover(function(){		
		if (!$(this).is(':animated')) {
			if (!$(this).parent().parent().parent().hasClass('point-disabled')) {return false}
				$(this).children('.point-red').hide();
				$(this).children('.point-red-hover').show();
				$(this).children('.point-yellow').hide();
				$(this).children('.point-yellow-hover').show();
				$(this).children('.point-blue').hide();
				$(this).children('.point-blue-hover').show();
			
		}
	}).mouseout(function(){
		if (!$(this).is(':animated')) {
				$(this).children('.point-red-hover').hide();
				$(this).children('.point-red').show();
				$(this).children('.point-yellow-hover').hide();
				$(this).children('.point-yellow').show();
				$(this).children('.point-blue-hover').hide();
				$(this).children('.point-blue').show();
		}		
	});
}

function map_functions(){
	$('#contact-map').click(function(){
		$.trueshow = $('.trueshow');
		$('.point-info').addClass('point-disabled');
		$.trueshow.removeClass('point-disabled');
		$('.trueshow').removeClass('trueshow');
		//point_hover();
		//$('.p-left').mouseout();
	});
	$('.point-info').click(function(){
		$(this).addClass('trueshow');			 
	});
	
	
	$('.point').hover(function(){
		/*$('#contact-map').click();
		$.parent = $(this).parent().parent().parent().parent('.point-info');
		
		if ($.parent.hasClass('point-disabled')) {
			$.parent.removeClass('point-disabled');
			$.parent.children('.p-left').mouseout();
			//$(this).parent().unbind('mouseover').unbind('mouseout');
		} else {
			$.parent.addClass('point-disabled');
		}		
		// ie6 needs width, dunno
		
		return false;*/
		$.parent = $(this).parent().parent().parent().parent('.point-info');
		
		$.parent.removeClass('point-disabled');
		
		if ($.browser.msie && $.browser.version == 6) {	
			$.parent.children('.shadow')
				.css('width', $.parent.width() + 'px')
				.css('height', $.parent.height() + 1 + 'px');
		}
		
	},function(){
		
	});
	$('.point-info').hover(function(){
	},function(){
		$.parent = $(this);
		$.parent.addClass('point-disabled');
				
	});
	
	
	if (window.location.hash == '#sport') {
		$('.p-E').removeClass('point-disabled');
	}
	if (window.location.hash == '#children') {
		$('.p-blue-1,.p-blue-2,.p-blue-3,.p-blue-4').removeClass('point-disabled');
	}
	if (window.location.hash == '#beauty') {
		$('.p-E').removeClass('point-disabled');
	}
	if (window.location.hash == '#shop') {
		$('.p-E').removeClass('point-disabled');
	}
	if (window.location.hash == '#food') {
		$('.p-E').removeClass('point-disabled');
	}
	if (window.location.hash == '#med') {
		$('.p-G').removeClass('point-disabled');
	}
	if (window.location.hash == '#service') {
		$('.p-E').removeClass('point-disabled');
	}
	if (window.location.hash == '#guard') {
		$('.p-S').removeClass('point-disabled');
	}
	if (window.location.hash == '#lake') {
		$('.p-H').removeClass('point-disabled');
	}
	if (window.location.hash == '#school') {
		$('.p-F').removeClass('point-disabled');
	}
	
	$('#mtop').mouseover(function(){
		$(this).children().show();
		if ($('#contact-map').css('top')=='0px') $(this).children().hide();
		else $(this).children().show();
		$('#contact-map').animate({
			'top': '0px'				  
		},2000, function(){
			$('#mtop').children().hide();
		});				   
	}).mouseout(function(){
		$(this).children().hide();
		$('#contact-map').stop();   
	});
	$('#mleft').mouseover(function(){	
		$(this).children().show();
		if ($('#contact-map').css('left')=='0px') $(this).children().hide();
		else $(this).children().show();
		$('#contact-map').animate({
			'left': '0px'				  
		},2000, function(){
			$('#mleft').children().hide();
		});				   
	}).mouseout(function(){
		$(this).children().hide();
		$('#contact-map').stop();   
	});
	$('#mdown').mouseover(function(){
		$(this).children().show();
		var mw = ($('#contact-map').height() - $('body').height()) * (-1);
		if ($('#contact-map').css('top')==(mw+'px')) $(this).children().hide();
		else $(this).children().show();
		
		$('#contact-map').animate({
			'top': mw + 'px'				  
		},2000, function(){
			$('#mdown').children().hide();
		});			   
	}).mouseout(function(){
		$('#mdown').children().hide();
		$('#contact-map').stop();   
	});
	$('#mright').mouseover(function(){			
		$(this).children().show();
		var mw = ($('#contact-map').width() - $(window).width()) * (-1);
		if ($('#contact-map').css('left')==(mw+'px')) $(this).children().hide();
		else $(this).children().show();
		$('#contact-map').animate({
			'left': mw + 'px'				  
		}, 2000, function(){
			$('#mright').children().hide();
		});				   
	}).mouseout(function(){
		$('#mright').children().hide();
		$('#contact-map').stop();   
	});

	body_assist();
}

function body_assist(){
		$('body').bind('mousedown', function(ui) {
			if (!$('body').hasClass('cmove')) {
				$('body').addClass('cmove');
			}
			var sx = ui.pageX - $('#contact-map').get(0).offsetLeft;
			var sy = ui.pageY - $('#contact-map').get(0).offsetTop;
			$(this).bind('mousemove', function(d) {
				
				var cLeft = d.pageX - sx;
				var cTop =  d.pageY - sy;
				
				if (cLeft < 0) {
					var mw = ($('#contact-map').width() - $(window).width()) * (-1);
					
					if (cLeft <= mw) {
						//$('#info').html(cLeft + '<br />' +  mw);
						$('#contact-map').css('left', mw + 'px');
					}
					else
						$('#contact-map').css('left', cLeft + 'px');
				}
				else
					$('#contact-map').css('left', '0px');
					
				if (cTop < 0) {
					var mw = ($('#contact-map').height() - $('body').height()) * (-1);
					if (cTop <= mw) 
						$('#contact-map').css('top', mw + 'px');
					else
						$('#contact-map').css('top', cTop + 'px');
				}
				else
					$('#contact-map').css('top', '0px');
					
			}).bind('mouseup', function(ui) {
				$(this).unbind('mousemove').removeClass('cmove');
			});
			
		}).bind('mouseup', function(ui) {
			$(this).unbind('mousemove').removeClass('cmove');
			if ($('#contact-map').css('left') != '0px')
				$('#mleft').show();
			if ($('#contact-map').css('top') != '0px')
				$('#mtop').show();
		}).addClass('cmap');

}


/*function map_resize_parallax() {	
	if (catcher >= 0) {
		if ($(window).width()<1505) {
			$('#overlay').jparallax({
				xtravel: '100%',
				ytravel: '100%'
			});
			catcher = -1;
		}
	}
	if (catcher == -1) {
		if ($(window).width()>=1505) {
			$('#overlay').jparallax({
				xtravel: '0%',
				ytravel: '100%'
			});
			catcher = 1;
		}
	}	
	
}*/

function map_load() {
	var img = new Image();
	$('#overlay').load('/map/map.html', function(){
		$(img).attr('src', '/i/map/map3.jpg').load(function(){ 
			$('.loadbar .bar').stop().css('width','100%');
			$('#contact-map').css('background-image','url(' + $(img).attr('src') + ')');
			//$('#contact-map').width($(img).width()).height($(img).height());
			$('#loadpage').unbind('click');
			point_hover();
			map_functions();
			
			setTimeout("$('.loadbar').remove();$('#mouse').show(); \
			$('.previewcap').html('<h1>Используйте мышку<br />для перемещения по карте</h1><p>Кликните для продолжения</p>'); \
			$('.previewcap.eng').html('<h1>Use the mouse<br />to navigate on the map</h1><p>Click to continue</p>');", 200);
			
			$('#loadpage, .previewcap').bind('click', function(){
				$('.previewcap, #loadpage').remove();
				//map_resize_parallax();				
			});
		});
	});
		
}

function map_load_ie() {
	$('#overlay').load('/map/map.html', function(){
		$('#src').attr('src', '/i/map/map3.jpg').load(function(){ 
			$('#src').hide();
		});
		$('.loadbar .bar').stop().css('width','100%');
			$('#contact-map').css('background-image','url(' + $('#src').attr('src') + ')');
			//$('#contact-map').width($('#src').width()).height($('#src').height());
			$('#loadpage').unbind('click');
			$('#mouse, .point').ifixpng();
			point_hover();
			map_functions();
		
			setTimeout("$('.loadbar').remove();$('#mouse').show(); \
			$('.previewcap').html('<h1>Используйте мышку<br />для перемещения по карте</h1><p>Кликните для продолжения</p>'); \
			$('.previewcap.eng').html('<h1>Use the mouse<br />to navigate on the map</h1><p>Click to continue</p>');", 200);
		
			$('#loadpage, .previewcap').bind('click', function(){
				$('.previewcap, #loadpage').remove();
				//map_resize_parallax();				
			});
	});
}

$(document).ready(function(){
	$('#logo img, #mouse, .point').ifixpng();
	
	if ($(window).width()>=1505) catcher = -1;
	else catcher = 1;

	point_hover();
	map_functions();
	if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7)) {	
		setTimeout("map_load_ie()", 1);
	} else {
		setTimeout("map_load()", 1);
	}	
	
	$('.loadbar').ajaxStart(function(){
	   $('.loadbar .bar').animate({
		width: 100 + '%'
	   }, 10000);
	});
	
	$(window).resize(function(){		
		//map_resize_parallax();			
	});
	
	$('#print a').click(function(){
		return false
	});
	
	$('#print').click(function(){
		window.print();
	});
	
	
	
	$('#toggle').toggle(function(){
		$('#full').hide();
		$('#toggle').html('Посмотреть карту поселка целиком');
	},function(){
		$('#full').show();
		$('#toggle').html('Посмотреть карту поселка подробно');
	});
	
	$('#full').click(function(){
		$('#toggle').click()
	});
	
});
	 