﻿/************************************************************/
/*	JAPANiCAN Hotel Ajax API								*/
/*	2010.09.03 : 	Release									*/
/*	2010.09.15 :	Callback support on setHotelPrice.		*/
/*					DefaultPriceType support.				*/
/*												Brain		*/
/************************************************************/

/*	HotelAjax Object	*/
var HotelAjax = {

/*	Replace	*/
replaceAll : function(expression, org, dest) {  
    return expression.split(org).join(dest);  
},

/*	Make Comma Separate Value	*/
num2csv : function(val) {
	var stCSV = new String(val);
	if (val < 1) {
		return stCSV;
	} else {
		while(stCSV != (stCSV = stCSV.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	}
	return stCSV;
},

/*	Initialize	*/
PhotoAjaxUrl : '/hotels/ajax/ListPhoto.php?st=',
PriceAjaxUrl : '/hotels/ajax/ListRoom.php?st=',
DetailPageUrl : "http://www.japanican.com/hotels/",

rp_HotelCode		: "%%HotelCode%%",			/*	Hotel Code	*/
rp_HotelQuery		: "%%HotelQuery%%",			/*	Hotel Code	*/
rp_HotelName		: "%%HotelName%%",			/*	Hotel Name	*/
rp_HotelImagePL		: "%%HotelImagePL%%",		/*	Photo Large Image of Pre	*/
rp_HotelImagePLL	: "%%HotelImagePLL%%",		/*	Photo Largest Image of Pre	*/
rp_HotelImageL		: "%%HotelImageL%%",		/*	Photo Large Image	*/
rp_HotelImageLL		: "%%HotelImageLL%%",		/*	Photo Largest Image	*/
rp_HotelImageNL		: "%%HotelImageNL%%",		/*	Photo Large Image of Next	*/
rp_HotelImageNLL	: "%%HotelImageNLL%%",		/*	Photo Largest Image of Next	*/

/*	部屋は、通常/人数指定時、該当人数にマッチする部屋情報がRoom１、Room２に入る。	*/
/*	部屋は、検索条件無し（””でクエリーをはずして検索した場合）だと、1名1室、2名1室、4名室でそれぞれに在庫があれば入る。	*/
rp_Room1Name		: "%%Room1Name%%",			/*	Room1:Facility Name / Hotel Name	*/
rp_Room1Type		: "%%Room1Type%%",			/*	Room1:RoomType / PerNight / PerRoom For ??? Person	*/
rp_Room1Price		: "%%Room1Price%%",			/*	Room1:Total Price	*/
rp_Room1Link		: "%%Room1Link%%",			/*	Room1:Detail Link	*/
rp_Room2Name		: "%%Room2Name%%",
rp_Room2Type		: "%%Room2Type%%",
rp_Room2Price		: "%%Room2Price%%",
rp_Room2Link		: "%%Room2Link%%",
rp_Room3Name		: "%%Room3Name%%",
rp_Room3Type		: "%%Room3Type%%",
rp_Room3Price		: "%%Room31Price%%",
rp_Room3Link		: "%%Room3Link%%",

/*	部屋の中で最低価格の物。検索条件無し（""を明示）の場合、"PerNight / PerRoom For 2 Person"のものを選択。	*/
rp_LowestName		: "%%LowestName%%",		/* Lowest Price Room's Facility Name	*/
rp_LowestType		: "%%LowestType%%",		/* Lowest Price Room's Type	*/
rp_LowestPrice		: "%%LowestPrice%%",	/* Lowest Price Room's Total Price 	*/
rp_LowestLink		: "%%LowestLink%%",		/* Lowest Price Room's Detail Link	*/


/*	Default Output Template	*/
DefaultTemplate : "<table><tr><td><a href=\"%%LowestLink%%\"><img src=\"%%HotelImageLL%%\" alt=\"%%HotelName%%\" width=\"200\"/></a></td><td><h3><a href=\"%%LowestLink%%\">%%HotelName%%</a></h3><br /><span style=\"color:#cc0000;\">JPY %%LowestPrice%% ~</span><br /><span style=\"color:#888888;font-size:10px\">%%LowestType%%</span></td></tr></table>\n",

/*	1room 2persons.	*/
DefaultPriceType : "PerNight / PerRoom For 2 Person",


/***************************************************************************************/
/*	setHotelInfoById	*/
/***************************************************************************************/
setHotelInfoById : function(id, code, tmpstr, querystr, photonum, func) {
	var flg1 = false;
	var flg2 = false;

	if ((tmpstr == undefined) || (tmpstr=="")) {
		tmpstr = HotelAjax.DefaultTemplate;
	}
	if ((photonum == undefined) || (photonum=="")){ photonum = "1"; }
	if (querystr == undefined) { querystr = "&pn=2&rn=1"; }


jQuery(function(){
	jQuery.ajax({
		url: HotelAjax.PhotoAjaxUrl+code+'&imgnum='+photonum,	
		dataType: 'xml',
		success : function(data){
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelCode, code);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelQuery, querystr);

				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelName, jQuery("item>title", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImagePL, jQuery("item>link_pre", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImagePLL, jQuery("item>link_pre_l", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImageL, jQuery("item>link", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImageLL, jQuery("item>link_l", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImageNL, jQuery("item>link_next", data).text());
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_HotelImageNLL, jQuery("item>link_next_l", data).text());
				flg1 = true;
				if (flg2 == true) {
						if (typeof(func) == 'function') {
							tmpstr = func(tmpstr);
						}
						jQuery(id).html(tmpstr);
				}
			},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			/*	alert("Ajax Error." + textStatus);	*/
			this;
			}
	})
	jQuery.ajax({
		url: HotelAjax.PriceAjaxUrl+code+querystr,
		dataType: 'xml',
		success : function(data){
				var strRoom1Name	= jQuery("item>facility", data).eq(0).text();
				var strRoom1Type	= jQuery("item>title", data).eq(0).text();
				var strRoom1Price	= jQuery("item>price", data).eq(0).text();
				var strRoom1Link	= HotelAjax.DetailPageUrl + jQuery("item>link", data).eq(0).text();
				var strRoom2Name	= jQuery("item>facility", data).eq(1).text();
				var strRoom2Type	= jQuery("item>title", data).eq(1).text();
				var strRoom2Price	= jQuery("item>price", data).eq(1).text();
				var strRoom2Link	= HotelAjax.DetailPageUrl + jQuery("item>link", data).eq(1).text();
				var strRoom3Name	= jQuery("item>facility", data).eq(2).text();
				var strRoom3Type	= jQuery("item>title", data).eq(2).text();
				var strRoom3Price	= jQuery("item>price", data).eq(2).text();
				var strRoom3Link	= HotelAjax.DetailPageUrl + jQuery("item>link", data).eq(2).text();
				var strLowestName;
				var strLowestType;
				var strLowestPrice;
				var strLowestLink;
				var bestRoom = 0;

				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room1Name, strRoom1Name);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room1Type, strRoom1Type);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room1Price, HotelAjax.num2csv(strRoom1Price));
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room1Link, strRoom1Link);

				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room2Name, strRoom1Name);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room2Type, strRoom2Type);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room2Price, HotelAjax.num2csv(strRoom2Price));
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room2Link, strRoom2Link);

				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room3Name, strRoom3Name);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room3Type, strRoom3Type);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room3Price, HotelAjax.num2csv(strRoom3Price));
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_Room3Link, strRoom3Link);

				if ((parseInt(strRoom1Price) < 1) || (strRoom1Price == "")) strRoom1Price = 9999999999;
				if ((parseInt(strRoom2Price) < 1) || (strRoom2Price == "")) strRoom2Price = 9999999999;
				if ((parseInt(strRoom3Price) < 1) || (strRoom3Price == "")) strRoom3Price = 9999999999;

				if (querystr == "") {
					if (strRoom1Type.indexOf(HotelAjax.DefaultPriceType) >= 0) bestRoom = 1;
					if (strRoom2Type.indexOf(HotelAjax.DefaultPriceType) >= 0) bestRoom = 2;
					if (strRoom3Type.indexOf(HotelAjax.DefaultPriceType) >= 0) bestRoom = 3;
				} else {
					if (parseInt(strRoom1Price) < parseInt(strRoom2Price)) {
						if (parseInt(strRoom1Price) < parseInt(strRoom3Price)){
							bestRoom = 1;
						} else {
							bestRoom = 3;
						}
					} else {
						if (parseInt(strRoom2Price) < parseInt(strRoom3Price)) {
							bestRoom = 2;
						} else {
							bestRoom = 3;
						}
					}
				}
				switch(bestRoom) {
					case 2:
						/*	2 is lowest	*/
						strLowestName = strRoom2Name;
						strLowestType = strRoom2Type;
						strLowestPrice = strRoom2Price;
						strLowestLink = strRoom2Link;
						break;
					case 3:
						/*	3 is lowest	*/
						strLowestName = strRoom3Name;
						strLowestType = strRoom3Type;
						strLowestPrice = strRoom3Price;
						strLowestLink = strRoom3Link;
						break;
					default:
							/*	1 is lowest	*/
						strLowestName = strRoom1Name;
						strLowestType = strRoom1Type;
						strLowestPrice = strRoom1Price;
						strLowestLink = strRoom1Link;
						break;
				}
				if (strLowestPrice == 9999999999) {
					strLowestPrice = "-";
				}
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_LowestName, strLowestName);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_LowestType, strLowestType);
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_LowestPrice, HotelAjax.num2csv(strLowestPrice));
				tmpstr = HotelAjax.replaceAll(tmpstr, HotelAjax.rp_LowestLink, strLowestLink);

				flg2 = true;
				if (flg1 == true) {
						if (typeof(func) == 'function') {
							tmpstr = func(tmpstr);
						}
						jQuery(id).html(tmpstr);
				}
			},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			/*	alert("Ajax Error." + textStatus);	*/
			this;
			}
	})
})
},

