function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	o = document.getElementById(objectId);
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

function getAbsoluteTop(objectId) {
	o = document.getElementById(objectId);
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}

function blockEvents(evt) {
	if(evt.target){
		evt.preventDefault();
	}else{
		evt.returnValue = false;
	}
}

////////////////////////////////////////////////////////////
// KB original
$(function()
{
	// add
	$('a._kb-btn_').click(function(){
		var _width = 200;

		$('#KB').remove();
		var url = $(this).attr('rel');
		var id = $(this).attr('id');
		var title = $(this).attr('title');

		/*
		// set xy position
		var _y = getAbsoluteTop(id) - 3;
		var _o = getElementWidth(id) + 11;
		var _x = getAbsoluteLeft(id) + _o;

		$('body').append(
			'<div id="KB">'+
				'<div id="KB_arrow"></div>'+
				'<div id="KB_title">'+title+'</div>'+
				'<div id="KB_content"></div>'+
			'</div>'
		);
		*/

		$.ajax({
			type: 'GET',
			url: url,
			dataType: 'xml',
			success: function(res){
				if ($('status', res).text() == 'success') {
					// num
					var v = $('num', res).text();
					$('#_kb-num_').html(v);
					// checkbox
					var v1 = $('id', res).text();
					var v2 = $('url', res).text();
					var v3 = $('name', res).text();
					$('#_kb-holder_').append(
						'<li>'+
							'<input type="checkbox" name="ids[]" value="'+v1+'" checked="checked" />'+
							'<a href="'+v2+'">'+v3+'</a>' +
						'</li>'
					);
					// message
					var v = $('message', res).text();
					alert(v);
					/*
					$('#KB_content').html(v);
					$('#KB').css({
						left: _x+'px',
						top: _y+'px'
					}).show().click(function(){
						$(this).remove();
					});
					*/
				}
			}
		});
		return false;
	});

	// contact
	$('#_kb-contact_').click(function(){
		var a = [];
		$('input[name="ids[]"]:checked').each(function(){
			a.push($(this).val());
		});
		var v = a.join(',');
		if (v == '') {
			alert('チェックを入れてください');
		} else {
			window.location.href = './?action=keep-contact&ids='+v;
		}
		return false;
	});

	// del
	$('#_kb-del_').click(function(){
		var a = [];
		$('input[name="ids[]"]:checked').each(function(){
			a.push($(this).val());
		});
		var v = a.join(',');
		if (v == '') {
			alert('チェックを入れてください');
		} else {
			$.get(
				'./?action=keepbox&del='+v,
				function(){
					window.location.reload();
				}
			);
		}
		return false;
	});

	// test
	$('#_kb-test_').click(function(){
		$('#_kb-result_').hide();
		var v = $('input[name=mail]').val();
		$.ajax({
			type: 'GET',
			url: './?action=keep-test&send=1&mail='+v,
			dataType: 'xml',
			success: function(res){
				if ($('status', res).text() == 'success') {
					$('#_kb-result_').show();
					$('input[name=mail]').val('')
				} else {
					var v = $('err', res).text();
					if (v) alert(v);
				}
			}
		});
	});
});

