jQuery(document).ready(function() {
	n3_applyIEMenuFix();
	n3_widget_init();
});

function n3_create_cookie(name, value, days) {
	var expires = '';
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	}
		
	document.cookie = name + "=" + value + expires + "; path=/";
}

function n3_read_cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
			
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	
	return null;
}

function n3_erase_cookie(name) {
	n3_create_cookie(name, "", -1);
}

function n3_applyIEMenuFix() {
	if (!jQuery.browser.msie)
		return;

	navRoot = jQuery('#navigation > ul')[0];
	for (var i = 0; i < navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName == "LI") {
			node.onmouseover = function() {
				jQuery(this).addClass("over");
			}
			node.onmouseout = function() {
				jQuery(this).removeClass("over");
			}
		}
	}
}

function n3_widget_isVisible(id) {
	return jQuery('.sidebar-item-left[n3_widget_id=' + id + ']').attr('n3_widget_visible') == 'yes';
}

function n3_widget_setVisible(id, visible, smooth) {
	var widget = jQuery('.sidebar-item-left[n3_widget_id=' + id + ']');
	
	if (visible) {
		widget.find('.n3_widget_toggle').css('background-position', '0px 0px');
		widget.attr('n3_widget_visible', 'yes');
		widget.children('h2').fadeTo('fast', 1.0);
		
		if (smooth)
			widget.children('*:not(h2)').slideDown('fast');
		else
			widget.children('*:not(h2)').show();
	} else {
		widget.find('.n3_widget_toggle').css('background-position', '20px 0px');
		widget.attr('n3_widget_visible', 'no');
		widget.children('h2').fadeTo('fast', 0.65);
		
		if (smooth)
			widget.children('*:not(h2)').slideUp('fast');
		else
			widget.children('*:not(h2)').hide();
	}

	n3_widget_updateCookie();
}

function n3_widget_toggle(id) {
	n3_widget_setVisible(id, !n3_widget_isVisible(id), true);
}

function n3_widget_updateCookie() {
	var tmp = [];
	
	for (var i = 0; i < jQuery('.sidebar-item-left').length; i++)
		tmp.push(n3_widget_isVisible(i) ? 'y' : 'n');
		
	n3_create_cookie('widgets', tmp.join(','), 365);
}

function n3_widget_init() {
	var widgetCount = jQuery('.sidebar-item-left').length;
	
	if (!n3_read_cookie('widgets')) {
		var tmp = [];
		for (var i = 0; i < widgetCount; i++)
			tmp.push('y');
			
		n3_create_cookie('widgets', tmp.join(','), 365);
	}

	var settings = n3_read_cookie('widgets').split(',');

	jQuery('.sidebar-item-left').map(function(index, element) {
		var widget = jQuery(element);
		widget.attr('n3_widget_id', index);
		widget.children('h2').css('cursor', 'pointer');
		widget.children('h2').click(function(event) { n3_widget_toggle(this.parentNode.attributes.n3_widget_id.value); });
//		widget.children('h2').prepend('<div class="n3_widget_toggle" title="expand/collapse widget" onClick="n3_widget_toggle(' + index + ');"></div>');
	
		n3_widget_setVisible(index, settings[index] == 'y', false);

		widget.children('h2').hover(function() {
			if (this.parentNode.attributes.n3_widget_visible.value == 'no')
				jQuery(this).fadeTo('fast', 1.0);
		}, function() {
			if (this.parentNode.attributes.n3_widget_visible.value == 'no')
				jQuery(this).fadeTo('fast', 0.65);
		});
	});
}
