var LangTbl_adjam = {
	'jp': {
		url: 'http://ad.jams.tv'
	},
	'en': {
		url: 'http://en-ad.jams.tv'
	}
};

function adJam( calltype, adid, lang, border_color, container_id, display, callback, classname )
{
	this.addLangMesTbl( LangTbl_adjam );
	this.lang = lang ? lang : 'en';

	this.calltype			= calltype;
	this.adid				= adid;
	this.display			= display || 10;
	this.callback			= callback || 'adJamCallBack';
	this.classname			= classname || 'adjam';

	this.bool_searching		= false;
	this.bool_put 			= false;
	this.bool_startScroll	= false;

	this.container_id		= container_id;
	this.divContainer		= xGetElementById( container_id || 'container_adjam' );

	this._init( border_color );
}

adJam.prototype = new baseLang();

adJam.prototype._init = function( border_color )
{
	this.datalist		= new Array();
	this.scrollHandler	= JMap2EventHandler( this, 'onScroll' );
//	this.layer_put		= document.body;
	this.layer_put		= xGetElementById( 'pHover' ) || document.body;

	this.divPopup = xCreateDiv();
	this.divPopup.style.position			= "absolute";
	this.divPopup.style.zIndex				=  20000;
	this.divPopup.style.border				= "0";
	this.divPopup.style.overflow			= "auto";
	this.divPopup.style.visibility			= "hidden";
	this.divPopup.style.backgroundColor		= "white";
	this.divPopup.style.padding				= '10px';

	this.divPopup.style.borderStyle			= 'dotted';
	this.divPopup.style.borderWidth			= '2px';
	this.divPopup.style.borderColor			= border_color || '#666666';
}

adJam.prototype.search = function( url )
{
	if( this.bool_searching == false ){
	if( this.bool_put == false ){

		xInnerHtml( this.divContainer, "Loading..<img src='http://www.jams.tv/img_jams/loading/small.gif'>" );

		this.bool_searching = true;

		this.parser = new JSONscriptRequest( this.getLangMes('url') + '/?callback=' + this.callback + '&type=' + this.calltype + '&adid=' + this.adid + '&display=' + this.display );
		this.parser.buildScriptTag();
		this.parser.addScriptTag();

	}
	}
}

function adJamCallBack( datalist )
{
	if( adjam.bool_searching ){

		if( datalist ){

			adjam.datalist = datalist;

			var html = "<div style='line-height: 150%;'>";

			html += '<table>'
			for(var i=0; i < datalist.length; i++) {

				var data = datalist[i];

				html += '<tr>';

				html += '<td nowrap valign=top align=right>';
				html += '<font size=2>';
				html += data['day_month'] + '/' + data['day_day'];
				html += '</font></td><td valign=top>';
				html += "<font size=2><a href='javascript:void();' onClick='adjam.popup(" + i + ");return false;'>" + data['title'] + '</a></font>';
				html += '</td>';

				html += '</tr>';
			}

			html += '</table>';

			xInnerHtml( adjam.divContainer, html );

			adjam.parser.removeScriptTag();
			adjam.bool_searching = false;
		}
	}
}

// rePos
adJam.prototype.rePos = function()
{
	setDivPosXY(  this.divPopup, 20, xScrollTop() + 20 );
	setDivSizeWH( this.divPopup, xClientWidth() - 80, xClientHeight() - 60 );
}

// scroll 
adJam.prototype.onScroll = function()
{
	if( this.bool_put == false ){ return; }
	this.rePos();
}

adJam.prototype.startScroll = function()
{
	if( this.bool_put == false ){ return; }

	if( this.bool_startScroll == false ){
		JMap2AttachEvent(window, 'scroll', this.scrollHandler );
		this.bool_startScroll = true;
	}
}

adJam.prototype.stopScroll = function()
{
	if( this.bool_put == false ){ return; }

	if( this.bool_startScroll == true ){
		JMap2DetachEvent(window, 'scroll', this.scrollHandler );
		this.bool_startScroll = false;
	}
}

// popup
adJam.prototype.popup = function( data_id )
{
	if( this.bool_searching == false ){
	if( this.bool_put		== false ){

		var data = this.datalist[data_id];

		if( data ){

			this.rePos();

			var html = '';

			html += "<div align='center'><a href='javascript:void();' onClick='" + this.classname +  ".close();return false;'><font size=3>X CLOSE X</font></a></div>";
			html += '<p></p>';
			html += '<table><tr><td>';
			html += '<font size=3><b>' + data['day_year'] + ' ' + data['day_month'] + '/' + data['day_day'] + ' : ' + data['title'] + '</b></font>';
			html += "<hr style='border-width:1px; border-style:dotted; border-color:#666666'>";
			html += data['detail'];
			html += '</td></tr></table>';

			html += "<hr style='border-width:1px; border-style:dotted; border-color:#666666'>";
			html += "<div align='center'><a href='javascript:void();' onClick='" + this.classname +  ".close();return false;'><font size=3>X CLOSE X</font></a></div>";

			xInnerHtml( this.divPopup, html );

			this.layer_put.appendChild( this.divPopup );
			this.bool_put = true;
		}
	}
	}

	if( this.bool_put ){
		this.startScroll();
		showDiv( this.divPopup );
	}
}

// close
adJam.prototype.close = function()
{
	hideDiv( this.divPopup );
	this.stopScroll();

	if( this.bool_put == true ){
		this.layer_put.removeChild( this.divPopup );
		this.bool_put = false;
	}
}

