// ======================================================
//
// memo
//
// ======================================================

function JMemo( lang )
{
	this.bool_init			= false;
	this.bool_init_append	= false;
	this.bool_posting		= false;
	this.bool_show			= false;
	this.bool_startScroll	= false;

	this.lang 		= lang ? lang : 'jp';
	this.size		= new Size( 200, 50 );

	this.divInfo = xCreateDiv();
	this.divInfo.style.position			= 'absolute';
	this.divInfo.style.visibility		= 'hidden';
	this.divInfo.style.backgroundColor	= '#ffffe0';
	this.divInfo.style.borderStyle		= 'solid';
	this.divInfo.style.borderWidth		= '2px';
	this.divInfo.style.borderColor		= '#cc6666';
	this.divInfo.zIndex					= 60000;
	setDivSize( this.divInfo, this.size );

	this.url			= '/jsonbin/addMemo';
	this.scrollHandler	= JMap2EventHandler( this, 'onScroll' );
	this.type_memo		= '';

	this.bool_init		= true;
}

// show, hide
JMemo.prototype.show = function()
{
	if( this.bool_init == false ){ return; }

	showDiv( this.divInfo );
	this.bool_show = true;
}
JMemo.prototype.hide = function()
{
	if( this.bool_init == false ){ return; }

	hideDiv( this.divInfo );
	this.bool_show = false;
	this.clearShowTime();
}

// showTime
JMemo.prototype.setShowTime = function()
{
	this.clearShowTime();
	this.id_showTime = setTimeout("obj_jsonJMemo.hide()", 4000);
}
JMemo.prototype.clearShowTime = function()
{
	if( this.id_showTime ){
		clearTimeout( this.id_showTime );
	}
	this.id_showTime = null;
}

// rePos
JMemo.prototype.rePos = function()
{
	if( this.bool_init == false ){ return; }

	setDivPosXY( this.divInfo, xClientWidth() - this.size.width - 30, xScrollTop() );
}

// scroll 
JMemo.prototype.startScroll = function()
{
	if( this.bool_init == false ){ return; }

	if( this.bool_startScroll == false ){
		JMap2AttachEvent(window, 'scroll', this.scrollHandler );
		this.bool_startScroll = true;
	}
}

JMemo.prototype.onScroll = function()
{
	if( this.bool_init == false ){ return; }

	this.rePos();
}

JMemo.prototype.stopScroll = function()
{
	if( this.bool_init == false ){ return; }

	JMap2DetachEvent(window, 'scroll', this.scrollHandler );
}

// post
JMemo.prototype.post = function( type_memo, id )
{
	if( this.bool_init	  == false ){ return; }
	if( this.bool_posting == true  ){ return; }
	if( this.bool_init_append == false ){
		document.body.appendChild( this.divInfo );
		this.bool_init_append = true;
	}

	this.type_memo = type_memo;

	this.bool_posting = true;

	var url = this.url;

	url += "/type_memo-" + type_memo;
	url += "/type_id-"	 + id;
	url += "/lang-"		 + ( this.lang || 'jp' );

	var xml = new JKL.ParseXML( url );
	xml.async( json_JMemo_results );
	xml.parse();

	this.clearShowTime();
	this.startScroll();
	this.rePos();
	this.show();

	this.divInfo.style.backgroundColor	= '#d0d0d0';

	var mes = '';
	if( this.lang == 'jp' ){
		mes = 'メモに追加中です';
	}else if( this.lang == 'en'){
		mes = 'Adding to MEMO';
	}
	xInnerHtml( this.divInfo, "<div align=right><a href='javascript:void();' onClick='javascript:obj_jsonJMemo.postCancel();return false;'>[ X ]</a></div><div align=center>" + mes + "<img src='/img_pitt/loading/small.gif'></div>" );
}

JMemo.prototype.postCancel = function()
{
	this.bool_posting = false;
	this.clearShowTime();
	this.hide();
}

// result
function json_JMemo_results( data )
{
	if(	obj_jsonJMemo.bool_posting == true ){
		obj_jsonJMemo.bool_posting = false;

		var url_list = 'http://user.pitt-travel.com.au/plog/user_memo/type_memo-' + obj_jsonJMemo.type_memo + '/search-clear';

		if( data['items']['bool_success'] ){
			obj_jsonJMemo.divInfo.style.backgroundColor	= '#ffffaa';
			xInnerHtml( obj_jsonJMemo.divInfo, "<BR><div align=center valign=middle>メモに追加しました<BR><a href='" + url_list + "'>メモ一覧を見る</a></div>" );
		}else{
			obj_jsonJMemo.divInfo.style.backgroundColor	= '#ffcccc';
			xInnerHtml( obj_jsonJMemo.divInfo, "<BR><div align=center valign=middle>" + data['items']['error_mes'] + "<BR><a href='" + url_list + "'>Check MEMO</a></div>");
		}

		obj_jsonJMemo.setShowTime();
	}
}

var obj_jsonJMemo = new JMemo();

