/* functions for page 'index' */
(function ($) {

$(document).ready(function(){

	window.move = function( eq )
	{	var percents = parseInt(40 + (68 - 40) * Math.random());
		var timeAnimate = parseInt(500 + (2000 - 500) * Math.random());
		var timeWait = parseInt(5000 + (20000 - 5000) * Math.random());
		$('.phrase').eq(eq).animate({left: percents + '%'}, timeAnimate, "linear", function(){
			window.setTimeout( "move(" + eq + ")", timeWait);
		});
	}

	$('.phrase')
		.css({left: '100%'})
		.each(function(i)
		{	var percents = 40 + (70 - 40) * Math.random();
			var timeAnimate = parseInt(500 + (2000 - 500) * Math.random());
			var timeWait = parseInt(5000 + (20000 - 5000) * Math.random());
			$(this).animate({left: parseInt(percents) + '%'}, timeAnimate, "linear", function(){
				//$(this).parents('#map').css({overflow: 'visible'});
				window.setTimeout( "move(" + i + ")", timeWait);
			});
		});

/*
	$('#lupe-bounds').bind('mouseenter', function(){
		$('#lupe').animate({left: '174px'}, 1500, 'linear', function(){
			$('#lupe-bounds').bind('mouseleave', function(){
				$('#lupe').animate({left: '-200px'}, 1500, 'linear', function(){});
			});
		});
	});
*/

	function getMousePosition(e)
	{	if (e.pageX || e.pageY)
		{	var posX = e.pageX;
			var posY = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{	var posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			var posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return {x:posX, y:posY}
	}

   $('#lupe-bounds').click(function(){
      window.location.href = $('#lupe a').attr('href');
	});
	
	var offsetLupeBounds = $('#lupe-bounds').offset();
	var lupeIn = false;
	var process = false;

	$(document).bind('mousemove', function(e){
		if ( process == false)
		{	if (!e) e = window.event;
			var posX = getMousePosition(e).x;
			var posY = getMousePosition(e).y;
         //$('#lupe-bounds').html('== == == == == == posX: ' + posX + '; posY: ' + posY);
			if (	(offsetLupeBounds.left < posX && posX < offsetLupeBounds.left + 400)	&&
					(offsetLupeBounds.top < posY && posY < offsetLupeBounds.top + 333)	)
			{	if ( lupeIn == false)
				{	process = true;
					$('#lupe').animate({left: '174px'}, 500, 'linear', function(){
      				lupeIn = true;
						process = false;
						//$('#lupe-bounds').html('== == == == == == posX: ' + posX + '; posY: ' + posY);
						//$('#lupe-bounds').html('lupeIn: ' + lupeIn + '; process: ' + process);
					});
				}
			}
			else
			{	if ( lupeIn == true)
				{	process = true;
					$('#lupe').animate({left: '-200px'}, 500, 'linear', function(){
						lupeIn = false;
						process = false;
						//$('#lupe-bounds').html('== == == == == == posX: ' + posX + '; posY: ' + posY);
						//$('#lupe-bounds').html('lupeIn: ' + lupeIn + '; process: ' + process);
					});
				}
			}
		}
	});
/*
		$(document).bind('mouseout', function(){
			$(document).unbind('mousemove');
			$(document).unbind('mouseover');
			if ($.browser.msie )	el.get(0).releaseCapture();
			//document.onselectstart = function(){	return true;	}
			//el.blur();
		});
*/
/*		if (e.stopPropagation) e.stopPropagation()
		else	e.cancelBubble = true;
		if (e.preventDefault) e.preventDefault()
		else	e.returnValue = false;

		//document.onselectstart = function(){	return false;	}
		//el.focus();
	});
*/
/*

	$('#lupe-bounds').bind('mouseover', function(){
		var thisEl = $(this);
		var offset = $(this).offset();
		var x, y;
		//thisEl.unbind('mouseout');
		$('#lupe').animate({left: '174px'}, 1500, 'linear', function(){
			thisEl.bind('mousemove', function(e){
				x = e.pageX;
				y = e.pageY;
				thisEl.html('Координаты: ' + x + ', ' + y);
				if (offset.left < x && x < offset.left + 400)
				{  thisEl.unbind('mouseover');
					thisEl.bind("mouseout", function(){
						$('#lupe').animate({left: '-200px'}, 1500, 'linear', function(){
							thisEl.bind('mouseover');
						});
					});
				}
				else
				{	$('#lupe').animate({left: '-200px'}, 1500, 'linear', function(){
						thisEl.unbind('mouseover');
					});
				}
			});
		});
	});
*/
});

})(jQuery);
