/*
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 แกนนอน], Y: [ตำแหน่งแกนตั้ง | หรือ object สำหรับ snap แกนตั้ง]}
		ตัวอย่าง = {x: 150, y: 200}
		หรือ = {x: document.getElementById('x_object'), y: document.getElementById('y_object')}
******************************
minimum_position = ตำแหน่งน้อยที่สุดของ ad 
	รูปแบบ minimum_position = {x: ตำแหน่งแกนนอน, Y: ตำแหน่งแกนตั้ง}
		ตัวอย่าง = {x: 30, y: 30}
******************************
background = สีพื้น
******************************
snap_type = รูปแบบการวาง ad ไว้ใน default_position กรณีที่เป้น object
	รูปแบบ snap_type = {x: รูปแบบของแกนนอน, y: รูปแบบของแกนตั้ง}
		แกนX = ['left'(ชิดขอบซ้ายภายใน) | 'center'(กลาง) | 'right'(ชิดขอบขวาภายใน) | 'snap'(ชิดขอบขวาด้านนอก)|'winleft'(ชิดขอบซ้าย browser) | 'winright'(ชิดขอบขวา browser)] 
		แกนY = ['top'(ชิดขอบบนภายใน) | 'middle'(กลาง) | 'bottom'(ชิดขอบล่างภายใน) | 'snap'(ชิดขอบล่างด้านนอก)|'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:'snap', y:'top'}, {w:120, h:430}, true, 'img/' );
//-->
</script>
////////////////////////////////////////////////////////
*/
function g(id){
	var o=document.getElementById(id);
	if(o) return o; else return null;
}
if (typeof(IE)=='undefined'){
	IE = (document.all);
	NS6 = (document.getElementById&&!IE);
}
///////////////////////////////////////////////////////////
var array_div = new Array();
function makeStatic(idx) {
	if (IE) { winY = truebody().scrollTop;}else{ winY = window.pageYOffset; }
	if (array_div[idx].active) {
		set_div_top( array_div[idx], winY); setTimeout('makeStatic("'+idx+'")', 5)
	}else {
		setTimeout( 'array_div["'+idx+'"].basessm.innerHTML="";', 5);
	}
}
var cast_test = false;
var count_test = 0;
function set_div_top(obj, winY){
	if (IE) { var b_top=obj.bssm.pixelTop; }else{ var b_top=parseInt( obj.bssm.top); }
	var o_top=obj.y();
	var mv = 0; var minY=obj._min.y
	if (winY!=obj.last_y){
		if(b_top>o_top||(winY+minY)>o_top) mv=(winY+minY)-b_top;
	}else if (obj.snap_last!=o_top)mv=o_top-obj.snap_last;

	var sm = mv>0 ? Math.ceil(mv * .2) : Math.floor( mv * .2);
	b_top+=sm; if (b_top<o_top) b_top=o_top;
	if(b_top==o_top) { obj.last_y=winY; obj.snap_last=o_top; }else {
		if (winY!=obj.last_y) { obj.last_y+=sm; }
		if (obj.snap_last!=o_top) { obj.snap_last+=sm; }
	}
	if (IE) { obj.bssm.pixelTop=b_top;}else{ obj.bssm.top=b_top+"px";}
	var winX=obj.x();
	if (IE) { obj.bssm.pixelLeft=winX; }else{ obj.bssm.left=winX+"px"; }
}
///////////////////////////////////////////////////////////
function 	truebody(){
	return (document.compatMode!="BackCompat")? document.documentElement : document.body;
}
///////////////////////////////////////////////////////////
var hk_slice = {
	set_div: function(obj){
		if (NS6){
			obj.thessm=g("thessm_"+obj.id);
			obj.basessm=g("basessm_"+obj.id);
			obj.ssm=obj.thessm.style;
			obj.bssm=obj.basessm.style;
			obj.bssm.clip="rect(0 "+obj.thessm.offsetWidth+" "+obj.thessm.offsetHeight+" 0)";
			obj.ssm.visibility="visible";
		}else if (IE) {
			obj.thessm=document.all("thessm_"+obj.id);
			obj.basessm=document.all("basessm_"+obj.id);
			obj.ssm=obj.thessm.style;
			obj.bssm=obj.basessm.style;
			obj.bssm.clip="rect(0 "+obj.thessm.offsetWidth+" "+obj.thessm.offsetHeight+" 0)";
			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 (IE||NS6) {
			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}, last_y: 0, 
				thessm: '', basessm: '', bssm: '', bssm2: '', ssm: '', snap_last: 0,
				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 (typeof(this._pos.x)=='object')	{
						var ob_pos=func.getMousePos(this._pos.x);
						var x_sc=IE? truebody().scrollLeft : window.pageXOffset;
						switch (this._align.x)	{
						case 'left': Xwin=ob_pos.x + 2; break;
						case 'right': Xwin=ob_pos.x + ob_pos.w - this._size.w - 2; break;
						case 'snap': Xwin=ob_pos.x + ob_pos.w + 2; break;
						case 'winleft': Xwin=x_sc + 2; break;
						case 'winright': Xwin=x_sc + func.get_page_size().w - this._size.w - 20; break;
						case 'wincenter': Xwin=x_sc + Math.floor((func.get_page_size().w - this._size.w) / 2); break;
						case 'center':
						default: Xwin=ob_pos.x + Math.floor((ob_pos.w - this._size.w) / 2);  break;
						}
					}else Xwin = this._pos.x;
					return Xwin;
				},
				y: function(){
					var Ywin=0;
					if (typeof(this._pos.y)=='object')	{
						var ob_pos=func.getMousePos(this._pos.y);
						var y_sc=IE? truebody().scrollTop : window.pageYOffset;
						switch (this._align.y) {
						case 'top': Ywin=Math.ceil(ob_pos.y + 2); break;
						case 'bottom': Ywin=ob_pos.y + ob_pos.h - this._size.h - 2; break;
						case 'snap': Ywin=ob_pos.y + ob_pos.h + 2; break;
						case 'wintop': Ywin=y_sc + 2; break;
						case 'winbottom': Ywin=y_sc + func.get_page_size().h - this._size.h - 2 - (show_close? 22 : 0); break;
						case 'winmiddle': Ywin=y_sc + Math.floor((func.get_page_size().h - this._size.h - (show_close? 20 : 0)) / 2); break;
						case 'middle':
						default: Ywin=ob_pos.y + Math.floor((ob_pos.h - this._size.h) / 2); break;
						}
					}else Ywin = this._pos.y;
					return Ywin;
				},
				test_w: ''
			}
			array_div[pid].init(pid, t_pos, t_min, t_align, t_size);
			var obj=g(pid);
			var msg=''; o_left=array_div[pid].x(); o_top=array_div[pid].y(); array_div[pid].snap_last = o_top;
			msg =
			'<DIV ID="basessm_'+pid+'"'+' style="Position : Absolute ; Left : '+o_left+'px ;Top : '+o_top+'px ;Z-Index : 20;">'+"\n"+
			'<DIV ID="thessm_'+pid+'" '+' style="Position : Absolute ;Top : 0 ;Z-Index : 20;" >'+"\n"+
						(show_close? '<div style="font-size: 10; text-align: right; background: transparent">'+"\n"+
							'<IMG SRC="'+path+'bt_close.gif" BORDER="0" ALT="" onclick="array_div[\''+pid+'\'].active=false;" '+
							'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>'+obj.innerHTML+'</td></tr></table>'+"\n";
			msg+='</DIV></DIV>';
			//var qr='<div style="width: 100%; height: 1; background: yellow">&nbsp;</div>';
			//g(pid).innerHTML=qr;
			//document.write(msg);
			obj.innerHTML=msg;
			//alert(msg);
			this.initSlide(pid);
		}
	}
}
