﻿/*
---
description: This provides a simple Drop Down menu with infinit levels
license: MIT-style
authors:
- Arian Stolwijk
requires:
  core/1.2.4: [Class.Extras,Element.Style,Element.Event]
provides: [MooDropMenu,Element.MooDropMenu]
...
*/
var MooDropMenu = new Class({
	Implements: [Options,Events],
	options: {
		onOpen: function(el){
			el.set('opacity',1);
		},
		onClose: function(el){
			el.set('opacity',0);
		},
		onInitialize: function(el){
			el.set('opacity',0);
		},
		mouseoutDelay: 200,
		mouseoverDelay: 0
	},
	
	initialize: function(menu, options, level){
		this.setOptions(options);
		
		if ($type(level) == 'number') {
			this.menu = document.id(menu); //attach menu to object
			this.fireEvent('initialize',menu);
			
			// hook up menu's parent with event to trigger menu
			this.menu.pel.addEvents({
				
				'mouseover': function(){
					// Set the DropDownOpen status to true			
					this.menu.pel.mel.store('DropDownOpen',true);
					
					// Clear the timer of the delay
					$clear(this.timer);
					// Fire the event to open the menu
					this.timer = (function(){
						this.fireEvent('open',this.menu.pel.mel);
					}).delay(this.options.mouseoverDelay,this);		
					
				}.bind(this),
				
				'mouseout': function(){
					// Set the DropDownOpen status to false
					this.menu.pel.mel.store('DropDownOpen',false);
					
					// Clear the timer of the delay
					$clear(this.timer);
					// Build a delay before the onClose event get fired
					this.timer = (function(){
						if(!this.menu.pel.mel.retrieve('DropDownOpen')){
							this.fireEvent('close',this.menu.pel.mel);
						}
					}).delay(this.options.mouseoutDelay,this);		
					
				}.bind(this)				
			});
		}
		else {
			level = 0;
			this.menu = document.id(menu);
		}
		
		// grab all of the menus children - LI's in this case		
		// loop through children
		this.menu.getChildren('li').each(function(item, index){
			var list = item.getFirst('ul'); // Should be an A tag
			// if there is a sub menu UL
			if ($type(list) == 'element') {
				item.mel = list; // pel = parent element
				list.pel = item; // mel = menu element
				new MooDropMenu(list, options, level + 1); // hook up the subMenu
			}
		});			
	},
	
	toElement: function(){
		return this.menu
	}
	
});

/* So you can do like this $('nav').MooDropMenu(); or even $('nav').MooDropMenu().setStyle('border',1); */
Element.implement({
	MooDropMenu: function (options){
		this.store('MooDropMenu',new MooDropMenu(this,options));
		return this;
	}
});

function resizeBG(){
	var sw=1320;
	var sh=900;	
	var mrs=0.5;
	var cw=20;
	var mw=mrs*sw;
	var mh=mrs*sh;
	var ratio=sw/sh;
	var bg=$('bfcCurtain');
	var iw=bg.getSize().x;
	var ih=bg.getSize().y;
	var ww=$(window).getSize().x;
	var wh=$(window).getSize().y;
	var bw=ww+cw;
	var bh=wh;

	if((bh<mh)&&(bw<mw)){
		bg.setStyle('height', mh);
		bg.setStyle('width', mw);
	}else{
		if(bh>bw){
			ih=bh;
			bg.setStyle('height', bh);
			iw=bh*ratio;
			bg.setStyle('width', iw);
			if(bw>iw){
				iw=bw;
				bg.setStyle('width', bw);
				ih=bw/ratio;
				bg.setStyle('height', ih);
			}
		}
		if(bw>=bh){
			iw=bw;
			bg.setStyle('width', bw);
			ih=bw/ratio;
			bg.setStyle('height', ih);
			if(bh>ih){
				ih=bh;
				bg.setStyle('height', bh);
				iw=bh*ratio;
				bg.setStyle('width', iw);
			}
		}
	}
}



window.addEvent('domready',function(){
	if(document.getElementById('bfcMainNav')){
		$('bfcMainNav').MooDropMenu({
			onOpen: function(el){
				el.fade('show');
			},
			onClose: function(el){
				el.fade('hide');
			},
			onInitialize: function(el){
				el.fade('hide').set('tween',{duration:50});
			}
		});
	}
	
	if(document.getElementById('bfcCurtain')){
		resizeBG();
	}
	
	
// implement accordion function
/*
	var myAccordion = new Fx.Accordion('h2.acc', 'div.item', {
		opacity: false,
		display: -1,
		alwaysHide: true,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#FFF');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#AD9961');
		}
	});
*/
});

window.addEvent('resize',function(){
	if(document.getElementById('bfcCurtain')){
		resizeBG();
	}
});



// Typoersetzung mittels cufon
Cufon.replace('#bfcClaim p',{fontFamily: 'HelveticaCnBold', textShadow:'0 1px 2px rgba(0, 0, 0, 0.1)'});
Cufon.replace('#bfcMainMenu p.main',{hover:true});
Cufon.replace('#bfcSubMenu p',{hover:true});
Cufon.replace('#bfcSite h1');
Cufon.replace('#bfcSite h2',{hover:true});
Cufon.replace('#bfcSite h3');
//Cufon.replace('#ewlCont h2');


