/* Created by In2sports nov 2010 / Joachim Jansen */

function KHS_Menu(){

	this.MenuItems = [];

	this.zLayerIndex = 500;
	this.openIndex = null;

}

KHS_Menu.prototype.Init = function(){
	var self = this;

	$('ul#mainmenu').children().each(function(){
		self.AddMenuButton( this) ;
	});

}

KHS_Menu.prototype.AddMenuButton = function( element ){
  var self = this;
	var index = this.MenuItems.length;

  if ( element.build == true ){
    return;
  }
  element.build = true;

	var ul = $(element).children('ul').get(0);

	var menu = {
		'element' : element,
		'ul' : ul,
		'list' : document.createElement( 'div' ),
		'index' : index,
		'mouse_on_button' : false,
		'mouse_on_submenu' : false
	};
	this.MenuItems.push( menu );

	var count_li = $(element).children('ul').children('li').length;
	var count_liParent = $(element).children('ul').children('li.parent').length;
	$(element).children('ul').children('li.parent').css({
		'width': '175px',
		'float': 'left'
	});

	var width_needed = 16 + 16 + Math.max( 175 + 12, count_liParent * ( 175 + 12 ) );

	var list = [ 'left', 'right' ];
	menu.list.className = 'menu_container';
	$(ul).addClass( 'menu_list' );
	$(menu.list).append( ul );
	$(menu.list).css( 'width', ( width_needed ) + 'px' );
  $(menu.list).append( ul );
	for( var i=0; i<list.length; i++ ) {
		var div = document.createElement( 'div' );
		div.className = 'menu_' + list[ i ];
		$(menu.list).append( div );
	}
	$(element).append( menu.list );
	$(ul).show();

	$(menu.list).children('ul').children('li.parent:last').next().css({
		'clear' : 'both'
	});


	var ul_height = 0;
	var ul_sublist_max = 0;
	
	$(ul).children().each(function(){
		if ( $(this).hasClass( 'parent' ) ){
			this.calc_offset = $(this).find('li').length * 15 + 13;
			ul_sublist_max = Math.max( ul_sublist_max, this.calc_offset );
		} else {	
			this.calc_offset = 15;
			ul_height += this.calc_offset;
		}

		ul_height += 10;
	});

	if ( count_liParent > 3 ){
		var lists = [];
		for( var i=2; i >= 1; i -- ){
			var li_list = document.createElement( 'li' );
			li_list.calc_offset = 0;
			li_list.className = 'list_' + i;
			$(ul).prepend( li_list );
			lists[ i ] = li_list;
			var ul_list = document.createElement( 'ul' );
			$(li_list).append( ul_list );
		}

		$(ul).children('li.parent').each(function( idx ){
			if ( idx < Math.floor( count_liParent / 2 ) ){
				$(lists[ 1 ]).children().get(0).appendChild( this );
				lists[1].calc_offset += this.calc_offset;
			} else {
				$(lists[ 2 ]).children().get(0).appendChild( this );
				lists[2].calc_offset += this.calc_offset;
			}
		});

		$(menu.list).css( 'width', ( 16 + 16 + ( 175 + 12 ) * 2 ) + 'px' );
		ul_sublist_max = Math.max( lists[1].calc_offset, lists[2].calc_offset );
	}

	ul_height += 20;

	$(ul).height( ( ul_height + ul_sublist_max + 7 ) + 'px'  );
  ul_height += ul_sublist_max + 28 + 10 + 7;
	$(menu.list).height( ul_height + 'px' );

  $(menu.list).children( 'div.menu_left' ).height( ul_height + 'px' );
  $(menu.list).children( 'div.menu_right' ).height( ul_height + 'px' );

	$(element).children('a').mouseenter(function(){
		self.MouseOverMenuButton( index );
	});

	$(element).children('a').mouseleave(function(){
		self.MouseOutMenuButton( index );
	});

	$(element).children('div.menu_container').mouseover(function(){
		self.MouseOverMenuList( index );
	});

	$(element).children('div.menu_container').mouseout(function(){
		self.MouseOutMenuList( index );
	});
}

