
/*Microsoft Internet Explorer
MSIE 6.0
*/

/* ver 1.0.0.1 */

var sx = 0;
var sy = 0;
var s_x;
var s_y;
var imageNo;
var imageName;
var	imageO;
var	imageDirname;
var	imageAid;
var presentPage=0;

var _cont;
var _aid;
var timerID;
var resData;
var requestCounter = 0;
var txtFocusFlag = 0;

document.onmousemove = dragwin;
document.onmouseup = dragoff;
document.onkeydown = cancel_tab;

var flag;
var winX = 0;
var mouseX;
var mouseY;

window.onload = function() {
	var colorful = new ColorfulInput;
	colorful.set();
	enableTooltips("content");

	if (!document.all) window.document.captureEvents(Event.KEYUP);
}

var mouseLeaveWin = function (event ,win){
	if(document.all){
		$(win).onmouseleave = function(event){
			showingMenu = 0;
			Element.remove(win);
			var showHide = new showHideSelectForm();
			showHide.show();
		}
	}else{
		$(win).onmouseout = function(event){
			eleObj = Element.getDimensions($(win));
			var menuTop = parseInt($(win).style.top.replace(/px/,""));
			var menuLeft = parseInt($(win).style.left.replace(/px/,""));
	
			mouseX = event.pageX;
			mouseY = event.pageY;
			if(menuTop > mouseY || (menuTop + eleObj.height) < mouseY || menuLeft > mouseX || (menuLeft + eleObj.width) < mouseX){
				Element.remove(win);
			}
		}
	}
}

function getMousePosition(event){
	if(document.all){
		scrolling();
		mouseX = event.x;
		if(navigator.appVersion.search(/MSIE 6.0/)){
			mouseY = event.y+s_y;
		}else{
			mouseY = event.y;
		}
	}else{
		mouseX = event.pageX;
		mouseY = event.pageY;
	}
}

function dragon(n){
	flag=true;
	iname=n;

	// pxを取り除く
	var b = parseInt(document.getElementById(n).style.left.split('px'));
	
	if(document.all){
		winX = event.x - b;
	}else{
		winX = mouseX - b;
	}

}
	
function dragoff(e){
	flag=false;
	obj = null;
}

function scrolling(){
	if(document.all){
		s_x = document.body.scrollLeft;
		//s_y = document.body.scrollTop  || document.documentElement.scrollTop;
		s_y = document.body.scrollTop;
	}else{
		s_x = self.pageXOffset;
		s_y = self.pageYOffset;
	}
}

function dragwin(e){

	scrolling();

	//IE以外の場合、マウス座標を取得
	if(!document.all){
		mouseX = e.pageX;
	}

	if(!flag) return;

	if(document.all){
		document.getElementById(iname).style.left = event.x - winX + 'px';
		document.getElementById(iname).style.top = event.y - 24+s_y + 'px';
	}else{
		document.getElementById(iname).style.left = e.pageX - winX + 'px';
		document.getElementById(iname).style.top = e.pageY - 24 + s_y + 'px';
	}
	return false;
}

/* Tab禁止 */
function cancel_tab(e) {

	if(e == undefined){

		switch(event.keyCode){
		
			case 9:
				if(txtFocusFlag){
					event.returnValue=false;
					InEmoji('\t','text');
					return false;
				}
				break;
			case 37:
				imageNo--;
				pminus();
				n = $('ps'+imageNo).value;
				viewImage2(imageO,imageAid,imageDirname,n,'minus')
				break;
			case 39:
				imageNo++;
				pplus();
				n = $('ps'+imageNo).value;
				viewImage2(imageO,imageAid,imageDirname,n,'plus')
				break;
		
		}

	} else {

		switch(e.keyCode){
			case 9:
				if(txtFocusFlag){
					e.returnValue=false;
					InEmoji('\t','text');
					return false;
				}
				break;
			case 37:
				imageNo--;
				pminus();
				n = $('ps'+imageNo).value;
				viewImage2(imageO,imageAid,imageDirname,n,'minus')
				break;
			case 39:
				imageNo++;
				pplus();
				n = $('ps'+imageNo).value;
				viewImage2(imageO,imageAid,imageDirname,n,'plus')
				break;
		}

	}
}

