var Fangfa=new Object();

Fangfa.EventMonitor = function(){
	this.listeners = new Object();
}
Fangfa.EventMonitor.prototype.broadcast=function(widgetObj, msg, data){
	var lst = this.listeners[msg];

	if(lst != null){
		for(var o in lst){
			lst[o](widgetObj, data);
		}
	}
}

Fangfa.EventMonitor.prototype.subscribe=function(msg, callback){
	var lst = this.listeners[msg];
	if (lst) {
		lst.push(callback);
	} else {
		this.listeners[msg] = [callback];
	}
}
Fangfa.EventMonitor.prototype.unsubscribe=function(msg, callback){
	var lst = this.listener[msg];
	if (lst != null){
		lst = lst.filter(function(ele, index, arr){return ele!=callback;});
	}
}

// Page scope event-monitor obj.
var event_monitor = new Fangfa.EventMonitor();

function load_event_monitor(root) {
	var re = /a_(\w+)/;
	var fns = {};
	$(".j", root).each(function(i) {
		var m = re.exec(this.className);
		if (m) {
			var f = fns[m[1]];
			if (!f) {
				f = eval("Fangfa.init_"+m[1]);
				fns[m[1]] = f;
			}
			f && f(this);
		}
	});
}

$(function() {
	load_event_monitor(document);
});

Fangfa.init_alert = function(o) {
	$(o).click(function(){
		alert($(this).parent().html());
	});
}

//投诉建议
Fangfa.init_suggest = function(o){
	$(o).submit(function(){
		var errmsg = '';

		if(!$.trim($('#sugest_title').val())){
			errmsg = '请填写主题。';
			$('#sugest_title').focus();
		}else if(!$.trim($('#sugest_u_address').val())){
			errmsg = '请填写联系地址。';
			$('#sugest_u_address').focus();
		}

		if(errmsg) alert(errmsg);

		return !errmsg;
	});
}

//预定
Fangfa.init_order = function(o){
	$(o).submit(function(){
		var errmsg = '';
		if(!$.trim($('#order_user_name').val())){
			errmsg = '请填写姓名。';
			$('#order_user_name').focus();
		}else if(!$.trim($('#order_user_document_name').val())){
			errmsg = '请填写证件名称。';
			$('#order_user_document_name').focus();
		}else if(!$.trim($('#order_user_document_no').val())){
			errmsg = '请填写证件号码。';
			$('#order_user_document_no').focus();
		}else if(!$.trim($('#order_room_cat').val())){
			errmsg = '请填写订房类型。';
			$('#order_room_cat').focus();
		}else if(!$.trim($('#order_room_cat').val())){
			errmsg = '请填写入住人数。';
			$('#order_user_number').focus();
		}else if(!$.trim($('#order_room_count').val())){
			errmsg = '请填写订房数。';
			$('#order_room_count').focus();
		}else if(!$.trim($('#order_start_time').val())){
			errmsg = '请填写进店日期。';
			$('#order_start_time').focus();
		}else if(!$.trim($('#order_user_phone').val())){
			errmsg = '请填写联系电话。';
			$('#order_user_phone').focus();
		}else if(!$.trim($('#order_user_email').val())){
			errmsg = '请填写Email。';
			$('#order_user_email').focus();
		}

		if(errmsg) alert(errmsg);

		return !errmsg;
	});
}

//留言
Fangfa.init_guestbook = function(o){
	$(o).submit(function(){
		var errmsg = '';
		if(!$.trim($('#guestbook_u_name').val())){
			errmsg = '请填写姓名。';
			$('#guestbook_u_name').focus();
		}else if(!$.trim($('#guestbook_title').val())){
			errmsg = '请填留言标题。';
			$('#guestbook_title').focus();
		}else if( (typeof($('#check_code').attr('name')) !== 'undefined') &&  !$.trim($('#check_code').val()) ){
			errmsg = '请填验证码。';
			$('#check_code').focus();
		}

		if(errmsg) alert(errmsg);

		return !errmsg;
	});
}

//验证码
Fangfa.init_checknum = function(o){
	$(o).click(function(){
		var r = $(this).attr('src').split('?')[0];
		$(this).attr('src',r+'?'+Math.random());
	});
}

//得到新闻详细
Fangfa.init_get_subject_news = function(o){
	$(o).click(function(){
		$('#news').hide();
		//alert($(this).parent().html());
		$('.page_list_li2').each(function(i){
			$(this).removeClass('page_list_li2').addClass('page_list_li');
		});
		$(this).parent().removeClass('page_list_li').addClass('page_list_li2');
		// get content;
		$.getJSON($(this).attr('href'),function(data){
			//
			$('#news').show();
			//
			$('#news_title').html(data.subject_title);
			$('#news_addtime').html(data.subject_ctime);
			$('#news_hits').html(data.subject_click);
			$('#news_co').html(data.subject_intro);
		});
		return false;
	});
}

Fangfa.init_search_q = function(o){
	$(o).focus(function(){
		if($(this).val() == 'search' || $(this).val() =='输入关键词'){
			$(this).val('');
		}
	});
	if($(o).val() == ''){
		$(o).val('输入关键词');
	}
}

Fangfa.init_user_login = function(o){

}
Fangfa.init_user_register_but = function(o){
	$(o).click(function(){
		window.location.href='user/register';
	});
}
//加入收藏夹
Fangfa.init_add_favorite = function(o){
	$(o).click(function(){
		var surl = window.location;
		var stitle = document.title;
		try{
			window.external.addFavorite(surl, stitle);
		}catch (e){
			try{
				window.sidebar.addPanel(stitle, surl, "");
			}catch (e){
				alert("加入收藏失败，请使用Ctrl+D进行添加");
			}
		}
	});
}
//设置为首页
Fangfa.init_sethome = function(o){
	$(o).click(function(){
		try{
			this.style.behavior='url(#default#homepage)';this.setHomePage(window.location);
		}
		catch(e){
			if(window.netscape) {
				try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				}catch (e) {
					alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
				}
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',window.location);
			}
		}

	});
}

//table 轮动
Fangfa.init_ld = function(o){
	$(o).mouseover(function(){
		$('.anli_bg2').each(function(){
			$(this).hide();
		})
		//
		var id = $(this).attr('id').split('_')[1];
		$('#ld_p_'+id).show();
	});
	//	$('#ld_1').mouseover();
	$('.a_ld').eq(0).mouseover();
}

Fangfa.init_room_create = function(o){
	$(o).click(function(){
		$("#room_create").before('<div ><input name="store_product_title_[]" type="text" value=""  style="width:5em;" /> <input name="store_product_price_[]" type="text" value=""  style="width:5em;" /></div>');
	});
	//$('#room_create').click();
}

Fangfa.init_booking_order = function(o){
	$('.orderthis').each(function(i){

		$(this).click(function(){
			var id = $(this).attr('id').split('-')[1];
			$('#order-count-'+id).toggle();
		});
	});


}

Fangfa.init_autoheight = function(o){

}

function closefloat(){
	document.getElementById("img1").style.visibility='hidden';
}

window.onload = function(){
	try{
		$("div#mid>ul:first-child").css("min-height","455px");
		$("div#page_view").css("min-height","512px");
	}catch(err){

	}
}


//window.onerror=function(){
//	return false;
//}
