/*************************************************
 * WIDGET用のJavascript
 * 2008.07.10 AZIA S.Sato
 *************************************************/
var PAGE_OFFSET_X = 10;
var PAGE_OFFSET_Y = 10;
var _REQUEST_TIMEOUT = 7000;
var _UNIQUE_PARAM = 'ajdt';

//-------------------------------------------------------------------
// PopupHelp widget
//-------------------------------------------------------------------
var popupHelpLayerId = 'widget-help';

function showPopupHelp(e, text, positionX, positionY) {
	if (positionX == null) {
		positionX = PAGE_OFFSET_X;
	} else {
		positionX = positionX + PAGE_OFFSET_X;
	}
	if (positionY == null) {
		positionY = PAGE_OFFSET_Y;
	} else {
		positionY = positionY + PAGE_OFFSET_Y;
	}
	
	showWidgetByPosition(Event.pointerX(e), Event.pointerY(e), popupHelpLayerId, positionX, positionY, text);
}

function hidePopupHelp() {
	hideWidget(popupHelpLayerId);
}

//-------------------------------------------------------------------
// Window
//-------------------------------------------------------------------
/**
 * ウィンドウを開く
 */
function openWindow(url, name, height, width, scrollbars, resizable, status) {
	var window_condition = "height=" + height + ",width=" + width + 
		",scrollbars=" + scrollbars + ",resizable=" + resizable + ",toolbar=no,status=" + status;
	
	subWindow = window.open(url, name, window_condition);
	subWindow.focus();
	return subWindow;
}

/**
 * 常に手前に表示するウィンドウを開く
 */
function openModalWindow(url, name, height, width, scrollbars, resizable, status) {
	ModalWindow = openWindow(url, name, height, width, scrollbars, resizable, status);
	onfocus = function onFocus() {
		try {
			if (null !=ModalWindow && !ModalWindow.closed) {
				try {
					ModalWindow.focus();
				} catch(e) {
					document.onmousemove = null;
				}
			} else {
				document.onmousemove = null;
			}
		} catch (ex) {}
	};
	document.onmousemove = onfocus;
	return ModalWindow;
}

//-------------------------------------------------------------------
// Execute function
//-------------------------------------------------------------------

/**
 * 指定したURLにRequestをGETで投げて、結果を指定したIDに返します。
 * Parametorで送信(POST)
 */
