/* Copyright (c) 2008 www.jaspervanveen.nl */

        var loop_max = 0;   // 0 = infinite;
        var loop = 0;

        var ver_lines = 7;
        var hor_lines = 7;

        var color = "#000000"
        var color_r = 90;
        var color_g = 10;
        var color_b = 90;
        var stability_r = 1;
        var stability_g = 4;
        var stability_b = 1;
        
        var images = 19; // slideshow
        var imageId = 1;

        //var ver_line_1_position = 120;        
        
        function init() {
            color = "#" + color_r + color_g + color_b;
            //var ver_lines_position = new Array(ver_lines-1);
            //ver_lines_position[0] = ver_line_1_position;
            //mood();
        }
/*        
        function mood() {
            for (i=1;i<=ver_lines;i++) {
                get_color();  
                document.getElementById("ver_line" + i).style.background = color;
                
                //document.getElementById("ver_line" + i).style.left = parseInt("ver_line" + i + "_left")       
              
                //window.alert(ver_lines_position[i-1]);  
              
            }
            for (i=1;i<=hor_lines;i++) {
                 get_color(); 
                 document.getElementById("hor_line" + i).style.background = color;                
            }
            //get_color();
            //document.body.style.background = "#" + Math.round((888888 + parseInt(color.substring(1,6))/10));
  
            // LOOP
            if(loop<loop_max==0){
                window.setTimeout("mood();",250);
                loop++;
            }
        }
*/        
        function get_color() {
            new_r = Math.round((color_r*stability_r + Math.round(100*Math.random()))/(stability_r+1));
            new_g = Math.round((color_g*stability_g + Math.round(100*Math.random()))/(stability_g+1));
            new_b = Math.round((color_b*stability_b + Math.round(100*Math.random()))/(stability_b+1));
            if(new_r.toString().length<2){
                new_r = "0" + new_r;
            }
            if(new_g.toString().length<2){
                new_g = "0" + new_g;
            }
            if(new_b.toString().length<2){
                new_b = "0" + new_b;
            }
            color = "#" + new_r + new_g + new_b;
        }

// slideshow

function goTo(target){
    if(target=="next"){
        if(imageId<images){
            imageId++;    
        } else {
            imageId = 1;            
        }
    } else if(target=="previous") {
        if(imageId==1){
            imageId = images;
        } else {
           imageId--;    
        }
    }
    document.getElementById("frame").src = "images/" + imageId_toString(imageId,4) + ".jpg";
}

function imageId_toString(number, digits) {
	number = number.toString();
	for (i=number.length; i<digits; i++) {
		number = "0" + number;
	}
	return number;
}

// Linelight ---------------------------------------------

// cluster: orientation,position,range,members
var start0 = new cluster("horizontal",20,40,3);
var start1 = new cluster("horizontal",210,80,5);
var start2 = new cluster("horizontal",360,140,3);
var start3 = new cluster("horizontal",-10,4,1);
var start4 = new cluster("vertical",40,180,8);
var start5 = new cluster("vertical",-180,50,2);

var max_width = 3;

var color_r = 77;
var color_g = 0;
var color_b = 33;
var stability_r = 1;
var stability_g = 9;
var stability_b = 9;

/* ------------------------------------------------------------------------------------- */

var number_of_clusters = 0;
var clusters = new Array();
var layers = "";
var delay_short = 25;
var delay_medium = 250;
var delay_long = 2500;
var delay = delay_short;
var counter = 0;

function linelight() {
    try { // search for clusters
        while(eval("start"+number_of_clusters)){
            number_of_clusters++;
        }        
    } catch(err) {
        // all clusters found
    }
    get_data("start");
    init_layers();
    write_layers();
    init_clusters();
}

function get_data(target) {
    for(j=0;j<number_of_clusters;j++){
        clusters[j] = eval(target + j);
    }
}

function init_layers() {
    for(j=0;j<number_of_clusters;j++){
        for(i=0;i<clusters[j].members;i++){
            get_color();
            init_layer(j,i);
        }
    }
}