/***************************************************************************************/
/*	setHotelPriceById	*/
/***************************************************************************************/
setHotelPriceById : function(id, code, querystr, func) {
	if (querystr == undefined) { querystr = "&pn=2&rn=1"; }

jQuery(function(){
	jQuery.ajax({
		url: HotelAjax.PriceAjaxUrl+code+querystr,
		dataType: 'xml',
		success : function(data){
				var strRoom1Type	= jQuery("item>title", data).eq(0).text();
				var strRoom1Price	= jQuery("item>price", data).eq(0).text();
				var strRoom2Type	= jQuery("item>title", data).eq(1).text();
				var strRoom2Price	= jQuery("item>price", data).eq(1).text();
				var strRoom3Type	= jQuery("item>title", data).eq(2).text();
				var strRoom3Price	= jQuery("item>price", data).eq(2).text();
				var strLowestPrice;

				if ((parseInt(strRoom1Price) < 1) || (strRoom1Price == "")) strRoom1Price = 9999999999;
				if ((parseInt(strRoom2Price) < 1) || (strRoom2Price == "")) strRoom2Price = 9999999999;
				if ((parseInt(strRoom3Price) < 1) || (strRoom3Price == "")) strRoom3Price = 9999999999;

				if (querystr == "") {
					if (strRoom1Type.indexOf(HotelAjax.DefaultPriceType) >= 0) strLowestPrice = strRoom1Price;
					if (strRoom2Type.indexOf(HotelAjax.DefaultPriceType) >= 0) strLowestPrice = strRoom2Price;
					if (strRoom3Type.indexOf(HotelAjax.DefaultPriceType) >= 0) strLowestPrice = strRoom3Price;
				} else {
					if (parseInt(strRoom1Price) < parseInt(strRoom2Price)) {
						if (parseInt(strRoom1Price) < parseInt(strRoom3Price)){
							strLowestPrice = strRoom1Price;
						} else {
							strLowestPrice = strRoom3Price;
						}
					} else {
						if (parseInt(strRoom2Price) < parseInt(strRoom3Price)) {
							strLowestPrice = strRoom2Price;
						} else {
							strLowestPrice = strRoom3Price;
						}
					}
				}
				if (strLowestPrice  == 9999999999) {
					tmpstr = "-";
				} else {
					tmpstr = HotelAjax.num2csv(strLowestPrice);
				}

				if (typeof(func) == 'function') {
					tmpstr = func(tmpstr);
				}
				jQuery(id).html(tmpstr);

			},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			/*	alert("Ajax Error." + textStatus);	*/
			this;
			}
	})
})
}

};


