
$(document).ready(function(){


	$("body").append("<div id='output'/>");

	page_url = $.getURLParam('s');
	if(page_url) {
		pages = page_url.split('/');
		page_depth = pages.length;
	}
	
	// debug(pages[0]);
	
	//TEXT SIDE
	
		// alert(page_depth);
	if(pages[0] == 'blog') {
		
		spalte_2 = $("<div id='spalte_2' />");
		
		$("#content_hidden div.announcement").each(function(){
			blogpost = $("<div class='blogpost' />");
			headline = $("<h2/>");
			information = $("<div class='information' />");
			content = $("<div class='content' />");
			
			headline.append($("h4 a", $(this)).text());
			// information.append($("span.timestamp", $(this)).text());
			content.append($("td.sites-layout-tile", $(this)).html());

			blogpost.append(headline);
			blogpost.append(information);
			blogpost.append(content);
			
			spalte_2.append(blogpost);
		});
		$("#content_text").append(spalte_2);
	
	} else if ($("#content_hidden").length > 0) {
		spalte = 2;
		$("#content_hidden .sites-layout-tile").each(function(){
			div_spalte = $("<div/>");
			div_spalte.attr('id', 'spalte_'+spalte);
			
			div_spalte.append($(this).html());
			$("#content_text").append(div_spalte);
			spalte = spalte + 1;
		});
	}
	
	
		//Løft picasa billeder i tekst ud af links:
	$("#content_text a:[imageanchor='1']").each(function() {
		//Hvis det er small, saa goer vi dem lidt smallere saa man kan have 2 ved siden af hinanden..
		// if ($("img", this).attr('width') == 200) {
			// $("img", this).css('width', 195);
			// $("img", this).css('border', '1px solid red');
		// }
		$(this).replaceWith($("img", this));
	});
	
	//Marker current topniveau og tilfoej div til undernavigation:
	$("#grafisk_navigation a[href$=?s="+pages[0]+"]").addClass('current').after("<div id='subnavigation' />");
	$('#subnavigation').hide();
	// $("#grafisk_navigation a[href$=?s="+pages[0]+"]").parent().css('background-color', '#555');
	
	

	// $table2 = str_replace(", '?s=', $table2);
	// $table2 = str_replace("/site/".$sites_name."/", '?s=', $table2);
	
	
	$("#top div.more").fadeTo(0,0);
	
	
	$("#top > div").hover(
		function(){
			$(this).stop();
			$(this).animate({ marginTop: "7px" }, 300 );
			// $("span.sub", $(this)).hide();
			$("div.more", $(this)).fadeTo(300, 1);
		// css('margin-top', '7px');
		},
		function(){
			$(this).stop();
			$(this).animate({ marginTop: "107px" }, 100 );
			// $("span.sub", $(this)).show();
			$("div.more", $(this)).fadeTo(0, 0);			
		// css('margin-top', '7px');
		}
	);

	// Navigation:
	
	// $("#navigation").html($("ul#navList").html());
	// gem underundermenuer:
	// $("li.closed ul").remove();
	// $("#navigation li div").css('padding-left', 0);
	
	// UNDERMENU
	undermenu = $("#subnavigation");
	
	if (page_depth == 1) {
		undermenu.append($("ul:first", $("li.topLevel.parent div.current-bg").parent()));
	} else {
		undermenu.append($("ul:first", $("li.topLevel.parent a.topLevel[href$="+pages[0]+"]").parent().parent()));
	}
	
	if(undermenu.text().length > 0 && pages[0] != 'blog') {
		undermenu.show();
	}
	
	//Remove styles
	$("div", undermenu).removeAttr('style');
	
	//Remove expanders
	$("div.expander", undermenu).remove();
	
	//Show 3rd level menu if we are in the parent:
	$("ul", $("li.parent div.current-bg").parent()).css('display', 'block');
	$("ul li div.current-bg").parent().parent().css('display', 'block');
	
	// $("div.current-bg").prepend('&#8226;&nbsp;');
	
	
	
	// Permanent sidebar:
	$("#permanent_sidebar").html($("div.sites-embed:last").html());

	// UNDERMENU
	// undermenu = $("#subnavigation");
	
	// if (page_depth == 1) {
		// undermenu.html($("ul", $("li.topLevel.parent div.current-bg").parent()).html());
	// } else {
		// undermenu.html($("ul", $("li.topLevel.parent a.topLevel[href=?s="+pages[0]+"]").parent().parent()).html());
	// }
	
	// gem underundermenuer:
	// $("li.parent.closed ul").remove();
	
	// $("#subnavigation li div").css('padding-left', 0);
	
	
	//Pagetitle:
	title = $("div.current-bg:first").text() + " - ICISCENTER . ORG";
	// alert(title);
	document.title = title;
	
	//Fix links:
	$("#page a[href]").each(function(){
		$(this).attr('href', function(i, href) {
			href = href.replace("http://sites.google.com", '');
			href = href.replace("/site/iciscenter/", '?s=');
			return href;
		});
	});
	
	$("#content_text a[href]").each(function(){
		//Set target til _blank for udgaaende links..
		if($(this).attr('href').indexOf('site/iciscenter') == -1){
			$(this).attr('target', '_blank');
		}
	});
	


	// $table2 = str_replace("http://sites.google.com/site/".$sites_name."/", '?s=', $table2);
	// $table2 = str_replace("/site/".$sites_name."/", '?s=', $table2);

	
	// $("div:empty").fadeTo(0,0);
	// $("#content_text").wrapInner("<div class='padding' />");
	// $("#navigation").wrapInner("<div class='padding' />");
	// $("#content div").wrapInner("<div class='padding' />");
	/*
	
*/
});




jQuery.extend({
 getURLParam: function(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  var bFound=false;
	  
	  var cmpstring = strParamName + "=";
	  var cmplen = cmpstring.length;

	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = aParam[1];
	        bFound=true;
	        break;
	      }
	      
	    }
	  }
	  if (bFound==false) return null;
	  return strReturn;
	}
});


function debug(what) {
	if (!($("#debug").length > 0)) {
		$("body").append("<div id='debug'></div>");
		$("#debug").css('position', 'absolute');
		$("#debug").css('top', '0');
		$("#debug").css('left', '0');
		$("#debug").css('width', '500');
		$("#debug").css('height', '100');
		$("#debug").css('background-color', '#FFF');
		$("#debug").css('color', '#000');
		$("#debug").css('overflow', 'auto');
	}
	$("#debug").prepend(what+"<br/>");

}

