
var flag_nav;
var flag_irq = [];
var flag_blk = 0;

function hasTail(hay, key)
{
    return key.length <= hay.length
         ? key.toLowerCase() == hay.substr(hay.length - key.length, key.length).toLowerCase()
         : false;
}



function getnodeid(elm)
{
    var node = elm;
    while (node.id == "") {
        if (!node.parentNode) {
            return "";
        }
        node = node.parentNode;
    }
    return node.id;
}

function irqActiveSubnav(time)
{
    if (flag_nav) {
        clearInterval(flag_nav)
    }
    flag_nav = setInterval(function(){
        $(".subnavigatie")
            .animate({"opacity":0}, 1000, function(){
                $(".subnavigatie").css({
                    'visibility' : 'hidden',
                    'opacity' : "1.0"
                });
                $(".subnavigatie.subnav-active")
                    .css({
                        'opacity' : "0.0",
                        'visibility':'visible'
                    })
                    .animate({"opacity":"1.0"}, 1000);
        });
        clearInterval(flag_nav);
    }, time);
}

function startupNav(time)
{
    /// show submenu, unless leave link after time
    $("#navigatie .navigator a").mouseover(function(e){
        var id = getnodeid(e.target);
        $(".subnavigatie").css("visibility", "hidden");
        $("#subnav" + getDashTail(id)).css("visibility", "visible");
    }).mouseleave(function(e){
        if (!$("#subnav" + getDashTail(getnodeid(e.target))).hasClass("subnav-active")) {
            irqActiveSubnav(time);
        }
    });

    /// show submenu, unless leave menu after time
    $("#navigatie .subnavigatie").mouseenter(function(e){
        if (flag_nav) {
            clearInterval(flag_nav);
        }
    }).mouseleave(function(){
        if (!$(this).hasClass("subnav-active")) {
            irqActiveSubnav(time);
        }
    });
}

function valiz_popupelm(elm)
{
    if (elm.id) {
        var page = elm.id.substr(elm.id.lastIndexOf("-")+1);
        $(elm).CreateBubblePopup({
            "selectable": true,
            "position" : 'right',
            "align"	 : 'center',
            "innerHtml": $("#popup-" + page).html(),
            "dropShadow": !$.browser.msie,
            "themeName": 'grey',
            "themePath": 'script/jquerybubblepopup-theme'
        });
        $(elm).css("cursor","pointer");
    }
}

function startup()
{
    var hostname = new RegExp("/" + window.location.host + "/");

    startupNav(5000);
    relativeLinksScrollTo();

    $("a[href]").each(function(){
        if (hasTail(this.href, 'pdf')) {
//            $(this).append('<img style="vertical-align:top;margin-left:2px;border:0px;" src="ima/pdf_icon_tiny.gif">');
            $(this).append(' <span style="font-size:x-small;">(pdf)</span>');
        }
    });

    //
    $("a").each(function(){
        if (!hostname.test(this.href)) {
            $(this).attr("target", "_blank");
        }
    });
    $(".popuplink").each(function(){
        valiz_popupelm(this);
    })
    $(".pub-paypal").each(function(){
        valiz_popupelm(this);
    })

    $(window).resize(startupIrq);
    startupIrq();
}

function startupIrq()
{
    for (var i=0,j=flag_irq.length;i<j;i++) {
        clearInterval(flag_irq[i]);
    }
    init();
    irqPadMove("#scrolltouch", window);
    irqPadMove("#pubpad-1", "#plaatjespubbox-art");
    irqPadMove("#pubpad-2", "#plaatjespubbox-photography");
    irqPadMove("#pubpad-3", "#plaatjespubbox-design");
    irqPadMove("#pubpad-4", "#plaatjespubbox-architecture");

    /// skip htmlmove on "publication" page
    /// by detecting existence of this var.
    if (typeof sitebaseurl == "undefined") {
        irqDivMove("html", {
            "padw":  32,
            "pade": 128,
            "spdw":    .5,
            "spde":    .125,
            "clsw": 'hcursor-w',
            "clse": 'hcursor-e'
        });
    }

    var pub = {
        "left": 160,
        "padw":  32,
        "pade": 128,
        "spdw":    .25,
        "spde":    .0675,
        "clsw": 'pcursor-w',
        "clse": 'pcursor-e'
    };
    irqDivMove("#plaatjespubbox-art",          pub);
    irqDivMove("#plaatjespubbox-photography",  pub);
    irqDivMove("#plaatjespubbox-design",       pub);
    irqDivMove("#plaatjespubbox-architecture", pub);
    irqMenuMove("#subnavauteurs");
}

function getDashTail(strid)
{
    return strid.substr(strid.lastIndexOf("-") + 1);
}

function relativeLinksScrollTo()
{
    var time = 1750;
    if (location.hash != "") {
        $.scrollTo($("a[name=" + location.hash.substr(1) + "]"), time, "x");
        block(time);
    }
    var hhash = 0;
    var lhash = 0;
    var hurl = "";
    var lurl = "";
    lhash = location.href.indexOf("#");
    lurl  = lhash < 0 ? location.href : location.href.substr(0, lhash);
    $("a").each(function(){
        hhash = this.href.indexOf("#");
        hurl = hhash < 0 ? this.href : this.href.substr(0, hhash);
        if (this.href && hhash > 0 && lurl == hurl) {
            $(this).click(function(){
                $.scrollTo($("a[name=" + this.href.substr(this.href.indexOf("#") + 1)+ "]"), time, "x");
                block(time);
                return false;
            });
        }
    });
}

function xc(o)
{
    var s = "";
    for (var e in o) {
        s += e +":" + o[e] + "\n";
    }
    return s;
}

