////////////////////////////////////////////
//
// Gtools: a library of trivial client side scripting stuff
//
// Version:          0.7
// Copyright:        2004-2010 Gilles van Eeden
//
////////////////////////////////////////////

/////////////////////////////////////////////
// *** BEGIN GLOBALS ***
/////////////////////////////////////////////

// momentarily recognized values:
// left, center, right
framepos = 'center';

// theme
// THEME='dark';

/////////////////////////////////////////////
// *** END GLOBALS ***
/////////////////////////////////////////////

/////////////////////////////////////////////
// MAINFRAME
/////////////////////////////////////////////

// miscellaneous initialization stuff.
function initMainframe()
{
    // any saved frame positions?
    framepos = readCookie('framepos');
    if (framepos == null) framepos = 'center';
    
    // redraw mainframe when necessary
    window.onresize = drawMainframe;
    // safari needs this one to cover cached pages
    if (window.addEventListener)
        window.addEventListener( 'pageshow', function(e){ drawMainframe(); }, false );

    // set main page width
    var mw = 0;
    if (document.getElementById("MENU_"))
    {
        mw = document.getElementById("MENU_").offsetWidth;
    }
    document.getElementById("MAIN_").style.left = mw + 'px';
    document.getElementById("MAIN_").style.right = 0 + 'px';
    
    // set navigation bar width
    if (document.getElementById("NAV_"))
    {
        document.getElementById("NAV_").style.left =
        document.getElementById("MAIN_").style.left;
    }
    
    // title coordinates
    document.getElementById("title01").style.right = 0;
    document.getElementById("title01").style.width = document.getElementById("MAIN_").offsetWidth;
    
    // set contents margins
    setPageMargins();
    
    // just a simple way of preventing text selection during drag&drop
    if (document.all) document.body.onselectstart = function () { return false; } // ie
    else document.body.onmousedown = function () { return false; } // rest of the known universe
    
    // THEMES: for now, enable standard styles
    if (document.getElementById('isisCSS'))
        document.getElementById('isisCSS').disabled = false;
    if (document.getElementById('prjCSS'))
        document.getElementById('prjCSS').disabled = false;
    if (document.getElementById('isisWhiteCSS'))
        document.getElementById('isisWhiteCSS').disabled = true;
    if (document.getElementById('prjWhiteCSS'))
        document.getElementById('prjWhiteCSS').disabled = true;
        
    document.getElementById('logoimg').src = RESROOT+'2010logo_cropped_alpha_h56.png';
}

// enable text selection
function enableTextSel()
{
    if (document.all) document.body.onselectstart = ''; // ie
    else document.body.onmousedown = ''; // rest of the known universe
}

// wrapper for mainframe drawing routines
// NOTE: this is called twice during page load
//       1) right before </BODY>, faster but unsafe
//       2) at document.onload, safe but slow
function drawMainframe()
{
    var w = f_clientWidth();
    var h = f_clientHeight();
    
    var m = document.getElementById("MAINFRAME_");    
    setMainframeXpos(m, w);
    
    var q = document.getElementById('menu_cont');    
    if (q != null)
    {
        setScrollViewHeight(h);
        checkScroller(h,q);
    }
    
    if (dragger != null && dragger != undefined)
    {
        keepXinside();
        keepYinside();
    }
}

// this function is called before onload, so we pass an
// object just to enable initialization check
function setMainframeXpos(obj, w)
{
    if (obj != null)
    {
        switch (framepos)
        {            
            case 'left':    {obj.style.left = '0px'; break;}
            case 'right':   {obj.style.left = w - obj.offsetWidth + 'px'; break;}
            default:        {obj.style.left = Math.floor(w/2) - Math.floor(obj.offsetWidth/2) + 'px';}
        
        }
        animateMainframe(0);
    }
}

function animateMainframe(t)
{
    var m = document.getElementById('MAINFRAME_');
    var newpos, w;
    w=f_clientWidth();
    
    switch (framepos)
    {        
        case 'left':    {newpos = 0; break;}
        case 'right':   {newpos = w - m.offsetWidth; break;}
        default:        {newpos = Math.floor(w/2) - Math.floor(m.offsetWidth/2);}
    
    }
    
    if (document.getElementById("sidefillbott") != null)
    {
        var sl = newpos+m.offsetWidth;
        $('#sidefillbott').animate({
        left: sl,
        width: w-sl,
        right: 0
        }, t, function() {});
    }
    
    $('#MAINFRAME_').animate({
    left: newpos
    }, t, function() {});
}

function setYCenter(id)
{
    var t = document.getElementById(id);
    t.style.top = Math.round(f_clientHeight()/2 - t.offsetHeight/2);
}

