/*******************************************************************************

	CSS on Sails Framework
	Title: Oseberg
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: November 2010

*******************************************************************************/


var Engine = {
	enhancements: {
		boxHover: function() {
			var ie = $.browser.msie;
			
			$('.box-a, .box-b').each(function(){
				var bgB = $('.box-bg-b', this);
				
				$(this).hover(
					function(){
						if(ie) {
							$(bgB).show();
						} else {
							$(bgB).stop().fadeTo(250, 1);
						}
					},
					function(){
						if(ie) {
							$(bgB).hide();
						} else {
							$(bgB).stop().fadeTo(250, 0);
						}
					}
				).click(function(e){					
					window.location = $("base").attr('href') + $('a:first', this).attr('href');
					e.preventDefault();
				});
			});
		},
		
		dropdownMenu : function() {
			var	dropdowns = $('li:has(.dropdown)');
			
			dropdowns.each(function(){
				var container = $(this),
					dropdownContainer = $('.dropdown', container),
					menuContainer = $('.menu', container),
					menu = $('> div', menuContainer),
					menuHeight = menuContainer.outerHeight(),
					slot = $('.slot', this).hide();

				menu.css('marginTop', -menuHeight + 'px');
				dropdownContainer.hide();

				container.hover(
					function(){
						dropdownContainer.fadeIn(150, function(e) {
							slot.show();
						});
						menu.stop().delay(125).animate({ marginTop: 0 }, 250);
					},
					function(e){
						menu.stop().animate({ marginTop: -menuHeight }, 250, function() {
							slot.hide();
							dropdownContainer.fadeOut(150);
						});
					}
				);
				
				if($.browser.msie && $.browser.version == '6.0') {
					dropdownContainer.height(menuHeight);
				}
			});
		},
		
				
		headerDivider: function() {
			$('.header-divider').each(function(){
				var bgWidth = $(this).width() - $('span', this).width() - 40;
					
					$('<span class="bg" />').css('width', bgWidth).appendTo(this);
			});
		}
	},
	
	fixes: {
		ie: function() {
			/* IE8 and below */
			if(!$.browser.msie || parseInt($.browser.version, 10) > 8) {
				return;
			}
		
			$('#navigation a')
				.bind('mousedown', function(){
					$(this).addClass('active');
				})
				.bind('mouseup', function(){
					$(this).removeClass('active');
				});
			
			$('.page-losninger, .page-ansatte').find('.primary .boxes:last-child').addClass('boxes-last');
			
			$('li:has(.dropdown)').hover(
				function(){ $(this).addClass('hover'); },
				function(){ $(this).removeClass('hover'); }
			);
		}
	}
};

$(document).ready(function() {
	$('body').addClass('js');
	
	Engine.enhancements.boxHover();
	Engine.enhancements.dropdownMenu();	
	
	setTimeout(function() {
		Engine.enhancements.headerDivider();
	}, 50);
	
	Engine.fixes.ie();
});
