//ブラウザ判断
var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var is_ie = (navigator.appName=="Microsoft Internet Explorer");

//高速化ハック
/*@cc_on
if (@_jscript_version < 9) {
   var _d = document;
   eval("var document = _d");
}
@*/

//共通オブジェクト
var soycms = {};

//toggle
function common_toggle(ele){

	if(typeof(ele) ==  "string"){
		ele = document.getElementById(ele);
	}
	
	if(ele.style.display != "none"){
		ele.style.display = "none";
	}else{
		ele.style.display = "";
	}
}

function common_toggle_password(ele){

	if(typeof(ele) ==  "string"){
		ele = document.getElementById(ele);
	}
	
	if(ele.type != "password"){
		ele.type = "password";
	}else{
		ele.type = "text";
	}

}

//click to layer
function common_click_to_layer(ele,option){
	
	if(!ele.tagName.match(/a/i)){
		alert(ele.tagName);
		return false;
	}
	
	var href = ele.getAttribute("href");
	common_to_layer(href,option);	
		
	//always
	return false;

}

//submit to layer
function common_submit_to_layer(ele,option){
	
	if(!ele.tagName.match(/form/i)){
		alert(ele.tagName);
		return false;
	}
	
	var action = ele.getAttribute("action");
	
	var targetId = (option && option.targetId) ? option.targetId : "click_to_layer_frame";
	
	common_to_layer("about:blank",option);	
	
	ele.setAttribute("target",targetId);
	
	//always
	return true;
}

//element_to_layer
function common_element_to_layer(ele,option){
	common_to_layer(ele,option);
	
	//always
	return true;
}

