/**
alert对话框
**/

define('mods/common/alert',function(require,exports,module){

	var $ = require('lib');
	var $reuse = require('mods/dialog/reuse');

	var TPL = {};

	TPL.dialog = [
		'<div class="fxj_layer fxj_layer_4">',
			'<div class="layer_h">',
				'<a href="javascript:;" class="close" data-action="cancel"></a>',
			'</div>',
			'<p class="layer_cont_center1" role="content"></p>',
			'<a href="javascript:;" class="layer_btn" data-action="ok" role="ok">确 定</a>',
		'</div>'
	];

	var cache = $reuse({
		template : TPL.dialog,
		target : 'screen',
		parent : null,
		mask : true,
		styles : {
			'z-index' : 100,
			'position' : 'absolute',
			'margin-left' : 0,
			'margin-top' : 0,
			'display' : 'none'
		}
	});

	module.exports = function(content, options){
		if(!options && $.isPlainObject(content)){
			options = content;
		}else{
			options = options || {};
			options.content = content;
		}

		var conf = $.extend({
			title : '提 示',
			styleOk : '确 定',
			relativeLayer : null,
			ok : $.noop,
			cancel : null
		}, options);

		var dialog = cache.get();
		dialog.role('title').html(conf.title);
		dialog.role('content').html(conf.content);
		dialog.role('ok').html(conf.styleOk);

		dialog.on('ok', conf.ok);
		if($.type(conf.cancel) === 'function'){
			dialog.on('cancel', conf.cancel);
		}else{
			dialog.on('cancel', conf.ok);
		}

		var relativeLayer = conf.relativeLayer;
		var showRelativeLayer = function(){
			if(relativeLayer && $.isFunction(relativeLayer.show)){
				relativeLayer.show();
			}
		};
		var hideRelativeLayer = function(){
			if(relativeLayer && $.isFunction(relativeLayer.hide)){
				relativeLayer.hide();
			}
		};

		//重用对话框时，清除了对话框自动绑定的 ok, cancel 事件
		//所以这里需要重新绑定一次
		dialog.on('ok', function(){
			showRelativeLayer();
			dialog.hide();
		});

		dialog.on('cancel', function(){
			showRelativeLayer();
			dialog.hide();
		});

		dialog.show();
		hideRelativeLayer();
	};

});
