/**
 *
 * @author Sun
 * @since 2008/8/1
 * @version v1.0
 * @email sun.kyc@gmail.com
 *
 */

Event.observe(window, 'load', function(){

	var browser = navigator.appName;

	// ff = ff3, ie = ie6
	if('Netscape' == browser){
		$$('body')[0].addClassName('ff');
	}else if('Microsoft Internet Explorer' == browser){
		$$('body')[0].addClassName('ie');
	}

});



/**
 * 動態改變 iframe 高度( 寬度 及 是否出現 scrolling( 預設為 no ) 為非必填 )
 *
 * @param iframeObject
 * @param width -
 *            數字 或 百分比<br>
 *            *** 注意, 百分比為文字 如:'100%' ***
 * @param scrolling -
 *            yes, no, auto
 * @return
 */
function setIframeObjectHeight(iframeObject, width, scrolling) {

	if (iframeObject.contentDocument
			&& iframeObject.contentDocument.body.offsetHeight) {

		iframeObject.height = iframeObject.contentDocument.body.offsetHeight + 40;

	} else if (iframeObject.Document && iframeObject.Document.body.scrollHeight) {

		iframeObject.height = iframeObject.Document.body.scrollHeight + 40;
	}

	if (width) {
		iframeObject.width = width;
	}

	if (scrolling) {
		iframeObject.scrolling = scrolling;
	} else {
		iframeObject.scrolling = 'no';
	}

}

/**
 * @deprecated -- 目前只為了 myWeb 使用, 可以考慮拿掉
 */
function hrefDisplay(url) {

	if (url == null || url == 'null' || url == '') {
		return '#';
	} else {
		return url;
	}

}

/**
 * 取得錢的顯示 10,000,000
 *
 * @param digit -
 *            傳入值為數字, 整數
 *
 */
function getDollor(digit) {

	var dollerString = "";

	var regex = /\d*/;

	// 如果 if 的判斷有問題, 請參考 checkDate 的注解
	if (regex.test(digit) && regex.exec(digit) == digit) {

		digit = digit + "";

		var group = Math.floor(digit.length / 3);

		for ( var i = 0; i < group; i++) {
			dollerString = ","
					+ digit.substring((digit.length - 3 * (i + 1)),
							(digit.length - 3 * (i + 1) + 3)) + dollerString;
		}

		if (digit.length % 3 == 0) {
			dollerString = dollerString.substring(1, dollerString.length);
		} else {
			dollerString = digit.substring(0, digit.length - 3 * group)
					+ dollerString;
		}

	} else {
		alert("getDollor:傳入參數必需為數字!");
	}

	return dollerString;
}

/**
 * 以半透明 div 擋住整個 winodw( 如沒有傳入任何參數, 以 "ajax-loader.gif" 顯示 ) <br>
 * z-index 設定值為 800
 *
 * @param pictureUrl -
 *            非必填
 * @return
 */
function blockWindow(pictureUrl) {

	if ($$('body')[0]) {

		var picture;

		if (pictureUrl) {
			picture = '#000 url(' + pictureUrl + ') no-repeat 50% 50%';
		} else {
			picture = '#000 url(ajax-loader.gif) no-repeat 50% 50%';
		}

		var windowBlocker = document.createElement('div');
		windowBlocker.setAttribute('id', 'windowBlocker');

		$$('body')[0].appendChild(windowBlocker);

		$('windowBlocker').setStyle( {
			'top' :'0',
			'left' :'0',
			'width' :'100%',
			'height' :'100%',
			'background' :picture,
			'position' :'absolute',
			'z-index' :'800'
		}).setOpacity(0.5);

	} else {
		alert("<body> body tag 必需存在!!!");
	}

}

/**
 * 取消以 blockWindow() 所產生的阻擋 div
 *
 * @return
 */
function unBlockWindow() {

	if ($$('body')[0]) {

		var windowBlocker = $('windowBlocker');

		if (windowBlocker) {
			$$('body')[0].removeChild(windowBlocker);
		}

	} else {
		alert("<body> body tag 必需存在!!!");
	}

}

