$(document).ready(initGamePage);

var gamewidth = '550';
var gameheight = '420';

function initGamePage() {
	loadGame();
	scoreListen();
	displayScore();
	resizeListen();
}
function loadGame() {
	var swf = new SWFObject('/games/189.swf','gameEmbed',gamewidth,gameheight,'9');
	swf.addParam('scale','SHOW_ALL');
	swf.addParam('wmode','opaque');
	swf.write('game');
}

//score displayen
function displayScore() {
	var score = '';
	if(score !== ''){
		$('#scorearrow').css('left',Math.round(score * 32)+'px');
		$('#scorebalk').css('width',Math.round(score * 32)+'px');
		$('#scorecontainer p').text(score);
		$('#scoresubmit').hide();
		$('#scoresubmit').unbind('click');
		$('#balkcontainer, #scorearrow').unbind('mousedown');
	}
}

//score dragger
function scoreListen() {
	bindMouseDown();
	$('#scoresubmit').click(function(){ submitScore(); });	
}
function bindMouseDown() {
	$('#balkcontainer, #scorearrow').unbind('mousedown');
	$('#balkcontainer, #scorearrow').bind('mousedown',function(e){ if(e.preventDefault) e.preventDefault(); bindMouseUp(); startDrag(e); })
}
function bindMouseUp() {
	$(document).unbind('mouseup');
	$(document).bind('mouseup',function(e){ bindMouseDown(); stopDrag(); })
}
function startDrag(e) {
	$('#gamebottom').bind('mousemove',drag);
	drag(e);
}
function stopDrag() {
	$('#gamebottom').unbind('mousemove',drag);
}
function drag(e) {
	var offset = $('#balkcontainer').offset();
	var newx = e.pageX - offset.left;
	if(newx > 320) newx = 320;
	if(newx < 0) newx = 0;
	$('#scorearrow').css('left',newx+'px');
	$('#scorebalk').css('width',newx+'px');
	var score = Math.round((newx / 320) * 10 * 10)/10;
	$('#scorecontainer p').text(score);
}
function submitScore() {
	var newscore = $('#scorecontainer p').text();
	if(!newscore) newscore = 0;
	$.ajax({
		type: 'POST',
		url: '/ajax/rate/penguin-skate',
		data: 'score='+newscore+'&submitted=1',
		success: function(msg){
			$('#currentscore span').text(msg);
			$('#balkcontainer, #scorearrow').unbind('mousedown');
			$('#scoresubmit').fadeOut(effectspeed);
		}
	});
}
//resizen van flash movie met scroller
function resizeListen(){
	bindResizeMouseDown();
}
function bindResizeMouseDown(){
	$('#scaling div').unbind('mousedown');
	$('#scaling div').bind('mousedown',function(e){ if(e.preventDefault) e.preventDefault(); bindResizeMouseUp(); startResize(e); });
}
function bindResizeMouseUp(){
	$(document).unbind('mouseup');
	$(document).bind('mouseup',function(e){ bindResizeMouseDown(); stopResize(); });
}
function startResize(e) {
	$(document).bind('mousemove',resize);
	resize(e);
}
function stopResize() {
	$(document).unbind('mousemove',resize);
}
function resize(e) {
	var offset = $('#scaling div:first').offset();
	var newx = e.pageX - offset.left - 6;
	if(newx > 100) newx = 100;
	if(newx < 0) newx = 0;
	$('#scaling div div').css('margin-left',newx+'px');
	//nieuwe breedte bepalen
	var breedtefactor = 500/100;
	var newwidth = newx * breedtefactor + 200;
	var newheight = newwidth / gamewidth * gameheight;
	$('#gameEmbed').css('width',newwidth);	
	$('#gameEmbed').css('height',newheight);
}
//$('#gameEmbed').css('width',100);