//to_layer
function common_to_layer(href,option){

	if(!option)option = {};
	if(!option.width)option.width = 640;
	if(!option.height)option.height = 480;
	
	var targetId = (option.targetId) ? option.targetId : "click_to_layer_frame";

	if($(targetId + "_wrapper")){
		document.body.removeChild($(targetId + "_wrapper"));
	}
	
	//wrapper
	var wrapper = new Element("div");
	
	with(wrapper.style){
		position = "absolute";
		width = option.width + "px";
		height = option.height + "px";
		left = Math.max(10,parseInt(document.body.scrollLeft + document.body.clientWidth/2 - option.width/2)) + "px";
		
		
		if(document.all){
			top = Math.min(
					Math.max(0,parseInt(document.body.scrollTop + document.body.clientHeight/2 - option.height/2)),
					Math.max(0,parseInt(document.body.scrollTop + document.documentElement.clientHeight/2 - option.height/2))
				)  + "px";
			
		}else{
			top = Math.max(0,parseInt(document.body.scrollTop + window.innerHeight/2 - option.height/2)) + "px"
		}
	}
	
	wrapper.setAttribute("id",targetId + "_wrapper");
	wrapper.addClassName("common_to_layer_wrapper");
		
	document.body.appendChild(wrapper);
	 
	//window bar
	var bar = new Element("div");
	bar.id = targetId + "layer_bar";
	bar.style.height = "25px";
	wrapper.appendChild(bar);
	
	var bar_left = new Element("div");
	bar_left.id = targetId + "_layer_bar_left";
	bar_left.addClassName("layer_bar_left");
	bar.appendChild(bar_left);
	
	var bar_mid = new Element("div");
	bar_mid.id = targetId + "_layer_bar_mid";
	bar_mid.addClassName("layer_bar");
	bar_mid.style.width = option.width - 27 + "px";
	bar.appendChild(bar_mid);
	
	var bar_right = new Element("div");
	bar_right.id = targetId + "_layer_bar_right";
	bar_right.addClassName("layer_bar_right");
	bar.appendChild(bar_right);
	
	bar._onmousemove = function(e){
		
		var x,y;
		
		if (document.all) {
			x = event.clientX - bar.offsetX + document.body.scrollLeft;
			y = event.clientY - bar.offsetY + document.body.scrollTop;
		}else{
			x = e.pageX - bar.offsetX;
			y = e.pageY - bar.offsetY;
		}
		
		if(x>0){
			wrapper.style.left = x + "px";
		}
		
		if(y>0){
			wrapper.style.top = y + "px";
		}
		
		return false;
	};
	bar.onmousedown = function(e){
		
		if(document.onmousemove == this._onmousemove){
			this.onmouseup();
		}
		
		document._onmousemove = document.onmousemove;
		document.onmousemove = this._onmousemove;
		
		var iframe = $(targetId);
		if(iframe)iframe.style.visibility = "hidden";
		
		if (document.all) {
			this.offsetX = event.offsetX + 2;
			this.offsetY = event.offsetY + 2;
		}else{
			this.offsetX = e.pageX - wrapper.offsetLeft;
			this.offsetY = e.pageY - wrapper.offsetTop;
		}
		
		$$("select").each(function(ele){
			ele._visibility = ele.style.visibility;
			ele.style.visibility = "hidden";
		});
		
		return false;
	};
	bar.onmouseup = function(){
		
		if(document._onmousemove){
			document.onmousemove = document._onmousemove;
		}else{
			document.onmousemove = null;
		}
		
		var iframe = $(targetId);
		if(iframe)iframe.style.visibility = "visible";
		
		$$("select").each(function(ele){
			ele.style.visibility = ele._visibility;
		});
	};
	
	//close button
	var close = new Element("div");
	close.setAttribute("id",targetId + "_close");
	close.addClassName("click_to_layer_close");
	close.innerHTML = "<a href='#'></a>";
	close.onmousedown = function(e){
		if(!e)e = event;
		e.cancelBubble = true;
		e.returnValue = false;
		
		close.addClassName("click_to_layer_close_down");
	
		return false;
	};
	close.onmouseup = function(e){
		if(!e)e = event;
		e.cancelBubble = true;
		e.returnValue = false;
		
		close.addClassName("click_to_layer_close");
	
		return false;	
	};
	
	close.onclick = function(){
		if(option.onclose){
			var result = option.onclose();
			if(result == false)return;
		}
		wrapper.parentNode.removeChild(wrapper);
	};
	if(option.disableClose == true){
		close.style.visibility = "hidden";
	}
	
	bar.appendChild(close);
	
	//small button
	var small = new Element("div");
	small.setAttribute("id",targetId + "_small");
	small.addClassName("click_to_layer_small");
	small.innerHTML = "<a href='#'></a>";
	bar.appendChild(small);
	
	if(option.disableClose == true){
		small.style.left = close.offsetLeft + "px";
	}
	
	small.onmousedown = function(e){
		if(!e)e = event;
		e.cancelBubble = true;
		e.returnValue = false;
		
		small.addClassName("click_to_layer_small_down");
	
		return false;
	};
	
	small.onmouseup = function(e){
		if(!e)e = event;
		e.cancelBubble = true;
		e.returnValue = false;
		
		small.addClassName("click_to_layer_small");
	
		return false;	
	};
	
	small.onclick = function(){
		$(targetId + "layer_left").toggle();
		$(targetId + "layer_right").toggle();
		$(targetId + "_frame_wrapper").toggle();
		//$(targetId + "_layer_bottom").toggle();
	};
	
	//iframeの両脇を作成
	var layer_left = new Element("div");
	layer_left.id = targetId + "layer_left";
	layer_left.addClassName("layer_left");
	wrapper.appendChild(layer_left);
	
	var layer_right = new Element("div");
	layer_right.addClassName("layer_right");
	layer_right.id = targetId + "layer_right";
	wrapper.appendChild(layer_right);
	
	var iframe_wrapper = new Element("div");
	iframe_wrapper.id = targetId + "_frame_wrapper";
	iframe_wrapper.addClassName("layer_wrapper");
		
	//iframeを生成する場合
	if(typeof(href) != "object"){
		//iframeを作成
		iframe_wrapper.innerHTML = '<iframe src="'+href+'" name="'+ targetId + '" frameborder="0" id="'+targetId+'" class="click_to_layer_frame"></iframe><div style="clear:both;height:0;width:0;line-height:0px;"><!----></div>';
	}else{
		iframe_wrapper.appendChild(href);
	}
	iframe_wrapper.style.width = option.width - 10 + "px";
	iframe_wrapper.style.height = option.height + "px";
	
	wrapper.appendChild(iframe_wrapper);
		
	//iframeの下を作成
	var bar_bottom = new Element("div");
	bar_bottom.id = targetId + "_layer_bottom";
	
	var layer_bottom_left = new Element("div");
	layer_bottom_left.id = targetId + "_layer_bottom_left";
	layer_bottom_left.addClassName("layer_bottom_left");
	bar_bottom.appendChild(layer_bottom_left);
	
	var layer_bottom_mid = new Element("div");
	layer_bottom_mid.id = targetId + "_layer_bottom_mid";
	layer_bottom_mid.addClassName("layer_bottom");
	layer_bottom_mid.style.width = option.width - 17 + "px";
	bar_bottom.appendChild(layer_bottom_mid);

	var layer_bottom_right = new Element("div");
	layer_bottom_right.id = targetId + "_layer_bottom_right";
	layer_bottom_right.addClassName("layer_bottom_right");
	bar_bottom.appendChild(layer_bottom_right);
	
	
	//resize
	layer_bottom_right.onmousedown = function(e){

		var iframe = $(targetId);
		if(iframe)iframe.style.visibility = "hidden";

		document.onmousemove = function(e){
				if(!e)e=event;
				var x = Event.pointerX(e);	
				var y = Event.pointerY(e);
				
				var newWidth = x - wrapper.offsetLeft;
				var newHeight = y - wrapper.offsetTop;
								
				common_resize_layer_by_targetId(targetId,{width:newWidth,height:newHeight});
				
				return false;
			};
			
			layer_bottom_right.onmouseup = document.onmouseup = function(e){
				document.onmousemove = function(){};
				var iframe = $(targetId);
				if(iframe)iframe.style.visibility = "visible";
			};
	}
	
	
	wrapper.appendChild(bar_bottom);
	
	var iframe = $(targetId);
	
	if(iframe){
		with(iframe.style){
			height = "100%";
			width = "100%";
		}
		
		$(targetId + "_frame_wrapper").style.overflow = "hidden";
		
		iframe.onload = iframe.onreadystatechange = function () {
			if (this.readyState == "complete" || !document.all) {
				wrapper.style.visibility = "visible";
			}
		};
		
		return iframe.contentWindow;
	}else{
		wrapper.style.visibility = "visible";
	}
	
}

