
function MM_swapImage() {
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d=document; 
    if(d.images){ 
	if(!d.MM_p) 
	    d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
	for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0) {	d.MM_p[j]=new Image; d.MM_p[j++].src=a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d) d=document; 
    if( (p=n.indexOf("?")) > 0 && parent.frames.length ) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if( !(x=d[n]) && d.all ) x=d.all[n]; 
    for(i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); 
    return x;
}

function toggleDisplay(turnon,divId){
	var tab = document.getElementById(turnon);
	var gtab = document.getElementById('maintextblock');

	if( tab.style.display != "none" ) { //tab.style.display == "inline" || tab.style.display == "block" ){
		//tab.style.display = "none";
		new Effect.Parallel( [new Effect.BlindUp(turnon),new Effect.Fade(turnon)] );
		gtab.setAttribute('currentTab','tab0');
		gtab.setAttribute('currentTabPlus','plus0');
	} else {
		//tab.style.display = "block";
		new Effect.Parallel( [new Effect.BlindDown(turnon),new Effect.Appear(turnon)] );
		var curTab = gtab.getAttribute('currentTab');
		// document.getElementById( curTab ).style.display = "none";
		if( curTab != "tab0" )
			new Effect.Parallel( [new Effect.BlindUp(curTab),new Effect.Fade(curTab)] );
		togglePlus( gtab.getAttribute('currentTabPlus') );
		gtab.setAttribute('currentTab',turnon);
		gtab.setAttribute('currentTabPlus',divId);
 	}

	togglePlus( divId );
	//window.location.hash += "," + turnon + "," + divId;
}

function togglePlus(divId) {
	if( divId == 'plus0' ) { return false; }

	var plus = document.getElementById(divId);

	if( plus.getAttribute('state') == '+' ) {
		plus.innerHTML = ' - '; // open
		plus.setAttribute('state','-');
	} else {
		plus.innerHTML = ' + '; // close
		plus.setAttribute('state','+');
	}
}

function ChangePage( block, url ) {
	var gtab = document.getElementById(block);
	gtab.setAttribute('currentTab','tab0');
        gtab.setAttribute('currentTabPlus','plus0');
	gtab.style.display = "none";
	gtab.innerHTML = "";
	new Ajax.Updater(block,url,{asynchronous:true,evalScripts:true,onComplete: function() { new Effect.Appear('maintextblock',{duration:0.5}); pageHistory();} });
	if( !hasSafariBug() )
	    window.location.hash = url;
	//gtab.style.display = "block";
}



function AjaxUpdater( block, url, on ) {
	document.getElementById(block).style.display = "none";
	//new Effect.Fade(block,{ duration:0.01,queue:{position:'front',scope:'myq'} });

	var topName = 'tut_decoration_top';
	var top     = document.getElementById(topName);
	var botName = 'tut_decoration_bottom';
	var bottom  = document.getElementById(botName);

	if( on ) {
		new Ajax.Updater( block, url, { asynchronous:true,evalScripts:true,onComplete:function(){ 
							new Effect.Appear('tutorial_space'); pageHistory(); } } );

		if( top != null ) {
			new Effect.Appear(topName,{duration:1.0});//top.style.display = "block";
		}
		if( bottom != null ) {
			new Effect.Appear(botName,{duration:1.0});//bottom.style.display = "block";
		}

		if(window.location.hash.length > 0 && !hasSafariBug() ) {
			if( window.location.hash.match("&t=") )
				window.location.hash = window.location.hash.replace(/&t=\w+.php/g,"&t="+url);
		 	else window.location.hash += "&t=" + url;
		}
	} else {
		new Ajax.Updater( block, url, { asynchronous:true,evalScripts:true,onComplete:function(){ new Effect.Fade('tutorial_space'); pageHistory(); } } );

		if( top != null ) {
			new Effect.Fade(topName,{duration:1.0});//top.style.display = "none";
		}
		if( bottom != null ) {
			new Effect.Fade(botName,{duration:1.0});//bottom.style.display = "none";
		}
		if(window.location.hash.length > 0 && !hasSafariBug() ) {
			window.location.hash = window.location.hash.replace(/&t=\w+.php/g,"");
		}
	}
}

function pageHistory() {
    doHighlight( );

    if( !hasSafariBug() )
	var url = window.location.hash.substring(1,window.location.hash.search(/php/)+3);
//	if( url == 'tutorials.php' )
//		loadLastTutorial();
}

function historyLoad( ) {

    if( hasSafariBug( ) ) {
	ChangePage('maintextblock', 'main.php' );
    	return;
    }
    
    var gtab = document.getElementById('maintextblock');
    gtab.innerHTML = '';

    var url = 'main.php';
    var tmp = '';

    if(window.location.hash.length > 0) {
	var index = (window.location.hash.indexOf('&') == -1) ? window.location.hash.length : window.location.hash.indexOf('&');
	url = window.location.hash.substring(1,index); 
	tmp = window.location.hash.substring(index);
    }
    ChangePage('maintextblock', url);
    window.location.hash += tmp;
    
    //if( url == 'tutorials.php' )
    //	loadLastTutorial();
}

function hasSafariBug( ) {

    if( BrowserDetect.browser == "Safari" ) {
	var version = parseFloat( BrowserDetect.version );
	if( version >= 312.0 && version < 420.0 )
	    return true;
    }
    return false;
}

loadLastTutorial = function( ) { // must use in this format for it to get evaluated in <script> block with Ajax.Update
    if(window.location.hash.length > 0) {
	var tutorial = window.location.hash;
	if( tutorial.search(/&t=/) == -1 ) return;
	tutorial = tutorial.substring(tutorial.search(/&t=\w+.php/)+3);
	tutorial = tutorial.substring(0,tutorial.search(/php/)+3);
	if( tutorial.length > 0 ) {
	    AjaxUpdater('tutorial_space',tutorial,true);
	}
    }
}

function doHighlight() {
    var h = new Highlighter(true);
    h.init();
}
