﻿Ctrip.module.viewControl = function(obt) {
	var viewers = obt.$("a");
	var viewImg = viewers[0];
	var view360 = viewers[1];
	var hotel_pics = $$.module.engHotel.hotel_pics;
	
	var pre = obt.parentNode;
	while (pre.className != "duma_layoutr")
		pre = pre.parentNode;
	
	var container = $(pre).$g(".hotel_detail_info1")[0];
	var detail = pre.$g(".detail_hotel_list")[0];
	var flash360 = pre.$g(".hotel_detail_info2")[0];

	function load360() {
		if(this.className!="hotel_view_360") return;
		if(!flash360.inited){
			add_hotel_info2(flash360);
			flash360.$("button")[0].onclick = function() { exchangeCurrent(detail) };
			flash360.inited = true;
		};
		exchangeCurrent(flash360);
	}
	
	function exchangeCurrent(obt) {
		container.style.display = "none";
		detail.style.display = "none";
		flash360.style.display = "none";
		obt.style.display = "";
	}

	function loadViewImg() {
		if (this.className != "hotel_view_pic") return;
		
		if (!container.inited) {
			init(container.id);
			container.inited = true;
		}

		exchangeCurrent(container);
	};

	viewImg.$r("click", loadViewImg);
	view360.$r("click", load360);
	var add_hotel_info1 = function(dom_obj) {
		dom_obj.innerHTML = "<div class=\"pic\">" +
						"<img src=\"http://pic.english.ctrip.com/hotels090720/pic_select_room.png\" alt=\"\" />" +
						"<div class=\"hotel_pages_split\">" +
						"<a href=\"###\" title=\"Back\" class=\"hotel_ctrl_previous\">Back</a>" +
						"<a href=\"###\" class=\"pages_split_now\">5</a>/<a href=\"#;\">12</a>" +
						"<a href=\"###\" title=\"Next\" class=\"hotel_ctrl_next\">Next</a>" +
						"<button class=\"icon_close\" type=\"button\">close</button>" +
						"</div>" +
						"</div>" +
						"<div class=\"list\">" +
						"<ol></ol>" +
						"</div>";
	}
	var add_hotel_info2 = function(dom_obj){
		/*dom_obj.innerHTML = "<div class=\"pic\">"+
						"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"330\" height=\"270\"><param name=\"movie\" value=\"酒店360.swf\" /><param name=\"quality\" value=\"high\" /><embed src=\"酒店360.swf\" wmode=\"transparent\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"330\" height=\"270\"></embed><param name=\"wmode\" value=\"transparent\"></param></object>"+
						"<button type=\"button\" class=\"icon_close\">close</button>"+
						"</div>"+
						"<div class=\"list\">"+
						"<ol>"+
						"<li class=\"line2\"><a href=\"javascript:void(0);\">one</a></li><li class=\"line3\"><a href=\"javascript:void(0);\">two</a></li><li class=\"line2\"><a href=\"javascript:void(0);\">three</a></li><li><a href=\"javascript:void(0);\">four</a></li>"+
						"</ol>"+
						"</div>";*/
	}
	//HOTEL内部元素结构
	var HOTEL = function() {
		this.display_box = this.$g("img")[0]; //显示图片的IMG
		this.pev_box = this.$g(".hotel_ctrl_previous")[0]; //Back按钮
		this.now_pic = this.$g("a.pages_split_now")[0]; //当前第几张图片
		this.total_pic = this.$g(".hotel_pages_split a")[2]; //总图片数
		this.next_box = this.$g(".hotel_ctrl_next")[0]; //Next按钮
		this.pic_list = this.$g("ol")[0]; //图片名字列表
		this.close = this.$g("button.icon_close")[0];
	}
	//展示图片
	var show_pic = function(hotel_id, number) {
		number += hotel_pics[hotel_id].name_list.length;
		number %= hotel_pics[hotel_id].name_list.length;
		$(hotel_id).display_box.src = hotel_pics[hotel_id].pic_list[number];
		var lines = $(hotel_id).pic_list.childNodes;
		for (var k = 0, l = lines.length; k < l; k++) {
			if (lines[k].className == "line3") {
				if (k % 2 == 0) { lines[k].className = "line2" }
				else lines[k].className = "";
			}
		}
		$(hotel_id).pic_list.childNodes[number].className = "line3";
		$(hotel_id).now_pic.innerHTML = number + 1;
		hotel_pics[hotel_id].now_pic = number;
	}
	//滚动图片
	var roll_pic = function(norp, hotel_id) {
		norp == "N" ? show_pic(hotel_id, hotel_pics[hotel_id].now_pic + 1) : show_pic(hotel_id, hotel_pics[hotel_id].now_pic - 1);
	}
	//初始化图片展示部分
	var init = function(hotel_id) {
		var hotel = $(hotel_id);
		$removeTextNode(hotel);
		if (hotel != null) {
			add_hotel_info1(hotel);
			HOTEL.apply(hotel);
			hotel.close.onclick = function() { exchangeCurrent(hotel.$parentNode().$g(".detail_hotel_list")[0]) };
			hotel.pev_box.onclick = function() { roll_pic("P", hotel_id) };
			hotel.next_box.onclick = function() { roll_pic("N", hotel_id) };
			hotel.now_pic.innerHTML = hotel_pics[hotel_id].now_pic + 1 || 0;
			hotel.total_pic.innerHTML = hotel_pics[hotel_id].pic_list.length;
			hotel.pic_list.innerHTML = "";
			for (var i = 0, l = hotel_pics[hotel_id].pic_list.length; i < l; i++) {
				var li = $c("li");
				if (i % 2 == 0) li.className = "line2";
				li.innerHTML = "<a href='javascript:void(0);'>" + hotel_pics[hotel_id].name_list[i] + "</a>";
				hotel.pic_list.appendChild(li);
			}
			var lis = hotel.pic_list.childNodes;
			var _lis = new Array(lis.length);
			_lis.each(function(li, i) {
				li = lis[i];
				li.onclick = function() {
					show_pic(hotel_id, i);
				};
			});
			show_pic(hotel_id, hotel_pics[hotel_id].now_pic || 0);
		}
	}
};
var dateTest;
$r("domready",function(){
	if(!__.$g(".ctrl_go"))return;
	/*var controls_box=__.$g(".controls")[1],
		inputs=controls_box.$("input"),
		CheckIn=inputs[0],
		CheckOut=inputs[1],
		ctrl_go=inputs[2];*/
	dateTest=function(checkin,checkout){
		var CheckIn=$(checkin)||$("calendarCheckIn2");
		var CheckOut=$(checkout)||$("calendarCheckOut2");
		if(!(CheckIn&&CheckOut))return;
		if(CheckIn.isNull()){$alert(CheckIn,"Please select your check-in date.");return false}
		if(!CheckIn.value.isDateTime()){$alert(CheckIn,"Check-in date is invalid!");return false}
		if(CheckOut.isNull()){$alert(CheckOut,"Please select your check-out date.");return false}
		if(!CheckOut.value.isDateTime()){$alert(CheckOut,"Check-out date is invalid!");return false}
		if(CheckIn.value.isDateTime()>=CheckOut.value.isDateTime()){$alert(CheckOut,"Check-out date must be later than check-in date");return false}
		if(CheckIn.value.isDateTime().addDate(28) <= CheckOut.value.isDateTime()){$alert(CheckOut,"Maximum time of your stay is 28 days!");return false}
		return true;
	}
	//ctrl_go.$r("click",function(){if(dateTest())document.forms[0].submit();});
});