var current_main;
var current_button;
var sHeight = screen.availHeight;
var sWidth = screen.availWidth;
var mWidth = sWidth - 590;
var mHeight = 400;


function eventTrigger (e) {
    if (! e)
        e = event;
    return e.target || e.srcElement;
}


function increase_z(e){
	var obj = eventTrigger (e);
	while (obj.className != 'entry'){
		obj = obj.parentNode;
	}
	current_top++
	obj.style.zIndex = current_top;
}

function vanish(nr){
	var this_box = document.getElementById(nr);
	try {
		var box_index = boxes.indexOf(nr);
	} catch(err) {
		var box_index = js_box_index(nr);
	}

	var removed_box = boxes.splice(box_index, 1);
	var removed_button = buttons.splice(box_index, 1);
	this_box.style.display = 'none';
	if (this_box == current_main){
		redistribute();
	} else {
		 distribute();
	}
}

function js_box_index(v){
	for(var i = boxes.length; i-- && boxes[i] !== v;);
	return i;
};

function to_main(nr,but){

	box = document.getElementById(nr);
	button = document.getElementById(but);

	if (current_main == box){
		box.style.width = "300px";
		box.style.height = "300px";
		current_button.innerHTML = '&gt;';
		box.style.overflow = 'hidden';
		box.scrollTop = 0-box.scrollHeight;
		redistribute(nr);
	} else {
	
		var c_left = box.style.left;
		var c_top = box.style.top;
		var c_width = box.style.width;
		var c_height = box.style.height;
		var c_zindex= box.style.zIndex;
		current_main.style.left = c_left;
		current_main.style.top = c_top;
		current_main.style.width = c_width;
		current_main.style.height = c_height;
		current_main.style.zIndex = c_zindex;
		current_main.style.overflow = 'hidden';
		current_main.scrollTop = 0-current_main.scrollHeight;
		current_button.style.display = 'inline';
		current_button.innerHTML = '&gt;';
		button.innerHTML = '&lt;';
		box.style.left = "550px";
		box.style.top = "30px";
		box.style.width = mWidth + "px";
		box.style.height = mHeight + "px";
		box.style.overflow = 'auto';
		box.style.zIndex = 1000;
		current_main = box;
		current_button = button;
		try {
			var box_index = boxes.indexOf(nr);
		} catch(err) {
	                var box_index = js_box_index(nr);
	        }
	        
	        var other_box =boxes.splice(box_index, 1, boxes[0]);
	        var other_button = buttons.splice(box_index, 1,buttons[0]);
		boxes.splice(0, 1, other_box);
		buttons.splice(0, 1, other_button);
	}
}


function redistribute(current_box){

	var box_count = boxes.length;
	var count = 1;
	boxes.push(boxes.shift());
	buttons.push(buttons.shift());
	if (boxes[0] == current_box){
		boxes.push(boxes.shift());
		buttons.push(buttons.shift());
		count++;
	}
	var box = document.getElementById(boxes[0]);
	while (box.style.display == 'none' && count != box_count){
		boxes.push(boxes.shift());
		buttons.push(buttons.shift());
		var box = document.getElementById(boxes[0]);
		count++;
        }
	distribute();
}

function distribute(){
	var box_count = boxes.length;
	var half = Math.ceil(box_count/2);
	var current = 0;
	var blue = '20';
	var current_width = 225;
	var flip = -1;

	for (x in boxes) {
		var box = document.getElementById(boxes[x]);

		box.style.position = 'absolute';
		box.style.zIndex = current;
		var colour = +blue + current;
		var b_colour = "#0000" + colour;
		box.style.backgroundColor = b_colour;

		if (current == 0){
			bHeight	= 30;
			bWidth = sWidth - mWidth -40;
			box.style.width = mWidth + "px";
			box.style.height = mHeight + "px";
			box.style.overflow = 'auto';
			current_button_id=buttons[0];
			current_button = document.getElementById(current_button_id);
			current_button.innerHTML = '&lt;';
			current_main = box;
			box.style.zIndex = 1000;
		} else {
			bHeight = 30 * (current -1);
			
			if (bHeight <= (mHeight - 50)){
				if (current_width > 225){
					flip = flip * -1;
				}
			} else {
				
				if (current_width > (sWidth - 340)){
					flip = flip * -1;
				}
			}

			if (current_width <= 0){
				flip = flip * -1;
				current_width = 2;
			}
			
			bWidth = current_width;
			box.style.overflow = 'hidden';
			current_width += (39 * flip);
		}
	        box.style.left = bWidth + "px";
       		box.style.top = bHeight + "px";
		current++;
	}

}