function write_layers() {
    document.open();
    document.write(layers);  
    document.close();
}

function init_clusters() {
    for(j=0;j<number_of_clusters;j++) {
        for(i=0;i<clusters[j].members;i++){
            init_line(j,i);
        }
    }
}

function init_layer(j,i) {
    clusters[j].lines[i] =  new line(clusters[j].orientation, clusters[j].position +  Math.round(Math.random() * clusters[j].range), get_width(), get_color(), get_z(j,i));    
    layers = layers + "<div class='line' id='cluster" + j + "line" + i + "'></div>";
}

function init_line(j,i) {
    if(clusters[j].lines[i].orientation=="horizontal") {
        document.getElementById("cluster" + j + "line" + i).style.width = "100%";
        document.getElementById("cluster" + j + "line" + i).style.height = clusters[j].lines[i].width + "px";
        if(clusters[j].lines[i].position<0){ // wraps around if negative
            document.getElementById("cluster" + j + "line" + i).style.bottom = Math.abs(clusters[j].lines[i].position) + "px";
        } else {
            document.getElementById("cluster" + j + "line" + i).style.top = clusters[j].lines[i].position + "px";
        }
        document.getElementById("cluster" + j + "line" + i).style.left = "0px";
    } else { // orientation == "vertical"
        document.getElementById("cluster" + j + "line" + i).style.width = clusters[j].lines[i].width + "px";
        document.getElementById("cluster" + j + "line" + i).style.height = "100%";        
        document.getElementById("cluster" + j + "line" + i).style.top= "0px";
        if(clusters[j].lines[i].position<0){ // wraps around if negative
            document.getElementById("cluster" + j + "line" + i).style.right = Math.abs(clusters[j].lines[i].position) + "px";
        } else {
            document.getElementById("cluster" + j + "line" + i).style.left = clusters[j].lines[i].position + "px";
        }
    }
    document.getElementById("cluster" + j + "line" + i).style.background = clusters[j].lines[i].color;
    document.getElementById("cluster" + j + "line" + i).style.zIndex = clusters[j].lines[i].z;
}

function line(orientation, position, width, color, z) {
    this.orientation = orientation;
    this.position = position;
    this.width = width;
    this.color = color;
    this.z = z;
}

function cluster(orientation,position,range,members) {
    this.orientation = orientation;
    this.position = position;
    this.range = range;
    this.members = members;
    this.lines = new Array(members);
}

function get_width() {
    return Math.ceil(Math.random() * max_width);
}

function get_z(j,i) {
    return Math.round(Math.random()*10000)*100 + j*10 + i;
}

function get_color() {
    new_r = Math.round((color_r*stability_r + Math.round(100*Math.random()))/(stability_r+1));
    new_g = Math.round((color_g*stability_g + Math.round(100*Math.random()))/(stability_g+1));
    new_b = Math.round((color_b*stability_b + Math.round(100*Math.random()))/(stability_b+1));
    if(new_r.toString().length<2){
        new_r = "0" + new_r;
    }
    if(new_g.toString().length<2){
        new_g = "0" + new_g;
    }
    if(new_b.toString().length<2){
        new_b = "0" + new_b;
    }
    return "#" + new_r + new_g + new_b;
}

function mood() {
    for(j=0;j<number_of_clusters;j++) {
        mood_color(j);
        mood_z(j);
        //mood_position(j);
    }
    counter++;
    if(delay<delay_medium){
        delay++;
    }
   window.setTimeout("mood();",delay); 
}

function mood_color(j) {
    for(i=0;i<clusters[j].members;i++){      
        document.getElementById("cluster" + j + "line" + i).style.background = get_color();
    }
}

function mood_z(j) {
    for(i=0;i<clusters[j].members;i++){      
        document.getElementById("cluster" + j + "line" + i).style.zIndex = get_z(j,i);
    }
}




