// <!-- $Date: 2011-07-24 21:01:01 +0200 (Sun, 24 Jul 2011) $ $Revision: 104 $ -->

// return the version of the msie rendering engine used or -1 for non msie browser
// http://blogs.msdn.com/b/giorgio/archive/2009/04/14/how-to-detect-ie8-using-javascript-client-side.aspx
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

var ie7 = getInternetExplorerVersion() == 7;
var nopartialtransparency = getInternetExplorerVersion() < 9 && getInternetExplorerVersion() >=0 ;

var contentwidth; // the original width of #content on loading the page. will be set by $(document).ready()
	
// animate the width of the #navdetail window to the given width w.
function setdetailwidth(w){
	// this hide and show stuff only necessary for ie7
	if(w>0 && ie7){ $("#navdetail *").show(); }
	$("#navdetail").stop().animate({width:w},{
		duration: 1000,
		step: function( now, fx ){
			$( "#content" ).css( "width", contentwidth-now );
		}
	});
	if(w<=0 && ie7){ $("#navdetail *").hide(); } // should happen on completion of animation
};


// add a suitable icon to an element
$.fn.iconify = function (icondir,iconmap) {
	var $this = $(this);
	icondir = icondir || $.fn.iconify.icondir;
	iconmap = iconmap || $.fn.iconify.iconmap;
	
	$this.each(function(i,e){
		e=$(e);
		var rel=e.attr('rel');
		var src="";
		var alt="";
		
		if(rel !== undefined && rel != ''){
			if(iconmap[rel]!==undefined){
				src='src="'+icondir+'/'+iconmap[rel]+'"';
			}else{
				src='src="'+icondir+'/'+rel+'-icon.png'+'"';
			}
			alt='alt="'+rel+' icon"';
			var img=$('<img '+src+' '+alt+' class="icon">');
			e.append(img);
		}
	});
	
	return $this;
};
$.fn.iconify.icondir='Icons';
$.fn.iconify.iconmap={
'python'	:'python-logo-24.png',
'blender'	:'blender-logo-24.png',
'photo'		:'camera-logo-24.png',
'wikipedia'	:'wikipedia-logo-24.png'
};


// create a table of contents from a the h2 elements in a given element
// returns a jQuery object
function tableofcontents(selector){
	var d=$('<div id="toc"/>');
	d.append($("<h3>Sections</h3>"));
	d.append($('<a href=".">'+"Top"+"</a>"));
	var h=$(selector+" h2");
	var nameindex = 1000 ;
	h.each(function(i,e){
		$(e).attr('name',"tocref"+nameindex);
		// the title attribute will show the full text that might be truncated by the width
		var a=$('<a href="#'+"tocref"+nameindex+'" title="'+$(e).text()+'">'+$(e).html()+"</a>");
		nameindex++;
		d.append(a);
	});
	return d;
};

// load a page in the #content element using ajax
// the actual content is just the div with a class="article"
// it also adds code highlighting to all suitable <pre> elements in the content and
// replaces the contents of the #sidebar with a table of contents generated from the h2 elements and
// adds the contents of the div with class="sidebarcontent"
function loadpage(url){
	//setdetailwidth(0);
	$("#content").load(url+" "+".article",function(){
		dp.SyntaxHighlighter.HighlightAll('code');
		//console.log(url+" "+".article");
		var sc=$('<div id="sidebarcontent" />')
		$("#fixedbar").load(url+" "+".sidebarcontent",function(r,s,xhr){
			//console.log(s+" "+" "+ xhr.status + " " + xhr.statusText);
			var toc=tableofcontents("#content .article"); //.css({width:'190px'});
			$("#fixedbar").prepend(toc);
			$(".sidebarcontent").prepend($("<h3>Links</h3>"));
			$("#fixedbar a").blend(600,nopartialtransparency).iconify().each(function(){
				var t=$(this).attr('title');
				if(t===undefined || t == ''){
					$(this).attr('title',$(this).text());
				}
			});
			$("#fixedbar a.internal").address();
		});
		$('#content a.lightbox').lightbox();
		$(".feedtitle").prepend($('<img src="Icons/blogger.png" alt="Blogger logo">'));
	});
	return false;
};

$(document).ready(function(){
	// alert('loaded'+window.location);
	// we moeten nog iets bedenken dat we width voor content en navdetail uit de dom vissen zodat we
	// niet ook de javascript moeten veranderen als we de breedte van 1 van de 2 in de css aanpassen
	
	contentwidth = $("#content" ).width(); 
	
	// clicks on main menu buttons cause an index page to be loaded together with an intro
	$(".regular").click(function(){
		setdetailwidth(0);
		$("#navdetail").empty();
		$("#navdetail").load($(this).attr('href')+" "+".index",function(){
			setdetailwidth(200);
			$("#navdetail a").blend(600,nopartialtransparency).iconify().each(function(){
				$(this).attr('title',$(this).text());
			});
			//will add a hash to the url once the link is clicked
			$('#navdetail a').address();
		});
		loadpage($(this).attr('href'));
		return false;
	});
	// home page button is a bit simpler, no submenu
	$(".homepage").click(function(){
		setdetailwidth(0);
		$("#navdetail").empty();
		loadpage($(this).attr('href'));
		return false;
	});
	//$(".homepage").blend(600,nopartialtransparency);
	
	// loading actual pages
	$(".subsection").live('click',function(){
		setdetailwidth(0);
		loadpage($(this).attr('href'));
		return false;
	});
	
	$(".section").blend(600,nopartialtransparency);
	
	$(".blogfeed").click(function(){
		setdetailwidth(0);
		loadpage($(this).attr('href'));
		return false;
	});
	
	//will add a hash to the url once the link is clicked
	$(".section").address();
	
	if(window.location.hash != "" && window.location.hash.substr(1,1) == "/" ){
		var clicked=false;
		$(".regular").each(function(i,e){
			//console.log($(e).attr('href'),window.location.hash.substr(2))
			if($(e).attr('href')==window.location.hash.substr(2)){
				clicked=true;
				$(e).click();
			}
		});
		if(!clicked){loadpage(window.location.hash.substr(2));}
	}else{
		$(".homepage").click();
	}
	
	// nasty hack to remove any double backgrounds
	if(ie7){
		$("div").each(function(i,e){
			var b=$(e).css('background-image');
			if(b!='none'){
				b = unescape(b.split(',')[0]);
				//console.log("background:",b);
				$(e).css('background-image',b);
			}
		});
	}
});

// clicks on table of content entries cause the browser to scroll to the section
$("#toc a").live('click',function(){
	var e;
	if($(this).attr('href')=="."){
		e=$("#header");
	}else{
		var name=$(this).attr('href').substr(1);
		e=$('[name='+name+']');
	}
	$(document).scrollTop(e.offset().top);
	return false;
});

