/**
 * Created by JetBrains PhpStorm.
 * User: helgehvding
 * Date: 09.09.11
 * Time: 11.15
 */

var dropdownMenu = function(obj)
{
	this.initialize(obj);
}

dropdownMenu.prototype = {

	initialize: function(obj)
	{
			var self = this;
			this.obj = obj;
			this.previousContent = 'none';
			this.currentPage = 'none';
			this.setObservers();
	},

	setObservers: function()
	{
		var self = this;

		$(this.obj.submenuContent).each(function(i,elm){
			var id = $(elm).attr('class').replace(/dropdownItem dropdownid_/g,'');

			if($(self.obj.menuBtn+id).hasClass('active')){
				self.currentPage = id;
			}

			$(self.obj.menuBtn+id).click(function()
			{
				if(i == 0)
                {
                    $(this).addClass('active first-active');
                }
				else
                {
                    $(this).addClass('active');
                }
				self.fadeOutBooking(id);
                self.showAndHide(id);
				self.previousContent = id;
                $(self.obj.closeBtn).fadeIn('slow');
				return false;
			});
		});

		if($(this.obj.closeBtn).size() > 0)
        {
			$(this.obj.closeBtn).click(function(){
				self.closeDropdown();
			});
		}
//        Skal ikke lenger vise nivå3:
//        $(this.obj.lvl2Btn).each(function(i,elm){
//			$(elm).click(function(event){
//                event.preventDefault();
//                $(self.obj.lvl2Content).eq(i).toggleClass('active');
//                $(self.obj.lvl2Btn).toggleClass('active');
//
//                var containerHeight = parseInt($(self.obj.submenuContainer).css('height'));
//                var sublevelHeight = parseInt($(self.obj.lvl2Content).outerHeight(true));
//                if($(self.obj.lvl2Content).hasClass('active'))
//                {
//                    var height = containerHeight + sublevelHeight;
//                }
//                else
//                {
//                    var height = containerHeight - sublevelHeight;
//                }
//
//                $(self.obj.submenuContainer).animate(
//                {
//                    height: height + 'px'
//                },
//                self.obj.speed);
//            });
//		});

	},

	showAndHide: function(id)
	{
		if(this.previousContent != 'none'){
			$(this.obj.submenuContentC+this.previousContent).hide();
			$(this.obj.menuBtn+this.previousContent).removeClass('active first-active');
		}

		$(this.obj.submenuContentC+id).show();
	},

	fadeOutBooking: function(id)
	{
		var self = this;
		if($(this.obj.booking).size() > 0)
		{
			if(self.currentPage != 'none'){
				$(self.obj.menuBtn+self.currentPage).removeClass('active first-active');
			}
			$(this.obj.booking).animate(
			{
				opacity: 0
			},
			500,
			'swing',
			function(){
				$(this).css('display','none');
				self.openDropdown(id);
			});
		}
		else
		{
			self.openDropdown(id);
		}

	},

	fadeInBooking: function(){

		if($(this.obj.booking).size() > 0){
			var self = this;

			$(this.obj.booking)
			.css('display','block')
			.animate(
			{
				opacity: 1.0
			},
			500);
		}
	},

	openDropdown: function(id)
	{
		var self = this;

		$(this.obj.submenuContainer).show();
		var height = $(self.obj.submenuContentC+id).outerHeight(true);

		$(self.obj.submenuContainer).animate(
		{
			height: height + 'px'
		},
		self.obj.speed);
	},

	closeDropdown: function()
	{
		var self = this;

        $(self.obj.closeBtn).hide();
		$(self.obj.submenuContainer).animate(
		{
			height: '0px'
		},
		self.obj.speed,
		'swing',
		function()
		{
			$(self.obj.submenuContainer).hide();
			self.fadeInBooking();
			$(self.obj.menuBtn+self.previousContent).removeClass('active first-active');
			if(self.currentPage != 'none'){
				$(self.obj.menuBtn+self.currentPage).addClass('active');
			}
		});
	}
};

$(document).ready(function()
{
	new dropdownMenu(
	{
		menuBtn: '.menuid_',
		closeBtn: '#close',
		submenuContainer: '#dropdownMenu',
		submenuContent: '.dropdownItem',
		submenuContentC: '.dropdownid_',
		booking: '#booking',
		speed: 1000,
        lvl2Btn: 'a.hasChildren',
        lvl2Content: 'ul.children'
	});
});

