(function ($) { $(function () {
$('ul.nav').css({left: (window.document.body.clientWidth) / 2 - 480});
loadXML(navigation);


function navigation () {
	var root   = $('ul.nav li');
	var parent = $('ul.parent li.parent');
	var parent_show = null;
	var child  = $('ul.child li.child');
	var child_show = null;
	var grand  = $('ul.grand li.grand');
	var grand_show = null;
	
	root.mouseover (function () {
		if ($(this).find('ul.parent').length)
			parent_show = $(this).find('ul.parent').show()
	});
	
	parent.mouseover (function () {
		if ($(this).find('ul.child').length)
			child_show = $(this).find('ul.child').show()
	});
	
	child.mouseover (function () {
		if (grand_show)
			grand_show.hide();
		if ($(this).parent().find('ul.grand').length) {
			$(this).parent().parent().find('a:first').addClass('on');
			grand_show = $(this).find('ul.grand').show()
		}
	});
	
	grand.mouseover (function () {
		$(this).parent().parent().find('a:first').addClass('on');
	});
	
	root.find('a').mouseout (function () {
		if (parent_show)
			parent_show.hide();
	});
	
	parent.find('a').mouseout(function () {
		if (child_show)
			child_show.hide();
	});
	
	child.find('a').mouseout(function () {
		if (grand_show)
			grand_show.hide();
		$(this).parent().parent().parent().parent().find('a').removeClass('on');
	});
	
	grand.find('a').mouseout (function () {
		$(this).parent().parent().parent().parent().parent().find('a:first').removeClass('on');
	});
}




function loadXML (fn) {
	$.ajax({
		url     : '/navigation/navigation.xml',
		dataType: 'xml',
		cache   : false,
		error   : function (r) {
			alert('error');
		},
		success : function (r) {
			var navs = $(r).find('nav');
			navs.each(function (i, nav) {
				var ul = [];
				var nav = $(nav);
				ul.push([
					'<ul class="parent">'
				].join(''));
				
				var parents = nav.find('parent');
				parents.each(function(i, parent){
					parent = $(parent);
					ul.push([
						'<li class="parent">',
						'<a href="' + parent.attr('link') + '">',
						parent.attr('title'),
						'</a>'
					].join(''));
					
					if (parent.find('child').length){
						var childs = parent.find('child');
						ul.push(['<ul class="child">'].join(''));
						
						childs.each(function (j, child){
							child = $(child);
							ul.push([
								'<li class="child' + ((j == 0) ? ' first' : '') + '">',
								'<a href="' + child.attr('link') + '">',
								child.attr('title'),
								'</a>'
							].join(''));
							
							if (child.find('grand').length) {
								var grands = child.find('grand');
								ul.push(['<ul class="grand">'].join(''));
								
								grands.each(function (k, grand) {
									grand = $(grand);
									
									ul.push([
										'<li class="grand' + ((k == 0) ? ' first' : '') + '">',
										'<a href="' + grand.attr('link') + '">',
										grand.attr('title'),
										'</a>'
									].join(''));
								});
								
								ul.push(['</ul>'].join(''));
							}
							
							ul.push([
								'</li>'
							].join(''));
						});
						ul.push(['</ul>'].join(''));
					}
					
					ul.push([
						'</li>'
					].join(''));
				});
				
				ul.push([
					'</ul>'
				].join(''));
				
				$('li.nav_' + (parseInt(i)+2)).append(ul.join(''));
				
				if (fn)
					fn();
			});
		}
	});
}

})})(jQuery);