function requestActionByParam(url, params, layerId, func) {
	var uniqueParam = new Date().getTime();
	url += '?' + _UNIQUE_PARAM + '=' + uniqueParam + '&' + params;
	
	var callback = {
		success: function(oResponse) {
			if (layerId != null && layerId != '') {
				$(layerId).innerHTML = oResponse.responseText;
			}
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		failure: function(oResponse) {
			// alert('エラーが発生しました。しばらくしてから再度お試し下さい。');
		},
		timeout: _REQUEST_TIMEOUT
	};
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

/**
 * 指定したURLにRequestをGETで投げて、結果を指定したIDに返します。
 * Formで送信
 */
function requestActionByForm(url, formName, layerId, func) {
	var uniqueParam = new Date().getTime();
	url += '?' + _UNIQUE_PARAM + '=' + uniqueParam;
	
	var formObject = $(formName);
	YAHOO.util.Connect.setForm(formObject, false);
	
	var callback = {
		failure : function(response) {
			// alert('エラーが発生しました。しばらくしてから再度お試し下さい。');
		},
		success: function(response) {
			if (layerId != null && layerId != '') {
				$(layerId).innerHTML = response.responseText;
			}
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		timeout: _REQUEST_TIMEOUT
	}
	
	var ajax = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

/*
 * Fileのアップロードを行います。
 */
function requestUploadAction(url, formName, layer_id, func) {
	var uniqueParam = new Date().getTime();
	url += '?' + _UNIQUE_PARAM + '=' + uniqueParam;
	
	var formObject = $(formName);
	YAHOO.util.Connect.setForm(formObject, true);
	
	var callback = {
		failure : function(response) {
			// alert('エラーが発生しました。しばらくしてから再度お試し下さい。');
		},
		success: function(response) {
			if (layer_id != null && layer_id != '') {
				$(layer_id).innerHTML = response.responseText;
			}
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		upload: function(response) {
			if (layer_id != null && layer_id != '') {
				$(layer_id).innerHTML = response.responseText;
			}
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		timeout: _REQUEST_TIMEOUT
	}
	
	var ajax = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

//-------------------------------------------------------------------
// Popup message
//-------------------------------------------------------------------
function showPopupWindowByParam(url, params, func) {
	var uniqueParam = new Date().getTime();
	url += '?' + _UNIQUE_PARAM + '=' + uniqueParam + '&' + params;
	
	disableMainWindow();
	
	var callback = {
		success: function(oResponse) {
			$('edit_page').innerHTML = oResponse.responseText;
			initPopupWindow(0,0,'edit_page',0,0);
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		failure: function(oResponse) {
			// alert('エラーが発生しました。しばらくしてから再度お試し下さい。');
		},
		timeout: _REQUEST_TIMEOUT
	};
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback);
}
function showPopupWindowByForm(url, formName, func) {
	var uniqueParam = new Date().getTime();
	url += '?' + _UNIQUE_PARAM + '=' + uniqueParam;
	
	var formObject = $(formName);
	YAHOO.util.Connect.setForm(formObject, false);
	
	disableMainWindow();
	
	var callback = {
		success: function(oResponse) {
			$('edit_page').innerHTML = oResponse.responseText;
			initPopupWindow(0,0,'edit_page',0,0);
			if (func != null) {
				setTimeout(function(){func();}, 1000);
			}
		},
		failure: function(oResponse) {
			// alert('エラーが発生しました。しばらくしてから再度お試し下さい。');
		},
		timeout: _REQUEST_TIMEOUT
	};
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback);
}
function hidePopupWindow() {
	$('edit_page').innerHTML = '';
	$('edit_page').hide();
	enableMainWindow();
}
function initPopupWindow(popupX, popupY, layerId, offsetX, offsetY) {
	var bubble = $(layerId);
	var needScroll = false;
	var windowSize = getWindowSize();
	var popupWidth = 500;
	
	deltaX =  window.pageXOffset
		|| document.documentElement.scrollLeft
		|| document.body.scrollLeft
		|| 0;
	deltaY =  window.pageYOffset
		|| document.documentElement.scrollTop
		|| document.body.scrollTop
		|| 0;
	
	if (popupY - deltaY < windowSize[1]/2) {
		if (windowSize[1] - (popupY - deltaY) < bubble.getHeight()) needScroll = true;
	} else {
		if (popupY - deltaY < bubble.getHeight()) needScroll = true;
	}
	
	if ((popupX + popupWidth) > windowSize[0]) {
		popupX = windowSize[0] - popupWidth;
	} else {
		popupX = (windowSize[0] - popupWidth) / 2;
	}
	
	if (popupY == 0) {
		popupY = (windowSize[1] + deltaY - 300) / 2;
	}
	
	document.getElementById(layerId).style.left = (popupX + offsetX) + "px";
	document.getElementById(layerId).style.top = (popupY + offsetY) + "px";
	
	setWidgetVisibility(layerId, true);
	bubble.show();
	
	if (needScroll) Element.scrollTo(bubble);
}
function disableMainWindow() {
	changeSelectVisible('hidden');
	setOpacity(5, 'wait_area');
}
function enableMainWindow() {
	changeSelectVisible('visible');
	setOpacity(0, 'wait_area');
}
function setOpacity(value, layerId) {
	var elm = $(layerId);
	var windowSize = getWindowSize();
	var deltaY =  window.pageYOffset
		|| document.documentElement.scrollTop
		|| document.body.scrollTop
		|| 0;
	
	if (elm && value > 0) {
		setWidgetLayerPosition(layerId, 0, 0);
		var windowSize = getWindowSize();
		elm.style.visibility = 'visible';
		elm.style.width = windowSize[0] + "px";
		elm.style.height = (windowSize[1] + deltaY) + "px";
		
		elm.style.opacity = value / 10;
		elm.style.MozOpacity = value / 10;
		elm.style.filter = 'alpha(opacity=' + value * 10 + ')';
		elm.style.zIndex = 5;
	} else {
		elm.style.visibility = 'hidden';
		
		elm.style.opacity = value / 10;
		elm.style.MozOpacity = value / 10;
		elm.style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
}

//-------------------------------------------------------------------
// Util function
//-------------------------------------------------------------------
var widgetText;

function setWidgetVisibility(layer_id, show_flag) {
	visibility = 'hidden';
	if (show_flag) {
		visibility = 'visible';
	}
	if (document.all) {
		//IE
		$(layer_id).style.visibility = visibility;
	} else if (document.getElementById) {
		//NN
		$(layer_id).style.visibility = visibility;
	} else {
		//NN4
		visibility = ('visible' == visibility) ? 'show' : 'hide';
		$(layer_id).style.visibility = visibility;
	}
}
function setWidgetLayerPosition(layer_id, left, top) {
	if (document.all) {
		//IE
		$(layer_id).style.left	= left;
		$(layer_id).style.top	= top;
	} else if (document.getElementById) {
		//NN
		$(layer_id).style.left	= left + "px";
		$(layer_id).style.top	= top  + "px";
	} else {
		//NN4
		$(layer_id).style.left	= left;
		$(layer_id).style.top	= top;
	}
}
function showWidget(e, layer_id, positionX, positionY) {
	mouse_x = Event.pointerX(e);
	mouse_y = Event.pointerY(e);
	scroll_y= (document.all) ? document.body.scrollTop: pageYOffset;
	
	setWidgetLayerPosition(layer_id, mouse_x + positionX, mouse_y + positionY + scroll_y);
	
	setWidgetVisibility(layer_id, 'true');
}
function showWidgetByPosition(mouse_x, mouse_y, layer_id, positionX, positionY, text) {
	scroll_y= 0;
	
	if (text != null) {
		$(layer_id).innerHTML = text;
	}
	
	setWidgetLayerPosition(layer_id, mouse_x + positionX, mouse_y + positionY + scroll_y);
	
	setWidgetVisibility(layer_id, true);
}
function hideWidget(layer_id) {
	setWidgetVisibility(layer_id, false);
}
function setWidgetInnerHTML(element_id, text) {
	if(document.all) {
		document.all(element_id).innerHTML	= text;
	} else if(document.getElementById) {
		document.getElementById(element_id).innerHTML	= text;
	}
}
function wait(condition, func) {
	if (condition()) {
		func()
	} else {
		var f = function() { wait(condition, func) };
		setTimeout(f, 100);
	}
}
function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return [myWidth, myHeight];
}
function getScrollHeight() {
	var scrollHeight = 0;
	
	if (document.documentElement) {
		// IE6
alert('Element');
		scrollHeight = document.documentElement.scrollWidth;
	} else if (document.body) {
		// Firefox
alert('Firefox');
		scrollHeight = document.body.scrollHeight;
	}
	
	return scrollHeight;
}

// SelectBoxの表示切り替え
function changeSelectVisible(param) {
	var i,j = 0;
	if (document.all) {
		for (i = 0; i < document.forms.length; i++) {
			for (j = 0; j < document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].type.indexOf("select") != -1) {
					document.forms[i].elements[j].style.visibility=param;
				}
			}
		}
	}
}

// ポップアップ用htmlを出力しておく
document.write('<div id="wait_area" style="z-index:5;"></div>');
document.write('<div id="edit_page" style="z-index:10;"></div>');
document.write('<div id="'+popupHelpLayerId+'"></div>');

