/** カラムフラグ. */
var moveColumn = true;
/** 移動オブジェクトのoffset値. */
var offset;
/** 移動前の場所. */
var old_parts_id;
/** マウス座標. */
var old_mouse_posY;
/** タイマー変数. */
var mypage_timer;
/** マウスボタンチェックフラグ. */
var mouse_button_flag = false;

/**
 * 移動開始の処理.
 *
 * @param o 親Node
 : @param e event
 */
function phMD(o, e) {
	// マウスイベントチェックはすぐにはしない
	mouse_button_flag = false;
	setTimeout(function() { mouse_button_flag = true; }, 2 * 1000);
	$('.partsCon', $(o)).css('visibility', 'hidden');
	partsHiddenFlash();
	// スクロール用処理
	old_mouse_posY = getMousePosY(e);
	mypage_timer = window.setInterval(chkScroll, 10);
	$('body').css('height', getBodyHeight());
	// 移動用処理
	moveColumn = true;
	if ($(o).parent().attr('id') == 'column0') {
		moveColumn = false;
	}
	old_parts_id = $(o).attr('id');
	if (FX) {
		mpB = o;
	} else {
		mpB = o.cloneNode(true);
		o.parentNode.insertBefore(mpB, o.nextSibling);
	}
	$(mpB).addClass('part999');
	offset = getOffset(o, e);
	$('#columnWrapper0').append(createDummy(o));
	mpO = $('#dmy_parts')[0];
	if (!FX) {
		$(o).remove();
	}
	document.onmousemove = phMM;
	document.onmouseup = phMU;
	$('body').css('cursor', 'move');
	$.each($('.parts'), function() {
		$(this).css('-moz-user-select', 'none');
	});
	onDummyTabs();
	return false;
}
/**
 * 
 */
function createDummy(obj) {
	var w = (obj.clientWidth || obj.offsetWidth);
	var h = (obj.clientHeight || obj.offsetHeight);
	var r = new Array('<div id="dmy_parts" style="');
	r.push('width: ' + w + 'px;');
	r.push('height: ' + h + 'px;');
	r.push('display: none; position: absolute; z-index: 100; border: solid 1px #CCCCCC; cursor: move;');
	r.push('">');
	r.push('</div>');
	return r.join('');
}
function createLoading() {
	var rtn = new Array('<div>');
	rtn.push('<table style="width : 100%;">');
	rtn.push('<tr><td style="height : 100px;" align="center" valign="middle">');
	rtn.push('<img src="/mypage/images/parts/common/IndicatorLite.gif" border="0"></td></tr>');
	rtn.push('</table>');
	return rtn.join('');
}
/**
 * 相対座標を取得する.
 */
function getOffset(o, e) {
	var fg = document.all;
	var mx = (fg) ? event.offsetX : e.pageX;
	var my = (fg) ? event.offsetY : e.pageY;
	var rx = (fg) ? mx : mx - o.offsetLeft;
	var ry = (fg) ? my : my - o.offsetTop;
	return { x : rx, y : ry };
}
/**
 * 移動座標を取得する.
 */
function getMove(e, offset) {
	var rx = ((e) ? e.pageX : event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) - offset.x;
	var ry = ((e) ? e.pageY : event.clientY + (document.documentElement.scrollTop  || document.body.scrollTop )) - offset.y;
	return { x: rx, y: ry };
}
/**
 * 移動中の処理.
 *
 * @param e event
 */
var _mvFlag = 1;
function phMM(e) {
	// スクロール用
	old_mouse_posY = getMousePosY(e);
	// マウスボタンチェック
	if (mouse_button_flag) {
		var eee = eventR(e);
		if (eee.which == 0) {
			phMU(e);
			return false;
		}
	}
	// ２回に１回処理をする
	if (_mvFlag^=1) {
		return;
	}
	var m = getMove(e, offset);
	var px = m.x;
	var py = m.y;
	$(mpO).css({
		left : px + 'px',
		top  : py + 'px',
		display : 'block'
	});
	// tab check
	chkMouseCursor(e);
	// 箱並び替え
	px += (mpO.clientWidth || mpO.offsetWidth) / 2;
	var f = false;
	var st = 0;
	var ed = 0;
	if (moveColumn) {
		st = 1;
		ed = 2;
	}
	for (var i = st; i <= ed; i++) {
		// 判定用の値を取得
		var o = document.getElementById('column' + i);
		var x1 = (o.offsetLeft || o.parentNode.offsetLeft);
		var y1 = (o.offsetTop  || absTop(o.parentNode));
		var x2 = x1 + (o.clientWidth || o.offsetWidth);
		var y2 = y1 + (o.clientHeight || o.offsetHeight);
		// カラムによって判定を変える
		var cflg = false;
		if (moveColumn) {
			cflg = (px > x1 && px < x2);
		} else {
			cflg = (py > y1 && py < y2);
		}
		if (cflg) {
			var p = o.firstChild;
			var y4 = 0;
			while (p) {
				if (p.nodeType == 1) {
					if (p == mpB) y4 = (p.clientHeight || p.offsetHeight) + 16;
					var y3 = ((document.all) ? absTop(p) : p.offsetTop) + p.offsetHeight / 2;
					if (py < y3 - y4) {
						break;
					}
				}
				p = p.nextSibling;
			}
			f = true;
			o.insertBefore(mpB, p);
		}
	}
	return false;
}
/**
 * 移動後の処理.
 *
 * @param e event
 */
