/**
 * @author kristiaan.thivessen
 */
 
 $(function() {
 	
 	Dela = {
 		/*
 		 * Main navigation (dropdown menu)
 		 */
 		MainNavigation: {
 			init: function() {
 				var self = this;
 				this.timer;
 				this.delay = 300;
 				this.duration = 200;
 				this.opened = false;
 				this.items = $('#main_nav ol>li').filter(function(){
 					return ($('.mnav_sub', this).length == 1);
 				});
 				this.items.bind('mouseover focusin', function() {
 					self.open($(this));
 				}).bind('mouseout focusout', function() {
 					self.close($(this));
 				});
 				$('.mnav_sub_body', this.items).hide();
 			},
 			open: function(item) {
 				var self = this;
 				if(this.opened) this.switchSubMenu(item);
 				else this.timer = window.setTimeout(function() {
 					item.addClass('open');
 					self.opened = true;
 					$('.mnav_sub_body', item).slideDown(self.duration);
 				}, this.delay); 				
 			},
 			close: function(item) {
 				var self = this;
 				window.clearTimeout(self.timer);
 				this.timer = window.setTimeout(function() {
					self.opened = false;
					$('.mnav_sub_body', item).slideUp(self.duration, function() {
						item.removeClass('open');
					});
				}, this.delay); 
 			},
 			switchSubMenu: function(item) {
 				var self = this;
 				window.clearTimeout(self.timer);
	 			if(!item.hasClass('open')) {
	 				// Only close all and open the new item when it is not already open.
	 				// This is needed to prevent focusin/focusout from closing a submenu
	 				// too early when traversing its content with the tab key.
	 				this.items.removeClass('open');
	 				$('.mnav_sub_body', this.items).hide();
	 				item.addClass('open');
	 				$('.mnav_sub_body', item).show();
	 			}
 			}
 		},
 		/* 
 		 * Page utilities
 		 */
 		PageUtilities: {
 			init: function() {
 				$('.page_utilities .print').bind('click', function(e) {
 					e.preventDefault();
 					Dela.PageUtilities.printPage();
 				})
 			},
 			// Print page
 			printPage: function() {
 				return window.print();
 			},
			openShadowBox: function(url) {
				Shadowbox.open({
			        content:    url,
			        player:     "iframe",
			        width:      560
			    });	
			}
 		},
 		
 		/*
 		 * Initialize all Dela objects here
 		 */ 
 		init: function() {
 			this.MainNavigation.init();
 			this.PageUtilities.init();
 		}
 	}
 	
 	Dela.init();
 	
 });