/**
 *
 * @param message - 要顯示訊息
 * @param relativeElement - 要顯示訊息的定位 element( 以此 element 右邊 + 5px 顯示 )
 * @param stayOnScreen - 顯示後是否要一直留在畫面上
 * @param timeToClose - 顯示時間長度 '千分之一秒' ( 需同時設 stayOnScreen = false )
 *
 */
function messageDisplayByElement(message, relativeElement, stayOnScreen, timeToClose) {


	if ($$('body')[0]) {

		var divMessageDisplayer = document.createElement('div');
		divMessageDisplayer.setAttribute('id', 'divMessageDisplayer');

		$$('body')[0].appendChild(divMessageDisplayer);

		var callBack = function(){

			$('divMessageDataTag').innerHTML = message;

			var offsetLeft = $(relativeElement).getWidth() + 5;

			$('divMessageDisplayer').clonePosition($(relativeElement), {offsetLeft:offsetLeft});

			// setting css...
			setNiftyDisplay();

			// still testing...
			//loadCss("displayUtil.css");


			$('divMessageDataTag').setStyle({
				'width' :'auto',
				'height' :'auto'
			});

			$('divMessageDisplayer').setStyle( {
				// ff3 可設 width:auto
				'width': '100',
				'height': 'auto',
				'position' :'absolute',
				'z-index' :'100',
				'background': '#9BD1FA'
			}).setOpacity(0.7);


			if( !stayOnScreen ){

				if(timeToClose){
					setTimeout(closeMessageDisplay, timeToClose);
				}

			}
		};

		try{
			ajaxSetTemplateByAjpId("displayUtil.ajp", "nifty", "divMessageDisplayer", callBack);
		}catch( e ){
			alert(e.message + "  *** 請確定有宣告 jsTemplateUtil.js (ajaxSetTemplateByAjpId 在裡面) ***");
		}

	} else {
		alert("<body> body tag 必需存在!!!");
	}

}

function setNiftyDisplay(){

	$('roundTop').setStyle({
		'display': 'block',
		'background': '#FFF'
	});

	$('roundBottom').setStyle({
		'display': 'block',
		'background': '#FFF'
	});

	if( $$('body.ff')[0] ){

		$('top1').setStyle({
			'margin': '0 5px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});

		$('bottom1').setStyle({
			'margin': '0 5px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});


		$('top2').setStyle({
			'margin': '0 3px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});

		$('bottom2').setStyle({
			'margin': '0 3px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});


		$('top3').setStyle({
			'margin':'0 2px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});

		$('bottom3').setStyle({
			'margin':'0 2px',
			'background': '#FFF',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});


		$('top4').setStyle({
			'margin': '0 1px',
			'height': '2px',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});

		$('bottom4').setStyle({
			'margin': '0 1px',
			'height': '2px',

			'display': 'block',
			'height': '1px',
			'overflow': 'hidden',
			'background': '#9BD1FA'
		});

	}else if( $$('body.ie')[0] ){

		$A($$('b.round1')).each(function(item){
			item.setStyle({
				'margin': '0 5px',
				'background': '#FFF',

				'display': 'block',
				'height': '1px',
				'overflow': 'hidden',
				'background': '#9BD1FA'
			})
		});

		$A($$('b.round2')).each(function(item){
			item.setStyle({
				'margin': '0 3px',
				'background': '#FFF',

				'display': 'block',
				'height': '1px',
				'overflow': 'hidden',
				'background': '#9BD1FA'
			})
		});

		$A($$('b.round3')).each(function(item){
			item.setStyle({
				'margin':'0 2px',
				'background': '#FFF',

				'display': 'block',
				'height': '1px',
				'overflow': 'hidden',
				'background': '#9BD1FA'
			})
		});

		$A($$('b.round4')).each(function(item){
			item.setStyle({
				'margin': '0 1px',
				'height': '2px',

				'display': 'block',
				'height': '1px',
				'overflow': 'hidden',
				'background': '#9BD1FA'
			})
		});
	}

}

function closeMessageDisplay() {

	if ($$('body')[0]) {

		var divMessageDisplayer = $('divMessageDisplayer');

		if (divMessageDisplayer) {
			$$('body')[0].removeChild(divMessageDisplayer);
		}

	} else {
		alert("<body> body tag 必需存在!!!");
	}

}