
var Integrame = {
	PreInit:	function(){
		Game.Init = Integrame.Init;		
		Game.Draw = Integrame.Draw;		
		Game.OverHighlight = Integrame.OverHighlight;
		Game.OutHighlight = Integrame.OutHighlight;
		Game.KeyDown = Integrame.KeyDown;
		Game.MouseClick = Integrame.MouseClick;
		Game.DefMouseClick = Integrame.DefMouseClick;
		Game.DefOverHighlight = Integrame.DefOverHighlight;
		Game.DefOutHighlight = Integrame.DefOutHighlight;
	},
	
	Init: function(ID, XMLFile, Path, AutoSave, TimerInterval){
		Game.GID = ID;
		Game.Path = Path;
		Game.XMLFile = XMLFile;
		Game.Reset();
		if(true == AutoSave)
		{
			Timer.Init(TimerInterval);
		}
		$.ajax({
			url:Game.Path + "data/integrame/" + XMLFile + ".xml",
			success: Integrame.Draw
		});
	},
	
	Draw: function(XMLContent){
		var xml = $(XMLContent);
		var i, j, p;
		
		Game.Table = document.getElementById('integrama-grid');
		Game.Reset();
 
		n = xml.find("inaltime:first").text();
		m = xml.find("latime:first").text();
		
		Game.Table.oncontextmenu = function(){
			return false;
		};
		
		Game.ActiveCell = null;
		
		for (i = 0; i < n; i++) {
			row = Game.Table.insertRow(i);			
			for (j = 0; j < m; j++) {
				cell = row.insertCell(j);
				cell.className = "editable-cell integrama-editable-cell";			
				cell.innerHTML = "&nbsp;";
				cell.tag = '*';				
				cell.unselectable = "on";
			}
		}
		
		xml.find('definitie').each(function(){
			var str = $(this).text();
			var substr = str.split("|")
			var x = parseInt(substr[0]);
			var y = parseInt(substr[1]);
							
			if(substr.length > 2)
			{
				Game.Table.rows[x].cells[y].className = "integrama-block-def";
				Game.Table.rows[x].cells[y].innerHTML = "";				
				for (j = 2; j < substr.length; j += 6) {			
					newDef = document.createElement("div");					
					newDef.innerHTML = substr[j];
					newDef.className = "integrama-def-base";
					newDef.len = substr[j + 2];
					newDef.direct = substr[j + 3];
					newDef.xx = substr[j + 4];
					newDef.yy = substr[j + 5];										
					if(substr.length > 6 || str.length > 35) {
						newDef.className += " no-padding integrama-def-small";
					}
					Game.Table.rows[x].cells[y].appendChild(newDef)					
					if (j < substr.length - 6) {
						newDef.style.borderBottom = "1px solid black";
					}					
					switch (substr[j + 1]) {
						case "1":
						case "2":
						case "6":
							Game.Table.rows[x].cells[y + 1].style.backgroundImage = "url('" + Game.Path + "images/arrows/" + substr[j + 1] + ".gif')";							
							break;
						case "3":
						case "4":
							Game.Table.rows[x + 1].cells[y].style.backgroundImage = "url('" + Game.Path + "images/arrows/" + substr[j + 1] + ".gif')"
							Game.Table.rows[x + 1].cells[y].style.backgroundPosition = "top center";
							break;
						case "5":
							Game.Table.rows[x + 1].cells[y].style.backgroundImage = "url('" + Game.Path + "images/arrows/" + substr[j + 1] + ".gif')";
							break;
						case "7":
							Game.Table.rows[x].cells[y - 1].style.backgroundImage = "url('" + Game.Path + "images/arrows/" + substr[j + 1] + ".gif')";
							Game.Table.rows[x].cells[y - 1].style.backgroundPosition = "top right";
							break;
						case "8":
							Game.Table.rows[x].cells[y + 1].style.backgroundImage = "url('" + Game.Path + "images/arrows/2.gif')";
							Game.Table.rows[x].cells[y + 1].style.backgroundPosition = "center left";
							break;
						case "9":
							Game.Table.rows[x - 1].cells[y].style.backgroundImage = "url('" + Game.Path + "images/arrows/9.gif')";
							Game.Table.rows[x - 1].cells[y].style.backgroundPosition = "bottom center";
							break;
						case "11":
							Game.Table.rows[x].cells[y + 1].style.backgroundImage = "url('" + Game.Path + "images/arrows/1.gif')";
							Game.Table.rows[x].cells[y + 1].style.backgroundPosition = "center left";
							break;
					}
				}
			}
			else
			{
				Game.Table.rows[x].cells[y].className = "integrama-block";				
				//Game.Table.rows[x].cells[y].setAttribute('background', Game.Path + "images/block.png");
				//Game.Table.rows[x].cells[y].innerHTML =  "<img width='100%' height='100%'  src = '" + Game.Path + "images/block.png />";
			}
		});
		
		xml.find('special').each(function(){
			var data = $(this).text().split("-");
			if (data[0] == 'h') {
				for (j = 0; j < data[3]; j++) {
					Game.Table.rows[data[1]].cells[new Number(data[2]) + j].style.backgroundColor = '#edf0c7';
				}
			}
			else if (data[0] == 'v') {
				for (j = 0; j < data[3]; j++) {
					Game.Table.rows[new Number(data[2]) + j].cells[data[1]].style.backgroundColor = '#edf0c7';
				}
			}
		});
		Game.Hover();
		Game.DefHover();
	},
	
	OutHighlight: function(){
		$(".integrama-cell-hover").removeClass("integrama-cell-hover");
	},
	
	OverHighlight: function(){
		var x = this.parentNode.rowIndex;
	    var y = this.cellIndex;
	    var n = Game.Table.rows.length;
	    var m = Game.Table.rows[0].cells.length;
	    
	    $(this).addClass("integrama-cell-hover");
	    
	    var i = x - 1, j = y;
	    
	    while (i >= 0 && $(Game.Table.rows[i].cells[j]).hasClass('editable-cell')) {
	        $(Game.Table.rows[i].cells[j]).addClass('integrama-cell-hover');
	        i--;
	    }
	    
	    i = x + 1;
	    while (i < n && $(Game.Table.rows[i].cells[j]).hasClass('editable-cell')) {
	        $(Game.Table.rows[i].cells[j]).addClass('integrama-cell-hover');
	        i++;
	    }
	    
	    i = x;
	    j = y - 1;
	    while (j >= 0 && $(Game.Table.rows[i].cells[j]).hasClass('editable-cell')) {
	        $(Game.Table.rows[i].cells[j]).addClass('integrama-cell-hover');
	        j--;
	    }
	    
	    i = x;
	    j = y + 1;
	    while (j < m && $(Game.Table.rows[i].cells[j]).hasClass('editable-cell')) {
	        $(Game.Table.rows[i].cells[j]).addClass('integrama-cell-hover');
	        j++;
	    }
	},
	
	DefOverHighlight: function(){
		
	},
	
	DefOutHighlight: function(){				
		$(".integrama-cell-hover").removeClass("integrama-cell-hover");
	},
	
	KeyDown: function(event){
		if (Game.ActiveCell)
		{ 
			var cell = $(Game.ActiveCell);
			var charCode = event.which;
			var key;
			
			if (charCode == 32 || charCode == 8 || charCode == 46) { //SPACE || BACKSPACE || DEL
				cell.html("&nbsp");
				cell.attr("tag", "*"); 
		        event.preventDefault();
		        Saving.Unsaved();
			}
			else {
				key = String.fromCharCode(charCode)
				
				key = key.toUpperCase();
				charCode = key.charCodeAt(0);
				
				if (charCode > 64 && charCode < 91) 				
				{
					var cssClass, nextCell;
					cell.html(key);		
					cell.attr("tag", key); 		
					
					var x = Game.ActiveCell.parentNode.rowIndex;
					var y = Game.ActiveCell.cellIndex;
					
					if (cell.hasClass("integrama-active-cell-down")) {
						cssClass = "integrama-active-cell-down"
						if (x + 1 < Game.Table.rows.length) 
							nextCell = Game.Table.rows[x + 1].cells[y];
					}
					else if (cell.hasClass("integrama-active-cell-right")) {
						cssClass = "integrama-active-cell-right";
						nextCell = Game.Table.rows[x].cells[y + 1];
					}
					
					if (nextCell != null) {
						var nc = $(nextCell);
						if (nc.hasClass("editable-cell")) {
							cell.removeClass(cssClass).removeClass("integrama-active-cell");
							nc.addClass(cssClass).addClass("integrama-active-cell");
							Game.ActiveCell = nextCell;
						}
					}
					Saving.Unsaved();
				}
			}
		}
	},
	
	DefMouseClick: function(event){				
		x = parseInt(event.target.xx);
		y = parseInt(event.target.yy);
		length = event.target.len;				
		direct = event.target.direct;						
		switch(direct){
			case "1":		//V						
				for (i = 0; i < length; i++) {					
					$(Game.Table.rows[x + i].cells[y]).addClass('integrama-cell-hover');						
				}
				Game.ActiveDirection = 'hh';
				$(Game.Table.rows[x].cells[y]).trigger('click');
				break;						
			case "2":		//H											
				for (i = 0; i < length; i++) {				 
					$(Game.Table.rows[x].cells[y + i]).addClass('integrama-cell-hover');										
				}
				Game.ActiveDirection = 'vv';
				$(Game.Table.rows[x].cells[y]).trigger('click');
				break;			
		}		
	},
	
	MouseClick: function(event){
		if(Game.ActiveDirection != null && Game.ActiveDirection.length == 1){			
			if(Game.ActiveCell != null)
			{
				var x_1 = Game.ActiveCell.parentNode.rowIndex;
				var y_1 = Game.ActiveCell.cellIndex;
				var x_2 = event.target.parentNode.rowIndex;
				var y_2 = event.target.cellIndex;
				var the_same_active_cell = (x_1 == x_2) && (y_1 == y_2);
			}
			else
			{
				var the_same_active_cell = true;
			}
		}
		else
		{
			the_same_active_cell = true;
		}
	    $(".integrama-active-cell").removeClass("integrama-active-cell").removeClass("integrama-active-cell-down").removeClass("integrama-active-cell-right");		    
	    if(Game.ActiveDirection == null || Game.ActiveDirection == 'v' || Game.ActiveDirection == 'vv' || !the_same_active_cell)
	    {
	    	$(this).addClass("integrama-active-cell-right").addClass("integrama-active-cell");
	    	Game.ActiveDirection = 'h';
	    }
	    else
	  	{
	  		$(this).addClass("integrama-active-cell-down").addClass("integrama-active-cell");
	  		Game.ActiveDirection = 'v';
	  	}	    	
	    Game.ActiveCell = event.target;	
	}
};

