
var menuStatus = Array();

/* used for the cookie thing. Remove val if
 * it's in the array and add it if not */
function csv2array (str) {
	if ( ! str) return new Array();
			var arr = new Array();
			var tmp = "";
			for (var i=0; i < str.length; i++) {
				if (str[i] == ",") {
					arr.push(tmp);
					tmp = "";
				} else 
					tmp += str[i];
			}
			arr.push(tmp);
			return arr;
}

function toggleInArray(arr, val) {
	if (val == "") return arr; // don't try to fool me!
	for (var i=0; i < arr.length; i++) {
		if (arr[i] == val) {
			arr.splice(i, 1);
			return arr;
			}
	}
	arr.push(val);
	return arr;
}

function toggleStatus(val) {
	$.cookie('tomk32_menu_status', toggleInArray(menuStatus, val));
}


/* helper to fix those bloddy IE 6 transparency	*/
jQuery.fn.fixPngBackgrounds = function() {
	if($.browser.msie)
		{
		this.each(function () {
		var currentBkg = this.currentStyle.backgroundImage;
		if(currentBkg && currentBkg.length > 0)
		{
			this.runtimeStyle.backgroundImage = "none"; // Remove the existing background
			var urlOnly = currentBkg.substring(5,
(currentBkg.length-2)); // Strip 'url(" and '")'
			this.runtimeStyle.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + urlOnly +
"', sizingMethod='scale')"
	$(this).attr("src", urlOnly);
		}
		});
		}
}

function debug(msg) { 
	if ($('#debug').length == 0) 
		$('body').prepend('<div id="debug"></div>');
	$('#debug').append("<br />" + msg);
}

function contentresize() {
	if ($('#column-content.multiple').length > 0) {
		if ($('.multiple #content').width() > 800) {
		if ( $('#content-left').length == 0)
			$('#content').prepend('<div id="content-left"></div>');
		if ( $('#content-right').length == 0)
			$('#content').prepend('<div id="content-right"></div>');
		if ( $('#content-right').children().length == 0) {
			$('#content').children('.post:even').appendTo($('#content-left'));
			$('#content').children('.post').appendTo($('#content-right'));
		}
	} else {
		if ($('.multiple #content').width() < 850) {
			$('#content-left').children().each(function() {
				$(this).appendTo('#content');
				$('#content-right').children(':first').appendTo('#content');
			});
			
		}
		}
	}
}

function toggleSidebar() {
	if ($('#sidebar.closed').length == 0) {
		var height = $('#sidebar').height();
		if (height > $('#column-content').height())
			height = $('#column-content').height();
		$.cookie('tomk32_menu_open', 0);
	} else {
		$.cookie('tomk32_menu_open', 1);
	}
	$('#sidebar').toggleClass('closed');
	$('#sidebar .handle a').html('&raquo;&raquo;&raquo;');
	$('#sidebar.closed .handle a').html('&laquo;');
	$('#sidebar').css('height', '100%');
	$('#sidebar.closed').css('height', height);
	$('#column-content').toggleClass('wide');
	
	contentresize();
}
/* here starts the important JQuery stuff */

	$(window).resize(function() {
		contentresize();
	});

$(document).ready(
	function() {

	// keep some posts extended
	if (typeof postsShowFully == "undefined")
		postsShowFully = 2;
	$(".post:lt(" + postsShowFully + ")").addClass('show');
	if ($('#column-content.multiple').length > 0) {
		if ($('.multiple #content').width() > 700)
		contentresize();
	}
	
	


	// read cookie and set the opened ones to class="show"
	var menuStatusString = $.cookie('tomk32_menu_status');

	if (menuStatusString != null ) {
		menuStatus = csv2array(menuStatusString);
		for (var i=0; i < menuStatus.length; i++) {
			$('#' + menuStatus[i]).addClass('show');
			}
	}

	$('.portlet').not('.show').children('.title').each(function(e) {
		var a = $('<a href="javascript:;" title="click to show/hide the box">' + $(this).text() + '</a>');

		$(this).text("");
		$(this).append(a);
	});


	$('.portlet .title a').click(function() {
		$(this).parents('.portlet:first').toggleClass('show');
		// we use the .portlet's id to store it in the cookie
		toggleStatus($(this).parents('.portlet:first').attr('id'));
		if ($(this).parents('.portlet:first').is('.show')) {
			$(this).parent().siblings().show(0);
		} else {
			$(this).parent().siblings().hide(0);
		}
	});


	$('.portlet').not('.show').children().not('.title').hide(0);



	$('.post').not('.show').children('h2.title a').one('click', function() {
		$(this).siblings().show(0);
		return false;
	});
	$('.post').not('.show').children().not('h2.title').hide(0);


	//	we put some additional divs into each postwrap
	$(".postwrap").add('#footer').prepend('<div class="corner-top-left"><div class="corner-top-right"></div></div>');
	
	$('#sidebar .corner-top-left').prepend($('<div class="handle"><a href="javascript:;">&raquo;&raquo;&raquo;</a></div>'));

	$('#sidebar .handle a').click(function() { toggleSidebar(); });
	if ($.cookie('tomk32_menu_open') == 0)
		toggleSidebar();
		
	/*	$('#sidebar.closed').hover(
		function() { $('#sidebar').toggleClass("over"); },
		function() { $('#sidebar').toggleClass("over"); }
		);
	*/
	// fix IE transparency
	$(".png").fixPngBackgrounds();
});

