var galleryArray = new Array();

galleryArray = [

{name:'北京', id:'beijing', properties:[
	{name:'環球金融中心', id:'wfc', photos:8},
	{name:'北京恒基中心', id:'hendersoncentre', photos:1},
	{name:'匯豪閣 － 北京', id:'stateapartments', photos:2}
]},

{name:'長沙', id:'changsha', properties:[
	{name:'恒基‧凱旋門', id:'archoftriumph', photos:4}

]},

{name:'成都', id:'chengduplace', properties:[
	{name:'成都環球貿易廣場', id:'chengdu', photos:1}

]},

{name:'重慶', id:'chongqing', properties:[
	{name:'恒基‧翔龍江畔', id:'nanan', photos:3},
	{name:'恒基‧翠庭', id:'erlang', photos:4}

]},

{name:'大連', id:'dalian', properties:[
	{name:'金石灘項目', id:'jinshitan', photos:2}
]},

{name:'廣州', id:'guangdong', properties:[
	{name:'廣州恒寶廣場', id:'hengbaoplaza', photos:1},
//	{name:'廣州恒茘灣畔花園', id:'hengli', photos:2}
	{name:'恒茘灣畔', id:'hengli', photos:2}

]},

{name:'鞍山', id:'anshan', properties:[
	{name:'舊體育館', id:'oldstadium', photos:1},
	{name:'營城子項目', id:'yingchengzi', photos:1}
]},

{name:'南京', id:'nanjing', properties:[
	{name:'金滿庭', id:'qixia', photos:2},
	{name:'玲瓏翠谷', id:'xianlin', photos:1}

]},

{name:'上海', id:'shanghai', properties:[
	{name:'恒匯國際大廈', id:'centro', photos:1},
	{name:'環智國際大廈', id:'lot147', photos:1},
	{name:'靜安區南京西路688號地塊', id:'lot688', photos:2},
	{name:'名人商業大廈', id:'metropolitan', photos:1},
	{name:'上海港匯廣場第二期辦公大樓', id:'grandgateway', photos:1},
	{name:'上海不夜城廣場', id:'skycity', photos:2}
]},

{name:'瀋陽', id:'shenyang', properties:[
	{name:'瀋陽國際金融中心', id:'ifc', photos:1},
	{name:'金河灣', id:'puhe', photos:1}

]},

{name:'蘇州', id:'suzhou', properties:[
	{name:'水漾花城', id:'xiangcheng', photos:5}

]},

{name:'鐵嶺', id:'tieling', properties:[
	{name:'新城中心發展項目', id:'newtowncentral', photos:1},
	{name:'蓮花湖發展項目', id:'lotuslake', photos:1}

]},

{name:'西安', id:'xian', properties:[
	{name:'恒基‧碧翠錦華', id:'jinhuagallery', photos:3},
	{name:'御錦城', id:'botanica', photos:4}

]},

{name:'徐州', id:'xuzhou', properties:[
	{name:'恒基‧ 雍景新城', id:'xuzhou', photos:4}

]},

{name:'宜興', id:'yixing', properties:[
	{name:'譽瓏島', id:'yicheng', photos:2},
	{name:'東氿譽瓏湖濱項目', id:'dongjiu', photos:1}

]}

];


