function breakout()
{
	if(top.location!=location)
	{
		top.location.href=document.location.href;
	}
}

var scSpeed = 0;
var halfWidth = 0;
var rightReset = 0;
var resetTo = 0;
var sclang = 'de';
var scpath = '';

/* the animation duration has to depend on the current location because the movement speed is ((targetPos - currentPos) / duration) */
function startLeftScroll()
{
	var theUl = $('ul#screenshotlist');
	var pos = parseInt(theUl.css('left'));
	var animTime = Math.abs(pos) / scSpeed * 27;
	theUl.animate( {left:'0px'},  {duration:animTime, easing:'linear', complete:function() {
		theUl.css( {left:'-' + halfWidth + 'px'});
		startLeftScroll();
	} } );	
}

function startRightScroll()
{
	var theUl = $('ul#screenshotlist');
	var pos = parseInt(theUl.css('left'));
	var animTime = (rightReset - Math.abs(pos)) / scSpeed * 27;
	theUl.animate( {left:'-' + rightReset + 'px'},  {duration:animTime, easing:'linear', complete:function() {
		theUl.css( {left:'-' + resetTo + 'px'});
		startRightScroll();
	} } );	
}

function initScreenshots()
{
	$('div#screenshotwrapper').mousemove(function(e) {
		var relativeX = e.pageX - $(window).width() / 2;
		var newSpeed = Math.round(relativeX / 30);
		var absSpeed = Math.max(Math.abs(newSpeed) - 1, 0); 
		
		if(absSpeed != scSpeed)
		{
			$('ul#screenshotlist').stop();	
			
			if(newSpeed > 2)
			{
				scSpeed = absSpeed;
				startRightScroll();
				/* startLeftScroll(); */
			}
			else if(newSpeed < -2)
			{
				scSpeed = absSpeed;
				startLeftScroll();
				/* startRightScroll(); */
			}
		}
		
	}).mouseleave(function() {
		$('ul#screenshotlist').stop();
		scSpeed = 0;
	}); 
	
	$('.smallscreenshot').click(function(e) {
		$('body').prepend('<div id="greylayer"></div>');
		
		var offset = $(this).offset();
		var theImagePath = $(this).attr("id");
		theImagePath = theImagePath.substring(0, theImagePath.length - 3);
		theImagePath = scpath + '/public/img/buttons/' + sclang + '/screenshots/' + theImagePath + 'large.jpg';
			
		$('body').prepend('<div id="bigscwrapper" class="bigscwrapper" style="left:' + offset.left + 'px; top:' + offset.top + 'px;"></div>');
		theImage = new Image();
		$(theImage).bind("load", {"w":e.target.width, "h":e.target.height, "o":$(e.target).offset(), "i":$(theImage)}, startImageAnimation);
		$(theImage).attr("id", "largescimg");
		theImage.src = theImagePath;
		
		$('#bigscwrapper').click(function() { $(this).remove(); $('#greylayer').remove(); } );
		$('#greylayer').click(function() { $('#bigscwrapper').remove(); $(this).remove(); } );		
	
	});
}

/* asynchronus callback to ensure the image is loaded before accessing width and height */
function startImageAnimation(e)
{
	var wwidth = $(window).width(); 
	var Eimg = e.data.i;
	var Escrwrp = $("#bigscwrapper");
	
	var natWidth  = Eimg[0].width;
	var natHeight = Eimg[0].height;
	
	Eimg.addClass('scaled');
	Eimg.css("width", e.data.w);
	Eimg.css("height", e.data.h);
	Eimg.css("left", e.data.o.left);
	Eimg.css("top", e.data.o.top);
	Escrwrp.append(Eimg);
	Escrwrp.css( {visibility:"visible"} );
	
	var pos = Math.round((wwidth - natWidth) / 2);
	
	Eimg.animate( {width:natWidth, height:natHeight},  {duration:600, easing:'linear'} );
	Escrwrp.animate( {left:pos, top:"100px"},  {duration:600, easing:'linear'} );	
}

var newBgPos = 0;

function animateBackground()
{
	$("body").css({backgroundPosition:"-" + Math.round(newBgPos) + "px 0px"});	
	newBgPos = newBgPos + 1;
	if(newBgPos > 1280) newBgPos = newBgPos - 1280;
} 

function prepareLangTip(callerID, generatorId)
{
	$('#'+callerID).hover(function(e){
		$('#'+generatorId).css('left',getLangTipLeft($('#'+callerID), $('#'+generatorId)));
		$('#'+generatorId).css('top' ,getLangTipTop($('#'+callerID)));			
		$('#'+generatorId).fadeIn();
	});	
	$('#'+callerID).mouseout(function(e){
		if(testMouseOverElement($('#'+generatorId), e))
			return ;		
		$('#'+generatorId).fadeOut(); 	
	});	
	$('#'+generatorId).mouseout(function(e){		
		$('#'+generatorId).fadeOut(); 	
	});
}

function getLangTipLeft(caller, toolTip)
{
	var left = caller.offset().left+toolTip.width()+10;
	if(left > $('body').width())
		left = $('body').width()-toolTip.width()-10;
	else
		left = caller.offset().left+10;	

	if(left < 0)
		return 0;			
	return left;
}

function getLangTipTop(caller)
{
	var offset = caller.offset();
	var top = offset.top-23;
	if(top < 0)
		return 0;	
	return top;
}

function testMouseOverElement(jElement, jmPosition)
{
	jELeft = jElement.offset().left;
	jETop  = jElement.offset().top;
	jERight = jELeft + jElement.width();
	jEBottom = jETop + jElement.height();	
	mX = jmPosition.pageX;
	mY = jmPosition.pageY;
	
	if(mX > jELeft && mX < jERight && mY > jETop && mY < jEBottom)		
		return true;	
	return false;
}
