

var initializeWindows = function(){

	MochaUI.mediaviewWindow = function(obj){
	
		var list_id = obj.get('list_id');
		
		if(list_id) {
			
			MochaUI.list_view = true;
			MochaUI.list_id = list_id;
		}else{
			
			MochaUI.list_view = false;
		}
		
		var href = obj.get('href');
		
		if(!href || href =="#") {
				return;
		}
			
		
		if($('projects_windows')){
		
			MochaUI.updateContent({
		         element: $('projects_windows'),
		         loadMethod: 'xhr',
		         url: href
		       });
		}
		else{
			
			
			
			new MochaUI.Window({
				id: 'projects_windows',
				title: 'projects',
				loadMethod: 'xhr',
				contentURL: href,
				width: 860,
				height: 520,
				x: 300,
				y:40,
				resizable: true,
				toolbar: true,
				toolbarURL: 'pages/projects/tabs.html',
				footerHeight: 0,
				draggable: true,
				cornerRadius: 0,
				headerStartColor: [229,229,229],
				headerHeight: 23,
				headerStopColor: [229,229,229],
				contentBgColor: '#666666',
				padding: { top: 0, right: 0, bottom: 0, left: 0 },
				onContentLoaded: function(windowEl){
					
					if(Browser.Engine.trident){
							$$('a.change_headline').each(function(item) {
								item.addEvent('click', function(e){
									new Event(e).stop();
									var new_image = new Element('img', {
										'events': {
											load : function(){
												resize(this, 460,400);
												this.replaces($('hl'));
												this.set('id', 'hl');
											}
										}
									});
									new_image.set('src', item.get('href'));
									return false;
								});
							});
						}
						
				
					if(MochaUI.list_view){	
						
						current_list = list_cat[MochaUI.list_id];

			
						if(!current_list || current_list== [])
							return;
						
						
						$$('div.t_mediaviewer').each(function(item) {
															  
							var thum = item.getChildren('img');
															  
							thum.addClass('opcity50');
							if(Browser.Engine.trident){
								thum.removeClass('crusor_hand');
							}else{
								thum.removeClass('crusor_pointer');
							}
							item.removeEvent('click');
							
						});
						
				
						for(var i = 0; i< current_list.length; i++){
							
								if(!$(current_list[i])) continue;
								var thum = $(current_list[i]).getChildren('img');
								thum.removeClass('opcity50');
								
								if(Browser.Engine.trident){
									thum.addClass('crusor_hand');
								}else{
									thum.addClass('crusor_pointer');
								}
								
								var address = $(current_list[i]).get("address");
								
								if(address && address != "#"){
								
									$(current_list[i]).addEvent('click', function(e){
										
										new Event(e).stop();					
										
										var temp = new Element('a', {'href': this.get("address"),'id':this.get("id")});
										MochaUI.mediaviewWindow(temp);
									
									});
								}
								
						}
						
						
						
					
						$$('a.tab_item').each(function(item) {
						
					
							if(item.get('id') == 'p_' + MochaUI.list_id){
								
								item.getParent().addClass('selected');
							}
							else if(item.getParent().hasClass('selected')){
								item.getParent().removeClass('selected');
							}
						});
					}				
					
				}
			});
		}
		
	};

	MochaUI.plainviewWindow = function(obj){

		new MochaUI.Window({
		
			id: obj.get('id') + 'windows',
			title:obj.get('text'),
			loadMethod: 'xhr',
			contentURL: obj.get('href'),
			width: 650,
			height: 650,
			x: 220,
			y:20,
			resizable: true,
			maximizable: true,
			footerHeight: 0,
			draggable: true,
			cornerRadius: 0,
			contentBgColor: '#666666',
			headerStartColor: [229,229,229],
			headerHeight: 23,
			headerStopColor: [229,229,229],
			padding: { top: 0, right: 0, bottom: 0, left: 0 },
			onContentLoaded: function(windowEl){
				$$('span.t_mediaviewer').each(function(item) {
					if(Browser.Engine.trident){
						item.addClass('crusor_hand');
					}else{
						item.addClass('crusor_pointer');
					}
					item.addClass('text_underlind');
					item.addEvent('click', function(e){
										
						new Event(e).stop();					
						
						var temp = new Element('a', {'href': this.get("address"),'id':this.get("id")});
						MochaUI.mediaviewWindow(temp);
					
					});
				});
			}
			
		});
		
	};


	MochaUI.pictureviewWindow = function(obj){

		new MochaUI.Window({
		
			id: obj.get('id') + 'windows',
			title:obj.get('text'),
			loadMethod: 'iframe',
			contentURL: obj.get('href'),
			width: 500,
			height: 400,
			x: 220,
			y:20,
			resizable: true,
			maximizable: true,
			footerHeight: 0,
			draggable: true,
			cornerRadius: 0,
			contentBgColor: '#666666',
			headerStartColor: [229,229,229],
			headerHeight: 23,
			headerStopColor: [229,229,229],
			padding: { top: 0, right: 0, bottom: 0, left: 0 }
			
		});
		
	};

	MochaUI.accordianWindow = function(){
		var id = 'accordian';
		new MochaUI.Window({
			id: id,
			title: 'ELEV',
			loadMethod: 'xhr',
			contentURL: 'pages/navigation.html?t=' + Math.ceil(Math.random() * 100),
			width: 180,
			height: 150,
			x: 10,
			y: 20,
			draggable: false,
			cornerRadius: 0,
			scrollbars: false,
			resizable: false,
			maximizable: false,	
			closable: false,
			bodyBgColor: [13,9,5],
			contentBgColor: '#20201e',
			footerHeight: 0,
			headerStartColor: [13,9,5],
			headerHeight: 40,
			headerStopColor: [1,1,1],
			padding: { top: 0, right: 0, bottom: 0, left: 0 },
			onContentLoaded: function(windowEl){
			
				$$('a.mediaviewer').each(function(item) {
		
					item.addEvent('click', function(e){	
						new Event(e).stop();
						MochaUI.mediaviewWindow(item);
					});
				});
				
				$$('a.plainviewer').each(function(item) {
					
					item.addEvent('click', function(e){	
						new Event(e).stop();
						MochaUI.plainviewWindow(item);
					});
				});
							

			
				this.windowEl = windowEl;
				var accordianDelay = function(){
					new Accordion('#' + id + ' h3.accordianToggler', "#" + id + ' div.accordianElement',{
						start: 'all-closed',
						opacity: true,
						alwaysHide: true,
						onActive: function(toggler, element){
								toggler.addClass('open');
						},
						onBackground: function(toggler, element){
								toggler.removeClass('open');
						},							
						onStart: function(toggler, element){
							this.windowEl.accordianResize = function(){
								MochaUI.dynamicResize($(id));
							}
							this.windowEl.accordianTimer = this.windowEl.accordianResize.periodical(10);
						}.bind(this),
						onComplete: function(){
							this.windowEl.accordianTimer = $clear(this.windowEl.accordianTimer);
							MochaUI.dynamicResize($(id)) // once more for good measure
						}.bind(this)
					}, $(id));
				}.bind(this);
				accordianDelay.delay(10, this); // Delay is a fix for IE
			}
		});
	};
	
	MochaUI.accordianWindow();
	
	
};