function setGallery()
{
	var select = d.getElementById('gallerySelect');
	var goBtn = d.getElementById('galleryGoBtn');
	var pullldown = d.getElementById('galleryListPullDown');
	var closeBtn = d.getElementById('galleryCloseBtn');
	var imgPath = '/tc/images/propertieschina/';
	var sectionNavs = new Array();
	var photoNavs = new Array();
	var location;
	var section = galleryArray[0].id;
	var curIndex;
	var highlightSection;
	var highlightPhoto;
	var self = this;
	
	this.init = function()
	{
		this.genSelect();
		this.setGoBtn();
		this.genPulldown(0);
	};
	
	this.genSelect = function()
	{
		select.options.length = 0;
		
		for (var i=0; i<galleryArray.length; i++)
		{
			select.options.add(new Option(galleryArray[i].name, i));
		}
	};
	
	this.setGoBtn = function()
	{
		goBtn.onclick = function()
		{
			setTimeout(function()
			{
				if (location != select.value)	self.changeLocation(select.value);
				self.showPulldown(true);
			}, 10);
		};
		
		closeBtn.onclick = function()
		{
			self.showPulldown(false);
		};
	};
	
	this.showPulldown = function(over)
	{
		pullldown.style.display = (over)?	'block'	:	'';
	};
	
	this.changeLocation = function(id)
	{
		self.genPulldown(id);
	};
	
	this.genPulldown = function(id)
	{
		location = id;
		var array = galleryArray[id].properties;
		var content = getElementsByClassName(pullldown, 'content')[0];
		content.innerHTML = '';
		section = null;
		highlightSection = null;
		sectionNavs = new Array();
		
		for (var i=0; i<array.length; i++)
		{
			var productgallery = createDiv('', 'productgallery');
			var imgPart = createDiv('', 'imgPart');
			var imgName = createDiv('', 'imgName');
			var img = createImg(imgPath + array[i].id + '/gallery_thumb_img_1.jpg', '');
			imgPart.appendChild(img);
			imgName.innerHTML = array[i].name;
			productgallery.appendChild(imgPart);
			productgallery.appendChild(imgName);
			productgallery.id = array[i].id;
			productgallery.num = i;
			
			productgallery.onmouseover = function()
			{
				if (section == this.id)	return;
				this.className += ' active';
			};
			productgallery.onmouseout = function()
			{
				if (section == this.id)	return;
				this.className = this.className.replace(/active/, '');
			};
			productgallery.onclick = function()
			{
				if (section == id)	return;
				self.sectionClick(this);
				self.showPulldown(false);
			};
			
			content.appendChild(productgallery);
			sectionNavs.push(productgallery);
		}
		
		if (!section)	self.sectionClick(sectionNavs[0]);
	};
	
	this.sectionClick = function(btn)
	{
		if (highlightSection)	highlightSection.className = highlightSection.className.replace(/active/, '');
		if (btn.className.indexOf('active') < 0)	btn.className += ' active';
		self.genContent(btn.id, btn.num);
		section = btn.id;
		highlightSection = btn;
	};
	
	this.genContent = function(id, num)
	{
		var obj = galleryArray[location].properties[num];
		var txt = d.getElementById('imgThumbDes');
		curIndex = null;
		highlightPhoto = null;
		
		txt.innerHTML = obj.name;
		setTimeout(function(){self.genNav(id, obj.photos);}, 10);
	};
	
	this.genNav = function(id, total)
	{
		var thumbs = d.getElementById('imgThumb');
		thumbs.innerHTML = '';
		photoNavs = new Array();
		
		for (var i=0; i<total; i++)
		{
			var a = createA('javascript:;', '');
			var img = createImg(imgPath + id + '/gallery_thumb_img_' + (i+1) + '.jpg', '', '', 39);
			var span = d.createElement('span');
			a.appendChild(img);
			a.appendChild(span);
			a.num = i;
			a.img = imgPath + id + '/gallery_img_' + (i+1) + '.jpg';
			
			a.onmouseover = function()
			{
				if (curIndex == this.num)	return;
				this.className += ' active';
			};
			a.onmouseout = function()
			{
				if (curIndex == this.num)	return;
				this.className = this.className.replace(/active/, '');
			};
			a.onclick = function()
			{
				if (curIndex == this.num)	return;
				self.photoClick(this);
				self.showPulldown(false);
			};
			
			thumbs.appendChild(a);
			photoNavs.push(a);
		}
		
		if (!curIndex)	self.photoClick(photoNavs[0]);
	};
	
	this.photoClick = function(btn)
	{
		if (highlightPhoto)	highlightPhoto.className = highlightPhoto.className.replace(/active/, '');
		if (btn.className.indexOf('active') < 0)	btn.className += ' active';
		
		var photo = d.getElementById('currentImgBg');
		setTimeout(function()
		{
			photo.src = btn.img;
			startFading(photo);
			
			curIndex = btn.num;
			highlightPhoto = btn;
			//self.showPulldown(false);
		}, 10);
	};
	
	this.init();
}