function pplus(){
	if(!$('ps'+imageNo)){
		if($('almax').value >= (imageNo+1)){
			presentPage++;
			viewalbum(imageAid,'detail',presentPage);
		}else{
			imageNo--;
		}
	}
}
function pminus(){
	if(!$('ps'+imageNo)){
		if(0 < (imageNo-1)){
			presentPage--;
			viewalbum(imageAid,'detail',presentPage);
		}else{
			imageNo++;
		}
	}
}


function InEmoji(InTexts,elements){
	var Txtarea = $(elements);
	InTexts = '' + InTexts + '';
	if(navigator.appName.indexOf("Microsoft") > -1){
			Txtarea.focus();
		if (!document.selection) return;
			var range = document.selection.createRange();
			range.text=InTexts;
			Txtarea.focus();
		} else {
			if (Txtarea.createTextRange && Txtarea.caretPos) {
			var caretPos = Txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? InTexts + '' : InTexts;
			Txtarea.focus();
		} else {
			var length = Txtarea.textLength;
			var start = Txtarea.selectionStart;
			var end = Txtarea.selectionEnd;
			var Txtarea1 = Txtarea.value.substring(0, start);
			var Txtarea2 = Txtarea.value.substr(end, length);
			Txtarea.value = Txtarea1 + InTexts + Txtarea2;
			Txtarea.focus();
		}
	}
}

function ajaxReq(url,cid,win,mode){

	if(mode){
		method = "post";
	}else{
		method = "get";
	}

	new Ajax.Request(url,{
		method:method,
		parameters:cid,
		onFailure:function(httpObj){
			$(win).innerHTML = "エラーで読み込めませんでした";
		},
		onComplete:function(httpObj){
			//$(win).innerHTML = decodeURIComponent(httpObj.responseText);
			if(win){
				Element.update($(win),decodeURIComponent(httpObj.responseText));
			}
			if(win=='popwindow2' || win=='workContainer' || win=='commentContainer' || win=='kaomojiPalette' || win=='photoSelector'){
				return;
			}else{
				fade(win);
			}
		}
	});
	
}

function delImage(){

	var date = new Date();
	var timestamp = date.getTime();
	var text="";
	var counter = 0;

	// k 以下のulタグコレクションを取得し、displayにnoneを設定する
	var uobj = document.getElementsByName("del");
	for(i = 0;i < uobj.length;i++){
		if(uobj[i].checked){
			text = text + '&num['+(counter++)+']='+uobj[i].value;
		}
	}
	ajaxReq("./viewAlbumAjax.php",'aid='+_aid+'&cont=detail&ap='+presentPage+text,'win_middle',1);

}

function doRichEditCommand3(a){

	InEmoji(a,'text');
	Element.remove("kaomojiPalette");

}

function popwinEmoji(event,richmode){

	var showHide = new showHideSelectForm();
	showHide.hide();

	new Insertion.Bottom("primary","<div id='kaomojiPalette'><div style='padding:8px;background-color:#555;color:#fff;'>読み込み・・・</div></div>");
	getMousePosition(event);

	$('kaomojiPalette').style.position = "absolute";
	$('kaomojiPalette').style.top = (mouseY)-80+"px";
	$('kaomojiPalette').style.left = (mouseX)-40+"px";
	$('kaomojiPalette').style.width = "400px";

	if(richmode==1){
		var a = 'mode=rich';
	}else{
		var a = '';
	}

	var date = new Date();
	var timestamp = date.getTime();
	ajaxReq("./emojiPalette.php",a,"kaomojiPalette");

	mouseLeaveWin(event,'kaomojiPalette');
}

function closePalette(win){

	if(win==""){win = 'popwindow';}

	//$(win).style.display = 'none';
	Element.remove(win);

	var showHide = new showHideSelectForm();
	showHide.show();
}

