/*--- Floating page config ----------------------------------------*/
//Step 1: enable or disable floating page.
var cfg_enable_floating = false;  //enable floating page value [true=enable], [false=disable]

//Step 2: edit config value.
var cfg_bg_active	   = false;  //floating background layer value [true=active], [false=deactive]
var cfg_bg_opacity 	   = 0.8;    //opacity background layer value [0.0 to 1.0]
var cfg_content_width  = 500;    //width of content area
var cfg_content_height = 390;    //height of content area
var cfg_content_url    = "/floating-page/announce.htm";
var cfg_scrollbar      = true;   //scrollbar status value [true=enable], [false=disable]
var cfg_btnClose       = true;   //visible close button status value [true=visible], [false=hidden]


/*-----------------------------------------------------------------*/
function floating_resize() { 
	if(document.getElementById("floating-layer")==null) { return; }
	var doc_width  = $(window).width() + $(document).scrollLeft();
	var doc_height = $(document).height();

	$("#floating-layer").width(doc_width);
	$("#floating-layer").height(doc_height);
		
	win_width = $(window).width();
	win_height = $(window).height();
	if (jQuery.browser.opera && jQuery.browser.version >= 9.50) {
		win_height = document.getElementsByTagName('html')[0].clientHeight;
	}

	
	var box_width  = $("#floating-box").width();
	var box_height = $("#floating-box").height();	
	//var box_left   = Math.ceil((win_width-box_width)/2) + $(document).scrollLeft();  //auto center
	//var box_top    = Math.ceil((win_height-box_height)/2) + $(document).scrollTop(); //auto center
	var box_left   = Math.ceil((win_width-box_width)/2);
	var box_top    = Math.ceil((win_height-box_height)/2);

	if(box_left<0) { box_left = 0; }
	if(box_top<0)  { box_top  = 0; }	
	$("#floating-box").css({left: box_left, top: box_top});
	
	$(".box-close").width($("#floating-box").width()); //Fix IE6

	/*-- Fix IE6 rander dropdown --*/
	if(jQuery.browser.msie && jQuery.browser.version <7.0) {
		var pIframe = $("#floating-fix-ie6");
		if($("#floating-layer").css("display")!="block") {
			$(pIframe).width(box_width);
			$(pIframe).height(box_height);
			$(pIframe).css({left:box_left, top:box_top , display:$("#floating-box").css("display"), opacity:0});		
		} else {
			$(pIframe).width(doc_width);
			$(pIframe).height(doc_height);
			$(pIframe).css({left:0, top:0 , display:$("#floating-box").css("display"), opacity:0});
		}
	}
	/*-----------------------------*/
	
}; 


function floating_initial(layer_opacity, content_width, content_height, content_url, scrollbar, btnClose) {
	if(document.getElementById("floating-layer")!=null) { return; }
	var tpl = '<div id="floating-layer"></div>'
			+ '<iframe id="floating-fix-ie6" src="" frameborder="0"></iframe>'
			+ '<div id="floating-box">'
			+ '  <div class="box-close"><a href="javascript:floating_close();"><span></span></a></div>'				
			+ '  <table border="0" cellpadding="0" cellspacing="0">'
			+ '    <tr>'
			+ '      <td><span class="box-tl"></span></td>'
			+ '      <td class="box-t"></td>'
			+ '      <td><span class="box-tr"></span></td>'
			+ '    </tr>'
			+ '    <tr>'
			+ '      <td class="box-l"></td>'
			+ '      <td class="box-m"><iframe id="floating-content" src="" scrolling="'+(scrollbar?"auto":"no")+'" frameborder="0"></iframe></td>'
			+ '      <td class="box-r"></td>'
			+ '    </tr>'
			+ '    <tr>'
			+ '      <td><span class="box-bl"></span></td>'
			+ '      <td class="box-b"></td>'
			+ '      <td><span class="box-br"></span></td>'
			+ '    </tr>'
			+ '  </table>'
			+ '</div>';
						
	$(tpl).appendTo("body");

	$("#floating-layer").css({	
		opacity: layer_opacity
	});
	
	$("#floating-content").css({		
		width: content_width,
		height: content_height
	});
	
	$("#floating-content").attr("src", content_url);

	$(".box-close").css({display: (btnClose?"block":"none") });
	
	floating_resize();
}

function floating_open(bg_active) {
	if(document.getElementById("floating-layer")==null) { return; }
	$("#floating-layer").css({display:(bg_active?"none":"block")});
	$("#floating-box, #floating-fix-ie6").css({display:"block"});	
}

function floating_close() {
	if(document.getElementById("floating-layer")==null) { return; }
	$("#floating-layer, #floating-box, #floating-fix-ie6").css({display:"none"});
}


/*--- Main Program ---[By Mr.Pratheep Tangworameth(Kaew) 2009-05-14]---*/
window.onload = function() {
	/*--------------------------------*/	
	if(!cfg_enable_floating) { return; }
	/*--------------------------------*/
	var resizeTimer = null;
	var scrollTimer = null;
	$(window).bind('resize', function() {    
		if (resizeTimer) clearTimeout(resizeTimer);    
		resizeTimer = setTimeout(floating_resize, 100);
	});
	
	$(window).bind('scroll', function() {    
		if (scrollTimer) clearTimeout(scrollTimer);    
		scrollTimer = setTimeout(floating_resize, 100);
	});
	/*--------------------------------*/
	floating_initial(cfg_bg_opacity, cfg_content_width, cfg_content_height, cfg_content_url, cfg_scrollbar, cfg_btnClose);
	floating_open(cfg_bg_active);
	/*--------------------------------*/	
};
