/*	
 *	jQuery accordeon 1.1.3
 *	www.frebsite.nl
 *	Copyright (c) 2010 Fred Heusschen
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 */
 
 
(function($) {
	$.fn.accordeon = function(options) {
		var isIE 	= $.browser.ie,
			isIE6	= isIE && $.browser.version < 7,
			isIE7	= isIE && $.browser.version < 8;
		
		return this.each(function() {

			var opts  = $.extend({}, $.fn.accordeon.defaults, options),
				$menu = $(this),
				$topl = $menu.find('> div'),
				idname = $(this).attr('id');

			$menu.find('.set_active > a').css({'color': opts.active_color});

			opts.inactive_color = $topl.find('> a').css('color');

			//$menu.find('.submenu').css({'display' : 'none'});
			if (opts.menueeffect.substr(2,5)=='mouse'){
				$menu.bind('mouseleave',function(){
					$menu.find('.expand').find('.fNiv').triggerHandler('mouseover');
					
					setTimeout(function(){
						$menu.find('.fNivDiv').each(function(){
							if (!$(this).hasClass('expand')) $.fn.accordeon.hideSubarea(this, opts);
						});
						/*
						$menu.find('.expand').find('.fNiv').each(function(){
							$(this).triggerHandler('mouseover');
						});
						*/
						//$menu.find('.expand').find('.fNiv').triggerHandler('mouseover');
					},opts.timeout);
	
				});
			}
			
			$menu.find('> div').each(function(i){
				var SubnavId = $menu.attr('id')+'_Subnav'+i;
				var listit = this,
					subnav = $.fn.accordeon.getSubnav(listit);

				if (!subnav) return;
				
				$(subnav).attr('id',SubnavId);
		
				if ( $(this).hasClass('expand') || ( opts.session_storage && sessionStorage[subnav.id] == 'true' )){
					$.fn.accordeon.showSubarea(this, opts, $menu);
				}else{
					$(subnav).css('display','none');
				}
	
				$(this).addClass('fNivDiv');
				//$(this).find('.fNiv').click(
				bindevent = opts.menueeffect.substr(2);
				
				$(this).find('.fNiv').bind(bindevent,
					function(){
						//var listit = this,
						//	subnav = $.fn.accordeon.getSubnav(listit);
						if ($.fn.accordeon.aufklappend) return;	
												
						if($(subnav).css('display')=='block'){
							if(opts.menueeffect.match(/click/g)) $.fn.accordeon.hideSubarea(listit, opts);
							//$.fn.accordeon.hideSubarea(listit, opts);
						}else{
							if(opts.menueeffect.match(/click/g)){
								$.fn.accordeon.showSubarea(listit, opts, $menu);
							}else{
								$.fn.accordeon.timer = setTimeout(function(){ $.fn.accordeon.showSubarea(listit, opts, $menu);},opts.timeout);	
							}
						}
					}
				).bind('mouseout',function(){
					clearTimeout($.fn.accordeon.timer);
				});
			});
			
			//$.fn.accordeon.render($menu, opts);
				
			setTimeout(function() {
				$.fn.accordeon.render($menu, opts);
			},400);
		});
	};
	
	$.fn.accordeon.hideSubarea = function(ele, opts, callback) {
		var listit = ele,
			subnav = $.fn.accordeon.getSubnav(listit);
			
		sessionStorage[subnav.id] = null;
			
		if (!subnav){
			$(listit).find('> a').css({'color'	: opts.inactive_color});
			return;
		}
		switch (opts.effect) {
			case 'slide':
				$(subnav).slideUp(opts.speed,function(){							
					$(listit).find('> a').css({
						'color'	: opts.inactive_color
					})
					if (typeof callback == "function") callback();

				});
				break;
			
			case 'fade':
				$(subnav).fadeOut(opts.speed,callback);
				break;
				
			default:
				$(subnav).hide(opts.speed,callback);
				break;
		}
	}
	
	$.fn.accordeon.showSubarea = function(ele, opts, $menu) {

		var listit = ele,
			subnav = $.fn.accordeon.getSubnav(listit),
			subcss = { zIndex: $.fn.accordeon.zIndex++ };
			
		$menu.find('.fNivDiv').each(function(){
			//var xsubnav = $.fn.accordeon.getSubnav(this);
			$.fn.accordeon.hideSubarea(this, opts);
			//if($(xsubnav).css('display')=='block') $.fn.accordeon.hideSubarea(this, opts);
		});
		
		sessionStorage[subnav.id] = true;
		$(listit).find('> a').css({'color'	: opts.active_color});
		
		if (!subnav) return;
		if ($(subnav).is(":animated")) return;	
			
		if (!opts.menueeffect.match(/click/g)) $.fn.accordeon.aufklappend = true;
		
		switch (opts.effect) {
			case 'slide':
				$(subnav).slideDown(opts.speed,function(){$.fn.accordeon.aufklappend = false;}); 
				break; 
			 
			case 'fade':
				$(subnav).fadeIn(opts.speed,function(){$.fn.accordeon.aufklappend = false;});
				break;
				
			default:
				$(subnav).show(opts.speed,function(){$.fn.accordeon.aufklappend = false;});
				$.fn.accordeon.aufklappend = false;
				break;
		}
	}
	
	$.fn.accordeon.getSubnav = function(ele) {
		if (ele.nodeName.toLowerCase() == 'div') {
			var subnav = $('> .submenu', ele);
			return subnav.length ? subnav[0] : null;
		} else {
			return ele;
		}
	}
	
	$.fn.accordeon.render = function($menu, opts) {

		var w = 0;
		var border = opts.border;
		$menu.find('> .linie').remove();
		if (opts.border_width=='equal' || typeof opts.border_width=='number'){
			if (typeof opts.border_width=='number'){
				w = opts.border_width;
			}else{
				$menu.find('> div').each(function(index){
					if ($(this).width()>w) w = $(this).width();
				});
			}
			$menu.find('> div').before('<div class="linie" style="width:'+w+'px;border-top:'+ border +'"></div>');
			$menu.find('> div:last').after('<div class="linie" style="width:'+w+'px;border-top:'+ border +'"></div>');
		}else{
			$menu.find('> div').each(function(index){
				
				if (index==0 || $(this).width()>w){
					width = $(this).width();
				}else{
					width = w;
				}
				
				$(this).before('<div class="linie" style="width:'+width+'px;border-top:'+ border +'"></div>');
				
				prev = this;
				w = $(prev).width();
			});
			$(prev).after('<div class="linie" style="width:'+w+'px;border-top:'+ border +'"></div>');
		}
	}
	
	$.fn.accordeon.timer;
	$.fn.accordeon.aufklappend = false;
	$.fn.accordeon.aktuell_aufklappen_timer;
	$.fn.accordeon.zIndex 	= 1000;
	$.fn.accordeon.defaults 	= {
		effect			: 'none',		//	'slide', 'fade', or 'none'
		speed			: 'normal',		//	'normal', 'fast', 'slow', 100, 1000, etc
		timeout			: 250,
		nbsp			: false,
		maxWidth		: 0,
		border			: '2px solid #ffffff',
		border_width	: 'equal',	// equal oder Zahlenwert für fest angegebene Breite
		bg_on_drop      : 'images/bg_30p_black.png',
		active_color	: '#000000',
		inactive_color	: '',
		session_storage : false,
		menueeffect		: 'onmouseover'
	};
})(jQuery);
