var hotels = null, dn = 0, wks = '', agent_id = 0;

//加载价格
function loadPrice(ids){
	var basePath = $($("body")[0]).attr("base");
	$.post(basePath + '?m=room.getListByHotel', { ids:ids }, function(data) {//alert(data);
		//document.write(data);
		hotels = eval("(" + data + ")");
		for(var i = 0; i < hotels.length; i++){
			loadHotel(hotels[i], true);
		}
	});
}
//加载酒店
function loadHotel(hotel, hide) {
	var O_RightPart = $("#hotelrightdiv" + hotel.hotelId);//右边总框架
	var aryIndate = hotel.indate.split('-');
	if(dn == 0) {
		dn = DateDiff('d', sToDate(hotel.indate), sToDate(hotel.outdate));
		var sIndate = sToDate(hotel.indate).format('MM\u6708dd\u65E5');
		var sOutdate = DateAdd('d',1,sToDate(hotel.indate)).format('MM\u65E5dd\u65E5');
		if(dn < 2){
			tb1 = '<span class="row2">' + sIndate + '</span>';
		} else if(dn == 2) {
			tb1 = '<span class="row2">' + sIndate + '</span><span class="row3">' + sOutdate + '</span>';
		} else {
			tb1 = '<span class="row2">' + sIndate + '</span><span class="row3">日均价</span>';
		}
		if(dn > 0) {
			for(var i = 0; i < dn; i++) {
				wks += '        <dt>' + DateAdd('d', i, sToDate(hotel.indate)).format('EE') + '</dt>';
				if(i > 5) i = 99999;
			}
			
		}
	}
	var	html = '<div class="room">';
	//title
	html += '<div class="room_title">';		
	html += '<div class="row"><span class="row8">说明</span><span class="row1">门市价</span>' + tb1 + '<span class="row4">返点现金</span><span class="row6">宽带</span><span class="row7">预订</span></div>';
	html += '房型名称';
	html += '</div>';	
	//content
	var showNum = hotel.rooms.length;
	if(hide && showNum > 3) { showNum = 3; }
	for(var j = 0; j < showNum; j++) {
		html += loadRoom(hotel.hotelId, hotel.rooms[j]);
	}
	//showAllBtn
	var height = 247;
	html += '<div class="room_more">';
	if(hide) {	
		html += '<a class="r_more" href="javascript:refreshHotel(' + hotel.hotelId + ',false);">所有房型(' + hotel.rooms.length + ')</a>';
	} else {
		html += '<a class="r_more1" href="javascript:refreshHotel(' + hotel.hotelId + ',true);">所有房型(' + hotel.rooms.length + ')</a>';
		if(hotel.rooms.length > 3) { height = 247 + 29 * (hotel.rooms.length - 3); }
	}
	html += '</div>';
	O_RightPart.height(height);
	html += '</div>';	
	$("#h" + hotel.hotelId).html(html);
}
//刷新酒店加载
function refreshHotel(hotelId, hide) {
	for(var k = 0; k < hotels.length; k++) {
		if(hotels[k].hotelId == hotelId) {
			loadHotel(hotels[k], hide);
			return;
		}
	}
}
//加载客房
function loadRoom(hotelId, room) {
	var basePath = $($("body")[0]).attr("base");
	html = '<div class="room_row">';
	html += '<div class="row">';
	html += '<span class="row8" onMouseOver="tips(this,\'v1\',\'' + hotelId + '\',\'' + room.roomId + '\',\'' + room.plans.planId + '\')"><a class="room_name" href="javascript:void(0)" onclick="showPanel(\'' + hotelId + '\',\'' + room.roomId + '\');">' + room.plans.planName + '</a></span>';
	html += '<span class="row1">' + (room.plans.marketPrice ? "<del>" + room.plans.marketPrice + "</del>元" : "无报价") + '</span>';
	html += '<span class="row2">' + (room.plans.date[0] ? room.plans.date[0].price + '元' : '无报价') + '</span>';
	if(dn >= 2) {
		if(room.plans.date.length > 0) {
			html += '<span class="row3" onMouseOver="tips(this,\'v3\',\'' + hotelId + '\',\'' + room.roomId + '\',\'' + room.plans.planId + '\')"><a href="javascript:void(0)" >' + parseInt(room.plans.totalprice / room.plans.date.length) + '元</a></span>';
		} else {
			html += '<span class="row3">无报价</span>';
		}
	}
	html += '<span class="row4" onMouseOver="tips(this,\'v2\',\'' + hotelId + '\',\'' + room.roomId + '\',\'' + room.plans.planId + '\')"><strong>' + room.plans.takeMoney + '</strong>元</span>';
	html += '<span class="row6">' + room.internet + '</span>';
	html += '<span class="row7">';
	switch(room.status) {
		case '0':
			html += '<div class="btn11"><a href="' + basePath + 'order/book-' + room.roomId + '.html" target="_blank">预订</a></div>';
			break;
		case '1':
			html += '<div class="btn12"><a>满房</a></div>';
			break;
		default:
			html += '<div class="btn12"><a>无报价</a></div>';
	}	
	html += '</span>';
	html += '</div>';
	html += '<a class="room_name" href="javascript:void(0)" onclick="showPanel(\'' + hotelId + '\',\'' + room.roomId + '\');">' + room.roomName + '</a>';
	html += '</div>';	
	html += '</div>';	
	//tips
	html += '<div id="ri_' + room.roomId + '" class="room_info" style="display: none;">';
	html += '<ul>';
	if(room.bed.length > 0) { html += '<li>·床位类型：' + room.bed + '</li>'; }
	if(room.size.length > 0) { html += '<li>·客房面积：' + room.size + '</li>'; }
	if(room.internet.length > 0) { html += '<li>·宽带服务：' + room.internet + '</li>'; }
	if(room.canAddBed.length > 0) { html += '<li>·可否加床：' + room.canAddBed + '</li>'; }
	if(room.plans.planName.length > 0) { html += '<li>·早餐服务：' + room.plans.planName + '</li>'; }
	if(room.notes) { html += '<li style="width:99%;">·房间介绍：' + room.notes + '</li>'; }
	html += '</ul>';
	html += '</div>';
	return html;
}
//显示隐藏客房介绍面板
function showPanel(hotelId,roomId) {
	var hoteldiv = _g('hotelrightdiv' + hotelId);
    var obj = _g('ri_' + roomId);
    var curHidden = _g('curHidden' + hotelId);
    
    var heighttmp = hoteldiv == null ? "" : hoteldiv.style.height;
    var height = heighttmp.length == 0 ? 247 : parseInt(heighttmp.replace('px', ''));
	if(obj.style.display != 'block'){
	    obj.style.display = 'block';//显示
	    if(curHidden != null && obj != null) {
	        curHidden.value = obj.clientHeight;
	        height = height + parseInt(curHidden.value);
	    }
	} else {
	    obj.style.display = 'none';//隐藏
	    if(curHidden != null) {
	        height = height - parseInt(curHidden.value);
	    }
	}
	if(hoteldiv != null) { hoteldiv.style.height = height + 'px'; }
}
//获取dom
function _g(id) { return document.getElementById(id); };
var mdiv = document.createElement("div"), timeOut = null;
//动态生成提示框
function tips(cusObj, type, hotelId, roomId, planId) {
	var html = '', _plan = gPlan(hotelId, roomId, planId);
	if(type == 'v1') {
		mdiv.className = 'room_tips';
		mdiv.style.width = '400px';
		mdiv.style.left = (gL(cusObj) - parseInt(mdiv.style.width) / 2 + 15) + 'px';		
			html += '<div class="rt_con">';
			if(agent_id == 20) {
				html += '<p class="rt_con_jj">预订并成功入住后，每间夜可获得<span>' + _plan.takeMoney + '</span>元奖金，离店后登录珠海假日旅游网即可申请提现，返现金额会在次月返还到指定银行卡中</p>';
			} else {
				html += '<p class="rt_con_jj">网上成功订购此房型，每间夜可获得<span>' + _plan.takeMoney + '</span>元返点奖金（如您订购一间住两晚或两间住一晚，均为两间夜，待入住核实后，可获得<span>' + _plan.takeMoney * 2 + '</span>元返点现金）。成功入住后对客房可以进行一次点评和上传5张相关图片，每条点评可获<span>' + _plan.commentTakeMoney + '</span>元点评金，每张图片可获<span>' + _plan.photoTakeMoney + '</span>元上传金。</p>';
			}
		if(_plan.description.promotion)
			html+='<p class="rt_con_cx">'+_plan.description.promotion+'</p>';
		if(_plan.description.garanteeRule)
			html+='<p class="rt_con_db">'+_plan.description.garanteeRule+'</p>';
			html+='<p class="rt_con_fj">支付方式：到店付款';
		if(_plan.description.addValues)
			//html+='<br/>'+_plan.description.AddValues;
			html += '</p>';		
			html += '</div>';
	} else if(type == 'v2') {
		mdiv.className = 'room_tips';
		mdiv.style.width = '300px';
		mdiv.style.left = (gL(cusObj) - parseInt(mdiv.style.width) / 2 + 25) + 'px';		
			html += '<div class="rt_con">';
			if(agent_id == 20) {
				html += '<p class="rt_con_jj">预订并成功入住后，每间夜可获得<span>' + _plan.takeMoney + '</span>元奖金，离店后登录珠海假日旅游网即可申请提现，返现金额会在次月返还到指定银行卡中</p>';
			} else {
				html += '<p class="rt_con_jj1">网上成功订购此房型，每间夜可获得<span>' + _plan.takeMoney + '</span>元返点奖金（如您订购一间住两晚或两间住一晚，均为两间夜，待入住核实后，可获得<span>' + _plan.takeMoney * 2 + '</span>元返点现金）。成功入住后对客房可以进行一次点评和上传5张相关图片，每条点评可获<span>1</span>元点评金，每张图片可获<span>1</span>元上传金。</p>';
			}
			html += '</div>';
	} else {		
		mdiv.className = 'room_price';
		var width = (dn > 7) ? 354 : (dn * 50 + 4);
		mdiv.style.width = width + 'px';
		mdiv.style.left = (gL(cusObj) - parseInt(mdiv.style.width) / 2 + 30) + 'px';
		html += '<dl>';
		html += wks;
		for(c in _plan.date) {
			var dt = _plan.date[c];
			html += '<dd>' + sToDate(dt.day).format('dd') + '日<em>' + dt.price + '元</em></dd>';
		}
		html += '</dl>';
	}
	mdiv.innerHTML = html;
	rmtips();
	cusObj.onmouseout = ycrmtips;
	document.body.appendChild(mdiv);
	mdiv.style.top = (gT(cusObj) + 20) + 'px';
}
//get plans 
function gPlan(hotelId, roomId, planId) {
	if(hotels != null) {
		for(var k = 0; k < hotels.length; k++) {
			if(hotels[k].hotelId == hotelId){
				for(var m = 0; m < hotels[k].rooms.length; m++) {
					if(hotels[k].rooms[m].plans.planId == planId) {
						return hotels[k].rooms[m].plans;
					}
				}
				return null;
			}
		}
		return null;
	}
	return null;
}
function gL(cusObj) {
	var l = 0;
	while(cusObj) {
		l += cusObj.offsetLeft;
		cusObj = cusObj.offsetParent;
	}
	return l;
}
function gT(cusObj) {
	var t = 0;
	while(cusObj) {
		t += cusObj.offsetTop;
		cusObj = cusObj.offsetParent;
	}
	return t;
}
function sToDate(d) {
	var ds = d.toString().split("-");
	return (new Date(parseFloat(ds[0]), parseFloat(ds[1]) - 1, parseFloat(ds[2])));
}
function rmtips(){
	try {
		if(timeOut) {
			window.clearTimeout(timeOut);
			timeOut = null;
		}
		if(mdiv) {
			document.body.removeChild(mdiv);
		}
	} catch(e) {
		//alert(e);
	}
}
function ycrmtips(){
	window.clearTimeout(timeOut);
	timeOut = window.setTimeout('rmtips()', 100);
}
Date.prototype.format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1,
		"d+" : this.getDate(),
		"h+" : this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,
		"H+" : this.getHours(),
		"m+" : this.getMinutes(),
		"s+" : this.getSeconds(),
		"q+" : Math.floor((this.getMonth() + 3) / 3),
		"S" : this.getMilliseconds()
	};
	var week = {
		"0" : "日",
		"1" : "一",
		"2" : "二",
		"3" : "三",
		"4" : "四",
		"5" : "五",
		"6" : "六"
	};         
	if(/(y+)/.test(fmt)){
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	if(/(E+)/.test(fmt)){
		fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "星期" : "周") : "")+week[this.getDay()+""]);
	}
	for(var k in o){
		if(new RegExp("("+ k +")").test(fmt)){
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		}
	}
	return fmt;
}
function sToDate(d){var ds = d.toString().split("-");return (new Date(parseFloat(ds[0]),parseFloat(ds[1])-1,parseFloat(ds[2])))}
function DateAdd(interval,number,date){
    switch(interval.toLowerCase()){
        case "y": return new Date(date.setFullYear(date.getFullYear() + number));
        case "m": return new Date(date.setMonth(date.getMonth() + number));
        case "d": return new Date(date.setDate(date.getDate() + number));
        case "w": return new Date(date.setDate(date.getDate() + 7 * number));
        case "h": return new Date(date.setHours(date.getHours() + number));
        case "n": return new Date(date.setMinutes(date.getMinutes() + number));
        case "s": return new Date(date.setSeconds(date.getSeconds() + number));
        case "l": return new Date(date.setMilliseconds(date.getMilliseconds() + number));
    } 
}
function DateDiff(interval,indate,outdate){
    var long = outdate.getTime() - indate.getTime();
    switch(interval.toLowerCase()){
        case "y": return parseInt(outdate.getFullYear() - indate.getFullYear());
        case "m": return parseInt((outdate.getFullYear() - indate.getFullYear()) * 12 + (outdate.getMonth() - indate.getMonth()));
        case "d": return parseInt(long / 1000 / 60 / 60 / 24);
        case "w": return parseInt(long / 1000 / 60 / 60 / 24 / 7);
        case "h": return parseInt(long / 1000 / 60 / 60);
        case "n": return parseInt(long / 1000 / 60);
        case "s": return parseInt(long / 1000);
        case "l": return parseInt(long);
    }
}