// Initialize MochaUI when the DOM is ready
window.addEvent('domready', function(){

	
	initializeWindows();
	MochaUI.bgs = ['images/background/ELEV-photo-sm-4.jpg','images/background/ELEV-ABB-2.jpg','images/background/ELEV-yading-cliff-hotel-1.jpg','images/background/ELEV-photo-sm-a2.jpg','images/background/ELEV-quilt-collage-copy.jpg', 'images/background/ELEV-SK.jpg','images/background/ELEV-photo-sm-a1.jpg'];
	MochaUI.bgs_index = 0;
	setTimeout('change_bg()', 10000);
});

// This runs when a person leaves your page.
window.addEvent('unload', function(){
	if (MochaUI) MochaUI.garbageCleanUp();
});

function $E(tag,el){return $(el||document).getElement(tag)}

window.addEvents({
	domready:function(){
		$extend(kina,{bg:$E('img',$E('#bg',db=$(document.body))),timer:(Browser.Engine.gecko||Browser.Engine.webkit)?null:setInterval(kina.fix,200),menu:$('menu')});gal.call(gal);
		if($$('dl')[0]){var hs=$$('dt'),fx=hs.map(function(el){return new Fx.Morph(el,{wait:false})});new Accordion(hs,hs.getNext(),{opacity:false,onActive:function(head,box){if($chk(this.active))fx[this.active].start({'color':'#000','backgroundColor':'#fff'});fx[this.active=this.togglers.indexOf(head)].start({'color':'#fff','backgroundColor':'#000'})}})}
	},
	keypress:function(e){var e=new Event(e);if(e.key=='down'||e.key=='right')e.stop()}
});

