(function($) {
	//
	// plugin definition
	//
	$.fn.slidemenu = function(options) {
		var opts = $.extend({}, $.fn.slidemenu.defaults, options);
		
		var menu = [], menuitems = [];
		// experimental initialization
		
		/*
		var menu = ["m1", "m2"];
		var menuitems = [];
		menuitems.push([{name:"tst1", link:"www.is.co.at"}, {name:"tst2", link:"www.is.co.at"}, {name:"tst3", link:"www.is.co.at"}]);
		menuitems.push([{name:"tst1.1", link:"www.is.co.at"}, {name:"tst1.2", link:"www.is.co.at"}, {name:"tst1.3", link:"www.is.co.at"}]);*/
		
		return this.each(function(){
			// init plugin
			var $this = $(this), items = [], containers=[], activeContainer, currentContainerCount = 0;
			
			$this.css({"height":$this.parent().height(), "zIndex":9999});
			
			$.post(opts.url,opts.postVariables,function(r){
				$r = $(r);
				
				//var path = $r.children("item").attr("path");
				items = xmlToArray($r,[]);
				
				var path = $r.children("item").attr("path");
				
				var temparr = path.split(","), patharr = [];
								
				for (var i=0; i<temparr.length; i++){
					patharr.push(parseInt(temparr[i]));
				}
												
				//showPath([0],false);
				showPath(patharr,false);
			});
			
			
			function xmlToArray($xml, path){
				var arr = [];
				$xml.children("item").each(function(i){
					var $me = $(this);
					var npath = path.slice(0)||[];
					
					npath.push(i);
					
					
					nitem = {title: $me.attr("title"),link: $me.attr("link"),ch: xmlToArray($me,npath),path:npath};

					arr.push(nitem);
				});
				
				return arr;
			}	
			
			
			
			function showPath(path, animate){
				animate = false;
				var ccount = path.length, itemsToShow = [], children = items; 
				
				
				for (var i=0; i<ccount; i++){
					if (children[path[i]].ch.length>0){
						itemsToShow.push(children[path[i]]);
						children = children[path[i]].ch;
					}
				}
								
				alterContainers(itemsToShow.length);
				
				for (i=0; i<itemsToShow.length; i++){
					markupContainer(itemsToShow[i],containers[i]);
					
					if (animate){
						containers[i].show().animate({width:"23"},700,function(){
							$(this).css("width","auto").find(".sm-items:last").width(1).hide();
						});
					} else {
						containers[i].show().css("width","auto").find(".sm-items:last").width(1).hide();;
					}
				}
				$(document).pngFix();

			}
			
			
			
			function markupContainer(item,$container){
				var children = item.ch, $item, $headline, $list;
				
				$headline = $("<div class='sm-headline'><a href='"+item.link+"'>"+writeVertically(item.title)+"</a></div>");
				$items = $("<div class='sm-items'><ul class='sm-list'></ul></div>");
				$list  =$items.find("ul");
								
				for(var i=0; i<children.length; i++){
					var $listitem;
					if (children[i].ch.length>0){
						var npath = item.path.slice(0);
						npath.push(i);
						$listitem = $("<a href='"+children[i].link+"'>"+children[i].title+"</a>");
						/*$listitem = $("<a class='sm-show-c' href='#'>"+children[i].title+"</a>")
							.data("path",npath)
							.click(function(e){
								showPath($(this).data("path"));
								e.preventDefault();
							});*/
					} else {
						$listitem = $("<a href='"+children[i].link+"'>"+children[i].title+"</a>");
					}
					$listitem.wrap("<li class='sm-list'></li>");
					$list.append($listitem);
				}
				
				$list.find("a").wrap("<li class='sm-list'></li>");
				
				$container.html("").append($headline).append($items);
				

				return $item;	
			}
			
			
		
			function alterContainers(count){
				var $container;
				if (currentContainerCount < count){
					for(var i=currentContainerCount; i<count; i++){
						$container = $this.append("<div class='sm-container'>").find("div:last").width(1).hide();
						containers.push($container);
						
						$container.hover(function(){
							$(this).children(".sm-items").queue("fx", []).stop().show().animate({width:130},{duration: 700});
						}, function(){
							$(this).children(".sm-items").queue("fx", []).stop().animate({width:1},{duration: 700}).queue(function(){$(this).hide();$(this).dequeue();});
						});
					}
					
				} else if (currentContainerCount>count){
					for(var i=count; i<currentContainerCount; i++){
						$this.find(".sm-container:last").animate({width:1},{duration:700},function(){$(this).remove();});
						containers.pop();
					}
				}
				currentContainerCount = count;
			}
			
			function writeVertically(str){
				if (str){
				
					var chars ={"z":[1,6], "y":[7,12], "x":[13,18], "w":[19,28], "v":[28,34], "u":[34,41], "t":[41,45], "s":[45,51], "r":[51,55], "q":[55,62], "p":[62,69], "o":[69,76], "n":[76,83], "m":[83,93], "l":[93,96], "k":[96,101], "j":[102,106], "i":[105,108], "h":[108,115], "g":[115,122], "f":[122,125], "e":[125,132], "d":[132,139], "c":[138,145], "b":[145,152], "a":[152,159], "0":[159,166], "9":[166,173], "8":[173,180], "7":[180,187], "6":[187,194], "5":[194,201], "4":[201,208], "3":[208,215], "2":[215,222], "1":[222,229], "Z":[229,237], "Y":[237,245], "X":[245,253], "W":[253,264], "V":[264,273], "U":[273,282], "T":[282,290], "S":[290,298], "R":[298,307], "Q":[307,317], "P":[317,325], "O":[325,335], "N":[335,344], "M":[344,355], "L":[355,362], "K":[362,370], "J":[370,376], "I":[376,380], "H":[380,389], "G":[389,398], "F":[398,406], "E":[406,414], "D":[414,423], "C":[423,432], "B":[432,441], "A":[441,449], " ":[449,456]}, rc = "", len=str.length;
					
					for (var i=len-1;i>=0;i--){
						var chr = chars[str.charAt(i)];
						
						if (chr)
						rc+="<div style=\"font-size:0px;left: 7px;position: relative;background-image:url('images/slidemenu/ABC.jpg');background-repeat:no-repeat; background-position:0 -"+chr[0]+"px; height:"+(chr[1]-chr[0])+"px; \"></div>";
					}
					
					
					return "<div style='width:13px; '>"+rc+"</div>";
				}
			}
			
		});
	}
	
	//
	// private functions
	//
	
	
	//
	// public functions
	//
	
	//
	// plugin defaults
	//
	$.fn.slidemenu.defaults = {
		url: "",
		postVariables:{}
	};
})(jQuery);


function log (m){
	console.log(m);
	var $log =  $("#log");
	if ($log.length==0){
		$log = $('body').prepend("<div id='log' style='z-index: 100000; padding: 5px; width: 100%;max-height: 100px; border: 1px solid black; position: fixed; left: 0px; bottom: 0px; background-color: black; font-family: Courier; color: #34ff43; font-weight: bold; opacity: 0.1; overflow: hidden;'></div>").find("#log");
		$log.hover(function(){$(this).css("opacity","0.8")},function(){$(this).css("opacity","0.1")});
	}
	
	$log.prepend("<div style=''>> "+m+"</div>");
}