var Site = {

	pe : new Object(),
	currImg : 0,
	currMenu : false,
	currIter : 0,
	currJump : false,
	subMenuHts : new Array(),

	start : function() {
		Site.setup();
		Site.behaviour();
		if($('bgFeature')){ Site.pe = new PeriodicalExecuter(Site.fadeRightImgs,2); }
	},
	
	setup : function() {
		$$('#bgFeature img').each(function(b,i){
			if(i !=0){ b.hide(); }
		});
/*
		$$('#jumpMenu li').each(function(b,i){
			var li_a = b.getElementsBySelector('a').first(); 
			//b.insert({ 'bottom': '<ul><li><a href="' + li_a.href + '"></a></li></ul>' }); 
			b.insert({ 'bottom': '<ul style="display:none;"><li></li></ul>' }); 
		});

		$$('#menuContainer > ul > li > ul').each(function(b, iter){ 
			Site.subMenuHts.push(b.offsetTop);
			var a_link = b.parentNode.firstChild;
			if(b.className != 'submenu_active'){ b.hide(); }
			else { Site.currMenu = b }	 
		});
*/		
//		$$('#memberForm').each(function(b){
//			b.hide();
//		});	
		$$('#studentFeature', '#featureControls', '#newsFeature').each(function(b){ 
			b.setStyle({ 'position' : 'absolute', 'top' : b.offsetTop + 'px', left: b.offsetLeft + 'px' });
		});
		$$('#studentFeature').each(function(b){ 
			b.hide();
			b.innerHTML = '<div style="width:100%;float:none;">' + b.innerHTML + '</div>';
		});
		$$('#newsFeature').each(function(b){ 
			b.innerHTML = '<div style="width:100%;float:none;">' + b.innerHTML + '</div>';
			b.setStyle({ 'overflow' : 'hidden', 'height' : b.getHeight() + 'px', 'width' : b.getWidth() + 'px' });
		});
		$$('#newsFeature > div').each(function(b){ 
			b.setStyle({ 'float' : 'none', 'position' :'absolute', 'top' :  b.parentNode.getHeight() + 'px' });
		});
/*		$$('#menuContainer').each(function(b){
			var ht = $('contentContainer').getHeight() + $('contentContainer').offsetTop - b.offsetTop;
			b.setStyle({ 'height' : ht + 'px' });
			
		});		
*/	},
	
	behaviour : function() {
/*
		$$('li#jl01', 'li#jl02', 'li#jl03').each(function(el, i){
			el.onmouseover = function(){
				var jump_img = el.getElementsBySelector('ul').first();
				console.log(Site.currJump, jump_img);
				if(Site.currJump === false){ 
					el.addClassName('jumpMenuActive');
					new Effect.BlindDown(jump_img, { duration: 0.7 });
					Site.currJump = jump_img; 
				} else {
					if(Site.currJump == jump_img){ return; }
					Site.currJump.parentNode.removeClassName('jumpMenuActive');
					new Effect.BlindUp(Site.currJump, 
						{ 	duration: 0.7, 
							afterFinish: function(){
								el.addClassName('jumpMenuActive');
								new Effect.BlindDown(jump_img, { duration: 0.7 });
								Site.currJump = jump_img;
							}
					});
				}
			};
		});

*/
/*
		$$('#jumpNav').each(function(el){
			Event.observe(el, 'mouseout', function(){
				$$('#jumpMenu li ul').each(function(b){
					if(b.visible()){ 
						b.hide(); 
						Site.currJump = false;
						$$('li#jl01', 'li#jl02', 'li#jl03').each(function(t){
							t.removeClassName('jumpMenuActive');
						})
					}
				});		
			}, useCapture=false);
		});

		$$('#menuContainer > ul > li').each(function(el, i){
			el.onmouseover = function(){
				var submenu = el.getElementsBySelector('ul').first();
				if(submenu == Site.currMenu){ return false; }
				if(Site.currMenu){
					var cm_ht = Site.currMenu.offsetTop;
					new Effect.Parallel(
						[ 
							new Effect.Move(Site.currMenu, { x : 0, y: cm_ht + 200, sync: true } ), 
							new Effect.Opacity(Site.currMenu, { to: 0.0, from: 1.0, sync: true } ) 
						],
						{ duration: 0.8, afterFinish: function(){ 
							if(!submenu){ Site.currMenu = false; }
							$$('#menuContainer > ul > li > ul').each(function(b){ b.hide(); });
						} } );
				}
				if(!submenu){ return false; }
				$$('#menuContainer > ul > li > ul').each(function(b, iter){ if(b == submenu){ Site.currIter = iter; } });
				Site.currMenu = submenu;
				submenu.setOpacity(0).setStyle({ 'top' : '0px' }).show();
				new Effect.Parallel(
					[ 
						new Effect.Move(submenu, { x : 0, y: Site.subMenuHts[Site.currIter], sync: true } ), 
						new Effect.Opacity(submenu, { to: 1.0, from: 0.0, sync: true } ) 
					],
					{ duration: 0.8 } );
			};
		});
*/
//		$$('#memberLink').each(function(el){
//			el.onclick = function(){
//				if($('memberForm')){
//					Effect.Appear($('memberForm'));
//				}
//			}
//		});	
		$$('#featureControls li').each(function(el, i){
			el.setStyle({ 'backgroundPosition' : '-300px 0px'});
			var length = el.getWidth();
			var duration = 0.7;
			el.onmouseover = function(){
				new Effect.Morph(el, { 
					duration: duration, 
					style: { 'backgroundPosition' : '-' + (300 - length) + 'px 0px', 'color' : '#FFFFFF' }
				});			
			}
			el.onmouseout = function(){
				new Effect.Morph(el, { 
					duration: duration, 
					style: { 'backgroundPosition' : '-300px 0px', 'color' : '#000000' }
				});			
			}
			el.onclick = function() {
				if(i == 0){
					new Effect.Move($$('#newsFeature > div').first(), { x:0, y:0, mode: 'absolute', duration: 0.8});		
				} else {
					if($('studentFeature').visible()){ return; }
					new Effect.SlideDown('studentFeature', { duration: 0.8});		
				}
			
			}
		});
		// Cross Fade Image Gallery
		$$('#img_gallery').each(function(el){ new Gallery('img_gallery'); });
	},
	
	fadeRightImgs : function() {
		var Imgs = $$('#bgFeature img')
		var numImgs = Imgs.length;
		var nextImg = Site.currImg + 1;
		if(nextImg == numImgs){ return; }
		new Effect.Fade(Imgs[Site.currImg], { duration: 0.7 });
		new Effect.Appear(Imgs[nextImg], { duration: 0.7 });
		Site.currImg = nextImg;
	}
	

}

