/************************\
|  tiles JS engine       |
| (c) 2011 james landrum |
\************************/

function Tile(label,style,url,func) {
	if (typeof label == "undefined") return null;
	this.label = label;
	this.object = null;
	this.style = (typeof style == "undefined")?"tile_default":style;
	this.url = (typeof url == "undefined")?"#":url;
	this.func = (typeof func == "undefined")?function(){return true;}:func;
	this.onhover = function() {
		$(this).animate({width: '172px', height: '172px', 'line-height': '308px', 'font-size': '17.8px', 'margin-left': '-6px',  'margin-top': '-6px'}, 100);
	}
	this.onleave = function() {
		$(this).animate({width: '160px', height: '160px', 'line-height': '290px', 'font-size': '16px', 'margin-left': '0px',  'margin-top': '0px'},100);
	}
	this.hide = function() {
		$(this.object).animate({height: '0px'},200).delay(1000);
	}
	this.show = function(delay) {
		$(this.object).delay(delay).animate({height: '160px'},200);
	}
}

function TileEngine(tiles,target,host) {
	this.tiles = tiles;
	this.target = $(target);
	this.host = $(host);
	this.naved = false;
	this.getTile = function(id) {
		return this.tiles[id];
	}
	this.createTiles = function() {
		for (t in this.tiles) {
			divHost = document.createElement('div');
			$(divHost).addClass('tilehost_default');

			divTile = document.createElement('div');
			$(divTile).addClass(this.tiles[t].style+' ixi');

			$(divTile).mouseenter(this.tiles[t].onhover);
			$(divTile).mouseleave(this.tiles[t].onleave);
			$(divTile).click(this.tiles[t].func);

			this.tiles[t].object = divTile;
			divTile.appendChild(document.createTextNode(this.tiles[t].label));

			$(divHost).append(divTile);
			this.target.append(divHost);
		}	
	}
	this.navigate = function(url,js) {
       	js = (typeof js == "undefined")?null:js;
		for (t in this.tiles) {
			this.tiles[t].hide();
		}
		$(target).delay(200).fadeOut(function (){/* nav here */});
		this.naved = true;
		$(host).load(url);
		$.getScript(js,function() {initScript();});
		
	}
	this.display = function() {
		if (this.naved) $(target).delay(400).fadeTo(200,0.99);
		else $(target).delay(400).fadeIn();
		for (t in this.tiles) {
			this.tiles[t].show(200);
		} 
	}
	
}



