var textTop = 0, textLeft = 0, textInterval = 32, imgInterval = 65, headingTop = 0,
    paddingOffset = 36, boatHeight = 0, boatWidth = 0, textTimer, imgTimer;

$(document).ready(init);

function init() {
    var itemWidths = 0;

	$('#inside').addClass('enhanced');
	$('#animation').prepend('<img id="rowboat" src="images/approach/rowboat.png" width="300" height="300" alt="Rowboat">');

	$('html').removeClass('js');

    textLeft   = $('#animation ul').position().left;
    textTop    = $('#animation ul').css('top').replace('px', '') * 1;
    headingTop = $('#animation h3').css('top').replace('px', '') * 1;
    boatHeight = $('#rowboat').height();

    textTimer = setTimeout(function() {setInterval(function() {slideText();}, 32);}, 500);
    imgTimer  = setTimeout(function() {setInterval(function() {rotateImg();}, 65);}, 500);
}

var slideText = function() {
    textLeft -= 1;
    $('#animation ul').css('left', textLeft);

    var $scrollItems = $('#animation ul li'), $scrollItemOne = $scrollItems.eq(0), firstItemWidth = $scrollItemOne.width();
    if (firstItemWidth < (textLeft + 48) * -1) {
        textLeft += firstItemWidth + paddingOffset;
        $('#animation ul').css('left', textLeft).append($scrollItemOne);
    }
}

var rotateImg = function() {
    $('#rowboat').rotate(1);

    var diffTop  = (boatHeight - $('#rowboat').height());
    $('#rowboat').css('top', Math.ceil(diffTop/2));

    $('#animation h3').css('top', headingTop + diffTop);
    $('#animation ul').css('top', textTop + diffTop);
}

jQuery.fn.rotate = function(angle,whence) {
	var p = this.get(0);

	// we store the angle inside the image tag for persistence
	if (!whence) {
		p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
	} else {
		p.angle = angle;
	}

	if (p.angle >= 0) {
		var rotation = Math.PI * p.angle / 180;
	} else {
		var rotation = Math.PI * (360+p.angle) / 180;
	}
	var costheta = Math.cos(rotation);
	var sintheta = Math.sin(rotation);

	if (document.all && !window.opera) {
		var canvas = document.createElement('img');

		canvas.src = p.src;
		canvas.height = p.height;
		canvas.width = p.width;

		canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
	} else {
		var canvas = document.createElement('canvas');
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
		} else {
			canvas.oImage = p.oImage;
		}

		canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
		canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);

		var context = canvas.getContext('2d');
		context.save();
		if (rotation <= Math.PI/2) {
			context.translate(sintheta*canvas.oImage.height,0);
		} else if (rotation <= Math.PI) {
			context.translate(canvas.width,-costheta*canvas.oImage.height);
		} else if (rotation <= 1.5*Math.PI) {
			context.translate(-costheta*canvas.oImage.width,canvas.height);
		} else {
			context.translate(0,-sintheta*canvas.oImage.width);
		}
		context.rotate(rotation);
		context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
		context.restore();
	}
	canvas.id = p.id;
	canvas.angle = p.angle;
	p.parentNode.replaceChild(canvas, p);
}

jQuery.fn.rotateRight = function(angle) {
	this.rotate(angle==undefined?90:angle);
}

jQuery.fn.rotateLeft = function(angle) {
	this.rotate(angle==undefined?-90:-angle);
}