/**
* Navigation - Javascript Version Initialisation 
*
* @author 		Zone
* @email		info@zonecontent.com
* @url 		    http://www.zonecontent.com/
* @copyright 	Copyright (c) 2009, zonecontent.com. All rights reserved.
* @version		0.0.1
*/
jQuery(document).ready(function($) {
	$('#footer-navigation').remove();
});

$('#navigation > ol').addClass('java');
$('#navigation > ol.java > li.active').prev('li').addClass('no-border');

$('#navigation ol.java li div.nav div:first-child').addClass('main');
$('#navigation ol.java li div.nav div:last-child').addClass('subnav');

$('#navigation ol.java li div.nav div.subnav').each(function() {
	$(this).children('ol').find('li').has('ol').addClass('subnav');

	var length = $(this).children('ol').find('li').length;
	var nth = Math.ceil(length / 3);
	
	$(this).parent().wrap('<div class="container"></div>');
	$(this).parent().addClass('show');
	
	var list = $(this).children('ol').detach();
	while (list.children('li').length > nth) {
		var new_length = $(this).children('ol:last-child').find('li').length;
		if (new_length > nth) {
			list = splitAttachLists($(this), list, new_length);
		} else {
			list = splitAttachLists($(this), list, nth);
		}
	}
	if (list.children('li').length > 0) {
		$(this).append(list);
		$(this).children('li').wrapAll('<ol></ol>');
	}
	
	var width = $(this).siblings('div.main').outerWidth() + $(this).outerWidth();
	$(this).parent().width(width + 12);
	
	$(this).parent().removeClass('show');
	$(this).parent().unwrap();
});

function splitAttachLists(container, list, nth) {

	var pos = list.find('li').eq(nth-1);
	if (pos.parent().parent().hasClass('subnav')) {
		pos = pos.parent().parent().next().index();
	} else {
		pos = pos.next().index();
	}
	
	var items = list.children('li');
	var newList = items.slice(0,pos);
	
	list.remove('li:lt(' + pos + ')');
	
	container.append(newList);
	container.children('li').wrapAll('<ol></ol>');
	return list;
}