/** 
 *	only avaiable when layer's id is default. 
 */
function common_close_layer(opener){
	common_close_layer_by_targetId("click_to_layer_frame",opener);
}

/*
 * 
 */
function common_close_layer_by_targetId(targetId,opener){
	if(opener){
		opener.document.getElementById(targetId + "_close").onclick();
	}else{
		document.getElementById(targetId +"_close").onclick();
	}
}



/** 
 *	only avaiable when layer's id is default. 
 */
function common_get_layer(opener){
	if(opener){
		return opener.document.getElementById("click_to_layer_frame").contentWindow;
	}else{
		return document.getElementById("click_to_layer_frame").contentWindow;
	}
}

function common_resize_layer(option,opener){
	common_resize_layer_by_targetId("click_to_layer_frame",option,opener);
}

function common_resize_layer_by_targetId(targetId,option,opener){
	
	var _common_get = function(id){
		if(opener){
			return opener.document.getElementById(targetId + id);
		}else{
			return document.getElementById(targetId + id);
		}
	};
	
	if(!option){
		return;
	}
	
	if(option.width){
		_common_get("_wrapper").style.width = option.width + "px";
		_common_get("_frame_wrapper").style.width = option.width - 10 + "px";
		_common_get("_layer_bar_mid").style.width = option.width - _common_get("_layer_bar_left").offsetWidth - _common_get("_layer_bar_right").offsetWidth + "px";
		_common_get("_layer_bottom_mid").style.width = option.width - _common_get("_layer_bottom_left").offsetWidth - _common_get("_layer_bottom_right").offsetWidth + "px";
	}
	
	if(option.height){
		_common_get("_wrapper").style.height = option.height + "px";
		_common_get("_frame_wrapper").style.height = option.height + "px";
	}
	
}

