
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')"
    }
    });
    }
} 


$(document).ready(
	function() {
	// fix IE transparency
	$(".png").fixPngBackgrounds();

	if (textShowFull == "") {
		textShowFull = "Show full entry"; }
	if (textHideFull == "") {
		textHideFull = "Hide full entry"; }
	if (textPreview == "") {
		textPreview = "Preview"; }


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

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

	// 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] + ' .title a').addClass('show');
	}


	$('.multiple .entrytext').each(function(i) {
		if ($(this).children().length > 2) {
		// show only the first two paragraphs of posts on index or archive pages
		$(this).append('<div class="more"><a href="javascript:;">' + textShowFull + '</a></div>');
		// keep first two shown
		var next = $(this).children(":first").not('img');
		next.addClass('show');
		for (var i=0; i < 2; i++) {
			next = next.next().not('img');
			next.addClass('show');
		}

		$(this).children('.more').addClass('show');
		$(this).children().not(".show").hide(0);
		} // fi
	}); // each


	$(".multiple .entrytext .more a").click(function() {
		if ($(this).parent().is('.show')) {
			$(this).parent().siblings().show(0);
			$(this).text(textHideFull);
		} else {
			$(this).parent().siblings().not('.show').hide(0);
			$(this).text(textShowFull);
		}
		$(this).parent().toggleClass('show');
	});


	$('.olderentries li').each(function(i) {
		$(this).append('<a class="more" href="javascript:;">' + textPreview + '</a> ');
		// keep first two shown
		$(this).children(".preview").hide(0);
	}); // each
	$(".olderentries .more").click(function() {
		$(".olderentries .more").show(0);
		$(this).hide();
		$(".olderentries .preview").hide(0);
		$(this).siblings(".preview").show(0);
		
	});
	// and some layout stuff

	// sidebar/menu
	$(".portlet .title").next().addClass('corner-top-right');
	$(".portlet").append('<div class="corner-bottom-right"></div>');
	$('.portlet .title a').not('.show').parent().siblings().hide(0);


	// corners at the left
	$(".post").add(".page").add('.olderentries').add('.navigation').add("#footer").addClass('corners');

	$(".corners > h2").next().addClass('corner-top-left');
	$("#footer").children(":first").addClass('corner-top-left');
	

	$(".corners").append('<div class="corner-bottom-left"></div>');
	$(".corner-bottom-left").prev().css('margin-bottom', '0');
	$(".corner-bottom-left").prev().css('padding-bottom', '0');
	$(".corner-bottom-right").prev().css('margin-bottom', '0');
	$(".corner-bottom-right").prev().css('padding-bottom', '0');
$(".corner-top-left").each(function(i) { 
	$(this).children(":first").css('margin-top', '0px');
	$(this).children(":first").css('padding-top', '0px');
});
$(".corner-top-right").each(function(i) { 
	$(this).children(":first").css('margin-top', '0px');
	$(this).children(":first").css('padding-top', '0px');
});

	$(".portlet >.title").siblings().css('margin-left', '25px');
	$(".portlet >.title").siblings().css('padding-left', '0');
	$(".corners > h2").siblings().css('margin-right', '25px');
	$(".corners > h2").siblings().css('padding-right', '0');
});
