// load sound manager
soundManager.url = '/fileadmin/js/lib/sound/soundmanager2.swf';
soundManager.debugMode = false;      // enable debugging output (div#soundmanager-debug, OR console..)
soundManager.useConsole = false;     // use firebug/safari console.log()-type debug console if available
soundManager.consoleOnly = false;   // if console is being used, do not create/write to #soundmanager-debug

soundManager.onload = function() {
	soundManager.url = '/fileadmin/js/lib/sound/soundmanager2.swf';
	
	soundManager.createSound({
		id: 'found',
		url: '/typo3conf/ext/hype_games/pi1/games/cash.mp3'
	});
}

$(document).ready(function() {
	var errors = $('#searchpicture #errors area').size();
	var found = 0;
	var timer;
	
	function show_message(name) {
		if(timer) {
			window.clearTimeout(timer);
		}
		
		$('.status span:visible').hide(0);
		
		$('.status .' + name + ':hidden').show(25, function() {
			if(name != 'done') {
				timer = window.setTimeout("$('.status span:visible').fadeOut(150);", 2000);
			}
		});
	}
	
	$('#picture_original').click(function() {
		show_message('notice');
	});
	
	$('#picture_errors').click(function() {
		show_message('error');
	});
	
	$('#searchpicture .close').click(function() {
		$('.msg_box li:visible').hide(0);
		//$('#picture_errors').attr('usemap', '#errors');
		//$('#searchpicture').prepend(map);
		//$('#searchpicture #errors area').bind('click',{element: $(this)},found);
		$('#evil_hack').hide(0);
		return false;
	});
	
	//$('#searchpicture #errors area').bind('click',{element: $(this)},found);
	
	$('#searchpicture #errors area').one('click', function() {
		// get clicked area
		area = $(this);
		
		// save details of clicked area
		//message = '#message_' + area.attr('rel');
		mydebug = area.attr('rel');
		message = '.msg_' + area.attr('rel');
		
		shape = area.attr('shape');
		
		//alert(shape);
		
		coords = area.attr('coords');
		coords = coords.split(',');
		
		//alert(coords[0] + "\n" + coords[1] + "\n" + coords[2] + "\n" + coords[3]);
		
		// remove area from DOM
		area.removeAttr('href');
		
		// calculate center of clicked area
		if(shape == 'rect' || shape == 'RECT') {
			myleft = (parseInt(coords[0]) + ((parseInt(coords[2]) - parseInt(coords[0])) / 2)) + 410 - 19;
			mytop = (parseInt(coords[1]) + ((parseInt(coords[3]) - parseInt(coords[1])) / 2)) - 19;
		} else {
			myleft = Math.round(parseInt(coords[0]) + (parseInt(coords[2]) / 2)) + 410 - 19;
			mytop = Math.round(parseInt(coords[1]) + (parseInt(coords[2]) / 2)) - 19;
		}
		
		//alert('left: ' + myleft + " \n " + 'top: ' + mytop);
		
		// place indicator image on the calculated coordinates
		myindicator = $('<img src="/typo3conf/ext/hype_games/pi1/games/found.png" alt ="" />')
			.css('position', 'absolute')
			.css('left', myleft + 'px')
			.css('top', mytop + 'px');
		
		$('#pictures').prepend(myindicator);
		
		$('img[@src$=.png]').ifixpng('/fileadmin/img/pixel.gif');
		
		// show message
		//$('.messages li:visible').hide(0);
		$('.msg_box li:visible').hide(0);
		$('.background:hidden').show(0);
		$('.interactive:hidden').show(0);
		$(message + ':hidden').show(0);
		//$(message + ':hidden').slideDown(250);
		
		$('#evil_hack').show(0).css('z-index', 999);
		
		soundManager.play('found');
		
		found = found + 1;
		
		if(errors == found) {
			$('#searchpicture img').unbind();
			show_message('done');
			$('.status .reboot').show(0);
			$('.messages li:hidden').show(0);
			$('.msg_box:visible').hide(0);
		} else {
			show_message('found');
		}
		
		return false;
	});
	
	$('.status .reboot').click(function() {
		window.location.href = document.URL;
	});
});

//'shape'				=> 'rect',
//'coordinates'		=> '387,37,408,78',

//'shape'				=> 'circle',
//'coordinates'		=> '373,113,36',