/* Initialisation stuff */

/* Upcounter */
var yr = 2007;
var mo = 7;
var da = 7;
var searchfor_id = 'countupie';
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var crosscount = '';

// getElementsByClassName is rather new, emulate it, if it's undefined
if (typeof getElementsByClassName == "undefined")
 {
    document.getElementsByClassName = function(needle)
    {
        var s = document.getElementsByTagName('*'),
        i = s.length,
        r = [],
        e,
        c;
        needle = ' ' + needle + ' ';

        while (i--)
        {
            e = s.item(i);

            if (e.className)
            {
                c = ' ' + e.className + ' ';
                if (c.indexOf(needle) != -1) r.push(e);
            }
        }
        return r;
    }
}

function start_countup()
 {
    if (document.layers)
    document.countupnsmain.visibility = "show";
    else if (document.all || document.getElementById)
    crosscount = document.getElementById && !document.all ? document.getElementById(searchfor_id) : countupie;
    countup();
}

if (window.addEventListener) {
    window.addEventListener("load", start_countup, false);
    window.addEventListener("load", flickr_correct, false);
    //window.addEventListener("load", flickr_load_collection, false)
} else {
    window.attachEvent('onload', start_countup);
    window.attachEvent('onload', flickr_correct);
    //window.attachEvent('onload', flickr_load_collection);
}
/* window.onload=start_countup; */


function countup()
 {
    var t = new Date();
    var tyear = t.getYear();
    if (tyear < 1000) tyear += 1900;

    var todaystring =
    montharray[t.getMonth()] + " " + t.getDate() + ", " + tyear + " "
    + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds()

    paststring = montharray[mo - 1] + " " + da + ", " + yr
    dd = Date.parse(todaystring) - Date.parse(paststring)
    dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1)
    dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1)
    dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1)
    dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1)

    var out = dday + " Days " + dhour + " hrs " + dmin + " min " + dsec + " sec"
    if (document.layers)
    {
        document.countupnsmain.document.countupnssub.document.write(out)
        document.countupnsmain.document.countupnssub.document.close()
    } else if (document.all || document.getElementById)
    crosscount.innerHTML = out

    setTimeout("countup()", 1000)
}

/* 
  Correct some things which makes me life easier using the flickr helper
  from ecto
*/
function flickr_correct()
 {
    /* Flickr stuff */
    var flickrImages = document.getElementsByClassName('flickr');
    for (var i = 0, len = flickrImages.length; i < len; i++)
    {
        // Extract the photoID from the img src attribute
        // -- > http://farm4.static.flickr.com/3045/2668544094_23ac8cd0ed.jpg
        var imageSrc = new String(flickrImages[i].getAttribute('src'));
        var photoID = imageSrc.split('/').pop().split('_').shift();

        // Just operate on img tags with class flickr which are linked
	var parentOfImg = flickrImages[i].parentNode;
	if (parentOfImg.tagName != "A") {
	    // We have no A element as parentnode, create one
	    var parentA = document.createElement('A');
	    parentA.setAttribute('id', photoID);
            parentA.setAttribute('rel', 'lytebox');
	    // FIXME: if we change the DOM lytebox' updateItems does not pick up on this element, so we do it ourselves.
	    parentA.setAttribute('onclick','myLytebox.start(this, false, false); return false;');

	    var imgCopy = flickrImages[i].cloneNode(true);
	    parentA.appendChild(imgCopy);

	    var parentDIV = document.createElement('DIV');
	    parentDIV.setAttribute('class','flickr');
	    parentDIV.appendChild(parentA);
	    parentOfImg.appendChild(parentDIV);
	    parentOfImg.replaceChild(parentDIV,flickrImages[i]) 
	} else {
	    // Img element does have A parent
            parentA = flickrImages[i].parentNode
	    parentA.setAttribute('id', photoID);
            parentA.setAttribute('rel', 'lytebox');
	}

        flickr.photos.getInfo({
	    photo_id: photoID,
	    onComplete: flickr_correct_parentlink
        })
    }
    // Make sure lytebox recognizes our changed images
    // todo: this still errors out on IE
    myLytebox.updateLyteboxItems();
}

function flickr_correct_parentlink(res)
 {
    var photoSizes = null;
    // Adjust the parentnode
    if (res.photo)
    {
        var toChange = document.getElementById(res.photo.id);
        // Get the sizes of this photo
        flickr.photos.getSizes(
        {
            photo_id: res.photo.id,
            onComplete: function(info)
            {
                // Set the source to the size just below the original
                toChange.setAttribute('href', info.sizes.size[info.sizes.size.length - 2].source);
                if(res.photo.title._content) toChange.setAttribute('title', res.photo.title._content);
            }
        })

    }
    return true;
}


/*
  Load sets from the GD collection out of my flickr account
*/
function flickr_load_collection()
 {
    // Get a set of photosets
    flickr.photosets.getList({
        user_id: '96151162@N00',
        onLoading: function()
        {
            document.getElementById('flickr_gd_collection').innerHTML = 'Loading sets...';
        },
        onComplete: function(res)
        {
            var out = document.getElementById('flickr_gd_collection');
            out.innerHTML = '';
            for (var i = 0, len = res.photosets.photoset.length; i < len; i++)
            {
                // Get info for each photoset
                if (res.photosets.photoset[i].id == '72157606169354915' || // Donor Parts
                res.photosets.photoset[i].id == '72157606178644239' ||     // Chassis
                res.photosets.photoset[i].id == '72157606166211117' ||     // Rear end
                res.photosets.photoset[i].id == '72157606162427434' ||     // Front end
                res.photosets.photoset[i].id == '72157606178797441' ||     // Tools
                res.photosets.photoset[i].id == '72157606179004781' ||     // Garage
                res.photosets.photoset[i].id == '72157611315675721' ||     // Engine
                res.photosets.photoset[i].id == '72157622414966110'        // Exhaust
                )
                {
                    flickr.photosets.getInfo(
                    {
                        photoset_id: res.photosets.photoset[i].id,
                        onComplete: function(info) {
                            // We got the primaries, get the info of those
                            flickr.photos.getInfo(
                            {
                                photo_id: info.photoset.primary,
                                onComplete: function(prim)
                                {
                                    out.innerHTML += '<div class="flickrSetPrimary"><a href="http://www.flickr.com/photos/marcelvdboom/sets/' +
                                    info.photoset.id + '"><img src="' + flickr.getPhotoUrl(prim.photo, 's') + '"/></a><div class="caption">' +
                                    info.photoset.title._content + '</div></div>'
                                }
                            })
                        }
                    })
                }
            }
        }
    })
}