function showHideSelectForm(){

	showHideSelectForm.prototype.hide = function(){
		var obj = $('primary').getElementsByTagName("select");
		for(i=0 ; i<obj.length ; i++){
			obj[i].style.visibility = "hidden";
		};
	};

	showHideSelectForm.prototype.show = function(){
		var obj = $('primary').getElementsByTagName("select");
		for(i=0 ; i<obj.length ; i++){
			obj[i].style.visibility = "visible";
		};
	};
}

function getBrowserWidth ( ) {   
    if ( window.innerWidth ) { return window.innerWidth; }   
    else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { return document.documentElement.clientWidth; }   
    else if ( document.body ) { return document.body.clientWidth; }   
    return 0;   
}

function fade(win){

	if(win != 'topImageSelecterBox' && win != 'popwindow2'){

		$(win).style.top = sy+'px';
		$(win).style.left = sx+'px';
	}
	$(win).style.display = 'block';
}

function fade2(){

	win = 'popwindow2';

	if(win == 'popwindow2'){

		scrolling();
		$(win).style.left = 10 + 'px';
		$(win).style.top = s_y + 100 + 'px';

	}
	$(win).style.display = 'block';

}

function viewfriendslist(aid,cont,ap,mode,user){
	var date = new Date();
	var timestamp = date.getTime();
	var groupid="";
	var winname = "win_middle";

	if($('fselect')){
		groupid = "&friends_group_id="+$('fselect').value;
	}

	if(viewfriendslist.arguments[5]){
		winname = viewfriendslist.arguments[5];
	}

	ajaxReq("./friendsListViewAjax.php",'t='+timestamp+'&aid='+aid+'&cont='+cont+'&ap='+ap+'&mode='+mode+'&user='+user+groupid,winname);
	var showHide = new showHideSelectForm();
	showHide.hide();

	presentPage = ap;
	_aid = aid;
	_cont = cont;
}

function viewalbum(aid,cont,ap){
	ajaxReq("./viewAlbumAjax.php",'aid='+aid+'&cont='+cont+'&ap='+ap,'win_middle');
	presentPage = ap;
	_aid = aid;
	_cont = cont;
}

function viewalbum2(aid,cont,ap){
	ajaxReq("./viewAlbumAjax.php",'aid='+aid+'&ap='+ap+'&type=viewonly','win_middle');
}

function viewalbumcomment(aid,cont,ap){
	requestCounter = 0;
	timerID = setInterval("checkNewComment('album')",45*1000);
	ajaxReq("./viewAlbumCommentAjax.php",'aid='+aid+'&cont='+cont+'&ap='+ap,'comment_box');
}

function friendsGroup(){

	var argStatus = new Array();
	var h = "";

	if($('fgform1')){
		argStatus.push('fgform1='+$('fgform1').value);
	}
	if($('fgform2')){
		argStatus.push('fgform2='+$('fgform2').value);
	}
	if($('fgform3')){
		argStatus.push('status='+$('fgform3').value);
	}
	if($('fgform4')){
		argStatus.push('gid='+$('fgform4').value);
	}

	for(i=0;i < friendsGroup.arguments.length;i++){
		argStatus.push(friendsGroup.arguments[i]);
	}

	if(argStatus.length > 0){
		h = argStatus.join("&");
	}

	ajaxReq("./friendsGroupManagerAjax.php",h,'send_list','post');

}

function checkNewComment(mode,did){

	requestCounter++;
	var a = $('comment_volumea').value;
	var b = $('comment_volumeb').value;

	switch(mode){

		case 'album':
			var requestURL = './checkNewCommentAjax.php';
			var param = 'aid='+_aid+'&a='+a+"&b="+b;
			var linkfunc = 'viewalbumcomment';
			var target_id = _aid;
			break;

		case 'diary':
			var requestURL = './checkNewCommentAjax.php';
			var param = 'aid='+did+'&a='+a+"&b="+b+"&mode=diary";
			var linkfunc = 'viewdiarycomment';
			var target_id = did;
			break;

	}

	new Ajax.Request(requestURL,{
		method:'get',
		parameters:param,
		onComplete:function(httpObj){
			resData = httpObj.responseText;
		},
		onSuccess:function(httpObj){
			resData = httpObj.responseText;
		}
	});

	if(!resData)
		resData = 0;

	if($('comment_volume').value < resData){
		clearTimeout(timerID);
		new Insertion.Top("comment_box","<div style=\"background-color:#FFFFCC;color:#ff0000;padding:5px;\"><a href=\"javascript:void(0)\" onclick=\""+linkfunc+"('"+target_id+"','0','0')\">新しいコメントがあります。</div>");
	}

	if(requestCounter > 30){
		clearTimeout(timerID);
		new Insertion.Top("comment_box","<div style=\"background-color:#FFFFCC;color:#ff0000;padding:5px;\"><a href=\"javascript:void(0)\" onclick=\""+linkfunc+"('"+target_id+"','0','0')\">自動更新を停止しました。</div>");
	}
}


