/*@FILE INFORMATION
----------------------------------------
Author:		Mark Wise 
File:		Block_1.0.4.js 
Created:	12/9/09 3:59 PM
Updated:	1/20/10 5:34 PM

/*@END--------------------------------*/


var Block = new Class({
	
	//CONSTRUCTOR
	initialize:function(ele){
		//Is the argument an element or a string
		this.block = ($type(ele) == "string")? $(ele) : ele;
			
		//Make sure element exists
		if(this.block){
			
			//Corner elements
			this.cornerTopLeft = new Element("div", {"class":"corner_top_left"}).inject(this.block);
			this.cornerTopRight = new Element("div", {"class":"corner_top_right"}).inject(this.block);
			this.cornerBottomRight = new Element("div", {"class":"corner_bottom_right"}).inject(this.block);
			this.cornerBottomLeft = new Element("div", {"class":"corner_bottom_left"}).inject(this.block);
			
			//Side elements
			this.sideTop = new Element("div", {"class":"side_top"}).inject(this.block);
			this.sideRight = new Element("div", {"class":"side_right"}).inject(this.block);
			this.sideBottom = new Element("div", {"class":"side_bottom"}).inject(this.block);
			this.sideLeft = new Element("div", {"class":"side_left"}).inject(this.block);
			
			//Content elements
			this.outsetContent = new Element("div", {"class":"outset_content"}).inject(this.block);
			this.insetContent = new Element("div", {"class":"inset_content"}).inject(this.outsetContent).grab(this.block.getFirst());
				
			//IE6 Fix
			this.updateIE6();
			
			//Return class reference so a single method can be chained
			return this;
					
		}
		
	},
	
	
	//RETURNS THE ELEMENT WHOSE ID WAS PASSED IN AS AN ARGUMENT TO THE CONSTRUCTOR
	getElement:function(){
		return this.block;
		
	},
	
	
	//IE6 1PX BUG FIX; ADJUST WIDTH AND HEIGHT OF SIDE ELEMENTS
	updateIE6:function(){
		if(navigator.userAgent.indexOf("MSIE 6") != -1){
					
			//Create class reference
			var $this = this;
					
			//Create an object to loop through
			var obj = {
				sideTopRight:    $this.sideTop.getStyle("right"),
				sideTopLeft:     $this.sideTop.getStyle("left"),
				sideRightTop:    $this.sideRight.getStyle("top"),
				sideRightBottom: $this.sideRight.getStyle("bottom"),
				sideBottomRight: $this.sideBottom.getStyle("right"),
				sideBottomLeft:  $this.sideBottom.getStyle("left"),
				sideLeftTop:     $this.sideLeft.getStyle("top"),
				sideLeftBottom:  $this.sideLeft.getStyle("bottom")
													
			}
			
			//Convert string values to integers or 0 if value is "auto"
			$each(obj, function(item, index, object){
				object[index] = (!$type(item.toInt()))? 0 : item.toInt();
																													
			});
						
			//outsetContentWidth; IE6 1px Bug Fix; Width cannot be an odd value
			//http://www.pmob.co.uk/temp/onepxgap.htm
			var ocw = this.block.getElement(".outset_content").getStyle("width").toInt();
			$this.outsetContent.setStyle("width", (ocw % 2 > 0)? ocw + 1 : ocw);
			
			//outsetContentHeight; IE6 1px Bug Fix; Height cannot be an odd value
			//http://www.pmob.co.uk/temp/onepxgap.htm
			var och = this.block.getElement(".outset_content").getStyle("height").toInt();
			$this.outsetContent.setStyle("height", (och % 2 > 0)? och + 1 : och);
					
			//sideTopWidth; If number is negative set to 0
			var stw = this.block.getSize().x - (obj.sideTopLeft + obj.sideTopRight);
			if(stw < 0) stw = 0;
							
			//sideRightHeight; If number is negative set to 0
			var srh = this.block.getSize().y - (obj.sideRightTop + obj.sideRightBottom);
			if(srh < 0) srh = 0;
					
			//sideBottomWidth; If number is negative set to 0
			var sbw = this.block.getSize().x - (obj.sideBottomLeft + obj.sideBottomRight);
			if(sbw < 0) sbw = 0;
					
			//sideLeftHeight; If number is negative set to 0
			var slh = this.block.getSize().y - (obj.sideLeftTop + obj.sideLeftBottom);
			if(slh < 0) slh = 0;
					
			//Set Values
			$this.sideTop.setStyle("width", stw);
			$this.sideRight.setStyle("height", srh);
			$this.sideBottom.setStyle("width", sbw);
			$this.sideLeft.setStyle("height", slh);
								
		}
			
	}

});