jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("images/test3.jpg", "images/test2.jpg", "images/test.jpg");


function setupGame(){
	tiles = new Object();
	
	tiles.gridsize = 3;
	
	tiles.hover = new Object();
	tiles.hover.horiz = 0;
	tiles.hover.vert = 0;
	
	tiles.grid = new Object();
	//tiles.notplayable = new Object();
	tiles.values = new Object();
	
	tiles.values[0] = new Object();
	tiles.values[1] = new Object();
	tiles.values[2] = new Object();
	
	tiles.grid[0] = new Object();
	tiles.grid[1] = new Object();
	tiles.grid[2] = new Object();
	
	tiles.blank = new Object();
	// Grid Square = Pic Class
	
	tiles.values[0][0] = "a1";
	tiles.values[1][0] = "b1";
	tiles.values[2][0] = "c1";
	
	tiles.values[0][1] = "a2";
	tiles.values[1][1] = "b2";
	tiles.values[2][1] = "c2";
	
	tiles.values[0][2] = "a3";
	tiles.values[1][2] = "b3";
	tiles.values[2][2] = "c3";
	
	tiles.grid[0][0] = "a1";
	tiles.grid[1][0] = "b1";
	tiles.grid[2][0] = "c1";
	
	tiles.grid[0][1] = "a2";
	tiles.grid[1][1] = "b2";
	tiles.grid[2][1] = "c2";
	
	tiles.grid[0][2] = "a3";
	tiles.grid[1][2] = "b3";
	tiles.grid[2][2] = "c3";
	
	tiles.blank.horiz = 2;
	tiles.blank.vert = 0;
}


function tilePlayable(theObject){
	if(tiles.blank.horiz > 0){
		var leftcheck = tiles.grid[tiles.blank.horiz-1][tiles.blank.vert];
	} else {
		var leftcheck = "notpos"
	}
	if(tiles.blank.horiz < tiles.gridsize-1){
		var rightcheck = tiles.grid[tiles.blank.horiz+1][tiles.blank.vert];
	} else {
		var rightcheck = "notpos"
	}
	if(tiles.blank.vert < tiles.gridsize-1){
		var upcheck = tiles.grid[tiles.blank.horiz][tiles.blank.vert+1];
	} else {
		var upcheck = "notpos"
	}	
	if(tiles.blank.vert > 0){
		var downcheck = tiles.grid[tiles.blank.horiz][tiles.blank.vert-1];
	} else {
		var downcheck = "notpos"
	}
	
			if($(theObject).attr("id") == leftcheck){
  			tiles.hover.horiz = tiles.blank.horiz-1;
				tiles.hover.vert = tiles.blank.vert;
  			var thereturn = true;
			}else if($(theObject).attr("id") == rightcheck){
  			tiles.hover.horiz = tiles.blank.horiz+1;
				tiles.hover.vert = tiles.blank.vert;
				var thereturn = true;
			}else if($(theObject).attr("id") == upcheck){
  			tiles.hover.horiz = tiles.blank.horiz;
				tiles.hover.vert = tiles.blank.vert+1;
				var thereturn = true;
			}else if($(theObject).attr("id") == downcheck){
  			tiles.hover.horiz = tiles.blank.horiz;
				tiles.hover.vert = tiles.blank.vert-1;
				var thereturn = true;
			}else{
  			var thereturn = false;
			}
		
		return thereturn;
}

function moveTile(theObject){
	var newBlank = tiles.grid[tiles.hover.horiz][tiles.hover.vert];
	var newBlankHoriz = tiles.hover.horiz;
	var newBlankVert = tiles.hover.vert;
	
	var oldValue = tiles.values[tiles.hover.horiz][tiles.hover.vert];
	var oldBlankValue = tiles.values[tiles.blank.horiz][tiles.blank.vert];
		
	var oldBlank = tiles.grid[tiles.blank.horiz][tiles.blank.vert];
	var oldBlankHoriz = tiles.blank.horiz;
	var oldBlankVert = tiles.blank.vert;
	

	$("#"+newBlank).removeClass(oldValue).addClass(oldBlankValue);
	$("#"+oldBlank).removeClass(oldBlankValue).addClass(oldValue);
	
	$('#'+newBlank).fadeTo("fast", 1).css("cursor", "default");
	
	tiles.values[newBlankHoriz][newBlankVert] = oldBlankValue;
	tiles.values[oldBlankHoriz][oldBlankVert] = oldValue;
	
	tiles.blank.horiz = newBlankHoriz;
	tiles.blank.vert = newBlankVert;
}

$(document).ready(function(){
	
	setupGame();
	
	$(".tile").bind("mouseenter",function(){
		if(tilePlayable(this) == true){
      	$(this).fadeTo("fast", 0.75).css("cursor", "pointer");
    }
  }).bind("mouseleave",function(){
      $(this).fadeTo("fast", 1).css("cursor", "default");
  });
  
  $('.tile').click(function() {
		if(tilePlayable(this) == true){
			moveTile(this);
		}
	});
	 $('#changePicButton').toggle(
     function () {
     $('.gameGrid').removeClass("image1");
     $('.gameGrid').addClass("image2");
	},
     function () {
     $('.gameGrid').removeClass("image2");
     $('.gameGrid').addClass("image1");
	});
	$('.changePic').click(function() {
		$('.tile').css("backgroundImage", "url(images/test2.jpg)");
		$('.c1').css("backgroundImage", "url(images/blanktile.jpg)");
		return false;
	});
	$("select#othergames").change(function () {
		var selectValue = $("#othergames option:selected").attr("value");
		window.location.href = "http://jedidiah.eu/"+selectValue+"/";
	});
});