kina={
	doc:{x:0,y:0},
	fix:function(){if(kina.bg.complete&&(db.offsetWidth!=kina.doc.x||db.offsetHeight!=kina.doc.y||kina.fix.src!=kina.bg.src)){var bg=kina.bg.getSize();kina.doc={x:db.offsetWidth,y:db.offsetHeight};kina.fix.src==kina.bg.src||$extend(kina.fix,{p:bg.x/bg.y,src:kina.bg.src});kina.bg.setStyles({width:(bg.y=kina.doc.x>(bg.x=Math.round(kina.doc.y*kina.fix.p)))?kina.doc.x:bg.x,height:bg.y?Math.round(kina.doc.x/kina.fix.p):kina.doc.y})}}
}

$extend(gal=function(){
	if(gal.el=$('photos')){$extend(this,{fx:new Fx.Tween(kina.menu,'bottom'),float:$('float').addEvent('click',gal.key)}).el.getElements('a').addEvent('click',this.swap)[0].addClass('cur');document.addEvents({mousemove:gal.mouse,keydown:gal.key})}
},{
	mouse:function(e){var w=db.offsetWidth/2,x=e.client.x,y=db.offsetHeight-e.client.y,el=gal.float;if(y<(gal.menu?85:20)){$clear(gal.timer);gal.menu||(gal.menu=gal.fx.start(20))}else{if(gal.menu)gal.menu=!(gal.timer=setTimeout(function(){gal.fx.start(-55)},700));else{if(y<85&&el.className)el.className=el.style.display='';else if(y>85){if(!el.className)el.style.display='block';if(x>w&&el.className!='nxt')el.className='nxt';else if(x<w&&el.className!='prv')el.className='prv';with(el.style){left=x-20+'px';top=e.client.y-10+'px'}}}}},
	swap:function(){$E('.cur',gal.el).removeClass('cur');kina.bg.src=this.addClass('cur').getElement('img').src},
	key:function(e){var nxt=e.key?(e.code==39):(gal.float.className=='nxt');return (e.key&&e.code!=37&&e.code!=39)?false:gal.swap.call($E('.cur',gal.el)[nxt?'getNext':'getPrevious']()||gal.el[nxt?'getFirst':'getLast']())},
	menu:true
});
function resize(item, max_width, max_height){
	old_width = item.get('width');
	old_height = item.get('height');
	
	if(old_width < max_width && old_height < max_height){
		return;
	}
	
	if((old_width/old_height) > (max_width/max_height)){
		item.set('width', max_width);
		item.set('height', old_height * (max_width/old_width));
	}else{
		item.set('height', max_height);
		item.set('width', old_width * (max_height/old_height));
	}
}

function change_bg(){
	if(MochaUI.bgs_index == 5){
		MochaUI.bgs_index = 0;
	}
	$('backgroud_img').set('src', MochaUI.bgs[MochaUI.bgs_index]);
	MochaUI.bgs_index++;
	setTimeout('change_bg()', 10000);

}


var list_cat = {
	"buildings" : ["gzt","sxh","tjc","glh","rzm","wht","csp","vlp","gxp","scp","sfp","sss","glm","kbs5","kbs7","yad","yrh","ord","dsg"],
	"urban_rural": ["tjc","glh","rzm","csp","vlp","gxp","scp","sss","kbs5","kbs7","lia","dsg","ord","yad","yrh"],
	"landscapes" : ["tjc","glh","rzm","csp","gxp","scp","kbs5","kbs7","lia","dsg","ord"],
	"interior_exhibition" : ["glh","sms","fjo","abb","glm","ord"],
	"furniture_products" : ["cab","cab2","lmp"],
	"others" : ["hbt","qlt"]
};





