	var View = {
		width: 480,
		height: 240
	};

	var Input = {
		pointer: {
			x: 0,
			y: 0,
			button1: false
		},
		
		getPointerX: function(){
			return this.pointer.x - Stage.scrollLeft();
		},

		getPointerY: function(){
			return this.pointer.y;
		}
	};

	var Stage = {
		width: 1000,
		minPosition: 50,
		maxPosition: 950,
		
		commands: Array(),
		scrollLeft: function(){
			var lScrollLeft  = -(this.figure.position.left-(View.width/2));
			
			if (lScrollLeft > 0) lScrollLeft = 0;
			if (lScrollLeft < (View.width - Stage.width)) lScrollLeft = (View.width - Stage.width);

			return lScrollLeft;
		},
		
		figure: {
			sprite: {
				width: 66,
				positionShift: -33
			},
			
			position: {
				left: 50,
				direction: 'right'
			},
			
			movement: {
				moving: false,
				speed: 0,
				distance: 0
			}
		},
		
		objects: [
			{
				id: 'imac',
				name: 'iMac',
				image: 'imac.gif',
				active: true,
				left: 100,
				top: 125,
				width: 43,
				height: 47,
				z: 50
			},
			{
				id: 'wine-glass',
				name: 'Sklenička vína',
				image: 'wine-glass.gif',
				active: true,
				left: 84,
				top: 154,
				width: 9,
				height: 15,
				z: 50
			},
			{
				id: 'bigmac',
				name: 'BigMac',
				image: 'bigmac.gif',
				active: true,
				left: 147,
				top: 161,
				width: 11,
				height: 9,
				z: 51
			},
			{
				id: 'iphone',
				name: 'iPhone',
				image: 'iphone.gif',
				active: true,
				left: 164,
				top: 155,
				width: 11,
				height: 14,
				z: 50
			},
			{
				id: 'zidle',
				name: 'Židle',
				image: 'chair1.gif',
				active: false,
				left: 92,
				top: 160,
				width: 57,
				height: 85,
				z: 101
			},
			{
				id: 'trash',
				name: 'Trash',
				image: 'trash.gif',
				active: true,
				left: 185,
				top: 178,
				width: 24,
				height: 29,
				z: 50
			},
			{
				id: 'pc',
				name: 'PC',
				image: 'pc.gif',
				active: true,
				left: 840,
				top: 126,
				width: 43,
				height: 48,
				z: 50
			},	
			{
				id: 'screwdriver',
				name: 'Šroubovák',
				image: 'screwdriver.gif',
				active: true,
				left: 813,
				top: 167,
				width: 13,
				height: 3,
				z: 50
			},	
			{
				id: 'cup',
				name: 'Kafe',
				image: 'cup.gif',
				active: true,
				left: 890,
				top: 160,
				width: 13,
				height: 11,
				z: 50
			},	
			{
				id: 'chair2',
				image: 'chair2.gif',
				active: false,
				left: 848,
				top: 165,
				width: 28,
				height: 56,
				z: 50
			},	
			{
				id: 'recycle-bin',
				name: 'Recycle Bin',
				image: 'recycle-bin.gif',
				active: true,
				left: 770,
				top: 180,
				width: 31,
				height: 34,
				z: 50
			},	
		],
		
		init: function(){
			$("#view").css({width: View.width + "px", height: View.height + "px", position: "relative", overflow: "hidden"});
			// $("#curtain").css({width: View.width + "px", height: "640px", position: "absolute", top: "0", left: "0", background: "url(inc/images/curtain.png) 0 0 no-repeat", "z-index": "1000"});
			$("#stage").css({width: "1000px", height: "300px", background: "url(inc/images/background.gif) 0 0 repeat-x", position: "absolute", top: "0", left: Stage.scrollLeft() + "px"});
			$("#figure").css({"position": "absolute", "left": (this.figure.position.left + this.figure.sprite.positionShift) + "px", "top": "130px", width: "66px", height: "100px", background: "url(inc/images/figure.gif) -66px 0 no-repeat", position: "absolute", "z-index": "100"});

			for(lKey in this.objects){
				var lObject = this.objects[lKey];
				$object = $("<div />");

				$label = $('<div class="label" />');
				$label.text(lObject.name);
				$label.css({"position": "absolute", "display": "none"});


				$object.append($label);
				$object.attr("id", lObject.id);
				$object.css({position: "absolute", background: "url(inc/images/" + lObject.image + ") 0 0 no-repeat", "z-index": lObject.z});
				$object.css({top: lObject.top + "px", left: lObject.left + "px", width: lObject.width + "px", height: lObject.height + "px"});
				$("#stage").append($object);

				$label.css({top: -($label.height()+5) + "px", left: ((lObject.width-$label.width()-10) / 2)+"px"});
			}

			$("#view").bind("mousemove", function(e){
				Input.pointer.x = e.pageX - $("#view").offset().left;
				Input.pointer.y = e.pageY - $("#view").offset().top;
			});
			
			$("#view").bind("click", function(e){
				e.preventDefault();
				e.stopPropagation();
				Stage.commands[0] = {
					action: "moveto",
					params: {
						position: e.pageX - $("#view").offset().left - Stage.scrollLeft()
					}
				};

				if(Stage.commands[0].params.position < Stage.figure.position.left){
					Stage.figure.position.direction = "left";
				} else if(Stage.commands[0].params.position > Stage.figure.position.left){
					Stage.figure.position.direction = "right";
				}

				Stage.figure.movement.moving = true;
			});
			
			// $("#curtain").animate({top: "-640px"}, 1000);
			
		},
			
		
		actualize: function(){
			var lTimeStart = (new Date()).getTime();
			
			if (this.figure.movement.moving){
				if (this.figure.position.direction == "right"){
					this.figure.position.direction = "right";
					this.figure.movement.speed += 1;
				}  else if (this.figure.position.direction == "left"){
					this.figure.position.direction = "left";
					this.figure.movement.speed -= 1;
				} 

				if (this.figure.movement.speed>5) this.figure.movement.speed = 5;
				if (this.figure.movement.speed<-5) this.figure.movement.speed = -5;

				this.figure.position.left += this.figure.movement.speed;
				this.figure.movement.distance += this.figure.movement.speed;

				if(this.commands[0] && this.commands[0].action == "moveto"){
					if(
						((this.commands[0].params.position >= this.figure.position.left) && (this.figure.position.direction == "left")) ||
						((this.commands[0].params.position <= this.figure.position.left) && (this.figure.position.direction == "right")) ||
						((Stage.minPosition >= this.figure.position.left) && (this.figure.position.direction == "left")) ||
						((Stage.maxPosition <= this.figure.position.left) && (this.figure.position.direction == "right"))
					){
						if ((Stage.minPosition >= this.figure.position.left) && (this.figure.position.direction == "left")){
							this.figure.position.left = Stage.minPosition;								
						} else if ((Stage.maxPosition <= this.figure.position.left) && (this.figure.position.direction == "right")){
							this.figure.position.left = Stage.maxPosition;
						} else {
							this.figure.position.left = this.commands[0].params.position;
						}
						this.figure.movement.moving = false;
						this.figure.movement.speed = 0;
					}
				}

				lLeftPosition = -this.figure.sprite.width + "px";
				if (this.figure.position.direction == "left") lLeftPosition = "0";

				if (this.figure.movement.speed != 0){
					var lFaze = this.figure.position.left % 100;
					if(lFaze >= 75){
						$("#figure").css("background-position", lLeftPosition + " -400px");
					} else if(lFaze >= 50){
						$("#figure").css("background-position", lLeftPosition + " -300px");
					} else if(lFaze >= 25){
						$("#figure").css("background-position", lLeftPosition + " -200px");
					} else if(lFaze >= 0){
						$("#figure").css("background-position", lLeftPosition + " -100px");
					}
				} else {
					$("#figure").css("background-position", lLeftPosition + " 0px");
				}

				$("#stage").css("left", this.scrollLeft() + "px");

				$("#figure").css("left", this.figure.position.left + this.figure.sprite.positionShift + "px");
			}
			
			$("#keyPressed").text(Input.getPointerX() + " - " + Input.pointer.y);
			
			var lTitulek = false;
			for(lKey in Stage.objects){
				var lObject = Stage.objects[lKey];
				if (lObject.active){
					if (!lTitulek && isIn(lObject, Input.getPointerX(), Input.getPointerY())){
						$("#" + lObject.id + " .label").css("display", "block");
						lTitulek = true;
					} else {
						$("#" + lObject.id + " .label").css("display", "none");
					}
				}
			};
			
			var lTimeEnd = (new Date()).getTime();

			setTimeout("Stage.actualize();", (40-(lTimeEnd-lTimeStart)));
		}
		
	};
	
	function isIn(pObject, pX, pY){
		var lRet = false;
		if ((pX >= pObject.left) && (pX <= pObject.left+pObject.width) && (pY >= pObject.top) && (pY <= pObject.top+pObject.height)){
			lRet = true;
		}
		return lRet;
	}
	
	$(function(){
		Stage.init();
		Stage.actualize();
	});