function phMU(e) {
	offDummyTabs();
	$('body').css('cursor', '');
	$('body').css('height', '');
	document.onmousemove = null;
	document.onmouseup = null;
	partsVisible();
	if ($(mpO)) {
		$(mpO).remove();
	}
	window.clearInterval(mypage_timer);
	//
	if (chkMouseUp(e)) {
		return;
	}
	// イベント設定＆id振り直し
	$.each($('.parts'), function() {
		$(this).css('-moz-user-select', '');
	});
	$(mpB).removeClass('part999');
	setEventListener($(mpB));
	var arrParts = renameParts();
	if (0 < $('.flash', $(mpB)).length) {
		$('.partsCon', $(mpB)).html(createLoading());
		loadingPartsObj(mpB);
	}
	if (old_parts_id == $(mpB).attr('id')) {
		return;
	}
	// 更新データ送信
	var url = new Array(parts_update_url);
	url.push('?mode=move');
	url.push('&pageseq=' + $('#tabs').attr('active'));
	url.push(arrParts.join(''));
	sendDataAPI(url.join(''),
		function() { return false; },
		function() { return false; });
	return;
}
/**
 *
 */
function absTop(o) {
	var n = 0;
	while (o) {
		n += (o.offsetTop) ? o.offsetTop : 0;
		o = o.parentNode;
	}
	return n;
}

/**
 * タブにドロップされた場合に呼ばれる.
 *
 * @param seq drop tab sequence
 */
function tabDrop(seq) {
	var pid = $(mpB).attr('seq');
	var id = $(mpB).attr('id');
	// 更新データ送信
	var url = new Array(parts_update_url);
	url.push('?mode=tab');
	url.push('&partsseq=' + pid);
	url.push('&topageseq=' + seq);
	url.push('&frompageseq=' + $('#tabs').attr('active'));
	$('#' + id).remove();
	var arrParts = renameParts();
	url.push(arrParts.join(''));
	sendDataAPI(url.join(''),
		function() { return false; },
		function() { return false; }, false);
	if (getPartsTotal() < 1) {
		returnTop();
	}
	return false;
}
/**
 * タブドロップ用変数設定
 */
var drop_tabs = new Array();
function setDropTab() {
	for (var i = 0; i < tabEle.length; i++) {
		var ooo = $(tabEle[i]);
		var o = ooo.offset();
		var h = ooo.height();
		var w = ooo.width();
		var s = ooo.attr('seq');

		drop_tabs[i] = new Array();
		drop_tabs[i].obj = ooo;
		drop_tabs[i].seq = s;
		drop_tabs[i].minx = (o.left + 1);
		drop_tabs[i].miny = (o.top + 1);
		drop_tabs[i].maxx = (o.left + 1) + w;
		drop_tabs[i].maxy = (o.top + 1) + h;
		if (!ooo.attr('id') && !ooo.attr('class')) {
			drop_tabs[i].flag = true;
		} else {
			drop_tabs[i].flag = false;
		}
	}
	return false;
}
/**
 *
 */
function getMousePos(e) {
	var sx = (e) ? e.pageX : event.clientX;
	var sy = (e) ? e.pageY : event.clientY;
	return { x : sx, y : sy };
}
/**
 * 移動中のマウスカーソルを制御.
 *
 * @param e event
 */
function chkMouseCursor(e) {
	var flag = true;
	var pos = getMove(e, {x: 0, y: 0});
	for (var i = 0; i < drop_tabs.length; i++) {
		flag = false;
		var obj = $('.label', drop_tabs[i].obj);
		obj.css('text-decoration', 'none');
		if ((drop_tabs[i].minx <= pos.x && pos.x <= drop_tabs[i].maxx) &&
			(drop_tabs[i].miny <= pos.y && pos.y <= drop_tabs[i].maxy)) {
			var obj = $('.label', drop_tabs[i].obj);
			if (drop_tabs[i].flag) {
				$('body').css('cursor', 'pointer');
				obj.css('text-decoration', 'underline');
			} else {
				$('body').css('cursor', 'default');
			}
		}
	}
	if (flag) {
		$('body').css('cursor', 'move');
	}
	return;
}
/**
 * タブ間移動チェック.
 *
 * @param e event
 */
