/**

 SproutWorks Tile Scrolling Engine


*/


function GameObject(graphic, x, y, width, height, id, screen) {
	this.xPos = x;
	this.yPos = y;
	this.screenx = x;
	this.width = width;
	this.height = height;
	this.image = graphic;
	this.id = id;
	this.screen = screen;
	this.onscreen = false;
	//this.createNode(x, y);	// change to take worldpos into account
}

GameObject.prototype.createNode = function(x, y) {
	var screenDiv = document.getElementById(this.screen);
	var newNode = document.createElement('img');
	newNode.src = '/images/game/' + this.image;
	newNode.id = this.screen + 'obj' + this.id;
	newNode.style.position = 'absolute';
	newNode.style.top = y + 'px';
	newNode.style.left = x + 'px';
	newNode.style.clip = 'rect(' + 0 + 'px, ' + 0 + 'px, ' + 0 + 'px, ' + 0 + 'px)';
	newNode.$speed = Math.floor(Math.random()*2 + 1);
	this.node = newNode;
	screenDiv.appendChild(newNode);
}

var screenWidth = $(window).width();
var screenHeight = 124;
var screenTop = 0;
var screenLeft = 0;

var mouseDown = false;

var IE = document.all?true:false;


function WorldLayer(screen, names, widths, heights, scale, numObjects, randHeight, randSpacing) {

	this.screen = screen;
	this.worldX=0;
	this.worldY=0;
	this.lastX = 0;
	this.lastY = 0;
	this.tilesY = 6;
	this.tilesX = 14;
	this.numTiles = 84;
	this.tileSize = 50;
	this.mapXSize = 100;
	this.mapYSize = 100;

	this.map = new Array();
	this.tileList = new Array();
	this.tileList.push('blank.png');
 	this.tileList.push('smallgray.gif');
	this.tileList.push('conveyorbelt.gif');
	this.tileList.push('greenpipe.png');
	this.objectList = new Array(3);
	
	this.play = true;
	
	//this.objectList[0] = new GameObject('green-marble.png', 200, 50, 0, screen);
	//this.objectList[1] = new GameObject('tree-1.png', 700, 50, 1, screen);
	//this.objectList[2] = new GameObject('plant-1.png', 300, 50, 2, screen);
	//var numObjects = 100;
	var numTypes = widths.length;
	var heightScale = 10;
	var curX = 0;
	this.objectList = new Array(numObjects);
	for (var objNum=0; objNum < numObjects; objNum++) {
		objID = Math.floor(Math.random()*numTypes);
		if (numTypes == 1) objID = 0;
		
		var height = (randHeight) ? Math.round(Math.random()*(500- heights[objID])) : screenHeight - heights[objID] + 3;
		var left = (randSpacing) ? objNum * (Math.round(Math.random()*scale)) : curX;
		curX += widths[objID];
		var top = Math.round(Math.sin(heightScale*objNum*3.14/numObjects)*heights[objID]*2) + 60;
		this.objectList[objNum] = new GameObject(names[objID], left, top, widths[objID], heights[objID], objNum, screen);
	}

	this.maxLeft = left + widths[objID];

	for(row=0; row < 100; row++)
	for(col=0; col < 100; col++) {
		if (row == 0) {
			if (col % 3 == 0) this.map.push(0);
			else this.map.push(0);
		} else if (row == 1) {
			this.map.push(0);
		}
		else {
			if (col % 4 == 0)
		this.map.push(0);
		else this.map.push(0);
		}
	}
}

WorldLayer.prototype.setupTiles = function() {
	var screenDiv = document.getElementById(this.screen);

	for(var tileNum=0; tileNum < this.numTiles; tileNum++) {
		var newNode = document.createElement('img');
        var xIndex = tileNum%this.tilesX;
		var yIndex = Math.floor(tileNum/this.tilesX);
	    var left = xIndex*this.tileSize;
        var top = yIndex*this.tileSize;
        var tileX = Math.floor((this.worldX+left)/this.tileSize);
		var tileY = Math.floor((this.worldY+top)/this.tileSize);
		graphic = this.tileList[this.map[tileX + tileY*this.mapXSize]];
		newNode.src = 'images/tiles/' + graphic;
		//newNode.src = 'includes/tree.php?xsize=100&ysize=100';
		newNode.style.position = 'absolute';
		newNode.style.left = left + 'px';
		newNode.style.top = top + 'px';
		newNode.setAttribute('class',  'tile');
		newNode.id = this.screen + tileNum;
		//clipNode(newNode);
		screenDiv.appendChild(newNode);
	}
}

WorldLayer.prototype.clipNode = function(node) {
	var left = parseInt(node.style.left);
	var top = parseInt(node.style.top);
	var clipBottom = screenHeight - top;
	var clipRight = screenWidth - left;
	if (clipRight < 0) clipRight = 0;
	if (clipBottom < 0) clipBottom = 0;
	if (left < 0) clipLeft = -left;
	else clipLeft = 0;
	if (top < 0) clipTop = -top;
	else clipTop = 0;
	node.style.clip = 'rect(' + clipTop + 'px, ' + (clipRight) + 'px, ' + clipBottom + 'px, ' + clipLeft + 'px)';
}

