/**
* @comment: file init.js
* contains so far random javascript code
* is loaded in every page so far
* 
*/

/**
 **/
var config = {
	menus: [
		{
			control: '#navigation1-toggler-submenu1',
			submenu: '#navigation1-submenu1'
		},
		{
			control: '#navigation1-toggler-submenu2',
			submenu: '#navigation1-submenu2'
		},
		{
			control: '#navigation1-toggler-submenu3',
			submenu: '#navigation1-submenu3'
		},
		{
			control: '#navigation1-toggler-submenu4',
			submenu: '#navigation1-submenu4'
		},
		{
			control: '#navigation1-toggler-submenu5',
			submenu: '#navigation1-submenu5'
		}
	],
	all_menu_items: '.navigation1-submenuitem',
	home_link: '#navigation1 #home-link-toggler',
	active_class: 'active',
	slide_down_delay: 100, // ms - effect
	slide_up_delay: 200, // ms - tolerance while mouse is out of controller/submenu
	wait_delay: 0, // ms - should be smaller than slide_down_delay 
	after_submenu_slide_down_ie6: function(){
	    jQuery('SELECT').css({visibility:'hidden'});
	},
	after_submenu_slide_up_ie6: function(){
	    jQuery('SELECT').css({visibility:'visible'});	    
	}
};
var noop = function(){};

var log,warn,info,error;
log = noop;
warn = noop;
info = noop;
error = noop;

var firebug_installed = (typeof console !== "undefined" &&
	typeof console.log !== "undefined" && 
	typeof _firebug !== "undefined");
	
if (firebug_installed){
	log = console.log; // console.log does not work for safari
	/*
	log = noop; // use this to disable logging
	*/
	warn = console.warn;
	info = console.info;
}
// only IE or browsers without firebug
var custom_logging_div_style_display = function(){ return 'none';}; /* must return 'block' or 'none' */
var logging_div_template = "<div id='js-logging' style='display:" + custom_logging_div_style_display() +  ";width:300px; background: white;height: 500px; overflow:auto; position:absolute; z-index:10000; left: 0px; top: 50px;'></div>";
var logging_div = null;


var init_tables = function(){
	$('#content tbody tr:odd').addClass('table-row-odd');
	$('#content tbody tr:even').addClass('table-row-even');
};