function irqPadMove(q, target)
{
    var mvx, msx, pad, spd, wdt, clc, lft;
    wdt = $(q).width();
    pad = Math.round(wdt/2);
    spd = 2;
    clc = 0;
    lft = parseFloat($(q).css("left"));

    $(q).mousemove(function(e){
        msx = e.clientX - lft;
        if (msx < pad) {
            clc = Math.round((pad - msx) * spd);
            mvx = clc == 0 ? "" : "-=" + clc
        } else
        if (msx > wdt-pad) {
            clc = Math.round((- wdt + pad + msx) * spd);
            mvx = clc == 0 ? "" : "+=" + clc
        } else {
            mvx = "";
        }
    });
    $(q).mouseleave(function(){
        mvx = "";
    });
    flag_irq.push(setInterval(function(){
        if (typeof mvx != "undefined" && mvx != "") {
            $(target).scrollTo({"left":mvx,"top":0}, 0);
            block(50);
        }
    }, 50));
}

function irqMenuMove(q)
{
    var clc, mvy, msy, pad, spd, hgt, div;
    div = $(q).offset();
    spd = .2;
    clc = 0;
    hgt = $(q).height();
    pad = hgt /3;

    $(q).mousemove(function(e){
        msy = e.clientY - div.top;
        if (msy<pad) {
            clc = Math.round((pad - msy) * spd);
            mvy = "-=" + clc == 0 ? "" : "-=" + clc;
        } else
        if (msy>hgt-pad) {
            clc = Math.round((- hgt + pad + msy) * spd);
            mvy = "+=" + clc == 0 ? "" : "+=" + clc;
        }
    });
    $(q).mouseleave(function(){
        mvy = "";
    });
    flag_irq.push(setInterval(function(){
        if (typeof mvy != "undefined" && mvy != "") {
            $(q).scrollTo(mvy, 1, "y");
        }
    }, 50));
}

function irqDivMove(q, i)
{
    var mvx, msx, wdt, div, o, mv;

    o = {};
    o.left = i.left ? i.left : 0;
    o.pad  = i.pad  ? i.pad  : 0;
    o.padw = i.padw ? i.padw : o.pad;
    o.pade = i.pade ? i.pade : o.pad;
    o.spd  = i.spd  ? i.spd  : 1;
    o.spdw = i.spdw ? i.spdw : o.spd;
    o.spde = i.spde ? i.spde : o.spd;
    o.cls  = i.cls  ? i.cls  : "";
    o.clsw = i.clsw ? i.clsw : o.cls;
    o.clse = i.clse ? i.clse : o.cls;
    o.fix  = i.fix  ? i.fix  : 0;

    div = $(q).offset();
    wdt = $(q).width();

    /// onMousemove
    $(q).mousemove(function(e){
//        $("title").html(e.target.tagName);

        if ($(e.target).hasClass("pub-paypal") || e.target.tagName == 'HTML') {
            $("." + o.clsw + ",." + o.clse ).removeClass(o.clsw + " " + o.clse);
            mvx = "";
        } else {
            msx = e.clientX - div.left; // position of cursor
            if (msx <= o.padw + o.left) {
                $(q).removeClass(o.clse).addClass(o.clsw);
                mv  = Math.round((o.padw - msx + o.left) * o.spdw);
                mvx = "-=" + mv;
            } else
            if (msx > wdt - o.pade) {
                $(q).removeClass(o.clsw).addClass(o.clse);
                mv = Math.round((- wdt + o.pade + msx) * o.spde);
                mvx = "+=" + mv;
            } else {
                $(q).removeClass(o.clsw +  " " + o.clse);
                mvx = "";
            }
        }
    });
    $(q).mouseleave(function(){
        mvx = "";
    });

    flag_irq.push(setInterval(function(){
//        $("title").html(mvx);
        if (typeof mvx != "undefined" && mvx != "") {
            $(q).scrollTo({"left":mvx,"top":0}, 0);
            block(50);
        }
    }, 50));
}


function block(time)
{
    flag_blk++;
    return setTimeout(function(){
        flag_blk--;
    }, time);
}

/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///



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 MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

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_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_swapImage() { //v3.0
  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];}
}


/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///


/*****************************************************
     Floater-Code (Javascript)

	 Code kann frei verwendet werden, wenn der
	 URHEBERHINWEIS NICHT ENTFERNT wird

     (c) Sandra Wiegard 2002
	 www.Elfengleich.de
	 www.Sandra-Wiegard.de

*****************************************************/
var floater;
var floatersub;
var LinkerRand = 0;
var old = LinkerRand;

function init()
{
    floater = new getObj('floater');
    floatersub = new getObj('floatersub');
    flag_irq.push(setInterval(movefloater, 7));
}

function getObj(name)
{
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    } else
    if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    } else
    if (document.layers) {
        this.obj = getObjNN4(document,name);
        this.style = this.obj;
    }
}

function movefloater()
{
    if (document.documentElement && document.documentElement.scrollLeft) {
        gescrolltes = document.documentElement.scrollLeft
    } else
    if (document.body) { // IE
        gescrolltes = document.body.scrollLeft
    }

    if (gescrolltes < LinkerRand) {
        gescrolltes = LinkerRand;
    } else {
        gescrolltes += 0;
    }

    if (flag_blk <= 0) {
        if (gescrolltes != old)	{
                /** Immer kleine werdende Schritte definieren **/
                gescrolltes = (.1 *(gescrolltes-old))+old;
                floater.style.left = gescrolltes + "px";
                floatersub.style.left = gescrolltes + "px";
        }
        old = gescrolltes;
    }
}