function writeComment(aid,del){
	var delmode;
	a = function (){
		if($('mess').value){
			return $('mess').value;
		}else{
			return "";
		}
	}
	var mes = encodeURIComponent(a());
	if(del){
		delmode="&delmode="+del;
	}else{
		delmode="&delmode=";
	}
	ajaxReq("./viewAlbumCommentAjax.php",'aid='+aid+'&mess='+mes+'&status=write'+delmode,'comment_box',1);
}

function viewImage(o,aid,dirname,image,num){
	var date = new Date();
	var timestamp = date.getTime();
	imageNo = num;
	imageName = image;
	imageO = o;
	imageDirname = dirname;
	imageAid = aid;
	cont='detail';
	ajaxReq("./viewImageAjax.php",'t='+timestamp+'&o='+o+'&aid='+aid+'&dirname='+dirname+'&cont='+cont+'&image='+image,'popwindow2');
}

function viewImage2(o,aid,dirname,image,arrow){
	cont='detail';
	ajaxReq("./viewImageAjax.php",'o='+o+'&aid='+aid+'&dirname='+dirname+'&cont='+cont+'&image='+image+'&arrow='+arrow,'popwindow2');
}

function topImageSelect(aid,cont,ap,event){
	ajaxReq("./topImageSelecterAjax.php",'aid='+aid+'&cont='+cont+'&ap='+ap,'topImageSelecterBox');
}

function setTopimageName(name){
	$('topimageform').value = name;
	closePalette('topImageSelecterBox');
}

function kakunin(a){

	var b;

	if(a=='1'){
		b ='データを削除しようとしています。\r\n\r\n削除してもよろしいですか？';
	}

	if (window.confirm(b)){
		return true;
	}else{
		return false;
	}
}

// sendリストに加える
function addsendlist(uid,mode,del){

	var win = "ac"+uid;
	if($(win)){
		$(win).style.display = 'none';
	}

	if(mode==""){
		mode = 0;
	}
	if(del==""){
		del = 0;
	}

	ajaxReq("./addSendListAjax.php",'aid='+_aid+'&del='+del+'&mode='+mode+'&uid='+uid,'send_list');

	if(del=="del"){
		viewfriendslist(_aid,_cont,presentPage,mode);
	}
}

function ColorfulInput() {
	this.skip  = [];
	this.color = { 'blur': '', 'focus': '#F7F0E6' };

	this.set = function() {
		for (var i = 0; i < document.forms.length; i++) {
			for (var f = 0; f < document.forms[i].length; f++) {
				var elm = document.forms[i][f];
				if(!this._checkSkip(elm)) continue;

				this._setColor(elm, 'focus');
				this._setColor(elm, 'blur');
			}
		}
	}

	this._checkSkip = function(elm) {
		for(var i in this.skip) {
			if(elm.type == this.skip[i]) return false;
		}
		return true;
	}

	this._setColor = function(elm, type) { 
		var color = this.color[type];
		var event = function() { 
			elm.style.backgroundColor = color;

			if(elm.type == 'textarea'){		// textareaにフォーカスがある時のみ、tabキーを無効にする
				if(type == 'focus'){
					txtFocusFlag = 1;
				}else{
					txtFocusFlag = 0;
				}
			}
		};
	
		if(elm.addEventListener) {
			elm.addEventListener(type, event, false); 
		} else if(elm.attachEvent) {
			elm.attachEvent('on'+type, event); 
		} else {
			elm['on'+type] = event;
		}
	}
}

