var zoral = {};

zoral.Header = {
	init: function(){
		var _this = this;
		this.headerArea = $('#marketingHeader');
		this.tabTriggers = this.headerArea.find('ul.marketing-tabs a');
		this.tabTriggers.click(function(){
			if(!_this.animating) {
				_this.switchTo(this);
			};
			return false;
		});
		
		this.tabs = this.headerArea.find('div.tab-content');
		this.activeTabTrigger = this.tabTriggers.filter('.active');
		var activeTabID = this.activeTabTrigger.attr('rel');
		var inactiveTabs = this.tabs.not('#' + activeTabID);
		inactiveTabs.css('display','none').removeClass('hidden');
		
		this.activeTab = $('#' + activeTabID);
		
	},
	switchTo: function(_link){
		var _this = this;
		var activeTab = $('#' + _link.rel);
		
		if(this.activeTab.attr('id') === _link.rel) return;
		
		this.animating = 1;
		
		var setText = function(tabBlock, textBlock){
			textBlock.animate({marginLeft: 0}, {
				duration: 200,
				complete: function() {
					_this.activeTab.css({
						'display': 'none',
						'zIndex': ''
					});
					_this.activeTab = tabBlock;
					_this.animating = 0;
				}
			});
		};
		
		this.activeTab.css('zIndex',1);
		
		
		var tabPicture = activeTab.find('div.tab-pic').css('opacity', 0);
		var tabText = activeTab.find('div.tab-text').css('marginLeft', '-60%');
		
		activeTab.css({
			'display': 'block',
			'zIndex': 2
		});
		
		tabPicture.fadeTo(200, 1, function(){
			setText(activeTab, tabText);
		});
		
		this.activeTabTrigger.removeClass('active');
		this.activeTabTrigger = $(_link).addClass('active');
	}
};

zoral.MainMenu = {
	init: function(){
		this.menuUL = $('#portal-globalnav');
		var menuItems = this.menuUL.children();
		var ie = $.browser.msie && /6.0/.test(navigator.userAgent);
		var timer;
		var overlay = 0;
		var submenu;
		
		menuItems.hover(function(){
			submenu = $(this).find('div.sub-menu');
                        submenu.width($('#portal-globalnav').width()+(submenu.width()-submenu.outerWidth(true))+2);
			if(!overlay && submenu.length) {
				zoral.MainMenu.showOverlay();
				overlay = 1;
			} else if(!submenu.length) {
				zoral.MainMenu.hideOverlay();
				overlay = 0;
			};
			window.clearTimeout(timer);
			if(ie){
				$(this).addClass('onhover');
			};
		}, function(){
			timer = window.setTimeout(function(){
				if(overlay) {
					zoral.MainMenu.hideOverlay();
					overlay = 0;
				};
			}, 100);
			if(ie){
				$(this).removeClass('onhover');
			};
		});
		
		this.createOverlay();
	},
	viewport: function() {
		// the horror case
		if ($.browser.msie) {
			
			// if there are no scrollbars then use window.height
			var d = $(document).height(), w = $(window).height();
			
			return [
				window.innerWidth || 							// ie7+
				document.documentElement.clientWidth || 	// ie6  
				document.body.clientWidth, 					// ie6 quirks mode
				d - w < 20 ? w : d
			];
		} 
		
		// other well behaving browsers
		return [$(window).width(), $(document).height()];
		
	},
	createOverlay: function(){
		var _css = {
		};
		this.overlay = $('<div></div>').attr({
			id: 'menuOverlay'
		}).css(_css).appendTo(document.body);
	},
	showOverlay: function(){
		var topOffset = parseInt(this.overlay.css('top'));
		this.overlay.css({
			display: 'block',
			opacity: 0,
			height: this.viewport()[1] - topOffset
		});
		this.overlay.stop().fadeTo(200, 0.5, function(){});
	},
	hideOverlay: function(){
		this.overlay.stop().fadeOut(200);
	}
	
};

