/** ***********
cfg参数支持以下选项：
	target			:公告html元素的id
	close			:关闭的html元素的id
	closenever		:关闭并不再显示(记在cookie里)的元素的id
	showtype		:显示方式｛slide|popup}, default 'slide'
	id				:避免跟以前的cookie id重复，如可以用日期来做id
	win_title		:popup方式时，窗口的标题，default 550
	win_width		:popup方式时，窗口的宽度
	win_height		:popup方式时，窗口的高度，不指定或指定为０，表示自动适应
	wincfg			:popup方式时，可以指定窗口的配置(Ext的标准形式，如{title:'×××的公告',header:false,closable:false}，可以在此配置里覆盖以上'win_'打头的配置
	urlflag_in			:可以指定只有当地址栏里有urlflag_in指定的一个或多个特殊标志时，公告才有效
	urlflag_ex			:可以指定只有当地址栏里有urlflag_ex指定的一个或多个特殊标志时，公告没有效
**************/
function Gongao(cfg){
	var _this = this;
	var initialized = false;
	//
	var target_id = Cfg(cfg,'target','');
	var close_id = Cfg(cfg,'close','');
	var closenever_id = Cfg(cfg,'closenever','');
	var showtype = Cfg(cfg,'showtype','slide'); // {slide|popup} default to slide
	var ckname_nevershow = 'starv_gongao_nevershow_'+Cfg(cfg,'id','0');
	//
	var popwin = null;
	
	var needShow = function() {
		var uf_in = Cfg(cfg,'urlflag_in','');
		var uf_ex = Cfg(cfg,'urlflag_ex','');
		var str_url = window.location.href;
		
		if (uf_in.length>0) {
			var f = false;
			if (typeof(uf_in)=='object') {
				for (var i=0; i<uf_in.length; i++) {
					if (str_url.indexOf(uf_in[i])>-1) {
						f = true;
						break;
					}
				}
			}
			if (typeof(uf_in)=='string') {
				if (str_url.indexOf(uf_in)>-1) {
					f = true;
				}
			}
			return f;
		}
		
		if (uf_ex.length>0) {
			var f = true;
			if (typeof(uf_ex)=='object') {
				for (var i=0; i<uf_ex.length; i++) {
					if (str_url.indexOf(uf_ex[i])>-1) {
						f = false;
						break;
					}
				}
			}
			if (typeof(uf_ex)=='string') {
				if (str_url.indexOf(uf_ex)>-1) {
					f = false;
				}
			}
			
			return f;
		}
		
		return true;
	}
	
	this.show = function() {
		//是否需要弹出
		if (!needShow()) return true;
		
		initialize();
		
		var obj = Ext.get(target_id);
		obj.setVisibilityMode(Ext.Element.DISPLAY);
		
		if (Cookies.get(ckname_nevershow)=='1') {
			obj.setVisible(false);
			return true;
		}
		
		if (showtype=='slide') {
			obj.setVisible(true);
			obj.slideIn('t',{duration:0.6, callback:function(){
				obj.highlight("3366FF", {
				    attr: "background-color", //can be any valid CSS property (attribute) that supports a color value
				    endColor: "ffff66",
				    easing: 'easeIn',
				    duration: 1
				});
			}});
		}
		
		if (showtype=='popup') {
			if (popwin==null) {
				obj.setVisible(true);
				var cbj = {
					closable:true, header:true, modal:true, title:Cfg(cfg,'win_title','')
					,width:Cfg(cfg,'win_width',550), layout:'fit', closeAction:'hide', plain:true
					,contentEl:target_id
				};
				
				if (Cfg(cfg,'win_height',0)==0) {
					cbj.autoHeight = true;
				}
				else{
					cbj.height = Cfg(cfg,'win_height',0);
				}
				
				if (Cfg(cfg,'wincfg',false)!=false) {
					Ext.apply(cbj, Cfg(cfg,'wincfg',false));
				}
				popwin = new Ext.Window(cbj);
			}
			popwin.show();
		}
	}
	
	this.hide = function() {
		hide();
	}
	
	this.hide_nevershow = function() {
		Cookies.set(ckname_nevershow, '1');
		hide();
	}
	
	var hide = function() {
		if (showtype=='slide') {
			var obj = Ext.get(target_id);
			obj.slideOut('t',{duration:0.5, callback:function(){obj.setVisible(false)}});
		}
		if (showtype=='popup') {
			popwin.hide();
		}
	}
	
	var initialize = function() {
		if (initialized) return true;
		if (Ext.get(close_id)) {
			Ext.get(close_id).on('click',function(){
				_this.hide();
			});
		}
		if (Ext.get(closenever_id)) {
			Ext.get(closenever_id).on('click',function(){
				_this.hide_nevershow();
			});
		}
		initialized = true;
	}
}
