var int_filialeChooser_padding_top = 40;
var int_filialeChooser_list_wrapper_padding = 30;
var int_menu_y_position = 4;

/*
 For filiale select in footer.
*/
function getListHeight () {
	// a brwoser-fix
	var int_height_fix = 0;
	if (jQuery.browser.msie && $.browser.version.substr(0,1)==7) {
		int_height_fix = 1;
	}
	var int_number = jQuery('#filialeChooser #filialeChooser_list li').size();
	var int_height = jQuery('#filialeChooser #filialeChooser_list a:first').outerHeight(true)+int_height_fix;
	return int_number*int_height;
}

var bool_menuOpen = false;

function isMenu (element) {
	// funktioniert auch, wenn man das Menu immer schließt, egal wo geklickt wurde
	return false;
}

var checkMouse = function(e) {
	if (!isMenu(e.target)) {
		closeMenu();
	}
}

var openMenu = function() {
	var filialeChooser_list = jQuery('#filialeChooser #filialeChooser_list_wrapper');
	if (filialeChooser_list.height() == 0) {
		// open
		filialeChooser_list.animate({ 
			height: getListHeight()+int_filialeChooser_list_wrapper_padding+"px",
			top: (-1*getListHeight()+int_filialeChooser_padding_top-int_filialeChooser_list_wrapper_padding-int_menu_y_position)+"px"
		}, 1000 );
		
		// click every where to close
		jQuery(document).bind('mousedown', checkMouse); 
		
		bool_menuOpen = true;
	}	
}

var closeMenu = function() {
	bool_menuOpen = false;
	jQuery(document).unbind('mousedown', checkMouse); 
	
	var filialeChooser_list = jQuery('#filialeChooser #filialeChooser_list_wrapper');	
	if (filialeChooser_list.css('opacity') == 1) {
		// close
		$(filialeChooser_list)
		// Timeout in animate geht (noch) nicht, aus diesem Grund musste setTimeout außenrum
      setTimeout( function()
      {
          filialeChooser_list.animate({ 
          height: "0px",
          top: (int_filialeChooser_padding_top-int_menu_y_position)+"px",
          opacity: 0.0
        }, 1000 ).animate({ 
          opacity: 1.0
        }, 1);
      }, 1500);
	}
}
/***************************/
/* here the actions starts */
/***************************/
jQuery(document).ready(function(){
// the new click way
	jQuery('#filialeChooser').click(function() {
		if (bool_menuOpen) {
			closeMenu();
		} else {
			openMenu();
		}
	});
	
// the old hover way - ugly in IE7 :(
//	jQuery('#filialeChooser').hover(
//		function() {
//			// show
//			//jQuery('#langChooser #langChooser_list').css('display', 'block');
//			var filialeChooser_list = jQuery('#filialeChooser #filialeChooser_list_wrapper');
//			if (filialeChooser_list.height() == 0) {
//				filialeChooser_list.animate({ 
//					height: getListHeight()+int_filialeChooser_list_wrapper_padding+"px",
//					top: (-1*getListHeight()+int_filialeChooser_padding_top-int_filialeChooser_list_wrapper_padding-int_menu_y_position)+"px"
//				}, 1000 );
//			}
//		},
//		function() {
//			// hide
//			//jQuery('#langChooser #langChooser_list').css('display', 'none');
//			var filialeChooser_list = jQuery('#filialeChooser #filialeChooser_list_wrapper');
//			if (filialeChooser_list.css('opacity') == 1) {
//				filialeChooser_list.animate({ 
//					height: "0px",
//					top: (int_filialeChooser_padding_top-int_menu_y_position)+"px",
//					opacity: 0.0
//				}, 1000 ).animate({ 
//					opacity: 1.0
//				}, 1);
//			}
//		}
//	);

});