function common_speack_soy_boy(str){
	$("popup_content").innerHTML = str;
}

function common_show_soy_boy(flag){
	
	if(!$("popup_content").innerHTML)return;

	if($('popup').effect){
		$('popup').effect.cancel();
		$('popup').appear();
	}

	
	$('popup').onmouseover = function(){common_show_soy_boy(true);};
	$('soy_logo').onmouseover = function(){};
	$('popup').onmouseout=function(){common_hide_soy_boy();};
	$("popup").show();
	
	
	if(!flag){
		setTimeout(common_hide_soy_boy,800);
	}
};

function common_hide_soy_boy(){
	$('popup').onmouseover = function(){common_show_soy_boy(true);};
	$('popup').onmouseout=function(){};
	$('popup').effect = new Effect.Fade("popup",{
		from:1,
		to : 0,
		afterFinish : function(){
			$('popup').onmouseover = function(){};
			$('soy_logo').onmouseover = function(){common_show_soy_boy(true);};
			$('popup').effect = null;
		}
	});	
	
}

var release_timer_id = null;
function common_show_message_popup(node,message){
	
	if(release_timer_id){
		clearTimeout(release_timer_id);
	}
	if($("message_popup")){
		if($("message_popup").effect){
			release_timer_id = setTimeout("common_show_message_popup("+node+","+message+")",300);
			return;
		}else{
			document.body.removeChild($("message_popup"));
		}
	}
	var div = new Element("div");
	var offset =Position.cumulativeOffset(node);
	div.innerHTML = message;
	
	div.addClassName("help_popup");
	div.style.left = (offset[0]+20) + "px";
	div.id = "message_popup";
	div.style.visible = "hidden";
	document.body.appendChild(div);
	
	var top = (offset[1]) - div.offsetHeight;
	if(top<0){
		top = 0;
	}
	div.style.top = top + "px";
	
//	$(div.id).hide();
	
//	div.effect = new Effect.Appear(div.id,{
//		duration:0.1,
//		afterFinish:function(){
//
//			div.effect = null;
//		}
//	});
	
	node.onmouseout = function(){
		if(release_timer_id){
			clearTimeout(release_timer_id);
		}
		if(div.effect){
			release_timer_id = setTimeout(node.onmouseout,300);
		}else{
			document.body.removeChild(div);	
			node.onmouseout = null;
		}
		
	}	
}

