(function($) {
	$.fn.jpanel = function(options) {
		var defaults = {
			value: "valami"
		};
		
		var opts = defaults;
		var wasClosed = false;
		
		//main 
			var elem = $(this);
			elem.find("li").each(function(index){
				if($(this).parent().parent().attr("id") == elem.attr("id")){
					$(this).hover(function(){
						if($(this).attr("id") != 'listspecial'){
							if(!wasClosed){
								closeBubble($('#listspecial'));
								wasClosed = true;
								$("#closeBubble").remove();
							}
							openBubble($(this));
						}
						else{
							if(wasClosed){
								openBubble($(this));
							}
						}
					},function(){
						if($(this).attr("id") != 'listspecial' || wasClosed == true){
							closeBubble($(this));
							//alert('close' + $(this).attr('id'));
						}
					});
				}
					
			});
			$('#listspecial ul li.hide').hide();
			
			var specbub = $("#listspecial div.list-bubble");
			if(specbub.hasClass("red")) selectSpecial("red");
			else if(specbub.hasClass("yellow")) selectSpecial("yellow");
			else if(specbub.hasClass("green")) selectSpecial("green");
			else selectSpecial("red");
			
			$("#changeRed").click(function(){ selectSpecial("red"); });
			$("#changeYellow").click(function(){ selectSpecial("yellow"); });
			$("#changeGreen").click(function(){ selectSpecial("green"); });
			
			openBubble($('#listspecial'));
			
			$("#closeBubble").click(function(){
				closeBubble($('#listspecial'), true);
				wasClosed = true;
				$(this).remove();
			});
		//endmain
		
		function openBubble(listItem){
			listItem.addClass("active");
			listItem.parent().find("div.list-bubble").stop(false,true);
			if(jQuery.browser.msie){
				listItem.find("div.list-bubble").show();
			}
			else{
				listItem.find("div.list-bubble").fadeIn("slow");		
			}
		}
		function closeBubble(listItem, fast){
			if(jQuery.browser.msie || fast == true){
				listItem.find("div.list-bubble").hide();
			}
			else{
				listItem.find("div.list-bubble").fadeOut("fast");
			}
			listItem.removeClass("active");
		}
		
		function selectSpecial(selclass){
			var list = $("div.listspecial-iconlists ul."+selclass);
			var count = list.find("li:not(.hide)").length;
			
			$('#listspecial-bigicon').removeAttr('class').addClass('bigicon'+selclass);
			
			if(jQuery.browser.msie){
				list.siblings('ul').hide();
			}
			else{
				list.siblings('ul').fadeOut("500");
			}
			var width = (count*50+95+21);
			$("#listspecial div.list-bubble").css("height", "180px").animate({ width: width+"px" }, 500, function(){
				if(jQuery.browser.msie){
					list.show();
				}
				else{
					list.fadeIn("500");
				}
				$(this).css("height", "");
			});
			
			$('#listspecial-iconlists-label').removeAttr('class').addClass(selclass);
			
			list.find("a").hover(function(){
				$('#listspecial-iconlists-label').text($(this).text());
				$(this).parent().addClass("active");
			},function(){
				$('#listspecial-iconlists-label').text('');
				$(this).parent().removeClass("active");
			});
		}
	};

	$(document).ready(function(){
		// Time out for switching menus.
		var isMouseOn = false;
		$("ul.navigation.main").hover(
			// Mouse enters main menu.
			function() {
				// Hides all menus, if any are open.
				$("ul.menulevel1.navigation").css('display', 'none');
				// Clear time out, if any.
				clearTimeout($(this).data('timeOutId'));
				// Find the first submenu relative to current main menu and display it.
				$(this).find("ul.menulevel1.navigation:first").css('display', 'block');
			},
			// Mouses leaves main menu.
			function() {
				var element = $(this);
				// Checks if the mouse is over the submenu.
				$("ul.menulevel1.navigation").hover(
					function() {
						isMouseOn = true;
					},
					function() {
						isMouseOn = false;
					}
				);
				// If it's not, time out to hide it.
				if (!isMouseOn) { 
					var timeOutId = setTimeout(function() {
						element.find("ul.menulevel1.navigation:first").css('display', 'none');
					}, 500);
					$(this).data('timeOutId', timeOutId);
				}
			}
		);
		
		
		$('#panel').jpanel();
		
		var maxheight = 0;
		var menu = $('#secondmenu ul.menulevel1');
		var isActive = false;
		menu.children().each(function(index){
			var elem = $(this);
			var list = elem.find("ul:first");
			var label = elem.find("span:first");
			
			var height = list.children().length * 14;
			if(height > maxheight) maxheight = height;
			
			list.hide();
			elem.click(function(e){
				//elem.parent().find("ul:animated").stop(true, true);
				list.toggle("fast");
				$(this).toggleClass("opened");
			});
			if(elem.hasClass("mainnav_ACT")){
				list.show("fast");
				$(this).addClass("opened");
				isActive = true;
			}
		});
		if(!isActive){
			menu.find("li:first").addClass("opened").find("ul:first").show("fast");
		}
		
		/*menu.mouseleave(function(){
			$(this).find("ul.menulevel2").hide();
		});*/
		//$('#secondmenu ul.menulevel2').height(maxheight);
	});
})(jQuery);
