/*-----------
	Event
------------*/
document.observe('mouseup', function(ev){
	var validate = readCookie("searchoff");
	if(validate!='' && validate==0){
		if(typeof tipobj_wp=="undefined" || tipobj_wp==null){
			posX = getMouseX(ev);
			posY = getMouseY(ev);
			var str = getText();
			if(str != null){
				CreateTip(0, str, posX, posY);
			}
		}
	}
},false);

document.observe('prototip:hidden', function(event) {
	if(!(typeof tipobj_wp=="undefined" || tipobj_wp==null)){
		Tips.remove('wrapper');
		tipobj_wp = null;
	}
});

/*-----------
	Global変数
------------*/
var tipobj = new Array();
var tipobj_wp = null;

/*-----------
	function
------------*/
function CreateTip(no, val, posX, posY){
	if(no > 0){
	//クリックtooltip
		if((typeof tipobj[no]=="undefined" || tipobj[no]==null)){
		tipobj[no] = new Tip('ptip['+no+']', {
			title : 'なぞり検索',
			target: 'ptip['+no+']',
			ajax: {
				url: '/js/tooltip_ajax.php',
				options: {
					parameters: "text=" + encodeURIComponent(val),
					onComplete: function(transport) {
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			width: '280px',
			hook: { target: 'bottomMiddle', tip: 'topMiddle' },
			stem: 'topMiddle',
			showOn: 'click',
			className: 'protoClassic',
			offset: { x: 15, y: 3 }
		});
		}
	}else{
	//ハイライトtooltip
		if(posX + 300 > getWindowSize()){
			posX = posX - 160;
			tipobj_wp = new Tip('wrapper', {
				title : 'なぞり検索',
				ajax: {
					url: '/js/tooltip_ajax.php',
					options: {
						parameters: "text=" + encodeURIComponent(val),
						onComplete: function(transport) {
						}
					}
				},
				hideOn: { element: 'closeButton', event: 'click' },
				width: '280px',
				fixed: true,
				//hook: { mouse: true, tip: 'topMiddle' },
				stem: 'topMiddle',
				showOn: 'click',
				className: 'protoClassic',
				offset: { x: posX, y: posY }
			});
		}else{
			tipobj_wp = new Tip('wrapper', {
				title : 'なぞり検索',
				ajax: {
					url: '/js/tooltip_ajax.php',
					options: {
						parameters: "text=" + encodeURIComponent(val),
						onComplete: function(transport) {
						}
					}
				},
				hideOn: { element: 'closeButton', event: 'click' },
				width: '280px',
				fixed: true,
				stem: 'topLeft',
				showOn: 'click',
				className: 'protoClassic',
				offset: { x: posX, y: posY }
			});
		}
	}
}


function getText(){
	var isMSIE = /*@cc_on!@*/false;
	var SelectedText;
	if(isMSIE){
		SelectedText = document.selection.createRange().text;
	}else{
		SelectedText = window.getSelection().toString();
	}
	str = SelectedText.trim();
	len = str.length;
	if(len > 0 && len <= 20){
		return str;
	}else{
		return null;
	}
}

function getMouseX(e){
	if(window.opera){
		return e.clientX;
	}else if(document.all){
		//return document.body.scrollLeft + event.clientX;
		return e.pageX;
	}else if(document.layers||document.getElementById){
		return e.pageX;
	}
}
function getMouseY(e){
	if(window.opera){
		return e.clientY;
	}else if(document.all){
		//return document.body.scrollTop+event.clientY;
		return e.pageY;
	}else if(document.layers||document.getElementById){
		return e.pageY;
	}
}

function getWindowSize(){
	var ua = navigator.userAgent; // ユーザーエージェント
	var nWidth;                   // サイズ
	var nHit = ua.indexOf("MSIE");// 合致した部分の先頭文字の添え字
	var bIE = (nHit >=  0);       // IE
	var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");// バージョン 6
	var bStd = (document.compatMode && document.compatMode=="CSS1Compat");

	if (bIE) {
		if (bVer6 && bStd) {
			nWidth = document.documentElement.clientWidth;
		} else {
			nWidth = document.body.clientWidth;
		}
	} else {
		nWidth = window.innerWidth;
	}
	return nWidth;
}

String.prototype.trim = function() {
    return this.replace(/^[\s　]+|[\s　]+$/g, '');
}

/*-----------
   機能On/Off
------------*/
function searchOff(){
	tm = new Date();
	tm.setTime(tm.getTime() + (365 * 1000 * 60 * 60 * 24));
	tm = tm.toGMTString();
	document.cookie = "searchoff=1;path=/;expires=" + tm;
	$('srch_trc').innerHTML = '<a href="#" onclick="searchOn();" class="pageSearchBtnOff" title="なぞり検索を使用する">なぞり検索ON</a>';
}
function searchOn(){
	tm = new Date();
	tm.setTime(tm.getTime() + (365 * 1000 * 60 * 60 * 24));
	tm = tm.toGMTString();
	document.cookie = "searchoff=0;path=/;expires=" + tm;
	$('srch_trc').innerHTML = '<a href="#" onclick="searchOff();" class="pageSearchBtnOn" title="なぞり検索を使用しない">なぞり検索OFF</a>';
}
function readCookie(key){
	var sCookie = document.cookie;
	var aData = sCookie.split(";");
	var oExp = new RegExp(" ", "g");
	key = key.replace(oExp, "");

	var i = 0;
	while (aData[i]) {
		var aWord = aData[i].split("=");
		aWord[0] = aWord[0].replace(oExp, "");
		if (key == aWord[0]) return unescape(aWord[1]);
		if (++i >= aData.length) break;
	}
	return '';
}