// page margin
function setPageMargins()
{
    var M = document.getElementById('MAIN_');
    var m;
    if (!document.getElementsByClassName) // f@ck M$
        m = getIEelementsByClassName('margin_default')[0];
    else m = document.getElementsByClassName('margin_default')[0];

    if (m != null)
    {
        m.style.marginLeft = (M.offsetWidth - m.offsetWidth) / 2 + 'px';
        m.style.marginRight = M.offsetWidth - m.offsetWidth - (M.offsetWidth - m.offsetWidth) / 2 + 'px';
        m.style.marginTop = '75px';
        m.style.height='auto';
        m.style.marginBottom = '75px';
    }
}

////////////////////////////////////////////
// WINDOW SIZES AND SCROLL POSITIONS
// far and away the cleanest and best documented
// implementation, no silly name guessing
// See: http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
////////////////////////////////////////////

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

////////////////////////////////////////////
// CORRECTIONAL FACILITIES
////////////////////////////////////////////

function keepInsideWindow()
{
    if (document.getElementById('draggy') != null)
    {
        var el = document.getElementById('draggy');
        if (parseInt(el.style.right) > f_clientWidth())
            el.style.right = f_clientWidth() + 'px';
        if (parseInt(el.style.bottom) > f_clientHeight())
            el.style.bottom = f_clientf_clientHeight() + 'px';
        if (parseInt(el.style.left) < 0)
            el.style.left = '0px';
        if (parseInt(el.style.top) < 0)
            el.style.top = '0px';
    }
}

////////////////////////////////////////////
// IE stuff
////////////////////////////////////////////

function getIEelementsByClassName(c)
{
    var allT=document.getElementsByTagName('*'), allCN=[], i=0, a;
    while(a=allT[i++])
    {
        a.className==c?allCN[allCN.length]=a:null;
    }
    return allCN
}

////////////////////////////////////////////
// STRING FUNCTIONS
////////////////////////////////////////////

function dirname(path) {
    return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
}

function basename (path, suffix)
{
    var b = path.replace(/^.*[\/\\]/g, '');
    
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix)
    {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

// parse location.href for value of argument 'arg'
function gup( arg )
{
  arg = arg.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+arg+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null ) return "";
  else return results[1];
}

// url parser
function parseUrl(data)
{
    var e=/^((file|http|ftp):\/)?\/?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}

////////////////////////////////////////////
// THEMES
////////////////////////////////////////////
function toggleTheme()
{
    if (THEME=='dark')
    {
        document.getElementById('isisCSS').disabled = true;
        document.getElementById('prjCSS').disabled = true;
        document.getElementById('isisWhiteCSS').disabled = false;
        document.getElementById('prjWhiteCSS').disabled = false;
        document.getElementById('logoimg').src = RESROOT+'2010logo_cropped_alpha_h56_blk.png';
        THEME='light';
    }
    else
    {
        document.getElementById('isisCSS').disabled = false;
        document.getElementById('prjCSS').disabled = false;
        document.getElementById('isisWhiteCSS').disabled = true;
        document.getElementById('prjWhiteCSS').disabled = true;
        document.getElementById('logoimg').src = RESROOT+'2010logo_cropped_alpha_h56.png';
        THEME='dark';
    }
}

var bglist = new Array();

function randBg()
{
    bgPath = RESROOT+"backgrounds/";
    
    if (!bglist.length)
    {
        // execute XMLHttpRequest
        var bgstr = DIR.ls(bgPath);
        bglist = bgstr.slice(0, -1).split("/");
    }
    limit = bglist.length;
    bg = bglist[ Math.floor(Math.random()*limit) ];
    
    document.body.style.backgroundImage = "url(" + bgPath + bg + ")";
    
}

////////////////////////////////////////////
// COOKIES
////////////////////////////////////////////

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

////////////////////////////////////////////
// SHORTHANDS
////////////////////////////////////////////

function showObject( id )
{
	document.getElementById(id).style.visibility='visible';
    document.getElementById(id).style.height = 'auto';
}

function hideObject( id )
{
	document.getElementById(id).style.visibility='hidden';
    document.getElementById(id).style.height = '0px';
}

////////////////////////////////////////////
// TITLE
////////////////////////////////////////////

function writeTitleDiv(title)
{
    document.write("<div id='titleback'>");
    
    document.write("<DIV id='titlelogo' style='left:5px;top:2px;'>");
    document.write("<A href='"+DOCROOT+"home.html' title='Home'>");
    document.write("<IMG id='logoimg' alt='Home' title='Home' src='"+RESROOT+"2010logo_cropped_alpha_h56.png' style='margin:0px;border-style:none;'>");
    document.write("</A>");
    document.write("</DIV>");
    
    document.write("<DIV class='title' id='title01'>");
    document.write(title);
    document.write("</DIV>");
    
    document.write("</DIV>");
}

