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

//高速化ハック
/*@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 = "";
	}
}

document.scroll = function(){
   return {
      x: this.body.scrollLeft || this.documentElement.scrollLeft,
      y: this.body.scrollTop  || this.documentElement.scrollTop
   };
};

//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";
		
		var scroll = document.scroll();
		
		left = Math.max(10,parseInt(document.body.scrollLeft + document.body.clientWidth/2 - option.width/2)) + "px";
		
		
		if(is_ie){
			if(document.documentElement.clientHeight == 0){
				top = Math.max(0,parseInt(scroll.y + document.body.clientHeight/2 - option.height/2));
			}else{
				top = Math.max(0,parseInt(scroll.y + document.documentElement.clientHeight/2 - option.height/2));
			}
			
			
		}else{
			top = Math.max(0,parseInt(scroll.y + 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 (is_ie) {
			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 (is_ie) {
			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" || !is_ie) {
				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";
	
	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){
			if(!textarea.selection)textarea.selection = document.selection.createRange();
			textarea.selection.text = html;
			textarea.focus();
			textarea.selection.select();
			
		}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.moveCursor = function(){
		if(document.selection != null){
			var sel=document.selection.createRange();
			textarea.selection = textarea.createTextRange();
			textarea.selection.moveToPoint(sel.offsetLeft,sel.offsetTop);
		}
	};
	
	//タブの入力
	textarea.onkeydown = function(e){
		if(!e)e = event;
		
		textarea.moveCursor();
		
		if(e.keyCode == 9){
			e.cancelBubble = true;
			e.returnValue = false;
			textarea.insertTab(e);
			return false;
		}	
				
		return true;
	}
	
}

//独自スタイルのボタン
function common_init_custom_button(){
	var targetClassName = "soycms_custom_button";
	var defaultBorderStyle = {
		color : "#909090",
		style : "outset"
	};
	
	var buttons = document.getElementsByClassName(targetClassName);
	Event.observe(window, "load", _init_custom_buttons);

	function _init_custom_buttons(){
		for(var i=0;i<buttons.length;i++){
			_init_custom_button(buttons[i]);
		}
	}

	function _init_custom_button(button){
		var bs = button.style;
	
		function setDefault(){
			bs.color = "#000080";
			bs.backgroundColor = "#ffffff";
			bs.borderStyle = defaultBorderStyle.style;
			bs.borderColor = defaultBorderStyle.color;
			bs.borderWidth = "1px";
		}
		function setOnFocus(){
			bs.borderColor = "#303030";
		}
		function setOnBlur(){
			bs.borderColor = defaultBorderStyle.color;
			bs.borderStyle = defaultBorderStyle.style;
		}
		function setOnPress(){
			bs.borderStyle = "inset";
		}

		setDefault();
		Event.observe(button, "mouseover", setOnFocus);
		Event.observe(button, "focus", setOnFocus);
		Event.observe(button, "mouseout", setOnBlur);
		Event.observe(button, "blur", setOnBlur);
		Event.observe(button, "mousedown", setOnPress);
		Event.observe(button, "keypress", setOnPress);
	}

}

//
function buildDateString(date,isdate,isend){
	var yy = date.getFullYear();
	var mm = date.getMonth() + 1;
	var dd = date.getDate();
	if (yy < 2000) { yy += 1900; }
	if (mm < 10) { mm = "0" + mm; }
	if (dd < 10) { dd = "0" + dd; }
	
	if(isdate){
		if(isend){
			return  yy + "-" + mm + "-" + dd + " " + "00:00:00";
		}else{
			return  yy + "-" + mm + "-" + dd + " " + "00:00:00";
		}
	}else{
		return  yy + "-" + mm + "-" + dd + " " + date.getHours() +":"+ date.getMinutes() +":" + date.getSeconds();
	}

}

function movedate(date,y,mo,d,h,mi,s){
	
	date.setFullYear(date.getFullYear() + y);
	date.setMonth(date.getMonth() + mo);
	date.setDate(date.getDate() + d);
	date.setHours(date.getHours() + h);
	date.setMinutes(date.getMinutes() + mi);
	date.setSeconds(date.getSeconds() + s);
	return date;
}


function soycms_check_site(id, url){
	
	var timerId = setInterval(function(){
		var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			onComplete: function(req){
				if(parseInt(req.responseText) == parseInt(id)){
					
				}else{
					alert(soycms.lang.common.double_login);
				}
			}
		});
	
	},300000);	//5 minute

}