/*
	Main JavaScript file for fotovaneerd.nl
	
	Copyright (c) 2003 Rutger van Eerd
*/

/******************************************************************************
    Query object.
******************************************************************************/

/*
    Decode the search string.
*/

function Query_decode() {
    // Create empty fields and values arrays.
    this.fields = new Array();
    this.values = new Array();
    
    // Discard the starting question mark and add a trialing ampersand.
    var query = location.search;
    query = query.substring(1, query.length) + "&";

    // Find the first ampersand.
    var pos = query.indexOf("&");

    // Find all field=value pairs.
    var pair, field, value;
    var i = 0;
    while (pos > 0) {
        // Get the pair and remove it from the query (including the ampersand).
        pair = query.substring(0, pos);
        query = query.substring(pos + 1, query.length);

        // Split the pair into field and value.
        pos = pair.indexOf("=");
        if (pos > 0) {
            field = pair.substring(0, pos);
            value = pair.substring(pos + 1, pair.length);
        } else {
            field = pair;
            value = "";
        }
        
        // Save the field and value.
        this.fields[i] = field;
        this.values[i] = value;
        i++;
        
        // Find next ampersand.
        pos = query.indexOf("&");
    }
}

/*
    Return the value of the given field or null of the fields is not defined.
*/
    
function Query_getValue(field) {
    for (var i = 0; i < this.fields.length; i++)
        if (this.fields[i] == field)
            return this.values[i];
            
    return null;
}

/*
    Determine whether a field is defined.
*/

function Query_isField(field) {
    for (var i = 0; i < this.fields.length; i++)
        if (this.fields[i] == field)
            return true;
    
    return false;
}

/*
    Create a new Query object and decode the search string.
*/
    
function Query() {
    // Set the fields.
    this.fields = null;
    this.values = null;
    
    // Set the methods.
    this.decode   = Query_decode;
    this.getValue = Query_getValue;
    this.isField  = Query_isField;
    
    // Decode the query.
    this.decode();
}

/******************************************************************************
    ImageStrip object.
******************************************************************************/

/*
    Open a new window for the given thumb.
*/

function ImageStrip_click(thumb) {
    window.open(this.href + "?image=" + this.images[this.position + thumb], 
        "_blank", "height=500,resizable,scrollbars,titlebar,width=600");
}

/*
    Show the next image on the image strip.
*/

function ImageStrip_next() {
    if (this.position + this.size < this.thumbs.length) {
        this.position++;
        this.update();
    }
}

/*
    Show the previous image on the image strip.
*/

function ImageStrip_previous() {
    if (this.position > 0) {
        this.position--;
        this.update();
    }
}

/*
    Update the images on the image strip.
*/

function ImageStrip_update() {
    for (var i = 0; i < this.size; i++)
        setImageSrc(this.name + i, this.thumbs[this.position + i]);
}

/*
    Create a new ImageStrip.
*/

function ImageStrip(name, thumbs, images, size, href) {
    // Set the fields.
    this.name     = name;
    this.thumbs   = thumbs;
    this.images   = images;
    this.size     = size;
    this.position = 0;
    this.href     = href;
    
    // Set the methods.
    this.click    = ImageStrip_click;
    this.next     = ImageStrip_next;
    this.previous = ImageStrip_previous;
    this.update   = ImageStrip_update;
}

/******************************************************************************
    Global functions.
******************************************************************************/

/*
	Writes a link using the given current page, the html and the target page.
*/

function writeLink(base, current, html, page) {
	if (current == page)
		document.writeln("<nobr><img src=" + base + "arrow.gif>");
	else
		document.writeln("<nobr><img src=" + base + "block.gif>");
		
	document.writeln("<a href=" + base + page + ".html>" + html + "</a></nobr><br>");
}

/*
	Write the header using the given base and current page.
*/

function writeHeader(base, page) {
	document.writeln("<table border=0 cellpadding=4 cellspacing=4 width=100%>");
	
	// Header bar.
	document.writeln("<tr>");
	document.writeln("<td colspan=3>");
	document.writeln("<table border=0 cellpadding=0 cellspacing=0 width=100%>");
	document.writeln("<tr>");
	document.writeln("<td align=left bgcolor=#0000c0>");
	document.writeln("<a href=" + base + "index.html><img border=0 src=" + base + "title.gif></a></td>");
	document.writeln("<td align=right bgcolor=#0000c0><img src=" + base + page + ".gif></td>");
	document.writeln("</tr>");
	document.writeln("<tr>");
	document.writeln("<td bgcolor=#000000 colspan=2><img src=" + base + "filler.gif></td>");
	document.writeln("</tr>");
	document.writeln("</table>");
	document.writeln("</td>");
	document.writeln("</tr>");

	// Navigation bar.
	document.writeln("<tr valign=top>");
	document.writeln("<td>");		
	writeLink(base, page, "Over ons", "about");
	writeLink(base, page, "Contactgegevens", "contact");
	writeLink(base, page, "Kinder- en babyportretten", "children");
	writeLink(base, page, "Portretten", "portraits");
	writeLink(base, page, "Bruidsreportages", "mariage");
	writeLink(base, page, "Bedrijfsfotografie", "ads");
	document.writeln("</td>");

	document.writeln("<td background=vline.gif><img src=filler.gif></td>");

	document.writeln("<td align=left width=100%>");
}

/*
	Writes the footer.
*/

function writeFooter() {
	document.writeln("<br clear=all>");
	document.writeln("</td>");
	document.writeln("</tr>");

	document.writeln("<tr valign=top>");
	document.writeln("<td colspan=3 align=right>");
	document.writeln("<a href=#>omhoog</a> | <a href=javascript:goBack()>terug</a>");
	document.writeln("</td>");
	document.writeln("</tr>");
	document.writeln("</table>");
    
    document.writeln("<p align=center><small>&copy; 2004 Combifoto en Vakfotografie Van Eerd. " +
        "Web design door <a href=mailto:rutgervaneerd@hotmail.com>Rutger van Eerd</a></small>");
}

/*
	Returns to the previous page in the browser history.
*/

function goBack() {
	if (window.history)
		window.history.back();
}

/*
	Preloads the given image.
*/

function preloadImage(src) {
	var img = new Image();
	img.src = src;
	return img;
}

/*
	Sets the source file of an image element with the given name.
*/

function setImageSrc(image, src) {
	var el = null;
	
	if (document.images)
		el = document.images[image];
		
	if (el)
		el.src = src;
}
								