function writeProjectTitle(tit)
{
    document.write("<title>Isis Projecten - "+tit+"</title>");
}

////////////////////////////////////////////
// NAVIGATION LINKS
////////////////////////////////////////////

function writeNavigation(nobut)
{
    // spacer for fullscreen navigation bar
    document.writeln('<div id="navspace">');
    
    if (nobut==null)
    {
        document.writeln('<DIV style="float:left;cursor:pointer;margin-left:4px;" ');
        document.writeln('onclick="(function(){createCookie(\'framepos\',\'left\',7);framepos=\'left\';animateMainframe(350);}())">');
        document.writeln('<IMG alt="Frame links" title="Frame links" src="'+RESROOT+'framebuttons/frameleft.png"></DIV>');
        document.writeln('<DIV style="float:left;cursor:pointer;margin-left:3px;" ');
        document.writeln('onclick="(function(){createCookie(\'framepos\',\'center\',7);framepos=\'center\';animateMainframe(350);}())">');
        document.writeln('<IMG alt="Frame midden" title="Frame midden" src="'+RESROOT+'framebuttons/framecenter.png"></DIV>');
        document.writeln('<DIV style="float:left;cursor:pointer;margin-left:3px;" ');
        document.writeln('onclick="(function(){createCookie(\'framepos\',\'right\',7);framepos=\'right\';animateMainframe(350);}())">');
        document.writeln('<IMG alt="Frame rechts" title="Frame rechts" src="'+RESROOT+'framebuttons/frameright.png"></DIV>');

        //document.writeln('<DIV style="float:left;cursor:pointer;margin-left:20px;" ');
        //document.writeln('onclick="(function(){randBg();}())">');
        //document.writeln('<IMG alt="Theme" title="Theme" src="'+RESROOT+'framebuttons/theme.png"></DIV>');
    }
    
    // THEMES
    //document.writeln('<DIV style="float:left;cursor:pointer;margin-left:3px;" ');
    //document.writeln('<a class="navlink" href="javascript:toggleTheme();" alt="theme">Theme</a></div>');
    
    document.writeln('<a class="navlink" href="'+DOCROOT+'home.html" alt="Home">Home</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+PRJROOT+'projects.html" alt="Projecten">Projecten</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'essay/essay.html" alt="Archief">Essay</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'verbeelding/index.html" alt="Verbeelding">Verbeelding</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'nieuws/nieuws.html" alt="Nieuws">Nieuws</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'over_isis/over_isis.html" alt="Over Isis">OverIsis</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'contact/contact.html" alt="Contact">Contact</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'archief/archief.html" alt="Links">Archief</a>&nbsp;');
    document.writeln('<a class="navlink" href="'+DOCROOT+'site_info/site_info.html" alt="Site Info">SiteInfo</a>&nbsp;&nbsp;');
    
    document.writeln('</div>'); 
}

// call this in 'fullscreen mode' only
function setNavSpacer()
{
    // any saved frame positions?
    framepos = readCookie('framepos');
    if (framepos == null) framepos = 'center';
    
    var w;
    switch (framepos)
    {
        case 'left':    {w = 0; break;}
        case 'right':   {w = f_clientWidth() - 824; break;}
        default:        {w = Math.floor(f_clientWidth()/2) - 412;}
    }
    document.getElementById('navspace').style.left = w + "px";
    
    window.onresize=setNavSpacer;
}

////////////////////////////////////////////
// SIDEFILL
////////////////////////////////////////////

function writeSidefill()
{
    var l;
    if (document.getElementById("MAINFRAME_")!=null)
    {
        l = document.getElementById("MAINFRAME_").offsetLeft + document.getElementById("MAINFRAME_").offsetWidth;
    }
    else if (document.getElementById("NAV_")!=null)
    {
        l = document.getElementById("NAV_").offsetLeft + document.getElementById("NAV_").offsetWidth;
    }
    else l = 0;
    var w = Math.ceil((f_clientWidth() - l)/2);
    document.writeln('<DIV id="sidefillbott" style="left:' + l + 'px;width:' + w + 'px;"></DIV>');
}

////////////////////////////////////////////
// SCRAMBLE EMAIL ADDRESS
////////////////////////////////////////////

function unscramble(adr,lnk)
{
    var boo = "isis-architectuur";
    var a = String.fromCharCode(64);
    var n = "nl";
    var m = "m";
    if (lnk == undefined || lnk == null)
        document.write("<a href=" + m + "ail" + "to:" + adr + a + boo + "." + n + ">" + adr + a + boo + "." + n + "</a>");
    else
        document.write("<a href=" + m + "ail" + "to:" + adr + a + boo + "." + n + ">" + lnk + "</a>");
}

////////////////////////////////////////////
// FIN
////////////////////////////////////////////








