var top = 0;
var mouseover = false;
var images = new Array();

function serial(e) {
    var a = e.getElementsByTagName('p');
    var x = a[1];
    clearInterval(x.intervalId);
    x.window = a[0];
    x.tokens = iter(x.innerHTML.split(' '));
    x.updateWindow = function () {
        try {
        x.window.innerHTML = x.tokens.next()
        } catch(ex) {
        clearInterval(x.intervalId);
        }
    }
    x.parentNode.childNodes[0].innerHTML = 'moo'; 
    x.intervalId = setInterval(x.updateWindow,100);
}
function clickedResume() {
    // with Mochikit, I am a DOM god!
    var d = doSimpleXMLHttpRequest('resume.xhtml');
    d.addCallback(function(req){
            replaceChildNodes($('resume'),req.responseXML.getElementsByTagName('body')[0].childNodes);
            }
    );
    d.addErrback( function(req) {
        $('resume').innerHTML = '<p>Unable to load career description document</p>';
        }
    );
}
function load() {
    var container = $('container');
    blindUp(container,{duration:0.0});
    // blindUp(getElementsByTagAndClassName('table','fd_maintable')[0],{duration:0.0});
    // if (GBrowserIsCompatible()) {
        // var map = new GMap2(document.getElementById("map"));
            // center = new GLatLng(18.86,50.25);
            // // center = new GLatLng(39.16,125.75);
            // map.setCenter(center, 10, G_SATELLITE_MAP);
    // // map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 40)));
    // }
    for(var i=0;i<10;i++) {
        images[i] = new Image();
        images[i].src = "images/lena/0" + i + ".jpg"; 
    }
    MochiKit.Visual.dropOut($('loading'),{duration: 0.8});
    showElement(container);
    blindDown(container,{delay:0.5,duration:0.6});
    draggables = getElementsByTagAndClassName('div','draggable');
    forEach(draggables, function(x) {
            //new Drag(x);
            // new Draggable(x);
        });
    /*
    new Droppable('magicbox', {
        accept: ['draggable'],
        ondrop: function (element) {
            $('magicbox').innerHTML = element.innerHTML;
        }
    });
    */
    morsels = getElementsByTagAndClassName('div','morsels');
    forEach(morsels, function(x) {
            r = parseInt(Math.random() * 100);
            g = parseInt(Math.random() * 100);
            b = parseInt(Math.random() * 100);
            bottom = parseInt(Math.random() * 200);
            left = parseInt(Math.random() * 100);
            x.style.background = 'rgb(' + r + ',' + g + ',' + b + ')';
            x.style.left = left + 'px';
            x.style.bottom = bottom + 'px';
            connect(x,'onclick',morselonclick);
        });
    // feeds
    url = "/feeds/upcoming.json";
    var d = loadJSONDoc(url);
    d.addErrback(function(x) { $('feedz').innerHTML="sorry, couldn't import feeds";});
    d.addCallback(
        function(response) 
        {
            var e = UL();
            $('feedz').appendChild(e);
            for (var i=response.length-1;i>=0;i--) 
            {
            e.appendChild(
                LI(null, 
                    SPAN(null,response[i].time + ' '),
                    A({target:'_blank',href:response[i].link},response[i].title)
                )
            );
                
            }
         });

    // end feeds

    pulseMorsels();
    d = $('delicious');
    baseurl = 'http://del.icio.us/whookey/';
    ul = UL({'class':'delicious','style':'list-style-type:none;'},null);
    forEach(keys(Delicious.tags),function (x) {
        var size = .9 + Delicious.tags[x] / 8;
        if (size > 2) { size=2; }
        c = Color.fromRGB(Math.random(),Math.random(),Math.random()*.8);
        c = c.darkerColorWithLevel(.40);
        s = A({'style':'color: '+ c.toHexString() + ';font-size:'+size+'em;','href':baseurl+x},x);
        appendChildNodes(ul,s,' ');
    });
    d.appendChild(ul);
    linkstorm();
        setTimeout("$('container').style.background='url(images/webbees.jpg)'" ,5000);
        // setTimeout("$('container').style.background='url(images/tulip.jpg)'" ,20000);

    try {
        var args = parseQueryString(document.location.search);
        if (args.morsel) {
            signal($(args.morsel),'onclick');
        }
    }catch (Exception) {}
}
function greyize() {
    if (top > 0 && ! mouseover) {
        top = top - 1;
        $('lena').src = images[top].src; 
        setTimeout("greyize()",100);
    } 
}
function colorize() {
    if (top < 10 && mouseover) {
        $('lena').src = images[top].src; 
        top++;
        setTimeout("colorize()",100);
    } else { 
    mouseover = false;
        setTimeout("greyize()",100);
    }
}
function pulseMorsels() {
    x = getElementsByTagAndClassName('div','morsels');
    parent = $('morselcontainer');
    forEach(x, function(x) { 
            if(Math.random()>.90) {
                // appear(x,{duration:.6, queue:{scope:'morsels'}});
                // fade(x,{delay:2.0,to:0.30,duration:0.6,queue:{scope:'morsels'}});
                appear(x,{duration:.6, });
                fade(x,{delay:2.0,to:0.30,duration:0.6});
            } else {
                left = parseInt(x.style.left) + Math.round(2.0 - Math.random()*4);
                bottom = parseInt(x.style.bottom) + Math.round(2.0 - Math.random()*4);
                x.style.left = left + 'px';
                x.style.bottom = bottom + 'px';
		try {
                parent.insertBefore(x,parent.firstChild);
		} catch(error) {
		}
            }
    });
    setTimeout("pulseMorsels()",2000);
}
 
function damagesvg() {
    toggle($('svg'),'blind',{duration:.5});
    // thanks, http://jwatt.org/svg/demos/scripting-across-object.html
    // var object = document.getElementsByTagName('object')[0];
    var object = document.getElementById('svg');
    if (object && object.contentDocument)
    svgdoc = object.contentDocument;
    else try {
    svgdoc = object.getSVGDocument();
    }
    catch(exception) {
    // alert('Neither the HTMLObjectElement nor the GetSVGDocument interface are implemented');
    }

    e = svgdoc.getElementsByTagName('g')[0];
    val = Math.round(Math.random()*9);
    e.setAttribute('fill','#' + Math.round(Math.random()*9)+val+Math.round(Math.random()*9)+val+val+val);
}
function linkstorm() {
    var u = getElementsByTagAndClassName("ul","delicious")[0];
    var l = u.getElementsByTagName("a");
    var tl = $('tl');
    for (var i=1;i<8;i++) {
        forEach(l, function(x) {
            c = x.cloneNode(true);
            c.style.color = 'rgb(200,200,200)';
            c.style.opacity = .75;
            c.style.fontSize = '7pt';
            tl.appendChild(c);
            tl.appendChild(document.createTextNode(' '));
            });
    }
}
function remove() {
    fold(this);
}
function morselonclick(){
    xd = DIV();
    xd.className = 'morselpopup';
    xd.innerHTML = "(click to remove)<br/>" + this.innerHTML;
    $('container').appendChild(xd);
    connect(xd,'onclick',remove);
}
