/* *********************************************************** */
/* Copyright (c) 2007 pennycms.com. All rights reserved.       */
/*                                                             */
/* This library is free software. You can share and change     */
/* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE */
/* Version 2.1, February 1999.                                 */
/* *********************************************************** */
function PennyFloatGrid ($preferences) {
	this.$grid_table;
	
	this.$id = $preferences["id"];
	if (this.$id && (this.$grid_table = document.getElementById(this.$id))) {
		// ... timer.
	} else {
		return;	// have not element.
	}
	
	this.$title = $preferences["title"] ? $preferences["title"] : "";
	this.$grid_width = $preferences["grid_width"] ? $preferences["grid_width"] : "160";
	this.$img_url = $preferences["img_url"] ? $preferences["img_url"] : "";
	this.$main_img_url = $preferences["main_img_url"] ? $preferences["main_img_url"] : "";
	this.$link_url = $preferences["link_url"] ? $preferences["link_url"] : "";
	
	this.createGrid();
	
//	this.setPosition();
//	setTimeout("setPosition('float_grid', 240)", 20);
//	window.onload=this.setPosition;
//	window.onscroll=this.setPosition;
//	window.onresize=this.setPosition;
}

PennyFloatGrid.prototype.createGrid = function () {	// grid.
	var $this = this;
	var $grid_table = this.$grid_table;
	this.setCssStyle($grid_table, "position:absolute;width:"+this.$grid_width+";");
	
	var $grid = document.createElement("div");
	this.setCssStyle($grid, "position:absolute;width:"+this.$grid_width+";");
	$grid_table.appendChild($grid);
	
	var $top = document.createElement("div");
	this.setCssStyle($top, "font-size:9pt;background:url("+this.$img_url+"top.gif);background-color:#fffff;font-family:MS SONG,SimSun,tahoma,sans-serif;height:20px;display:block;white-space:nowrap;width:"+this.$grid_width+";");
	$grid.appendChild($top);
	
	var $top_left = document.createElement("div");
	this.setCssStyle($top_left, "float:left; padding:3px 0px 0px 5px; font-weight:bold; color:#ffffff;");
	$top.appendChild($top_left);
	
	$top_left.appendChild(document.createTextNode(this.$title));
	
	var $top_right = document.createElement("div");
	this.setCssStyle($top_right, "float:right; padding:1px 5px 0px 5px; text-align:right;");
	$top.appendChild($top_right);
	
	var $minimize_button = document.createElement("img");
	$minimize_button.setAttribute("src", this.$img_url+"minimize_button.gif");
	$minimize_button.onclick = function() {$this.hide($main);};
	$top_right.appendChild($minimize_button);
	
	var $maximize_button = document.createElement("img");
	$maximize_button.setAttribute("src", this.$img_url+"maximize_button.gif");
	$maximize_button.onclick = function() {$this.show($main);};
	$top_right.appendChild($maximize_button);
	
	var $closed_button = document.createElement("img");
	$closed_button.setAttribute("src", this.$img_url+"closed_button.gif");
	$closed_button.onclick = function() {$this.hide($grid_table);};
	$top_right.appendChild($closed_button);
	
	var $main = document.createElement("div");
	$grid.appendChild($main);
	
	var $main_a = document.createElement("a");
	$main_a.setAttribute("href", this.$link_url);
	$main_a.setAttribute("target", "_blank");
	$main.appendChild($main_a);
	
	var $main_button = document.createElement("img");
	$main_button.border="0";
	$main_button.setAttribute("src", this.$img_url+this.$main_img_url);
	$main_a.appendChild($main_button);
}

PennyFloatGrid.prototype.show = function ($obj) {
	$obj.setAttribute("style", "display:inline");
	$obj.style.cssText = "display:inline";
	$obj.style.visibility="visible";
}

PennyFloatGrid.prototype.hide = function ($obj) {
	$obj.setAttribute("style", "display:none");
	$obj.style.cssText = "display:none";
	$obj.style.visibility="hidden";
}

PennyFloatGrid.prototype.setClassName = function ($item, $class_name) {
	if (!$item) return;
	if(document.uniqueID) {		// IE.
		$item.setAttribute("className", $class_name);
	} else {
		$item.setAttribute("class", $class_name);
	}
}

PennyFloatGrid.prototype.setIdName = function ($item, $id_name) {
	if (!$item) return;
	if(document.uniqueID) {		// IE.
		$item.id = $id_name;
	} else {
		$item.setAttribute("id", $id_name);
	}	
}

PennyFloatGrid.prototype.setCssStyle = function ($item, $style_script) {
	if (!$item) return;
	$item.setAttribute("style", $style_script);
	$item.style.cssText = $style_script;
}

PennyFloatGrid.prototype.setPosition = function () {
//	this.$grid_table = document.getElementById($id);
//	var $obj = this.$grid_table;
	var $obj = document.getElementById(this.$id);
	var $wwidth;
	var $wheight;
	var $position;
	var $left_p;
	var $top_p;
//	alert();
	if (document.uniqueID) {	// IE.
		$wwidth = document.body.clientWidth;
		$wheight = document.body.clientHeight;
//		alert(this.$grid_table.offsetHeight);
//		$position = document.body.scrollTop + document.body.clientHeight - 20;
		$position = document.body.scrollTop + document.body.clientHeight;
//alert($position);
		$left_p = $wwidth - this.$grid_width;
		$top_p = this.$grid_width*2;
	} else {
		$wwidth = window.innerWidth;
		$wheight = window.innerHeight;
		
		$position = window.pageYOffset+window.innerHeight-$obj.offsetHeight-15;
		$left_p = $wwidth - this.$grid_width;
		$top_p = 0;
	}
	
//	this.setCssStyle($obj, "left:"+$left_p);
	$obj.style.left = $left_p;
	$obj.style.top = $top_p;
}

function setPosition ($id, $site) {
	var $grid_width = 240;
	var $obj = document.getElementById($id);
	var $wwidth;
	var $wheight;
	var $position;
	var $left_p;
	var $top_p;
	
	if (document.uniqueID) {	// IE.
		$wwidth = document.body.clientWidth;
		
		var scrollPos;
		if (typeof window.pageYOffset != 'undefined') {
			scrollPos = window.pageYOffset;
		} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
			scrollPos = document.documentElement.scrollTop;
		} else if (typeof document.body != 'undefined') {
			scrollPos = document.body.scrollTop;
		}
		
		$wheight = (document.documentElement.clientHeight == 0) ? document.body.clientHeight : document.documentElement.clientHeight;
		
		$left_p = $wwidth - $grid_width;
		$top_p = scrollPos + $wheight - 200;

		if ($site==0) {
			$left_p = 0;
			$top_p = scrollPos;
		} else if ($site==1) {
			$left_p = $wwidth - $grid_width;
			$top_p = scrollPos;
		} else if ($site==2) {
			$left_p = $wwidth - $grid_width;
			$top_p = scrollPos + $wheight - 200;
		} else {
			$left_p = 0;
			$top_p = scrollPos + $wheight - 200;
		}
	} else {
		$wwidth = window.innerWidth;
		$wheight = window.innerHeight;
		
		$position = window.pageYOffset+window.innerHeight-$obj.offsetHeight-15;
		$left_p = $wwidth - $grid_width;
		$top_p = 0;
	}
	
	$obj.style.left = $left_p;
	$obj.style.top = $top_p;
}