//Generic Image Frame Collection

var imgfrm_imageframe = function (ver, hor, verpos_contents, horpos_contents)
{
	//Own properties
	this.ver = ver;
	this.hor = hor;
	this.verpos_contents = verpos_contents;
	this.horpos_contents = horpos_contents;
	
	//Array of images of frames
	this.img = [];
	for (var i=0; i<this.ver; i++)
	{
		this.img[i] = [];
	}
	
	//Arrays containing the dimensions of each row and cols for ratioing purpose
	this.img_width = [];
	this.img_height = [];
	
	//Assign imagepath to array
	this.set_img = function(verpos, horpos, img_path)
	{
		//if (typeof this.img[verpos] == "undefined")
		//{
		//	this.img[verpos] = new Array();
		//}
		
		this.img[verpos][horpos] = img_path;
	}
	
	//Assign image width to array
	this.set_width = function(horpos, width)
	{
		this.img_width[horpos] = width;
	}
	
	//Assign image height to array
	this.set_height = function(verpos, height)
	{
		this.img_height[verpos] = height;
	}
	
	
	//Prints imageframe
	this.print_frame = function(id, img_path, contents_width, contents_height, frame_width, contents)
	{
		var return_value = "";
		
		//Set Calculations for reference width
		var frame_reference_width = ( (typeof this.img[this.verpos_contents][this.horpos_contents-1]) == "undefined" ) ? this.img_width[this.horpos_contents+1] : this.img_width[this.horpos_contents-1];
		var frame_ratio = frame_width / frame_reference_width;
		
		
		var table_height = 0;
		var table_width = 0;
		
		for (var i = 0; i<this.ver; i++)
		{
			current_height = (i == this.verpos_contents) ? contents_height : Math.round(this.img_height[i] * frame_ratio);
			table_height += current_height;
			
			return_value += "\n  <tr id='tr_imageframe_"+id+"_"+i+"'>";
			
			for (var j = 0; j<this.hor; j++)
			{
				current_width = (j == this.horpos_contents) ? contents_width : Math.round(this.img_width[j] * frame_ratio);
				
				//Count the table_width only if its the first row
				if (i == 0)
				{
					table_width += current_width;
				}
				
				//alert(i + "  " + j + " " + this.img[i][j]);
				
				//if its the contents cell
				if ((i==this.verpos_contents) && (j==this.horpos_contents))
				{
					return_value += "\n    <td id='td_imageframe_"+id+"_"+i+"_"+j+"' width='"+current_width+"' height='"+current_height+"'  style='background-image:url("+img_path+this.img[i][j]+"); background-repeat:repeat;'>";
					//Put the contents in
					if ((contents.length >= 5) && (contents.lastIndexOf(".") == contents.length-4))
					{
						var extension = contents.substring(contents.length-4, contents.length);
						switch (extension.toLowerCase())
						{
						case ".jpg":
						case ".gif":
						case ".png":
							contents = "<img id='img_imageframe_contents_"+id+"' width='"+current_width+"' height='"+current_height+"' src='"+contents+"'/>";
							break;
						default:
							//Just print contents
							//contents = contents;
						}
					} else {
						//Just print contents
						//contents = contents;
					}
					return_value += "\n      "+contents;
				} else {
					//otherwise print the frame
					return_value += "\n    <td id='td_imageframe_"+id+"_"+i+"_"+j+"' width='"+current_width+"' height='"+current_height+"'>";
					return_value += "\n      <img id='img_imageframe_"+id+"_"+i+"_"+j+"' width='"+current_width+"' height='"+current_height+"' src='"+img_path+this.img[i][j]+"'/>";
				}
				
				return_value += "</td>";
			}
			
			return_value += "\n  </tr>";
		}
		
		return_value = "<table id='tb_imageframe_"+id+"' cellpadding='0' cellspacing='0' border='0' width='"+table_width+"' height='"+table_height+"' style='table-layout:fixed;'>" + return_value;
		
		return_value += "\n</table>";
		//alert(return_value);

		return return_value;
	}
	
	//Adds image frame to existing element
	this.add_frame = function(element, id, img_path, contents_width, contents_height, frame_width)
	{
		//alert(element.id + " " + element.clientWidth + " " + element.clientHeight);
		element.innerHTML = this.print_frame(id, img_path, contents_width, contents_height, frame_width, element.innerHTML);
	}
	
	//Adds image frame to more than one existing elements
	this.add_frame_multi = function (elements, id_prefix, img_path, frame_width)
	{
		switch (typeof elements)
		{
			case "string":
				elements = elements.split(" ");
			case "object":
				for (var i in elements)
				{
					//Check if element exists
					if (document.getElementById(elements[i]))
					{
						this.add_frame(document.getElementById(elements[i]), id_prefix+"_"+i, img_path, document.getElementById(elements[i]).clientWidth, document.getElementById(elements[i]).clientHeight, frame_width);
					}
				}
				break;
		}
	}
}
