/*
hk_slice.init(
	id_modal_tag:				string,
	default_position:			[object|numeric],
	minimum_position:		numeric,
	background:				color,
	snap_type:					string,
	ads_size:						numeric,
	show_close:				boolean,
	path_img_close:			string
);
********************************************************************
id_modal_tag = id ของ tag ต้นแบบ
******************************
default_position = ตำแหน่งเริ่มต้นของ ad
	รูปแบบ default_position = {x: [ตำแหน่งแกนนอน | หรือ object สำหรับ snap แกนนอน | หรือ 'window' สำหรับ browser ], Y: [ตำแหน่งแกนตั้ง | หรือ object สำหรับ snap แกนตั้ง | หรือ 'window' สำหรับ browser ]}
		ตัวอย่าง = {x: 150, y: 200}
		หรือ = {x: document.getElementById('x_object'), y: document.getElementById('y_object')}
		หรือ = {x: 'window', y: 'window')}
******************************
minimum_position = ตำแหน่งน้อยที่สุดของ ad หรือ ในกรณีที่ default_position='window' จะเป็น ระยะห่างจากขอบ browser
	รูปแบบ minimum_position = {x: ตำแหน่งแกนนอน, Y: ตำแหน่งแกนตั้ง}
		ตัวอย่าง = {x: 30, y: 30}
******************************
background = สีพื้น
******************************
snap_type = รูปแบบการวาง ad ไว้ใน default_position กรณีที่เป้น object
	รูปแบบ snap_type = {x: รูปแบบของแกนนอน, y: รูปแบบของแกนตั้ง}
		แกนX = ['left'(ด้านซ้ายใน ออปเจค) | 'center'(กลาง ออปเจค) | 'right'(ด้านขวาใน ออปเจค) | 'outLeft'(ด้านซ้ายนอก ออปเจค) | 'outRight'(ด้านขวานอก ออปเจค) | 'winCenter'(กลาง browser)|'winLeft'(ด้านซ้าย browser) | 'winRight'(ด้านขวา browser)]
		แกนY = ['top'(ดา้นบนใน ออปเจค) | 'middle'(กลาง ออปเจค) | 'bottom'(ด้านล่างใน ออปเจค) | 'outTop'(ด้านบนนอก ออปเจค)| 'outBottom'(ด้านล่างนอก ออปเจค)| 'winMiddle'(กลาง browser)|'winTop'(ด้านบน browser) | 'winBottom'(ด้านล่าง browser)]
******************************
ads_size = ขนาดความกว้าง/ยาวของ ad มีรูปแบบ ads_size = {w: size_width, h: size_height}
********************************************************************
********************************************************************
ตัวอย่างสำหรับ TAG DIV มี id=hk_id
<div id='hk_id' style="width: 300; height: 1; background-color: transparent; visibility: hidden;"><tag โฆษณา /></div>
/////////////////////////////////////////////////////////
สำหรับ javascript
ให้ใส่ไว้หลัง TAG DIV Block สุดท้าย ที่ต้องการ (ถ้าให้ดีก็ ก่อน Tag ปิด </body> 1 บรรทัด ชัวร์ว่าสุดท้ายแน่ๆ)

<SCRIPT SRC="hk_slice.js" language="JavaScript1.2"></SCRIPT>
<script language="JavaScript">
<!--
var x_object = document.getElementById('main_tb');
var y_object = document.getElementById('menu_area');
hk_slice.init('slide_ads_right',{x:x_object, y:y_object}, { x:30, y:30}, 'black', {x:'outRight', y:'top'}, {w:120, h:430}, true, 'img/' );
//-->
</script>
////////////////////////////////////////////////////////
*/
var array_div = new Array();
function makeStatic(idx) {
	if (array_div[idx].active) {
		set_div_top( array_div[idx]);
		setTimeout('makeStatic("'+idx+'")', 10)
	}else {
		//setTimeout( 'array_div["'+idx+'"].basessm.innerHTML="";', 1);
		array_div[idx].basessm.innerHTML="";
		//array_div[\''+pid+'\'].active=false;
	}
}
var cast_test = false;
var count_test = 0;
function set_div_top(obj){
	var cur_top=hk_slice.IE? obj.bssm.pixelTop : parseInt( obj.bssm.top);
	var o_top=parseInt(obj.y()), o_left=parseInt(obj.x());
	var scroll = hk_slice.get_scroll();
	var winY=scroll.top;
	var new_top=0, move_top=0, new_left = 0, move_left=0;
	if (obj._pos.y=='window'){
		new_top=o_top;
		if(obj.id=='xfloating'){
			hk_slice.O('test_move').innerHTML=obj.id+'||o_top:'+o_top+'||new_top:'+new_top+'||cur_top:'+cur_top +'||windowHeight:'+hk_slice.browser_size().h;
			//alert(hk_slice.browser_size().h + ':o_top:'+o_top);
		}
		if (cur_top!=new_top)	 {
			move_top=new_top - cur_top;
			var sub_move = move_top>0 ? Math.ceil(move_top * .2) : Math.floor( move_top * .2);
			cur_top+=sub_move;
			if (hk_slice.IE) { obj.bssm.pixelTop=cur_top;}else{ obj.bssm.top=cur_top+"px";}
		}
	}else{
		if(o_top>(winY + obj._min.y)) new_top=o_top;
		else new_top=winY + obj._min.y;
		if (cur_top!=new_top)	 {
			move_top=new_top - cur_top;
			var sub_move = move_top>0 ? Math.ceil(move_top * .2) : Math.floor( move_top * .2);
			cur_top+=sub_move;
			if (hk_slice.IE) { obj.bssm.pixelTop=cur_top;}else{ obj.bssm.top=cur_top+"px";}
		}
	}
	var winX=obj.x();
	if (hk_slice.IE) { obj.bssm.pixelLeft=winX; }else{ obj.bssm.left=winX+"px"; }
}
///////////////////////////////////////////////////////////
var hk_slice = {
	IE: false,
	NS: false,
	Body: false,
	create: function() {
		this.IE = (document.all);
		this.NS = (document.getElementById&&!this.IE);
		if (this.IE) this.Body=(document.compatMode!="BackCompat")? document.documentElement : document.body;
	}, ///////////////////////////////////////////////////////////
	browser_size: function(){
		//w_W=document.all?document.body.clientWidth:window.innerWidth;
		//w_H=document.all?document.body.clientHeight:window.innerHeight;
		w_W=this.IE?this.Body.clientWidth:window.innerWidth;
		w_H=this.IE?this.Body.clientHeight:window.innerHeight;
		return { w: w_W, h: w_H};
	}, ///////////////////////////////////////////////////////////
	O: function(id){
		var o=this.Ox(id);
		if (o) return o; else{
			if (this.IE) o=document.getElementByName(id);
			else {
				var x=typeof(document.getElementsByName);
				if (x!='object') alert(x);
				else o=document.getElementsByName(id);
			}
			if(o) return o; else return null;
		}
	}, ///////////////////////////////////////////////////////////
	Ox: function(id){
		var o=document.getElementById(id);
		if(o) return o; else return null;
	}, ///////////////////////////////////////////////////////////
	get_page: function() {
		var pos={w: 0, h: 0}
		if (this.IE) { pos.h = this.Body.clientHeight; pos.w = this.Body.clientWidth}
		else { pos.h = window.innerHeight; pos.w=window.innerWidth;}
		return pos;
	}, ///////////////////////////////////////////////////////////
	get_scroll: function() {
		var pos={top: 0, left: 0}
		if (this.IE) { pos.top=this.Body.scrollTop; pos.left=this.Body.scrollLeft;}
		else {pos.top=window.pageYOffset; pos.left=window.pageXOffset;}
		return pos;
	}, ///////////////////////////////
	get_pos: function(obj) {
		if (obj){
			var pos = {x: obj.offsetLeft||0, y: obj.offsetTop||0, w: obj.offsetWidth-obj.offsetLeft, h: obj.offsetHeight-obj.offsetTop};
			while(obj = obj.offsetParent) { pos.x += obj.offsetLeft||0; pos.y += obj.offsetTop||0; }
			return pos;
		} else return { x:0, y:0, w:0, h:0};
	}, ///////////////////////////////////////////////////////////
	set_div: function(obj){
		if (hk_slice.NS){
			obj.thessm=this.O("thessm_"+obj.id);
			obj.basessm=this.O("basessm_"+obj.id);
			obj.ssm=obj.thessm.style;
			obj.bssm=obj.basessm.style;
			obj.bssm.clip="rect(0px "+obj.thessm.offsetWidth+"px "+obj.thessm.offsetHeight+"px 0px)";
			obj.ssm.visibility="visible";
		}else if (hk_slice.IE) {
			obj.thessm=this.O("thessm_"+obj.id);
			obj.basessm=this.O("basessm_"+obj.id);
			obj.ssm=obj.thessm.style;
			obj.bssm=obj.basessm.style;
			obj.bssm.clip="rect(0px "+obj.thessm.offsetWidth+"px "+obj.thessm.offsetHeight+"px 0px)";
			obj.bssm.visibility = "visible";
		}
	},
	initSlide: function (idx) {
		this.set_div(array_div[idx]);
		makeStatic(idx);
	}, ///////////////////////////////////////////////////
	init: function (pid, t_pos, t_min, slideBGColor, t_align, t_size, show_close, path) {
		if (this.IE||this.NS) {
			array_div[pid] = {
				id: 0, active: true, _pos: {x:0, y: 0} , _min: {x: 0, y: 0}, _align: {x: '', y: ''}, _size: {w: 0, h: 0},
				thessm: '', basessm: '', bssm: '', bssm2: '', ssm: '',
				init: function(o_id, o_pos, o_min, o_align, o_size){
						this.id=o_id; this._pos=o_pos; this._min=o_min; this._align=o_align; this._size=o_size;
				},
				x: function(){
					var Xwin=0;
					if (this._pos.x=='window') {
						var ob_pos=hk_slice.browser_size();
						var x_sc=hk_slice.get_scroll().left;
						switch (this._align.x)	{
							case 'winLeft': Xwin=Math.ceil(x_sc + this._min.x); break;
							case 'winRight': Xwin=Math.floor(x_sc + ob_pos.w - (this._min.x + this._size.w + 20)); break;
							case 'winCenter': Xwin=Math.floor(x_sc + ((ob_pos.w - this._size.w) / 2)); break;
						}
					}else
					if (typeof(this._pos.x)=='object')	{
						var ob_pos=hk_slice.get_pos(this._pos.x);
						switch (this._align.x)	{
							case 'left': Xwin=Math.ceil(ob_pos.x + 2); break;
							case 'right': Xwin=Math.floor(ob_pos.x + ob_pos.w - this._size.w - 2); break;
							case 'outLeft': Xwin=Math.ceil(ob_pos.x - 2 - this._size.x); break;
							case 'outRight': Xwin=Math.floor(ob_pos.x + ob_pos.w + 2); break;
							case 'center':
							default: Xwin=Math.floor(ob_pos.x + ((ob_pos.w - this._size.w) / 2));  break;
						}
					}else Xwin = this._pos.x;
					return Xwin;
				},
				y: function(){
					var Ywin=0;
					if (this._pos.y=='window') {
						var ob_pos=hk_slice.browser_size();
						var y_sc=hk_slice.get_scroll().top;
						switch (this._align.y)	{
							case 'winTop': Ywin=Math.ceil(y_sc + this._min.y); break;
							case 'winBottom': Ywin=Math.floor(y_sc + ob_pos.h - (this._min.y + this._size.h + 10));
								if(this.id=='xfloating'){
									hk_slice.O('test_move').innerHTML=this.id+'||Y_win:'+Ywin+'||y_sc:'+y_sc+'||ob_pos.h:'+ob_pos.h;
								}
								break;
							case 'winMiddle': Ywin=Math.floor(y_sc + ((ob_pos.h - this._size.h) / 2) - 11); break;
						}
					}else
					if (typeof(this._pos.y)=='object')	{
						var ob_pos=hk_slice.get_pos(this._pos.y);
						switch (this._align.y) {
						case 'top': Ywin=Math.ceil(ob_pos.y + 2); break;
						case 'bottom': Ywin=Math.floor(ob_pos.y + ob_pos.h - this._size.h - 2); break;
						case 'outTop': Ywin=Math.ceil(ob_pos.y - 2 - this._size.h); break;
						case 'outBottom': Ywin=Math.floor(ob_pos.y + ob_pos.h + this._size.h + 2); break;
						case 'middle':
						default: Ywin=Math.floor(ob_pos.y + ((ob_pos.h - this._size.h) / 2)); break;
						}
					}else Ywin = this._pos.y;
					return Ywin;
				},
				hide: function(){
					this.basessm.style.visibility='hidden'; this.active=false; this.basessm.innerHTML='';
				},
				test_w: ''
			}
			array_div[pid].init(pid, t_pos, t_min, t_align, t_size);
			var msg=''; o_left=array_div[pid].x(); o_top=array_div[pid].y();
			msg =
			'<DIV ID="basessm_'+pid+'"'+' style="Position : Absolute ; Left : '+o_left+'px ;Top : '+o_top+'px ;Z-Index : 999;">'+"\n"+
			'<DIV ID="thessm_'+pid+'" '+' style="Position : Absolute ;Top : 0 ;Z-Index : 999;" >'+"\n"+
				(show_close? '<div style="font-size: 10; text-align: right; background: transparent">'+"\n"+
				'<IMG SRC="'+path+'bt_close.gif" BORDER="0" ALT="" style="cursor: pointer" onclick="array_div[\''+pid+'\'].hide();" '+
				'onmouseover="this.src=\''+path+'bt_close_over.gif\';" onmouseout="this.src=\''+path+'bt_close.gif\';"></div>'+"\n" : '');
			msg+='<table border=0 cellpadding=0 cellspacing=0 '+(slideBGColor? 'style="background-color:'+slideBGColor+'"': '')+'>'+"\n";
			msg+='<tr><td>'+this.O(pid).innerHTML+'</td></tr></table>'+"\n";
			msg+='</DIV></DIV>';
			hk_slice.O(pid).innerHTML=msg;
			this.initSlide(pid);
		}
	},
	test: 'text'
}
hk_slice.create();