Preview = function(oConfig, tagMngr)
{
	this.init(oConfig, tagMngr)
}

Preview.prototype = {
	
	/*
	 * props
	 */
	config: null,	// config object
	tagMngr: null,	// tagSelector instance
	view: {
		area:		null,
		width:		760,
		height:		450,
		frameId:	'prevFrame'
	},
	form: {
		filtered: null
	},
	
	/*
	 * methods
	 */
	init : function(oConfig, tagMngr)
	{
		this.config	= oConfig;
		this.tagMngr = tagMngr;
		
		YAHOO.util.Event.addListener(
					oConfig.prvBtnId,
					'click',
					this.handlerClick,
					this
					);
	},
	
	handlerClick: function(e, me)
	{
		me.checkRequired();
		me.tagMngr.setPrvHiddens();
		me.filterInput();
	},
	
	checkRequired: function()
	{
		
	},
	
	/* run the input filter by api */
	filterInput: function()
	{
		YAHOO.util.Connect.setForm(this.config.formId);
		
		callback	= {
			success : function(o)
			{
				var response = YAHOO.lang.JSON.parse(o.responseText);
				if (response.result == false)
				{
					alert(response.message);
				}
				else
				{
					var me				= o.argument[0];
					me.form.filtered	= response.data;
					me.initView(me);
				}
			},
			failure : function(o){alert('サーバと通信できませんでした。')},
			argument: [this]
		};
		YAHOO.util.Connect.asyncRequest(
					'post',
					 this.config.prvApi,
					 callback);
	},
	
	/* create preview iframe with template */
	initView: function(me)
	{
		this.view.area	= YAHOO.util.Dom.get(this.config.prvViewId);
		this.view.area.innerHTML	= '<iframe id="' + this.view.frameId + 
								'" width="' + this.view.width +
								'" height="' + this.view.height +
								'" src="' + this.config.prvTpl +
								'">';
		this.view.area.innerHTML	+= "プレビュー機能を使用するには、iframe対応ブラウザをご使用ください。";
		this.view.area.innerHTML	+= '</iframe>';
		
		YAHOO.util.Event.addListener(
					this.view.frameId,
					'load',
					this.show,
					this);
	},
	
	show: function(ev, me)
	{
		var iDoc		= (this.contentWindow || this.contentDocument);
		if (iDoc.document)
		{
			iDoc	= iDoc.document;
		}
		iDoc.getElementById("prv_title").innerHTML		= me.form.filtered.title;
		iDoc.getElementById("prv_descript").innerHTML	= me.form.filtered.descript;
		iDoc.getElementById("prv_link").innerHTML		= 'リンク先URL：' + me.form.filtered.link;
		iDoc.getElementById("prv_author").innerHTML	= '投稿者： ' + me.form.filtered.author;
		
		var tagarea	= iDoc.getElementById('prv_tagarea');
		var tags	= me.form.filtered.tags;
		if (tags.length > 0)
		{
			for (i = 0; i < tags.length; i++)
			{
				var s	= iDoc.createElement('span');
				s.setAttribute( 'class', 'post_tag' );
				s.innerHTML	= tags[i];
				tagarea.appendChild( s );
			}
		}
		
		var now	= new Date();
		iDoc.getElementById("prv_date").innerHTML	= now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + '日';
	}
}