document.observe('contentloaded', Site.start);

var Gallery = Class.create();
Gallery.prototype = {
	
	defaultOptions : {
		thumbnails 	: 'thumbnails',
		duration 	: 0.7
	},
	initialize: function(container) {
		this.container = $(container);
		options = arguments[1] || {};
		this.options = Object.extend(Object.extend({},this.defaultOptions), options || {});
		this.options.thumbnails = $(this.options.thumbnails);
		this.start();
	},
	start: function() {
		$$("#thumbnails > a").each(function(el,item){
		//this.options.thumbnails.getElementsBySelector('a').each(function(el,item){
			//el.onclick = function(evt){evt.preventDefault();this.reveal(item); return false;}.bind(this);
			el.onclick = function(){this.reveal(item); return false;}.bind(this);
		}.bind(this));
	},
	getCurrItem : function() {
		var items =  $$('#images > img');
		//var items = $(this.container).getElementsByClassName('main_img');
		var iter = 0;
		//console.log(items);
		items.each(function(el,i){
			if(el.style.display != 'none'){ iter = i; }
		});
		//console.log(iter);
		return iter;
	},
	reveal : function(i){
		this.images = $$('#images > img');
		
		//this.images = $(this.container).getElementsByClassName('main_img');
		this.currItem = this.getCurrItem();
		
		var after_finish = function(){
			this.images.each(function(b, iter){ if(iter != i){ b.hide(); } });
		}.bind(this);
		
		if(this.currItem == i){ return; }
		
		this.images[this.currItem].style.zIndex = 1;
		new Effect.Fade(this.images[this.currItem],{ duration: this.options.duration });
		
		this.images[i].style.zIndex = 2;
		new Effect.Appear(this.images[i],{ duration: this.options.duration, afterFinish: after_finish });
	}
		
}
