function $A(iterable) {
	var results = [];
	for (var i = 0; i < iterable.length; i++)results.push(iterable[i]);
	return results;
}
Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}
function Focus(){ this.initialize.apply(this, arguments) };
Focus.prototype = {
	initialize:function(imgObj1,imgObj2){
		this.roomPic = $("roomPic");
		this.hotelPic = $("hotelPic");
		this.flash = $('flash_hotel');
		this.roomType = $("roomType").$("dl"); //房型介绍
		this.imgs=imgObj1;
		this.sort = null;
		this.link = [];
		this.imgs_full = imgObj2;
		this.len2 = this.imgs_full.length;
		this.prev= $("prev");
		this.next = $("next");
		var lastpage = $('lastpage');
		var nextpage = $('nextpage');
		var r_sPic = $("r_sPic");
		var r_bPic = $("r_bPic");
		this.now = 0 ;
		this.start = 0 ;
		this.end = 11;
		this.range = this.end - this.start + 1;
		this.img= null;
		this.ul = null;
		this.list = [];
		this.picSort = $('picSort');
		this.li = $('pageClass').$('li');
		this.index = 0;
		this.id =  $("room_pic").value ? $("room_pic").value : 0 ;//房型ID
		this.idList= [];
		for( var y = 0; y< this.imgs.length; y++){
			this.idList.push(this.imgs[y].id);
		}
		this.roomSort = indexArr(this.id,this.idList) || 0;
		this.page = (window.location.href.indexOf('type=action')>0) ? "fullView" : $("room_pic").value!=0 ? "room" : "hotel" ;
		
		//隐藏没有数据的room页按钮

		if(this.imgs.length == 0){
			if(this.page = "room")	this.page = "hotel";
			this.li[2].style.display ="none";
		}
		//隐藏没有实景图的按钮
		if(!$("divB360"))	this.li[1].style.display ="none";
		
		//初始化房型分类

		for(var i = 0; i<this.imgs.length; i++){
			var picLi = $c('li');
			this.sort = this.imgs[i].sort;
			picLi.innerHTML = this.sort;
			this.picSort.appendChild(picLi);
		}
		var imgLi = this.picSort.$('li');
		var mr = this.roomSort ? this.roomSort :0;
		if(imgLi[mr] && imgLi[mr].parentNode == this.picSort)		imgLi[mr].style.borderTop = '0px';
		if(imgLi[mr] && imgLi[mr].parentNode == this.picSort)		imgLi[mr].className = 'select_left';
		if(mr && imgLi[mr-1] && imgLi[mr-1].parentNode == this.picSort)		imgLi[mr-1].className = '';
		if(this.roomType[mr])	this.roomType[mr].style.display ="";
		
		//初始化
		if(this.page == 'room' ){
			this.roomPic.style.display="none";
			this.hotelPic.style.display="none";
			this.flash.style.display="none";
			this.roomPic.style.display="";
			pageClass(this.page);
			if(this.imgs[mr])	this.roomsHTML(this.imgs[mr].link) ;
			this.page ='room';
		}
		else if(this.page=='fullView'){
			this.roomPic.style.display="none";
			this.hotelPic.style.display="none";
			this.flash.style.display="none";
			this.flash.style.display="";
			pageClass(this.page);
			this.page='fullView';
		}
		else if(this.page =='hotel'){
			this.hotelPic.style.display="none";
			this.roomPic.style.display="none";
			this.flash.style.display="none";
			this.hotelPic.style.display="";
			pageClass(this.page);
			this.hotelsHTML();
			this.page = 'hotel';
		}
		//切房型
		if(imgLi){
			
			for(var p = 0; p< imgLi.length;p++){
				(function(t){
					imgLi[t].onclick = function(){
						for(var x= 0; x<imgLi.length; x++){
							imgLi[x].className = '';
							if(_this.roomType[x])	_this.roomType[x].style.display="none";
						}
						if(imgLi[t] && imgLi[t].parentNode == _this.picSort)		imgLi[t].className = 'select_left';
						if(t && imgLi[t-1] && imgLi[t-1].parentNode == _this.picSort)		imgLi[t-1].className = '';
						_this.roomsHTML(_this.imgs[t].link) ;
						if(_this.roomType[t])	_this.roomType[t].style.display ="";
						_this.index = 0;
					}
				})(p);
			}
		}
		//切页面
		for( var m= 0; m< this.li.length; m++){
				var _this = this;
				(function(t){
					_this.li[t].onclick = function(){ 
						if(t == 1 && _this.page!= 'fullView')	{
							_this.flash.style.display="none";
							_this.roomPic.style.display="none";
							_this.hotelPic.style.display="none";
							_this.flash.style.display="";
							_this.page='fullView';
							pageClass(_this.page);
						}
						else if (t==2 && _this.page != 'room'){
							_this.flash.style.display="none";
							_this.roomPic.style.display="none";
							_this.hotelPic.style.display="none";
							_this.roomPic.style.display="";
							_this.page ='room';
							pageClass(_this.page);
							if(_this.imgs[_this.roomSort])	_this.roomsHTML(_this.imgs[_this.roomSort].link) ;
						}
						else if(t ==0 && _this.page != 'hotel'){
							_this.flash.style.display="none";
							_this.hotelPic.style.display="none";
							_this.roomPic.style.display="none";
							_this.hotelPic.style.display="";
							_this.page = 'hotel';
							pageClass(_this.page);
							_this.hotelsHTML();
						}
					}
				})(m);
		}
		nextpage.onclick=function(){
			if((_this.len2 - _this.start) > _this.range){
				_this.start=_this.start+12;
				_this.end=_this.end+12;
				_this.roll(_this.start,_this.end,r_sPic);
				_this.change(_this.start,r_bPic,_this.imgs_full);
				lastpage.className="prev";
			}	
			
		}
		lastpage.onclick=function(){
			if(_this.start >0){	
				_this.start=_this.start-12;
				_this.end=_this.end-12;
				_this.roll(_this.start,_this.end,r_sPic);
				_this.change(_this.start,r_bPic,_this.imgs_full);
				nextpage.className="next";
			}
			
		}
		
		
		this.next.onclick = function(){
			if (_this.next.className=="end") return false;
			if(_this.now == _this.end){
				if((_this.len2 - _this.start) > _this.range){
					_this.start=_this.start+12;
					_this.end=_this.end+12;
					_this.roll(_this.start,_this.end,r_sPic);
					_this.change(_this.start,r_bPic,_this.imgs_full);
				}	
				lastpage.className="prev";
			}
			else{
				_this.now++;
				var i=_this.now;
				_this.change(i,r_bPic,_this.imgs_full);
				if (_this.list[_this.index])	_this.list[_this.index].className = "select_pic";
			}
		}
		this.prev.onclick = function(){
			if (_this.prev.className=="begin") return false;
			if(_this.now == _this.start){
				if(_this.start >0){	
					_this.start=_this.start-12;
					_this.end=_this.end-12;
					_this.roll(_this.start,_this.end,r_sPic);
					_this.change(_this.end,r_bPic,_this.imgs_full);
				}
			nextpage.className="next";
			}
			else{
				_this.now--;
				var j=_this.now;
				_this.change(j,r_bPic,_this.imgs_full);
				if (_this.list[_this.index])	_this.list[_this.index].className = "select_pic";
			}
		}

	},
	hotelsHTML:function(){
		var r_bPic = $("r_bPic");
		var r_sPic = $("r_sPic");
		if(r_bPic){
			this.img = $c("img");
			this.img.onload = appendImg.bind(null, r_bPic, this.img);
			this.img.src =  this.imgs_full[0] ? this.imgs_full[0].max : null;
			if(this.imgs_full[0])	this.img.title = this.imgs_full[0].title;
		}
		if(r_sPic){
			this.roll(this.start,this.end,r_sPic);
			if(this.list[0])	this.list[0].className = "select_pic";
		}
	},
	roll:function(m,n,parent){
		var r_sPic = $("r_sPic");
		var r_bPic = $("r_bPic");
		var r_sPic_ul = r_sPic.$("ul")[0];
		var bom = $("bom");
		var len = this.imgs_full.length;
		var prev = $('prev');
		var next = $('next');
		var lastpage = $('lastpage');
		var nextpage = $('nextpage');
		prev.className ="begin";
		if(len == 0) next.className ="end";
		else next.className ="next";
		lastpage.className ="prev";
		nextpage.className ="next";
		if(r_sPic_ul && r_sPic_ul.parentNode)	r_sPic_ul.parentNode.removeChild(r_sPic_ul);
		this.ul = $c('ul');
		this.ul.className="picture_list";
		if(bom){
			parent.insertBefore(this.ul, bom);
		}		
		for( var i = 0; i<len; i++){
			this.list[i] = $c('li');
			this.list[i].innerHTML='<img src="'+this.imgs_full[i].min+'" width="46px" height="35px;" title="'+this.imgs_full[i].title+'"; style="display:block" />';
			this.ul.appendChild(this.list[i]);
			if( i>n || i<m )	this.list[i].style.display = 'none';
			var _this = this;
			(function(t){
				_this.list[t].onclick = function(){ 
					if(t != _this.index)	_this.change(t,r_bPic,_this.imgs_full);
				}
			})(i);
		}
		if(m <= 11) lastpage.className ="begin";
		if((len - m) <= (n - m+1)) nextpage.className = "end";
		
	},
	roomsHTML:function(link){
		var h_bPic = $("h_bPic");
		var img = h_bPic.$("img");
		var h_sPic_ul =$("h_sPic");
		if(img[0] && img[0].parentNode) img[0].parentNode.removeChild(img[0]);
		if(h_sPic_ul && h_sPic_ul.parentNode )	h_sPic_ul.parentNode.removeChild(h_sPic_ul);
		this.ul = $c('ul');
		// 生成小图
		for(var j = 0; j<link.length; j++){
			this.list[j]=$c("li");
			this.list[j].innerHTML='<img src="'+link[j].min+'" width="46px" height="35px;" title="'+link[j].title+'"; style="display:block" />';
			this.ul.appendChild(this.list[j]);
			var _this = this;
			(function(t){
				_this.list[t].onclick = function(){ 
					if(t != _this.index)	_this.change(t,h_bPic,link);
				}
			})(j);
		}
		// 生成大图
		if(h_bPic){
			this.img = $c("img");
			this.img.onload = appendImg.bind(null, h_bPic, this.img);
			this.img.src = link[0] ? link[0].max: null;
			if(link[0] && link[0].title)	this.img.title = link[0].title;
		}
		if(h_bPic.parentNode){	
			h_bPic.parentNode.appendChild(this.ul);
			this.ul.className = "picture_list layoutfix";
			this.ul.id = "h_sPic";
		}
		if (this.list[0])	this.list[0].className = "select_pic";
	},
	change:function(n,ob,image){
		var len = this.imgs_full.length;
		var prev = $('prev');
		var next = $('next');
		var imgContent = image;
		if(this.img && this.img.parentNode == ob){
			ob.removeChild(this.img);
			this.img = $c("img");
			this.img.onload = appendImg.bind(null, ob, this.img);
			this.img.src = imgContent[n].max;
			if(imgContent[n].title)	this.img.title = imgContent[n].title;
			for(var i = 0; i <imgContent.length;i++)
				this.list[i].className = '';
			this.index = n;	
			this.list[this.index].className = "select_pic";
		}
		else if(this.img){
			this.img = $c("img");
			this.img.onload = appendImg.bind(null, ob, this.img);
			this.img.src = imgContent[n].max;
			if(imgContent[n].title)	this.img.title = imgContent[n].title;
			for(var i = 0; i <imgContent.length;i++)
				this.list[i].className = '';
			this.index = n;
			this.list[this.index].className = "select_pic";
		}
		this.now=n;
		if(this.now == len-1){
			next.className = "end";
		}
		else
			next.className = "next";	
		if(this.now == 0){
			prev.className ="begin";
		}
		else 
			prev.className="prev";
		//alert(this.end);
		//alert(this.now);
	}
};
function pageClass(page){
	var pClass = $('pageClass');
	var li = pClass.$('li');
	li[0].className="";
	li[1].className="";
	li[2].className="";
	if(page == 'room'){
		if($('pageClass').$('li')[1].style.display =="none"){
			li[0].className ="";
			li[2].className ="select_pic"
		}
		else{
			li[0].className ="";
			li[1].className ="";
			li[2].className ="select_pic" 
		}
	}
	else if(page=='fullView'){
		li[0].className ="";
		li[1].className ="select_pic";
		li[2].className =""
	}
	else if(page =='hotel'){
		li[0].className ="select_pic";
		li[1].className ="";
		li[2].className ="" 
	}
}
function appendImg(box, img){
	img.style.display = 'block';
	var height = box.clientHeight-10;
	var width = box.clientWidth-10;
	if(img.width > width || img.height > height){
		if((img.width / width)  >= (img.height / height)){
			img.height = img.height / (img.width / width);
			img.width = width ;
		}
		else{
			img.width = img.width /(img.height / height);
			img.height = height;
		}
	}
	if(/MSIE/.test(navigator.userAgent)){
		img.style.marginTop = (height - img.height+10) / 2 +"px";
	}
	else{
		img.style.marginTop = (height - img.height) /2 +"px";
	}
	img.style.marginLeft = (width - img.width) / 2 +"px";
	box.innerHTML = '';
	box.appendChild(img);
}
function indexArr(id,arr){
	for(var b = 0; b< arr.length; b++) {
		if(id == arr[b])	return b;
	}
}
window.onload = function(){
	window.init = new Focus($$.module.picture.room,$$.module.picture.hotel);
	$('viewRoomBtn').onclick = function(){
		window.init.li[2].onclick();
	};
}
