var maxSectionHeight = "400";
var minMenuHeight = "270";
var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
var section = 0;
var ua = navigator.userAgent.toLowerCase(); 
var crapBrowser = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) ); 

maxSectionHeight = (windowHeight - 240) < minMenuHeight  ? minMenuHeight : (windowHeight - 240);

function init(){ var mainMenu = new menu(); mainMenu.init();}

addEventToObject(window, 'onload',init);


function menu(){
	
	var me = this;
	
	this.container = $('main_menu_container');
	this.main = $('main_menu');
	this.menuItems = $$('.menu_title');
	this.menuSections = $$('.menu_section');
	this.title = $('menu_head');
	
	this.openTog;
	this.openSection = false;
	this.closedSections = new Array();
	this.exposeSub = true;
	
	this.expose = me.menuSections[0];
	
	this.test = me.expose.getSize().y;
	
	var myAccordion;
	
	var exposeScroller;
	
	this.init = function(){
		
		//Thanks Microsoft !!!!!!!!
		if (crapBrowser) { 
			
			$$('.menu_title').each(function(item){ item.src = item.src.substring(0, item.src.indexOf('.png')) + ".gif"; });
			
		}
		
		me.container.style.zIndex = 2;
		
		me.menuItems.each(function(item){ item.set('opacity', 0.6);});
	
		//$$('.sub_menu_link').each(function(item){ item.onclick = function(){ me.subMenu(item); return false;};});
		
		var subItems = $$('.sub_menu_link');
		for(var i=0; subItems[i]; i++) {
			
			subItems[i].onclick = function() { me.subMenu(this); return false;};	
		}
		
		//SET EXCAVATE
		
		var advanced = new Array;
		
		for(var i=0; $$('.advanced')[i]; i++){
			advanced[i] = new Fx.Slide($$('.advanced')[i]).hide();
		}
		
		$('opt_search').onclick = function() {
			advanced.each(function(item){ item.toggle(); }); 
			me.openSection.style.height = '';
		};
		
		var excavate = $('excavate_form');
		$('search_field').onclick = function() { if(this.value == 'Search') this.value = ''; };
		excavate.onsubmit = function() {
			if(excavate.search_field.value == 'Search') excavate.search_field.value = '';
			
			if(excavate.search_field.value || excavate.keyword_field.value || excavate.date1.checked || excavate.date2.checked ||excavate.date3.checked || excavate.date4.checked ) {
	
			var htmlRequest = new Request.HTML({url: '/excavate/',
			onSuccess: function(request){
				$('gallery').innerHTML = '';
				$('gallery').adopt(request);
				initilize();
				window.location.replace('#');
				
			},
			onRequest: function(){
				$('gallery').innerHTML = 'loading';
				
			}}).get(this);
			
			}
			else { alert('Please enter a search word'); }
			return false;
		};
		
		/////////////////// SET INITIAL IMAGE ////////////
		if(randinator){
		var htmlRequest = new Request.HTML({url: randinator,
			onSuccess: function(result){
				$('gallery').innerHTML = '';
				$('gallery').adopt(result);
				initilize();
				window.location.replace('#' + menuEl.href);
				
			},
			onRequest: function(){
				$('gallery').innerHTML = 'loading';
				
			}}).get({'menu':true });
		
		}
		
		/////////////////////
		
		
		myAccordion = new zadokAccordion(me.menuItems, me.menuSections, {
			display: section,					 
			alwaysHide: true,
			maxHeight:maxSectionHeight,
			onActive: function(toggler,el){
				//fade in the current toggler
				var fadeIn = new Fx.Morph(toggler, {duration:300});
				fadeIn.start({'opacity': [0.6, 1]});
				if(el.id == 'expose') { me.expose('show'); }
				me.openSection = el;
				me.openTog = toggler;		
			},
			onBackground: function(toggler,el){
				var fadeOut = new Fx.Morph(toggler, {duration:300});
				fadeOut.start({'opacity': [1, 0.6]});
				
				me.closedSections.push(el.id);
				
			},
			onComplete: function(){
				
				me.closedSections.each(function(item){ if(me.openSection.id == item) { me.openSection = ''; }});
			
			
				if(me.openSection.id == 'expose') { 
					
					if($('expose_slider').getSize().y > maxSectionHeight) {
						exposeScroller = new scrollBar(me.container, $('expose_slider'), $('expose'));
						exposeScroller.show();
					}
					//me.expose('show');
					
				}
				else if(exposeScroller) { exposeScroller.remove(); exposeScroller=''; }
				/*me.closedSection.each(function(item){
					if(item == 'expose') { 
						exposeScroller = new scrollBar(me.container, $('expose_slider'), $('expose'));
						exposeScroller.show();
						}
					else { 
						exposeScroller.remove();
					}
				});
				*/
				me.closedSections = [];
			
			}
		});
		
		makeDragable(me.title, me.container, 1);
	};
	
	this.expose = function(option){
		
		if(option == 'show'){
			
			$$('.sub_menu_image').each(function(item){
			var myFx = new Fx.Slide(item, {
   				mode: 'vertical',
				onComplete : function() {  }
			}).slideIn();
		});
			
			
		}
		
		
	};
	/*
	this.subMenuShow = function(toggler, el) {
		
		$$('.toggler').each(function(item){
			item.set('opacity', 0.6);
		});
		$$('.sub_menu_item').each(function(item){
			item.style.color = '#999';
		});
		$$('.sub_menu_image').each(function(item){
			var myFx = new Fx.Slide(item, {
   				mode: 'vertical',
				onComplete : function() { myAccordion.display(0); }
			}).slideIn();
		});
		
		
	};
	*/
	this.subMenu = function(menuEl) {
	
		//if(me.openSection) me.openSection.style.height = '';
		
		//if(exposeScroller) exposeScroller.remove();
		
		//new scrollBar(me.container).hide();;
		//if expose section then hide all thumbnails
		
		//fade out the previous selected main menu item
		$$('.toggler').each(function(item){
			item.set('opacity', 0.6);
		});
		
		$$('.sub_menu_link').each(function(item){
			item.style.color = '#999';
		});
		/*
		$$('.sub_menu_image').each(function(item){
			var myFx = new Fx.Slide(item, {
   				mode: 'vertical',
				onComplete: function(){
					$$('.open').each(function(item){
						//item.style.height = '';
					});
				}
			}).slideOut();
		});
		*/
		
		var fadeIn = new Fx.Morph(menuEl, {duration:300});
		fadeIn.start({'color': ['#999', '#000'] });
			
		//window.location.replace(menuEl.href);
		
		var htmlRequest = new Request.HTML({url: menuEl.href,
			onSuccess: function(tree){
				$('gallery').innerHTML = '';
				$('gallery').adopt(tree);
				initilize();
				window.location.replace('#' + menuEl.href);
				
			},
			onRequest: function(){
				$('gallery').innerHTML = 'loading';
				
			}}).get();
		
	};
}


var zadokAccordion = new Class({
	Extends: Accordion,
	
	options: {
		maxHeight: false,
		maxWidth: false
	},
	
	display: function(index){
 
	index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
 
	if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
 
	this.previous = index;
 
	var obj = {};
 
	this.elements.each(function(el, i){
 
		obj[i] = {};
 
		var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
 
		this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
 
		for (var fx in this.effects) {
 
			obj[i][fx] = hide ? 0 : el[this.effects[fx]];
 
			if (fx == 'height' && obj[i][fx] > this.options.maxHeight && this.options.maxHeight) obj[i][fx] = this.options.maxHeight;
 
			if (fx == 'width'  && obj[i][fx] > this.options.maxWidth && this.options.maxWidth)  obj[i][fx] = this.options.maxWidth;			
 
		}
 
	}, this);
 
	return this.start(obj);
	
	}
});