function friendsetting(){

	var argStatus = new Array();
	var dispWidth =getBrowserWidth();
	var h;

	var i=6;
	while(i--){
		if($('gid'+i)){
			argStatus.push('gid'+i+'='+$('gid'+i).value);
		}
	}
	if($('fsform2')){
		argStatus.push('fsf2='+$('fsform2').value);
	}
	if(friendsetting.arguments[1]){
		argStatus.push('status=write');
	}

	scrolling();
	var containerWidth = 500;

	if($('workContainer')){
		Element.remove('workContainer');
	}
	new Insertion.Bottom("primary","<div id='workContainer'></div>");
	$('workContainer').style.width = containerWidth + "px";
	$('workContainer').style.position = "absolute";
	$('workContainer').style.top = (s_y+200)+"px";
	$('workContainer').style.left = ((dispWidth-containerWidth)/2)+"px";
	new Insertion.Top("workContainer","<div id='wcin' style='background-color:#fff;border:1px solid #614A2E;'></div>");
	new Draggable("workContainer");
	friendsetting.arguments[0]

	if(argStatus.length > 0){
		h = argStatus.join("&");
	}

	//ajaxReq("./friendSettingAjax.php",h+'&uid='+friendsetting.arguments[0],'wcin','post');

	var reloadFriendsList = new function(){
		if(friendsetting.arguments[1]){
			setTimeout("viewfriendslist(0,0,0,3,$('userid').value)",200);
		}
	};

	new Ajax.Request('./friendSettingAjax.php',{
		method:'post',
		parameters:h+'&uid='+friendsetting.arguments[0],
		onComplete:function(httpObj){
		},
		onSuccess:function(httpObj){
			$('wcin').innerHTML = decodeURIComponent(httpObj.responseText);
			reloadFriendsList();
		}
	});
}

//----
// album管理
//----
albummanage = new albummanage;
function albummanage() {

	this.firstAttach = function(dat){
		if($('r5').checked){
			this.viewGroup($('r5'),dat);
		}
	},
	this.viewGroup = function(form,dat){
		if(form.value == 4){
			if(!$('groupViewBox')){
				new Insertion.Bottom(form.parentNode,"<div id=\"groupViewBox\"></div>");
			}
			new Ajax.Request('./groupDataJSONAjax.php',{
				method:'post',
				parameters:'',
				onFailure:function(httpObj){
					alert("Ajax通信でエラーが発生しました");
				},
				onComplete:function(httpObj){
					var obj = eval("("+decodeURIComponent(httpObj.responseText)+")");
					var op1 = "";var op2 = "";var op3 = "";
					var pg = dat.split(",");
					for(var i=0;i<obj.length;i++){
						// わかってる。キミの言いたい事はわかってるよ。
						// ↓この書き方は良くないっていうんだろ？
						// わかってるって・・・
						if(obj[i].seq == pg[0]){var selected1 = ' selected';}else{var selected1 = '';}
						if(obj[i].seq == pg[1]){var selected2 = ' selected';}else{var selected2 = '';}
						if(obj[i].seq == pg[2]){var selected3 = ' selected';}else{var selected3 = '';}
						op1 += "<option value=\""+obj[i].seq+"\""+selected1+">"+obj[i].group_name+"</option>"
						op2 += "<option value=\""+obj[i].seq+"\""+selected2+">"+obj[i].group_name+"</option>"
						op3 += "<option value=\""+obj[i].seq+"\""+selected3+">"+obj[i].group_name+"</option>"
					}
					var nosel = "<option value=\"\">指定しない</option>";
					var sel1 = "グループ1：<select name=\"permit1\">"+nosel+op1+"</select><br />";
					var sel2 = "グループ2：<select name=\"permit2\">"+nosel+op2+"</select><br />";
					var sel3 = "グループ3：<select name=\"permit3\">"+nosel+op3+"</select>";
					$('groupViewBox').innerHTML = "<div>■グループを選択してください</div>"+sel1+sel2+sel3;
				}
			});
		}else{
			if($('groupViewBox')){
				Element.remove('groupViewBox');
			}
		}
	}
	
}
