/****************
	oMenu.js
	This functions controls the Menu Structure for the Hobbyversum Community
	It is nessesery to include jQuery!
	
	author: Dennis Mandel
	Version: 1.0
	create date: 26.09.2010
	changelog:
		-1.0	|	initial relaease

****************/
function debug(open, close){
	
	var form = "<div class='debug-container' style='width: 800px; position:absolute;top:0px;'><input type='text' class='open' value='" + open + "'><input type='text' class='close' value='" + close + "'><input type='button' class='temp_buttondebug' value='save temporary config'></div>";
	jQuery("#motto-menu").after(form);
	jQuery(".temp_buttondebug").click(function(){
		openTime = jQuery(".open").val();
		closeTime = jQuery(".close").val();
	});
	
}

jQuery(document).ready(function() {
	/* prepare all motto-normals, an give'em an id */
	var moped=1;
	jQuery(".motto-normal").each(function(){
		jQuery(this).attr("id", "mono-" + moped++);
	});

	openTime = 200;
	closeTime = 250;
	
	// debug(openTime, closeTime);
	
	/* if has no childs, just make the background colorful and beauty ;) */
	oldCol="";
	jQuery(".first-motto-nochilds").hover(
	function(){
		jQuery(this).parent().css("background-color", jQuery(this).css("color"));
		oldCol = jQuery(this).css("color");
		jQuery(this).css("color", "#FFFFFF");
	}, 
	function(){
		jQuery(this).parent().css("background-color", "transparent");
		jQuery(this).css("color", oldCol);
	});
	
	
	
	
	/* calculate the y-position of an element to fix the background bug */
	var tmpPos = 0;
	jQuery.each(jQuery("#motto-menu ul li.parent"), function(k,v){
		if(tmpPos != 0){
			if(v.offsetTop != tmpPos){
				jQuery(v).children(".first-motto").css("background-image", "none");
				jQuery(v).children(".first-motto").addClass("no-bg");
				tmpPos = v.offsetTop;
			}else{
				tmpPos = v.offsetTop;
			}
		}else{
				tmpPos = v.offsetTop;
		}
			
		
	});
	
	/* appends this little flag to the menu elements */
	jQuery("li.parent").append("<li class='first-flag'></li>")
	
	var accId = 0;
	var pos = -1000;
	
	
	jQuery(".motto-normal").mouseover(function(){
		window.clearInterval(activeEvent);
		//console.log(".motto-normal: mouseover");
	});
	
	
	
	
	jQuery(".motto-normal").mouseout(function(){
		window.clearInterval(farOut);
		 //console.log(".motto-normal: mouseout");
		 activeEvent = window.setTimeout(function(){
			jQuery(".first-motto").css("background-color","transparent");
			jQuery(jQuery(".motto-normal")).hide();
			jQuery(jQuery(".first-flag")).hide();
			jQuery("#"+accId).parent().children(".first-motto").css("background-image", "url(/wp-content/themes/hobbyversum-community/img/bg_menu.png)");
			jQuery("#"+accId).parent().children(".first-motto").removeClass("overlay");
		 }, closeTime);
		 
	});
	
	
	
	
	jQuery(".first-motto").mouseover(function(e){
		if(typeof(farOut) !== 'undefined')
			window.clearInterval(farOut);
		/* clear setTimeout interval */
		if(typeof(activeEvent) !== 'undefined'){
			//console.debug(activeEvent);
			window.clearInterval(activeEvent);
		}

		oldColor = jQuery(this).css("color");
					
		var self = this;
		
		farOut = window.setTimeout(function(){
							
						if(self.offsetTop < 145 && self.offsetTop > 10)
							pos = 142;
						else if(self.offsetTop > 144 && self.offsetTop < 199)
							pos = 186;
						
						/* when switching from one to the other menu point you have to change some css stuff*/
						if(accId != 0 && jQuery(self).parent().children(".motto-normal").attr("id") != accId){
							jQuery("#"+accId).parent().children(".first-motto").css("background-color", "transparent");
							jQuery("#"+accId).parent().children(".first-motto").removeClass("overlay");
							jQuery("#"+accId).parent().children(".first-motto").css("background-image", "url(/wp-content/themes/hobbyversum-community/img/bg_menu.png)");
							jQuery("#"+accId).hide();
							jQuery("#"+accId).parent().children(".first-flag").hide();
						}
						
						
						/* show the menu */
						jQuery(self).parent().children(".motto-normal").css("background-color", jQuery(self).css("color"));
						jQuery(self).parent().children(".motto-normal").show();
						
						if( jQuery(self).parent().children(".first-motto").css("width") == "auto")
							jQuery(self).parent().children(".first-flag").css("width", jQuery(self).parent().children(".first-motto").outerWidth() - 16);
						else
							jQuery(self).parent().children(".first-flag").css("width", jQuery(self).parent().children(".first-motto").css("width"));
						
						jQuery(self).parent().children(".first-flag").css("background-color", jQuery(self).css("color"));
						// jQuery(self).parent().children(".first-flag").css("top", pos + "px");
						jQuery(self).parent().children(".first-flag").show();
						jQuery(self).css("background-image", "none");
						jQuery(self).addClass("overlay");


						/* set some css stuff */
						jQuery(self).css("background-color", jQuery(self).css("color"));
						accId = jQuery(self).parent().children(".motto-normal").attr("id");
		}, openTime);
		
	});
	
	
	
	
	jQuery(".first-motto").mouseout(function(){
		window.clearInterval(farOut);
		 activeEvent = window.setTimeout(function(){ 
			jQuery(jQuery(".first-motto")).parent().children(".motto-normal").hide();
			jQuery(jQuery(".first-motto")).parent().children(".first-flag").hide();
			jQuery(jQuery(".first-motto")).css("background-color", "transparent");
			jQuery(jQuery(".first-motto")).removeClass("overlay");
			jQuery("#"+accId).parent().children(".first-motto").css("background-image", "url(/wp-content/themes/hobbyversum-community/img/bg_menu.png)");
		} , closeTime);
			
	});
	
});
