﻿
var c_fav_action = "http://www.ctrip.com/Member/memberHotelAdd.asp";
var c_form_name = "aspnetForm";
function addFavHotel(){
	submitForm(c_fav_action, "_self");
}

function submitForm(action, target, data, ieonly){
	if(ieonly && !browserTest()){
		return;
	}
	var g_form;
	if(!g_form){
		g_form = document.forms[c_form_name];
	}
	if(g_form.__VIEWSTATE){
		g_form.__VIEWSTATE.name = "NOVIEWSTATE";
	}
	if(data){
		for(var f in data){
			if(g_form[f]){
				g_form[f].value = data[f];
			}
		}
	}
	g_form.action = action;
	g_form.target = target;
	g_form.submit();
}


function setpar(name,value,str){	//string,string,[string]:string
	if(str){
		var url = str;
	}else{
		url = location.href;
	}
	var domain = url.split("?")[0];
	var pars = url.split("?")[1]?url.split("?")[1].split("&"):"";
	var tempurl = domain;
	var hpars = {};
	if(pars){
		for(var i = 0,j = pars.length;i<j;i++){
			var temp = pars[i].split("=");
			hpars[temp[0]]=temp[1];
		}
	}
	hpars[name] = value;
	if(hpars){
		tempurl += "?";
		for(var i in hpars){
			tempurl+=i+"="+hpars[i]+"&";
		}
		tempurl = tempurl.slice(0,(tempurl.length-1));
	}

	return tempurl;
}

$myajax=function(url,content,callback){ //url,content[post],{ready:function(response){},waitting:function(){},falt:function(){}}
	var xmlVer=["MSXML2.XMLHTTP","Microsoft.XMLHTTP"],xmlObj;
	try {
		xmlObj=new XMLHttpRequest();
	} catch(e) {
		for (var i=0;i<xmlVer.length;i++)
			try {
				xmlObj=new ActiveXObject(xmlVer[i]);
				break;
			} catch(e) {}
	}
	if (!xmlObj) return;
	xmlObj.open(content?"POST":"GET",url||_.location.href,!!callback);
	xmlObj.setRequestHeader("Content-Type","application\/x-www-form-urlencoded");
	xmlObj.setRequestHeader("If-Modified-Since",new Date(0));
	if (callback&&callback["ready"])
		xmlObj.onreadystatechange=function(){
			if (xmlObj.readyState==4){
				if(xmlObj.status==200){
					var txt=xmlObj.status==200?(/xml/i.test(xmlObj.getResponseHeader("content-type"))?xmlObj.responseXML:xmlObj.responseText):null;
					if (callback["ready"](txt)===true){
						setTimeout(function(){
							_.$ajax(url,content,callback);
						},1000);
					}
				}else{
					callback["falt"]();
				}
				
			}else{
				callback["waitting"]();
			}
		};
	xmlObj.send(content||'');
	return callback?xmlObj:(xmlObj.status==200?(/xml/i.test(xmlObj.getResponseHeader("content-type"))?xmlObj.responseXML:xmlObj.responseText):null);
};

/**
*
*	点击联系方式后隐藏标题，并显示电话号码
*
*/
function showContact(){
	var objtoshow = $("conttoshow");
	var objtohide = $("conttohide");

	var change = function(){
		objtoshow.style.display = "";
		objtohide.style.display = "none";
	}

	objtohide.onclick = change;
}

