var MotionGrid = new Class({
	
	BASE_TITLE : "販促市場24",
	
	UNIT_SIZE  : 130,
	MARGIN     : 10,
	PADDING    : 5,
	NAV_SIZE   : 140,
	MIN_WIDTH  : 780,
	MIN_HEIGHT : 600,
	
	SIDE_PADDING : 20,
	
	masterNode     : null,
	navNode        : null,
	bgNode         : null,
	bgImage        : null,
	buttons        : null,
	worksButton    : null,
	footNode       : null,
	resizeTimer    : 0,
	scrollTimer    : 0,
	fontSizeTimer  : 0,
	masterWidth    : 0,
	masterHeight   : 0,
	masterOffsetX  : 0,
	navOffsetX     : 0,
	worksMenuInner : 0,
	isInited       : false,
	isRunnning     : false,
	fontSizeBuffer : "",
	activeCategory : "all",
	gridNodes      : new Array(),
	props          : new Array(),
	bgImageProps   : new Object(),
	
	worksMenuState : "",
	isMenuOpenedOnce : false,
	
	ua : "",
	
	
	//----------------------------------------------------------------------------------------------------
	//initialize
	
	initialize : function(bglist) {
		var i;
		var len;
		var nodes;
		var self = this;
		
		this.ua = navigator.userAgent;
		
		document.body.style.visibility = "hidden";
		
		
		this.masterNode = document.getElementById("grid-content");
		this.navNode = document.getElementById("nav");
		this.footNode = document.getElementById("foot");
		
		
		this.initBgNode(bglist);
		this.initNav();
		
		
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, function(evt){ self.onChangeAddress(evt.path) });
		//this.parseJsonData($newsJsonData);
		//this.parseJsonData($blogJsonData);
		
		
		nodes = this.masterNode.childNodes;
		len = nodes.length;
		
		for(i=0; i<len; i++) {
			var node = nodes[i];
			if(node.nodeType == 1) {
				this.gridNodes.push(node);
				node.style.margin = "0";
				node.style.position = "absolute";
			}
		}
		
		this.masterNode.style.position = "relative";
		this.masterNode.style.width = "auto";
		this.masterNode.style.zIndex = 20;
		this.navNode.style.position = "absolute";
		
		//this.footNode.style.position = "absolute";
		//this.footNode.style.zIndex = "5";
		
		//this.setPosition();
		//this.fitToGrid();
		
		
		window.onresize = function() {
			self.resizeHandler();
		}
		
		window.onscroll = function() {
			self.scrollHandler();
		}
		
		this.setFontSizeListener();
	},
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Parse JsonData
	
	parseJsonData : function($data) {
		var json = $data;
		var i;
		var len = json.length;
		for(i=0; i<len; i++) {
			var data = json[i];
			
			var targetBox = document.getElementById("box-" + data.category);
			if(!targetBox) {
				continue;
			}
			
			var div = targetBox.getElementsByTagName("div")[0];
			
			if(data.title) {
				var heading = document.createElement("h2");
				heading.innerHTML = data.title;
				div.appendChild(heading);
			}
			
			if(data.content) {
				mainText = data.content;
				mainText = mainText.replace(/.*<img.*?\/>.*/ig, "");
				var main = document.createElement("div");
				main.className = "body-text";
				main.innerHTML = mainText;
				div.appendChild(main);
			}
			
			if(data.url) {
				setLink(div, data.url);
			}
		}
		
		var atags = main.getElementsByTagName("a");
		len = atags.length;
		for(i=0; i<len; i++) {
			var atag = atags[i];
			atag.onclick = function() {
				
			}
		}
		
		
		
		function setLink(node, url) {
			node.className += " has-link";
			node.onclick = function(evt) {
				if(evt) {
					elem = evt.target;
				}
				else {
					elem = event.srcElement;
				}
				
				if(elem && elem.tagName.toLowerCase() != "a") {
					location.href = url;
				}
			}
		}
	},
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Set Position
	
	setPosition : function() {
		var unitWidth = this.UNIT_SIZE + this.PADDING * 2 + this.MARGIN;
		var w = Math.max(this.MIN_WIDTH, $(document.body).getSize().x);
		var width = w - unitWidth - (this.SIDE_PADDING * 2);
		var colMax = Math.floor(width / unitWidth);
		
		this.masterWidth = colMax * unitWidth;
		this.masterNode.style.width  = this.masterWidth  + "px";
		
		
		var o = this.NAV_SIZE - this.UNIT_SIZE;
		o = 12;
		this.navOffsetX = Math.floor((width - this.masterWidth) / 2) - o + this.SIDE_PADDING;
		this.masterOffsetX = this.navOffsetX + unitWidth + o;
		
		var matrix = [ [0, this.masterWidth, 0] ];
		var maxHeight = 0;
		
		var blankList = new Array();
		var totalHeight = 0;
		
		var i;
		var firstAry = new Array();
		for(i=0; i<colMax; i++) {
			firstAry.push(0);
		}
		blankList.push(firstAry);
		
		
		var ts = (new Date()).getTime();
		
		var len = this.gridNodes.length;
		for(i=0; i<len; i++) {
			var node = this.gridNodes[i];
			var size;
			var point;
			
			if(node.style.display == "none") {
				size = [0, 0];
			}
			else {
				size = this.getSizeIncludeMargin(node);
			}
			
			if(node.style.display == "none") {
				point = [0,0];
			}
			else {
				point = this.getAttachPoint(matrix, size[0]);
				matrix = this.updateAttachArea(matrix, point, size);
				maxHeight = Math.max(maxHeight, point[1] + size[1]);
			}
			

			this.props[i] = {x:point[0], y:point[1]};
			
			
			/*
			var spans = node.getElementsByTagName("span");
			var span;
			
			if(!spans.length) {
				span = document.createElement("span");
				span.style.display = "block";
				span.style.position = "absolute";
				span.style.left = "0";
				span.style.top = "0";
				span.style.background = "#fff";
				span.style.fontSize = "12px";
				span.style.fontFamily = "monospace";
				node.appendChild(span);
			}
			else {
				span = spans[0];
			}
			span.innerHTML = i + " : " + point + "<br>" + matrix;
			*/
		}
		
		//alert((new Date()).getTime() - ts);
		
		
		//var footWidth = w - 15;
		//this.footNode.style.width = footWidth + "px";
		
		this.masterHeight = Math.max(this.MIN_HEIGHT, (maxHeight + 50));
		
		//this.footNode.style.width = "100%";
		
		
		if($(document.body).getSize().y > this.masterHeight) {
			if(this.footNode.style.position != "absolute") {
				this.footNode.style.visibility = "hidden";
			}
			this.footNode.style.position = "absolute";
			this.footNode.style.bottom = "0";
		}
		else {
			if(this.footNode.style.position != "relative") {
				this.footNode.style.visibility = "hidden";
			}
			this.footNode.style.position = "relative";
			this.footNode.style.bottom = "";
		}
	},
	
	
	resetPosition : function() {
		var len = this.gridNodes.length;
		for(var i=0; i<len; i++) {
			var node = this.gridNodes[i];
			node.style.left = "0";
			node.style.top  = "0";
		}
	},


	
	
	
	//----------------------------------------------------------------------------------------------------
	//Direct Fit
	
	fitToGrid : function() {
		var len = this.gridNodes.length;
		for(var i=0; i<len; i++) {
			var node = this.gridNodes[i];
			node.style.left = this.props[i].x + "px";
			node.style.top  = this.props[i].y + "px";
			node.style.visibility = "visible";
		}
		
		//this.masterNode.style.width  = this.masterWidth  + "px";
		this.masterNode.style.height = this.masterHeight + "px";
		this.masterNode.style.left   = this.masterOffsetX + "px";
		
		this.navNode.style.height = this.masterHeight + "px";
		this.navNode.style.left = this.navOffsetX + "px";
		
		//this.fitBgImage(true);
	},
	
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Tween And Fit

	tweenAndFit : function() {
		var isMove = false;
		var i;
		var len;
		var node;
		var self = this;
		
		this.fitBgImage(false);
		
		len = this.gridNodes.length;
		for(i=0; i<len; i++) {
			node = this.gridNodes[i];
			var pos = this.getOffsetPosition(node);
			if(pos.x != this.props[i].x || pos.y != this.props[i].y) {
				isMove = true;
			}
		}
		
			
		if(isMove) {
			var masterFx = new Fx.Morph(this.masterNode, {duration: 400, transition: Fx.Transitions.Sine.easeOut});
			masterFx.start({
				"left"   : this.masterOffsetX,
				//"width"  : this.masterWidth,
				"height" : this.masterHeight
				//"height" : $(document.body).getSize().y - 20
			});
			
			var navFx = new Fx.Morph(this.navNode, {duration: 400, transition: Fx.Transitions.Sine.easeOut});
			navFx.start({
				"left"   : this.navOffsetX,
				"height" : this.masterHeight
			});
			
			
			var lastNode;
			len = this.gridNodes.length;
			for(i=0; i<len; i++) {
				node = this.gridNodes[i];
				if(node.style.display != "none") {
					lastNode = node;
				}
			}
			
			
			var count = 0;
			for(i=0; i<len; i++) {
				node = this.gridNodes[i];
				var isLastNode = Boolean(node === lastNode);
				
				if(node.style.display == "none") {
					continue;
				}
				else {
					var pos = this.getOffsetPosition(node);
					if(pos.x == this.props[i].x && pos.y == this.props[i].y) {
						node.style.visibility = "visible";
						continue;
					}
					
					else {
						count++;
					}
				}
				
				var delay = count * 10;
				if(delay > 2000) {
					delay = 2000;
				}
				
				if(this.ua.match("MSIE")) {
					delay = 0;
				}
				
				var zindex = 0;
				
				var callback = isLastNode ? function(){ self.onTweened(); } : null;
				var onstart  = function(el) {
					el.style.visibility = "visible";
				}
				
				var fx = new Fx.Morph(node, {duration: 500, fps: 60, transition: Fx.Transitions.Sine.easeOut, onComplete: callback, onStart: onstart});
				fx.start.delay(delay, fx, {
					"left" : this.props[i].x,
					"top"  : this.props[i].y
				});
				//node.style.left = this.props[i].x + "px";
				//node.style.top  = this.props[i].y + "px";
			}
		}
	},
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Fit BgImage
	
	fitBgImage : function(isDirect) {
		var bodySize = $(document.body).getSize();
		var bodyWidth = bodySize.x;
		var bodyHeight = bodySize.y;
		var imageWidth = this.bgImageProps.w;
		var imageHeight = this.bgImageProps.h;
		//alert( (imageWidth / imageHeight) + ":" + (bodyWidth / bodyHeight))
		
		
		var w, h;
		
		if( (imageWidth / imageHeight) > (bodyWidth / bodyHeight) ) {
			h = bodyHeight;
			w = Math.floor(imageWidth * (bodyHeight / imageHeight));
		}
		else {
			w = bodyWidth;
			h = Math.floor(imageHeight * (bodyWidth / imageWidth));
		}
		
		
		if(isDirect) {
			this.bgImage.width = w;
			this.bgImage.height = h;
		}
		else {
			var fx = new Fx.Morph(this.bgImage, {duration: 300, transition: Fx.Transitions.Sine.easeOut});
			fx.start({
				"width"  : w,
				"height" : h
			});
		}
	},
	
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//OnTweeed
	
	onTweened : function() {
		this.appearFoot();
		this.isRunnning = false;
	},
	
	
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Init Bg

	initBgNode: function(bglist) {
		var self = this;
		
		this.bgNode = document.createElement("div");
		
		
		if(this.ua.match("MSIE 6")) {
			this.bgNode.style.position = "absolute";
			/*
			window.onscroll = function() {
				var offset = $(window).getScroll().y;
				var img = self.bgNode.getElementsByTagName("img")[0];
				img.style.position = "relative";
				img.style.left = "0";
				img.style.top = offset + "px";
			}
			*/
		}
		else {
			this.bgNode.style.position = "fixed";
		}
		
		this.bgNode.style.top = "0";
		this.bgNode.style.left = "0";
		this.bgNode.style.zIndex = 1;
		this.bgNode.style.width = "100%";
		this.bgNode.style.height = "100%";
		this.bgNode.style.overflow = "hidden";
		
		var len = bglist.length;
		var rnd = Math.floor( Math.random() * len );
		var file = bglist[rnd];
		
		this.bgImage = new Image();
		this.bgImage.className = "";
		this.bgImage.src = "./bg/" + file;
		
		this.bgImage.onload = function() {
			if(this.className == "loaded") {
				return;
			}
			
			this.className = "loaded"
			self.bgImageProps = { w:this.width, h:this.height };
			self.fitBgImage(true);
		}
		
		
		if(this.bgImage.complete) {
			this.bgImage.onload();
		}
		
		
		
		this.bgNode.appendChild(this.bgImage);
		document.body.appendChild(this.bgNode);
	},
	
	
		
		
	//----------------------------------------------------------------------------------------------------
	//Init Nav

	initNav: function() {
		var i;
		var len;
		var self = this;
		this.buttons = this.navNode.getElementsByTagName("a");
		
		var worksNode;
		
		len = this.buttons.length;
		for(i=0; i<len; i++) {
			var button = this.buttons[i];
			
			if(button.getAttribute("href", 2) == "/#/works") {
				//alert("button:"+button);
				this.worksButton = button;
				//worksの入っているulのブロックをworksNodeに
				worksNode = button.parentNode.getElementsByTagName("ul")[0];
				//alert(worksNode.className);
				this.worksMenuInner = $(worksNode).getSize().y;
				//alert("this.worksMenuInner:"+this.worksMenuInner);
				button.href = "javascript:void(0);";
				//button.onclick = function() {
				this.worksButton.onclick = function() {
					self.toggleWorksMenu();
				}
			}
			/*  koyama profileのボタン */
			if(button.getAttribute("href",2) == "/#/profile") {
				
				this.profileButton = button;
				//alert(button);
				//profileNode = button.parentNode.getElementsByTagName("ul")[0];
				//alert(profileNode);
				var href = this.profileButton.getAttribute("href", 2);
				
				var rel = this.profileButton.href.replace(/.*\/#\/(.+)$/, "$1");
				//alert("profile rel:"+rel);
				button.rel = rel;
				button.href = "javascript:void(0);";
				this.profileButton.onclick = function() {
					if(!this.className.match("active")){
						self.changeAddress(this);
						//alert("profile changeAddress");
					}
				}
			}
			if(button.getAttribute("href",2) == "/#/service") {
				
				this.serviceButton = button;
				//alert(button);
				//serviceNode = button.parentNode.getElementsByTagName("ul")[0];
				//alert(serviceNode);
				var href = this.serviceButton.getAttribute("href", 2);
				
				var rel = this.serviceButton.href.replace(/.*\/#\/(.+)$/, "$1");
				//alert("service rel:"+rel);
				button.rel = rel;
				button.href = "javascript:void(0);";
				this.serviceButton.onclick = function() {
					if(!this.className.match("active")){
						self.changeAddress(this);
						//alert("service changeAddress");
					}
				}
			}
			
			if(button.getAttribute("href",2) == "/#/contact") {
				
				this.contactButton = button;
				//alert(button);
				//profileNode = button.parentNode.getElementsByTagName("ul")[0];
				//alert(profileNode);
				var href = this.contactButton.getAttribute("href", 2);
				
				var rel = this.contactButton.href.replace(/.*\/#\/(.+)$/, "$1");
				//alert("contact rel:"+rel);
				button.rel = rel;
				button.href = "javascript:void(0);";
				this.contactButton.onclick = function() {
					if(!this.className.match("active")){
						self.changeAddress(this);
						//alert("contact changeAddress");
					}
				}
			}
			/*  koyama */
		}
		
		var worksMenus = worksNode.getElementsByTagName("a");
		
		len = worksMenus.length;
		for(i=0; i<len; i++) {
			var node = worksMenus[i];
			var href = node.getAttribute("href", 2);
			//alert("href:"+href);
			var rel = node.href.replace(/.*\/#\/(.+)$/, "$1");
			//alert("rel:"+rel);
			node.href = "javascript:void(0);";
			node.rel = rel;
			node.onclick = function() {
				if(!this.className.match("active")) {
					self.changeAddress(this);
				}
			}
		}
		
	},
	
	
	
	toggleWorksMenu : function(mode) {
		var self = this;
		var menu = this.worksButton.parentNode.getElementsByTagName("ul")[0];
		

		if(!mode) {
			mode = (this.worksMenuState != "show") ? "show" : "hide";
			//alert("mode:"+mode);
		}
		
		if(mode == "show" && this.worksMenuState != "show") {
			this.isMenuOpenedOnce = true;
			this.worksMenuState = "show";
			this.worksButton.className = "active";
				
			menu.style.height = "0";
			menu.style.display = "block";
			
			if(this.isInited) {
				var fx = new Fx.Morph(menu, {duration: 400, transition: Fx.Transitions.Sine.easeOut});
				fx.start({
					"height" : this.worksMenuInner
				});
				this.changeAddress({rel:"works"});
			}
			else {
				menu.style.height = this.worksMenuInner + "px";
			}
		}
		
		if(mode == "hide" && this.worksMenuState != "hide") {
			this.worksMenuState = "hide";
			this.worksButton.className = "";
			
			if(this.isMenuOpenedOnce) {		
				//var fx = new Fx.Morph(menu, {duration: 200, transition: Fx.Transitions.Sine.easeOut, onComplete: onclosed });
				var fx = new Fx.Morph(menu, {duration: 200, transition: Fx.Transitions.Sine.easeOut });
				fx.start({
					"height" : 0
				});
			}
			else {
				menu.style.height = "0";
				menu.style.display = "none";
			}
		}
	
		
		function onclosed() {
			//alert("onclosed");
			menu.style.display = "none";
			self.changeAddress({rel:""});
		}
	},
	
	
	
	
	
	
	changeAddress : function(elem) {
		var key = elem.rel;
		var hash = "";
		if(key && key != this.activeCategory) {
			hash = "/" + key;
			//alert("changeAddress-hash:"+ hash);
		}
		location.hash = hash;
	},
	
	
	
	
	onChangeAddress : function(hash) {
		$makedocument.writeLastAccessData(location.href);
		
		var key = hash.replace(/.*\/(.*)/, "$1");
		//alert("ChangeAddress-key:"+key);
		this.refineBlocks(key);
		
		if(key | key == "profile" | key == "contact" | key == "service"){		//ここでメニュのWorksの時は詳細オープン、それ以外は詳細閉じる、その他は一個一個指定
			this.toggleWorksMenu("hide");
		} else if(key) {
			this.toggleWorksMenu("show");
			this.worksButton.className = "active";
			if(key == "works") {
				SWFAddress.setTitle(this.BASE_TITLE + " | works");
			}
			else {
				SWFAddress.setTitle(this.BASE_TITLE + " | works | " + key.toUpperCase());
			}
		}
		else {
			this.toggleWorksMenu("hide");
			this.worksButton.className = "";
			SWFAddress.setTitle(this.BASE_TITLE);
		}
		
		
		if(!this.isInited) {
			this.scrollToPrevPosition();
			this.isInited = true;
		}
		
	},
	
	
	
	scrollToPrevPosition : function() {
		var offset = $makedocument.reaaScrollOffset();
		if(offset) {
			window.scrollTo(0, offset);
		}
	},
	
	
	
	
	refineBlocks : function(key) {
		//alert("befor activeCategory:"+this.activeCategory);
		if(key == this.activeCategory) {
			this.activeCategory = "all";
		}
		else if(key == "works") {
			this.activeCategory = "works";
		}
		else if(key == "") {
			this.activeCategory = "all";
		}
		else {
			this.activeCategory = key;
		}
		
		//alert("activeCategory:"+this.activeCategory);
		var i;
		var len;
		
		len = this.buttons.length;
		//alert("len:"+len);
		for(i=0; i<len; i++) {
			var button = this.buttons[i];
			//alert("this.activeCategory:"+this.activeCategory);
			//if(this.activeCategory !== ""){ 	//activeCategory == "" は最初の読み込みの時、button全てOff
				//alert("button.rel:"+ button.rel);
				if(button.rel && (button.rel.match(this.activeCategory) && this.activeCategory != "works") && this.activeCategory != "all") {
				//if(button.rel && (button.rel.match(this.activeCategory))) {
					$(button).addClass("active");
					//alert("button.className:"+button.rel+"-"+button.className);
				} else {
					$(button).removeClass("active");
					//alert("removeClass1");
					
				}
			//} else {
				//$(button).removeClass("active");
				//alert("removeClass2");
			//}
			
		}
		
		/* 条件にあったものをdisplay="block"で　条件に合わないものをdisplay="none"で 表示するブロックを「選んで」いる */
		len = this.gridNodes.length;
		for(i=0; i<len; i++) {
			var node = this.gridNodes[i];
			if(this.activeCategory == "all" || (this.activeCategory == "works" && node.className.match("works")) || node.className.match(this.activeCategory)) {
				node.style.display = "block";
				node.style.visibility = "hidden";
			}
			else {
				node.style.display = "none";
			}
		}
		
		
		this.setPosition();
		
		if(this.isInited) {
			clearTimeout(this.resizeTimer);
			this.isRunnning = true;
			this.resetPosition();
			this.tweenAndFit();
		}
		else {
			this.fitToGrid();
			document.body.style.visibility = "visible";
			this.appearFoot();
		}
	},
	
	
	
	appearFoot : function() {
		if(this.footNode.style.visibility != "hidden") {
			return;
		}
		
		this.footNode.style.visibility = "visible";
		
		var fh = $(this.footNode).getSize().y;
		var inner = this.footNode.getElementsByTagName("*")[0];
		inner.style.top =  fh + "px";
		
		var fx = new Fx.Morph(inner, {duration: 400, transition: Fx.Transitions.Sine.easeOut});
		fx.start.delay(200, fx, {
			"top"  : 0
		});
	},
	
	




	//----------------------------------------------------------------------------------------------------
	//onResize
	
	resizeHandler : function() {
		clearTimeout(this.resizeTimer);
		
		this.isRunnning = true;
		this.setPosition();
		
		var self = this;
		
		this.resizeTimer = setTimeout(function(){
				self.tweenAndFit();
			},
		200);
	},
	
	

	//----------------------------------------------------------------------------------------------------
	//onScroll
	
	scrollHandler : function() {
		clearTimeout(this.scrollTimer);
		this.scrollTimer = setTimeout(function(){
				$makedocument.writeScrollOffset();
			},
		200);
	},
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//OnFontSizeChange
	
	setFontSizeListener : function() {
		var self = this;
		
		var style = document.body.currentStyle || document.defaultView.getComputedStyle(document.body, "");
		this.fontSizeBuffer = style.fontSize;
		
		
		this.fontSizeTimer = setInterval(function(){
				var s = document.body.currentStyle || document.defaultView.getComputedStyle(document.body, "");
				if(self.fontSizeBuffer == s.fontSize) {
					return;
				}
				
				if(self.isRunnning) {
					return;
				}
				
				self.fontSizeBuffer = s.fontSize;
				
				self.setPosition();
				self.tweenAndFit();
			},
		2000);
	},
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//----------------------------------------------------------------------------------------------------
	//Math Funcs
	
	getSizeIncludeMargin : function(elem) {
		var prop = $(elem).getSize();
		var ary = [prop.x + this.MARGIN, prop.y + this.MARGIN];
		return ary;
	},
	
	
	
	getOffsetPosition : function(elem) {
		var pos = $(elem).getPosition();
		var obj = {x:pos.x - this.masterOffsetX, y:pos.y};
		return obj;
	},
	
	
	
	
	
	
	
	getAttachPoint : function(mtx, width) {
		var _mtx = mtx.concat().sort(this.matrixSortDepth);
		var _max = _mtx[_mtx.length-1][2];
	
		for(var i=0,imax=_mtx.length; i<imax; i++) {
			if(_mtx[i][2] >= _max)  {
				break;
			}
			
			if(_mtx[i][1] - _mtx[i][0] >= width) {
				return [_mtx[i][0], _mtx[i][2]];
			}
		}
		return [0, _max];
	},
	
	
	
	updateAttachArea : function(mtx, point, size) {
		var _mtx = mtx.concat().sort(this.matrixSortDepth);
		var _cell = [point[0], point[0] + size[0], point[1] + size[1]];
		for(var i=0,imax=_mtx.length; i<imax; i++) {
			if(_cell[0] <= _mtx[i][0] && _mtx[i][1] <= _cell[1]) {
				delete _mtx[i];
			}
			else {
				_mtx[i] = this.matrixTrimWidth(_mtx[i], _cell);
			}
		}
		return this.matrixJoin(_mtx, _cell);
	},
		
		
		
	matrixSortDepth : function(a, b) {
		if(!a || !b) return 0;
		return ((a[2] == b[2] && a[0] > b[0]) || a[2] > b[2]) ? 1 : -1;
	},
		
		
		
	matrixSortX : function(a, b) {
		if(!a || !b) return 0;
		return (a[0] > b[0]) ? 1 : -1;
	},
		
		
		
	matrixJoin : function(mtx, cell) {
		var _mtx = mtx.concat([cell]).sort(this.matrixSortX);
		var _mtx_join = [];
		for(var i=0,imax=_mtx.length; i<imax; i++) {
			if(!_mtx[i]) {
				continue;
			}
			
			if(_mtx_join.length > 0
				&& _mtx_join[_mtx_join.length-1][1] == _mtx[i][0]
				&& _mtx_join[_mtx_join.length-1][2] == _mtx[i][2]) {
				_mtx_join[_mtx_join.length-1][1] = _mtx[i][1];
			}
			else {
				_mtx_join.push(_mtx[i]);
			}
		}
		return _mtx_join;
	},
		
		
		
	matrixTrimWidth : function(a, b) {
		if(a[0] >= b[0] && a[0] < b[1] || a[1] >= b[0] && a[1] < b[1]) {
			if(a[0] >= b[0] && a[0] < b[1]) {
				a[0] = b[1];
			}
			else {
				a[1] = b[0];
			}
		}
		return a;
	}




});