KHS_Menu.prototype.MouseOverMenuButton = function( index ){
	this.MenuItems[ index ].mouse_on_button = true;
	this.OpenMenu( index );
}

KHS_Menu.prototype.MouseOutMenuButton = function( index ){
	this.MenuItems[ index ].mouse_on_button = false;
	this.InitCloseMenu( index );
}

KHS_Menu.prototype.MouseOverMenuList = function( index ){
	this.MenuItems[ index ].mouse_on_submenu = true;
}

KHS_Menu.prototype.MouseOutMenuList = function( index ){
	this.MenuItems[ index ].mouse_on_submenu = false;
	this.InitCloseMenu( index );
}

KHS_Menu.prototype.OpenMenu = function( index ){
	var menu = this.MenuItems[ index ];
	$(menu.list).show();
	menu.list.style.zIndex = this.zLayerIndex++;
	if ( this.openIndex !== null && this.openIndex !== index ){
		this.InitCloseMenu( this.openIndex, 50 );
	}
	this.openIndex = menu.index;
}

KHS_Menu.prototype.InitCloseMenu = function( index, timeout ){
	timeout = timeout ? timeout : 500;
	var self = this;
	setTimeout( function(){
		var menu = self.MenuItems[ index ];
		if ( menu.mouse_on_button == false && menu.mouse_on_submenu === false ){
			self.CloseMenu( menu );
		}
	}, timeout );
}

KHS_Menu.prototype.CloseMenu = function( menu ){
	$(menu.list).hide();
	this.openIndex = null;
}

var khs_menu = null;
$(document).ready(function(){
	khs_menu = new KHS_Menu();
	khs_menu.Init();

	update_links_for_popup();
});

function update_links_for_popup(){
	$('a.Inline').click( function(){
		if ( this.href.substring( 0, 10 ) == 'javascript' ){
			var node = this.parentNode;
			while( node.parentNode ){
				if ( node.tagName.toLowerCase() == 'form' ){
					if ( ! node.onsubmit ){
						jQuery.fancybox.showActivity();
						var input = document.createElement('input');
						input.type='hidden';
						input.name='template';
						input.value='klm-health-services-popup';
						jQuery(node).append( input );
						var data = jQuery(node).serializeArray();
						jQuery.post( node.action, data, function(data){ jQuery.fancybox(data); });
						return false;
					}
				}
				node = node.parentNode;
			}
		} else {
			var href = this.href;
			href += ( href.indexOf( '?' ) == '-1' ? '?' : '&' ) + 'template=klm-health-services-popup';
			jQuery.get( href, function( data ){
				jQuery.fancybox(data);
			});	
			return false;
		}
		return true;
	} );

	$( 'input.buttonZoeken' ).click(submitInWindow);

	$('form.poll,form#searchform,form#subscribe').find('input[type=text]').keypress(function( event ){
		if (event.keyCode == '13'){
			if ( ! this.form.contact ){
				event.preventDefault();
				submitInWindow.apply( this );
				return false;
			}
		}
	}	);
}

function submitInWindow( ){
	jQuery.fancybox.showActivity();

	var form = this.form;
	if ( ! this.form && this && this.tagName && this.tagName.toLowerCase() == 'form' ){
		form = this;
	}

	var data = jQuery(form).serializeArray();

	data.push( { name: 'template', value: 'klm-health-services-popup' } );
  
	jQuery.post( form.action, data, function(data){ jQuery.fancybox(data); });

	return false;
}

function submitform( form_id, button_name ){
	var form = document.getElementById( form_id );

	if ( form == null ){
		return true;
	}

	if ( form[ button_name ] ){
		form[ button_name ].value = button_name;
	} else {
		var input = document.createElement( 'input' );
		input.type = 'hidden';
		input.name = button_name;
		input.value = button_name;
		form.appendChild( input );
	}

	form.submit();

  return false;
}