WorldLayer.prototype.moveObjects = function(moveX, moveY) {
	var screenRight = screenLeft + screenWidth;

	if (this.worldX > this.maxLeft) {
		this.worldX = -screenWidth;
	}

	var numObjects = this.objectList.length;
	for(var objNum=0; objNum < numObjects; objNum++) {
		object = this.objectList[objNum];
		var objClip = screenLeft - object.width;
		var left = object.xPos - this.worldX;
		var top = object.yPos - this.worldY;
		if (object.onscreen) {
			var objectNode = object.node;
			//left *= objectNode.$speed;


			if (left < objClip || left > screenRight) {
				document.getElementById(this.screen).removeChild(objectNode);
				object.onscreen = false;
			} else {
				objectNode.style.left = left + 'px';
				objectNode.$left = left;
				objectNode.$top = top;
				this.clipObject(objectNode);
			}
		} else {
			if (left < screenRight && left > objClip) {
				object.createNode(left, top);
				object.onscreen = true;
			}
		}

	}

}

WorldLayer.prototype.clipObject = function(objectNode) {
	var left = objectNode.$left;
	var top = objectNode.$top;
	var clipTop;
	var clipBottom = screenHeight - top;
	var clipRight = screenWidth - left;
	if (clipRight < 0) clipRight = 0;
	if (clipBottom < 0) clipBottom = 0;
	if (left < screenLeft) var clipLeft = screenLeft - left;
	else var clipLeft = 0;
	if (top < screenTop) clipTop = -top + screenTop;
	else clipTop = 0;

	objectNode.style.clip = 'rect(' + clipTop + 'px, ' + clipRight + 'px, ' + clipBottom + 'px, ' + clipLeft + 'px)';
}

WorldLayer.prototype.moveTiles = function(moveX, moveY) {

	this.worldX -= moveX;
	this.worldY -= moveY;

	screenDiv = document.getElementById(this.screen);
	if (false) {
	for (var tileNum=0; tileNum < this.numTiles; tileNum++) {
		div2 = document.getElementById(this.screen + tileNum);
		var left = parseInt(div2.style.left) + moveX;
		var top = parseInt(div2.style.top) + moveY;

		var updateGraphic = false;

		// tile went past right side
		if (left > screenWidth) {
			left -= screenWidth + this.tileSize;
			updateGraphic = true;
		}
		// tile went past left side
		else if (left < -this.tileSize) {
			left += screenWidth + this.tileSize;
			//left = screenWidth;
			updateGraphic = true;
		}
		// tile went past lower side
		if (top > screenHeight && moveY > 0) {
			top -= this.tilesY * this.tileSize;
			updateGraphic = true;
		}
		// tile went past upper side
		else if (top < -this.tileSize && moveY < 0) {
			//top += screenHeight + tileSize;
			top += this.tilesY * tileSize;
			updateGraphic = true;
		}

		if (updateGraphic) {
			var tileX = Math.floor((this.worldX+left)/this.tileSize);
			var tileY = Math.floor((this.worldY+top)/this.tileSize);
			if (tileX < this.mapXSize && tileY < this.mapYSize && tileX >= 0 && tileY >= 0) {
				graphic = this.tileList[this.map[tileX + tileY*this.mapXSize]];
				div2.src = 'images/tiles/' + graphic;

			} else div2.src = 'images/tiles/blank.png';
		}

		var clipBottom = screenHeight - top;
		var clipRight = screenWidth - left;
		if (clipRight < 0) clipRight = 0;
		if (clipBottom < 0) clipBottom = 0;
		if (left < screenLeft) var clipLeft = -left + screenLeft;
		else var clipLeft = 0;
		if (top < screenTop) var clipTop = -top + screenTop;
		else var clipTop = 0;

		div2.style.clip = 'rect(' + clipTop + 'px, ' + (clipRight) + 'px, ' + clipBottom + 'px, ' + clipLeft + 'px)';
		div2.style.left = (left + 'px');
		div2.style.top = (top + 'px');

	}
	}
	//moveBalls();
	//moveObjects();
	this.moveObjects(moveX, moveY);
	if (this.play)
		self.setTimeout(this.screen + '.moveTiles(' + moveX + ',' + moveY + ')', 40);
	return false;
}


function handleMouseDown(e) {

	if (IE) {
		lasxX = event.clientX;
		lastY = event.clientY;
	}
	else {
		lastX = e.clientX;
		lastY = e.clientY;
	}
	//window.event.returnValue=false

    if (!(lastX > screenLeft && lastX < screenLeft + screenWidth && lastY > screenTop && lastY < screenTop + screenHeight)) return true;
    mouseDown = true;

	return false;
}

function handleMouseUp() {
	mouseDown = false;
	//window.event.returnValue=false
	return false;
}

function handleMouseMove(e) {
    if (curTool == 'hand')
        return moveTile(e);
    return true;
}

function makeObjects() {
	var screenDiv = document.getElementById('gamescreen');
	for (obj=0; obj < objectList.length; obj++) {
		newNode = document.createElement('img');
		newNode.src = '/images/game/' + objectList[obj].image;
		newNode.style.position = 'absolute';
		newNode.style.left = objectList[obj].xPos + 'px';
		newNode.style.top = objectList[obj].yPos + 'px';
		newNode.id = 'obj' + obj;
		screenDiv.appendChild(newNode);
	}
}

function moveObjects() {
	for (obj=0; obj < objectList.length; obj++) {
		gameObject = objectList[obj];
		gameObjectNode = document.getElementById('obj' + obj);
		gameObjectNode.style.left = gameObject.xPos - worldX + 'px';
		gameObjectNode.style.top = gameObject.yPos - worldY + 'px';
	}
}