//TextAreaに関数付加
function init_text_area(textarea){
	
	//テキストのペースト
	textarea.insertHTML = function(html){
		if (document.selection != null){
			textarea.focus();
			textarea.selection = document.selection.createRange();
			textarea.selection.text = html;
		}else{
			var start = textarea.selectionStart;
			var end = textarea.selectionEnd;
			
			var beforeString = textarea.value.substring(0,start);
			var afterString = textarea.value.substring(end);
			
			var scroll = textarea.scrollTop;
			var scrollLeft = textarea.scrollLeft;
			
			textarea.value = beforeString + html + afterString;
			
			textarea.scrollTop = scroll;
			textarea.scrollLeft = scrollLeft;
			
			textarea.setSelectionRange(start,start + html.length);
			
			textarea.focus();
		}
	};
	
	//タブの挿入
	textarea.insertTab = function(e){
		
		if (document.selection != null){
			textarea.selection = document.selection.createRange();
			
			var value = textarea.selection.text;
			
			if(textarea.selection.compareEndPoints('StartToEnd',textarea.selection) == 0){		
				textarea.selection.text = String.fromCharCode(9);
			}else{
				if(e.shiftKey){
					value = value.replace( /\n\t/g, "\n" );
					if(value.substr( 0, 1 ) == "\t"){
						value = value.substr( 1, value.length-1 ) + "\n";
					}
				}else{
					value = value.replace( /\n/g, "\n\t" );
					value = "\t" + value + "\n";
				}
				
				textarea.selection.text = value;
			}
			return;
		}else{
			var start = textarea.selectionStart;
			var end = textarea.selectionEnd;
			
			var scroll = textarea.scrollTop;
			
			var beforeString = textarea.value.substring(0,start);
			var afterString = textarea.value.substring(end);
			
			if(start == end){
				textarea.value = beforeString + "\t" + afterString;
				textarea.scrollTop = scroll;
				textarea.setSelectionRange(start + 1,start + 1);
			}else{
				var value = textarea.value.substring(start,end);
				if(e.shiftKey){
					value = value.replace( /\n\t/g, "\n" );
					if(value.substr( 0, 1 ) == "\t"){
						value = value.substr( 1, value.length-1 );
					}
				}else{
					value = value.replace( /\n/g, "\n\t" );
					if(value.substr(value.length-1, 1) == "\t") {
						value = "\t" + value.substr( 0, value.length-2 ) + "\n";
					}else{
						value = "\t" + value;
					}
				}
				
				textarea.value = beforeString + value + afterString;
				textarea.scrollTop = scroll;
				textarea.setSelectionRange(start,start + value.length);
			}
			return;
		}
	};
	
	//タブの入力
	textarea.onkeydown = function(e){
		if(!e)e = event;
		
		if(e.keyCode == 9){
			e.cancelBubble = true;
			e.returnValue = false;
			textarea.insertTab(e);
			return false;
		}	
				
		return true;
	}
	
}

/* copy from soy2layoutmanager.js */
var SOY2LayoutManager = {

	/**
	 * 引数で指定したボックスの幅をそろえる
	 */
	arrangeHeight : function(elements){
		this.arrangeSize(elements,false,true);
	},
	
	/**
	 * 引数で指定したボックスの幅をそろえる
	 */
	arrangeWidth : function(elements){
		this.arrangeSize(elements,true,false);
	},
	
	/**
	 * 幅と高さをそろえる
	 */
	arrangeSize : function(elements,is_width,is_height){
		var maxWidth = 0;
		var maxHeight = 0;
		
		for(var i=0,j=elements.length; i<j; i++){
			var ele = elements[i];
			maxWidth = Math.max(maxWidth,ele.offsetWidth);
			maxHeight = Math.max(maxHeight,ele.offsetHeight);
		}
		
		
		
		for(var i=0,j=elements.length; i<j; i++){
			if(is_width)elements[i].style.width = maxWidth + "px";
			if(is_height)elements[i].style.height = maxHeight + "px";
			
			elements[i].onclick = function(){
				var str = this.offsetWidth + " x " + this.offsetHeight + "\n";
				str += this.style.width + " x " + this.style.height;
				alert(str);
			}
		}
	},
	
	
	/**
	 * 内部の要素によって大きさを拡大する
	 */
	expandSize : function(element,is_width,is_height){
		var oldOverflow = element.style.overflow;
		
		element.style.overflow = "auto";
		var maxWidth = Math.max(element.offsetWidth,element.scrollWidth);
		var maxHeight = Math.max(element.offsetHeight,element.scrollHeight);
		
		if(is_width)element.style.width = maxWidth + "px";
		if(is_height)element.style.height = maxHeight + "px";
		
		element.style.overflow = oldOverflow;
	},
	
	/**
	 * 大きさを変更して親のボックスのサイズに合わせる
	 */
	adjustSize : function(element,is_width,is_height,node){
		var p_node = (node) ? node : element.parentNode;
		var height = p_node.offsetHeight;
		var width = p_node.parentNode.scrollWidth;
		height = height - element.offsetTop;
		width = width - element.offsetLeft;
		
		if(is_height)element.style.height = height + "px";	//borderとかの計算はとりあえず無視
		if(is_width)element.style.width = width + "px";	//borderとかの計算はとりあえず無視
		
	}
}