$r("domReady",function(){
	var datasaver = $("ajaxdate");
	
/*hotel thumb animation*/
	var hotelpics = {
		thumbs:hotelthumbs?hotelthumbs:[],
		pics:hotelimages?hotelimages:[],
		wrap:$("hotelpicwrap"),
		pic:$("hotelpicwrap").$("img")[0],
		thumbbuttons:$("hotelpicwrap").$("li")
	}

	function handlclick(i){
		//hotelpics.pic.setAttribute("src",hotelpics.pics[i][0]);
		hotelpics.pic.style.background = "url("+hotelpics.pics[i][0]+") no-repeat center center";
		hotelpics.pic.setAttribute("title",hotelpics.pics[i][1]);
		for(var n = 0,l = hotelpics.thumbbuttons.length;n<l;n++){
			hotelpics.thumbbuttons[n].className = "";
		}
		hotelpics.thumbbuttons[i].className = "on";
	}

	for(var i = 0,j = hotelpics.thumbbuttons.length;i<j;i++){
		(function(i){
			hotelpics.thumbbuttons[i].onclick = function(){
				handlclick(i);
			}
		})(i)
	}

/*
	var hotelpics = {
		queryfirst:0,
		nowpic:0,
		totalpics:5,
		movestep:47
	};

	hotelpics.isleftedge = function(){
		if(hotelpics.queryfirst == 0){
			return true;
		}else return false;
	}

	hotelpics.isrightedge = function(){
		if(hotelpics.queryfirst == hotelpics.thumb.length-hotelpics.totalpics){
			return true;
		}else return false;
	}

	hotelpics.disablearrleft = function(){
		showelm.arrow.leftarrow.className = "arrowleft_off";
	}

	hotelpics.enablearrleft = function(){
		showelm.arrow.leftarrow.className = "arrowleft";
	}

	hotelpics.disablearrright = function(){
		showelm.arrow.rightarrow.className = "arrowright_off";
	}

	hotelpics.enablearrright = function(){
		showelm.arrow.rightarrow.className = "arrowright";
	}

	hotelpics.createthumb = function(imgurls){
		var temp = "";
		for(var i = 0,j = imgurls.length;i<j;i++){
			temp += '<li><img width="45" height="38" style="display: block;" src="'+imgurls[i]+'" alt=""/></li>'
		}
		temp += '<li class="nopic"/>';
		return temp;
	}

	hotelpics.init = function(){
		showelm.scroll.innerHTML = hotelpics.createthumb(hotelpics.thumb);
		showelm.arrow.rightarrow.className = "arrowright";
		if(hotelpics.isleftedge()){
			hotelpics.disablearrright();
		}
		if(hotelpics.isrightedge()){
			hotelpics.disablearrleft();
		}
	}

	var showelm = $("detail_scroll")?$("detail_scroll"):{};
	showelm.showpannel = $("showpannel");
	showelm.arrow = {
		leftarrow:$("arrowleft"),
		rightarrow:$("arrowright")
	};
	showelm.scroll = $("detail_scrollpic");

	// direction[left,right];steplong;changefunction;frame;totaltime
	var moveto = function(dir,step,func,fra,ttime){
		var curpos = showelm.scroll.style.marginLeft?parseInt(showelm.scroll.style.marginLeft):0;
		if(curpos%Math.abs(step)==0){
			var moved = 0;
			var frame = 0;
			var fs = fra/ttime;

			if(dir == "left"){
				if(hotelpics.isrightedge()){
				}else{
					hotelpics.enablearrleft();
					hotelpics.queryfirst+=1;
					hotelpics.enablearrleft();
					(function(){
						frame+=fs;
						moved += step*func(frame);
						if(moved<step){
							showelm.scroll.style.marginLeft = curpos - moved + "px";
							var tempfunc = arguments.callee;
							setTimeout(tempfunc,50);
						}else{
							showelm.scroll.style.marginLeft = curpos - step + "px";
						}
					})()
					if(hotelpics.isrightedge()){
						hotelpics.disablearrleft();
					}else{
						hotelpics.enablearrright();
					}
				}
			}

			if(dir == "right"){
				if(hotelpics.isleftedge()){
				}else{
					hotelpics.enablearrright();
					hotelpics.queryfirst-=1;
					hotelpics.enablearrright();
					(function(){
						frame+=fs;
						moved += step*func(frame);
						if(moved<step){
							showelm.scroll.style.marginLeft = curpos + moved + "px";
							var tempfunc = arguments.callee;
							setTimeout(tempfunc,50);
						}else{
							showelm.scroll.style.marginLeft = curpos + step + "px";
						}
					})()
					if(hotelpics.isleftedge()){
						hotelpics.disablearrright();
					}else{
						hotelpics.enablearrleft();
					}
				}
			}
		}
	};

	var movelist = [];
	movelist.moving = false;
	var num = 0;

	function play(){
		if(movelist.moving == false){
			movelist.moving = true;
			while(movelist.length){
				eval("moveto("+movelist[0]+")");
				movelist.shift();
			}
			movelist.moving = false;
		}else{
			return false;
		}
	}

	hotelpics.init();

	showelm.arrow.leftarrow.onclick = function(){
		movelist.push('"left",hotelpics.movestep,sin,50,500');
		play();
	}

	showelm.arrow.rightarrow.onclick = function(){
		movelist.push('"right",hotelpics.movestep,sin,50,500');
		play();
	}


/* google map */

	var point = $("hotelCoordinate").value?$("hotelCoordinate").value.split("|"):[0,0];
	EMap.getMap(function(){
		var mapDiv = $('map');
		var map = new GMap2(mapDiv);
		map.addControl(new GSmallMapControl());
		
		var mark = new GLatLng(point[0],point[1]);
		map.setCenter(mark, 15);
		var marker = new GMarker(mark);
		GEvent.addListener(marker,'click', function(){
			EMap.showPop($('ctl00_MainContentPlaceHolder_lnkHotelName').innerHTML, mark)
		});
		map.addOverlay(marker);
		var div = $c('div');
		div.style.background = 'url(http://pic.ctrip.com/hotels081118/pop_map_btn.gif) no-repeat right center';
		div.style.cursor = 'pointer';
		div.style.width = '40px';
		div.style.marginTop = '5px';
		div.title = $s2t('查看大地图');
		div.appendChild(document.createTextNode($s2t('放大')));
		map.getContainer().appendChild(div);
		div.onclick = function(){
			EMap.showPop($('ctl00_MainContentPlaceHolder_lnkHotelName').innerHTML, mark);
		};
		mapDiv.parentNode.insertBefore(div, mapDiv.nextSibling)


		
		var si = setInterval(function(){
			var nodeList = $('map').childNodes;
			for (var i = 0; i < nodeList.length; i++) {
				if ( /地图数据/.test(nodeList[i].innerText || nodeList[i].textContent)) {
					nodeList[i].innerHTML = '';
					clearTimeout(si);
					break;
				}
			}
		}, 1000);
	});

	
	function remove(element) {
		element = $(element);
		element.parentNode.removeChild(element);
		return element;
	}

	function clear(ti){
		clearInterval(ti);
	}

/* ajax */
	//var url = "roomlist.ajax.asp?";
	

	var piclink = [$("lnkHotelRoomPic",true)||{},$("lnkHotelRoomPic2",true)||{}];
	var url = "/Domestic/Tool/AjaxHotelPrice.aspx?hotel=";
	var today = new Date();
	var tomorrow = new Date();
	tomorrow = new Date(tomorrow.setDate(tomorrow.getDate()+1));

	var resElm = $("base_maincontent");
	var subElm = $("rechoose");
	var historydata = $pageValue.get("ajaxdata")||"";
	var hotelid = $("hotel").value||"10";

	var datestart = $("startdate").value||$('txtCheckIn').value||today.toStdString();
	var dateend = $("depdate").value||$('txtCheckOut').value||tomorrow.toStdString();
	$("startdate_1").value = datestart;
	$("depdate_1").value = dateend;
	var submithandler = function(){
		datestart = $("startdate_1").value||today.toStdString();
		dateend = $("depdate_1").value||tomorrow.toStdString();


		if(checkDate(["startdate_1","depdate_1"])){
			$("startdate").value = datestart;
			$("depdate").value = dateend;
			if(datestart&&dateend){
				HotelQuery.checkOut = dateend;
				HotelQuery.checkIn = datestart;
				var tempurl = url + hotelid + "&StartDate=" + datestart+"&DepDate="+dateend;
				var hotelajax = $myajax(tempurl,"",{
					ready:callbackready,
					waitting:callbackwaitting,
					falt:callbackfalt
				})

				for(var i = 0,j = piclink.length;i<j;i++){
					piclink[i].href = setpar("StartDate",datestart,piclink[i].href);
					piclink[i].href = setpar("DepDate",dateend,piclink[i].href);
				}
			}else{
				datestart.value?$alert(dateend,"请选择离店时间"):$alert(datestart,"请选择入住时间");
			}
		}
	}

	var callbackready = function(resTXT){
		if(resTXT=="null"){
			resElm.innerHTML = "已无房间";
		}else{
			$pageValue.set("ajaxdata",resTXT);
			
			HotelQuery.checkOut = dateend;
			HotelQuery.checkIn = datestart;
			//alert(HotelQuery.checkOut+":"+HotelQuery.checkIn);
			resElm.innerHTML = showRoomList(resTXT,hotelid);
		}
		enablesubmit();
	}
	var callbackwaitting = function(){
		resElm.innerHTML = "正在更新，请稍候 ";
		disablesubmit();
	}
	var callbackfalt = function(){
		resElm.innerHTML = "更新失败，请重试 ";
		enablesubmit();
	}
	if(historydata){
		callbackready(historydata);
	}

	function disablesubmit(){
		subElm.onclick = function (){return false};
	}

	function enablesubmit(){
		subElm.onclick = submithandler;
	}

	subElm.onclick = submithandler;
	submithandler();
/**
*
* show and display contact
*
**/
	showContact();

},56)