function chkMouseUp(e) {
	var pos = getMove(e, {x: 0, y: 0});
	for (var i = 0; i < drop_tabs.length; i++) {
		if ((drop_tabs[i].minx <= pos.x && pos.x <= drop_tabs[i].maxx) &&
			(drop_tabs[i].miny <= pos.y && pos.y <= drop_tabs[i].maxy)) {
			if (drop_tabs[i].flag) {
				$('.label', drop_tabs[i].obj).css('text-decoration', 'none');
				tabDrop(drop_tabs[i].seq);
				return true;
			}
			return false;
		}
	}
	return false;
}

/**
 * コンテンツ追加リンク.
 */
function hrefContents(keyword){
	location.href= 'admin-contents?category=' + encodeURIComponent(keyword);
}
/**
 * パーツのタイトルを設定する.
 *
 * @param obj object
 * @param ttl title
 */
function setPartsTitle(obj, ttl) {
	$('.name', obj).html(convRetString(ttl));
	return false;
}
function convRetString(val) {
	if(navigator.appName == 'Netscape') { // mozilla familly only
		var res = "";
		var pos = 0;
		for (chIdx = 0; chIdx < val.length; chIdx++ ) {
			var ch = val.charAt(chIdx);
			if (ch == '\r' || ch == '\n' || ch == '\r\n') { // escape return code
				res += ch;
				continue;
			}
			if (ch == '&') {
				pos = val.indexOf(';', chIdx) - chIdx;
				if (!(pos < 0 || pos > 6)) {
					// skip ref
					res += val.substring(chIdx, (chIdx + pos + 1));
					res += '<wbr>';
					chIdx += pos;
					continue;
				}
			}
			if (ch == '<') {
				pos = val.indexOf('>', chIdx) - chIdx;
				if (!(pos < 0 || pos > 7)) {
					// skip ref
					res += val.substring(chIdx, (chIdx + pos + 1));
					chIdx += pos;
					continue;
				}
			}
			// single byte only
			if (isMultibyteCharacters(ch)) {
				res += ch;
			} else {
				res += ch;
				res += '<wbr>';
			}
		}
		return res;
	}
	return val;
}
// check multibyte
function isMultibyteCharacters(ch) {
	return !(ch.charCodeAt(0) < 256);
}

/**
 * パーツ内容を書き換える.
 *
 * @param obj object
 * @param val 表示する内容
 */
function setPartsContents(obj, val) {
	$('.partsCon', obj).html(val);
	return false;
}
/**
 * eventを共通で使えるように.
 */
function eventR(e) {
	if(typeof(e) == 'undefined') e = window.event;
	if(typeof(e.layerX) == 'undefined') e.layerX = e.offsetX;
	if(typeof(e.layerY) == 'undefined') e.layerY = e.offsetY;
	if(typeof(e.which) == 'undefined') e.which = e.button;
	return e
}
/**
 * マウスY座標(相対)
 */
function getMousePosY(e) {
	var pos = getMousePos(e);
	return pos.y - ((e) ? (document.documentElement.scrollTop || document.body.scrollTop) : 0);
}
/**
 * スクロール.
 */
function chkScroll() {
	var mov = 5;
	if (old_mouse_posY < 80) {
		window.scrollBy(0, -mov);
	}

	if (getWindowHeight() + (document.documentElement.scrollTop || document.body.scrollTop) < getBodyHeight()) {
		if ((getWindowHeight() - 80) < old_mouse_posY) {
			window.scrollBy(0, mov);
		}
	}
}
/**
 * 表示ウィンドウの高さを取得する.
 */
function getWindowHeight() {
	var h;
	if (window.innerHeight) {
		h = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		h = document.documentElement.clientHeight;
	} else {
		h = document.body.offsetHeight;
	}
	if (h < document.body.clientHeight) {
		return h;
	}
	return document.body.clientHeight;
}
/**
 * ウィンドウ全体の高さを取得する.
 */
function getBodyHeight() {
	if (document.documentElement.clientHeight < document.body.scrollHeight) {
		return document.body.scrollHeight;
	}
	return document.documentElement.clientHeight;
}
/**
 *
 */
function onDummyTabs() {
	if (FX) {
		return false;
	}
	$.each(tabEle, function(i) {
		if (!$(this).attr('id') && !$(this).attr('class')) {
			$('a', $(this)).css('cursor', 'pointer');
		} else {
			$('a', $(this)).css('cursor', 'default');
			if ($(this).attr('class') && -1 != $(this).attr('class').indexOf('deletable')) {
				$(this).removeClass('deletable');
			}
		}
	});
	return false;
}
function offDummyTabs() {
	if (FX) {
		return false;
	}
	$.each(tabEle, function(i) {
		$('a', $(this)).css('cursor', '');
		if ($(this).attr('class') && -1 != $(this).attr('class').indexOf('on')) {
			$(this).addClass('deletable');
		}
	});
	return false;
}