$(document).ready(function(){
    
    // custom logging for non firebug browsers
	if (!firebug_installed){
		// insert logging div
		$("BODY").append(logging_div_template);
		logging_div = $('#js-logging');
		log = function(){
		
			$.map(arguments, function(argument){
				try{
					logging_div.html( argument + '<br/>' + logging_div.html() );
				} catch (error){}
			});
		};
		warn = log;
		info = log;
		error = log;
		//log("logging div created");
	}
	
	var is_ie6 = function(){
	    return jQuery.browser.msie && jQuery.browser.version.indexOf('6.')===0;
	};

     /* TODO; cleanup */
	var toggle_active_class = function(){
		//log("toggle_active_class", $(this));
		$(this).toggleClass("active");
	};
	
	/* slide down effect for submenus of navigation1 */
	var timeout = {};
	var delay_timeout = null;
	
	var active_menu = null;
	var next_active_menu = null; 	
	var slide_down_in_progress = false;

	var get_active_menu = function(){
		return active_menu;
	};
	
	var set_active_menu = function(new_active_menu){
		////info("ACTIVE MENU SET TO" , new_active_menu);
		active_menu = new_active_menu;
	};

	var is_active = function(menu){
		return (menu &&     
        	get_active_menu() && 
        	get_active_menu().submenu === menu.submenu);
	};
	
    
    /**
    * @note: the slide_up_timer is neccessary to avoid div collapse, if 
    *        the mouse is moved from control div to submenu div
    *        cause then mouseovers events take place
    */
	var start_slide_up_timer = function(menu){
		//clear_slide_up_timer(menu);// this was just a hotfix
     	//log("start_slide_up_timer setting timeout of " + config.slide_up_delay);
     	timeout[menu.submenu] = window.setTimeout(function(){
     		slide_up(menu);
     	}, config.slide_up_delay);
     	//log("start_slide_up_timer done");
     };
     var clear_slide_up_timer = function(menu){
     	//log("clear_slide_up_timer",menu);
    		if (menu && menu.submenu && timeout[menu.submenu]){
    		 	//log("clearing timeout of ", menu.submenu);
   				window.clearTimeout(timeout[menu.submenu]);
   				timeout[menu.submenu] = null;
    		}
     	//log("clear_slide_up_timer done");
     };
     
     /**
     * slides up menu.submenu, callback is executed after that!
     */
     var slide_up = function (menu, callback) {
		if (typeof callback !== "function"){ callback = noop; }
		clear_slide_up_timer(menu);
		
		// hide all submenus
		if (menu){
			$(config.all_menu_items).each(function(){
	         	if (this.id !== menu.submenu ){ 
	         		$(this).hide();
				}
			});
			
	        // remove active classes
			$(menu.control).removeClass(config.active_class);
			/*
			jQuery.each(config.menus, function(){
				$(this.control).removeClass(config.active_class);
			});
			*/
			
		} else {
			////info("SLIDE_UP: no menu given!", menu , get_active_menu(), next_active_menu);
		}
		if (is_ie6()) {
		    config.after_submenu_slide_up_ie6();
		}
		callback();
		//set_active_menu(null); - wrong -do not do this here! todo: cleanup
		
		/*
       		$(menu.submenu).slideUp(
       			0,
       			function(){
       				//warn("SLIDE_UP slideUp Effect finished!");
	         	callback();
       				//warn("SLIDE_UP slideUp callback finished!");
	         }
		);
		*/
				
         
     };
     
     /**
     * slides down a submenu (sync's the slideDown effect, too)
     */
     var start_slide_down = function(menu){
         //log("start_slide_down", menu.id)
         /**
         * slides down the submenu
         */
         var do_slide_down = function(){
			var temp_menu = next_active_menu || menu;
	         if (!temp_menu){ 
	         	//warn("DO_SLIDE_DOWN: no next_active_menu!", temp_menu, menu, next_active_menu );
	         	return; 
	         }
	         
	         /**
	         * @note: global var used slide_down_in_progress 
	         *        to sync the timeout delay of slideDown
	         */
	         slide_down_in_progress = true;
	         $(temp_menu.control).addClass(config.active_class);
	         ////info("DO_SLIDE_DOWN slideDown Effect", temp_menu.submenu);
	         $(temp_menu.submenu).slideDown(config.slide_down_delay, function(){
		         set_active_menu(temp_menu);
	         	next_active_menu = null;
	         	slide_down_in_progress = false;
	         	if (is_ie6()) {
	         	    config.after_submenu_slide_down_ie6();
	         	}
	         	////info("DO_SLIDE_DOWN slideDown Effect DONE", temp_menu.submenu);
	         });
	         
	         //log("do_slide_down done",  temp_menu, $(temp_menu.control), get_active_menu());
         };
         
         /**
         * synchronizes, the slideDown effect
         * if this effect is in progress, the function will wait
         */
         var wait_for_do_slide_down = function(){
         	 
         	 if (slide_down_in_progress){
	         	////info("setting timeout cause slide down in progress:", get_active_menu(), next_active_menu);
	         	if (delay_timeout){
	         		window.clearTimeout(delay_timeout);
	         	}
	         	delay_timeout = window.setTimeout(wait_for_do_slide_down, config.wait_delay);
	         	return;
	         }
	         // the timeout in the next line is a testfix for the delay caused by slideUp
	         //delay_timeout = window.setTimeout(do_slide_down, config.wait_delay);
	         do_slide_down();
         };
         
         //warn("start_slide_down next_active_menu =", next_active_menu);
         
         
         if (get_active_menu()){        
         	// new submenu to be shown 
         	//log("START_SLIDE_DOWN: new submenu to be shown, ", menu, next_active_menu);
         	slide_up(get_active_menu(), wait_for_do_slide_down);// wait_for_... before

         } else {
         	// first time call 
         	//log("START_SLIDE_DOWN: DO SLIDE DOWN WITHOUT ACTIVE MENU!",menu);
         	wait_for_do_slide_down();// wait_for_... before
         }
     };
     
     /**
      * calls timer functions to start_slide_down finally
      */
     var menu_over = function(menu){
     	//log("MENU_OVER",menu);
     	next_active_menu = menu;
     	clear_slide_up_timer(menu);
     	
     	if (is_active(menu)){
     		////info("MENU_OVER is_active");
     		

     		/**
     		* set_menu_active(null) is disabled
     		* so we have got the false true assumption of active menus who are not active!
     		* @TODO: how to handle this?
     		**/
     		// hotfix!
     		//warn("HOTFIX",$(menu.submenu),$(menu.control));
     		$(menu.submenu).show();
     		$(menu.control).addClass(config.active_class);
     		
     		
     	} else {
     		start_slide_down(menu);// TEST: wait_for ...?
     	}
     };
     
     var submenu_over = function(menu){
     	//info("SUBMENU_OUT",menu);
     	clear_slide_up_timer(menu);
     };
     var menu_out = function(menu){
     	//info("MENU_OUT",menu);
     	start_slide_up_timer(menu);
     }; 
     
     var submenu_out = function(menu){
     	//info("SUBMENU_OUT",menu);
     	start_slide_up_timer(menu);
     };
     
     var test = function(){
     	//info("TEST CALLED");
     };
     var init_menu = function(){
     	//info("init_menu");
     	// startup code for menu menu
	    jQuery.each(config.menus,function(){
		    var menu = this;
		    //log("INIT MENU", menu);
		    $(this.control).hover(
		    	function(){menu_over(menu);},
		    	function(){menu_out(menu);}
		    );
		    $(this.submenu).hover(
		    	function(){submenu_over(menu);},
		    	function(){submenu_out(menu);}
			);
	    });
			
		$(config.home_link).hover(toggle_active_class, toggle_active_class);
		//info("init_menu finished!");
     };
	     
    init_menu();

    // init table styles
    init_tables();
     
 });


//MEMO: open marked images onclick in new window
$(document).ready(function(){
    $('img.onClickInNewWindow').click(function() {
		var bigImageSrc = this.src.replace(/\.jpg$/,'_big.jpg');
        window.open(bigImageSrc,'','menubar=no');
		
		
    });
	
	// javascript bugfix: button background images did not have the right position
    if (jQuery.browser.safari) {
       $('#navigation2 li').css("width", "200px");        
    }

//	$("a[href$='.pdf']").toggleClass('pdf-inline-link').toggleClass('inline-link');
});

