/* MAD Slider */
//default hidden content to hidden
addLoadEvent(defaultView);
function defaultView(){
	var sliders = getElementsByClassName("slider");
	for ( var i=0; i < sliders.length; i++){
		sliders[i].style.left = '245px';
	}
}
function closeEmAll(){//only 1 can be open at time. this should change back
	var sliders = getElementsByClassName("slider");
	for ( var i=0; i < sliders.length; i++){
		var sliderIDs = sliders[i].id;
		closeBox(sliderIDs);
	}
}
//get all slideboxes on page, attach onclick event to all switches, add parent id as parameter to slide, slide only if 'closed'
//or if 'open'. Removed onmouseover event and 'slideBack' function 03/06/08
/*addLoadEvent(slideBox);
function slideBox(){
	var switches = getElementsByClassName("switch");
	if (switches.length < 0) return false;
	for ( var i=0; i < switches.length; i++){
		switches[i].onclick = function(){
			if(this.parentNode.style.left == '245px'){
				slide(this.parentNode.id);
			}
			if(this.parentNode.style.left == '0px'){
				closeBox(this.parentNode.id);
			}
		return false;
		}
	}
}*/
//dodgy hack that will be undone with any luck
addLoadEvent(slideBox);
function slideBox(){
	var switches = getElementsByClassName("switch");
	if (switches.length < 0) return false;
	for ( var i=0; i < switches.length; i++){
		switches[i].onclick = function(){
			//on open, close others
			if(this.parentNode.style.left == '245px'){
				closeEmAll();//close all of them
				slide(this.parentNode.id);//open the required box
				
			}
			if(this.parentNode.style.left == '0px'){
				closeBox(this.parentNode.id);
			}
		return false;
		}
	}
}

//make magic
function slide(parentBox){
	var box1 = $(parentBox);
	var switcher = box1.getElementsByClassName("switch");

	if (box1.movement){
		clearTimeout(box1.movement);
	}
	var xpos = parseInt(box1.style.left);
	if (xpos == 0){
		for (var i=0; i<switcher.length; i++){
			switcher[i].style.backgroundPosition = '0px -210px';
		}
		return true;
	}
	if (xpos > 0){
		var dist = Math.floor((0 - xpos) / 2);
		xpos = xpos + dist;
	}
	box1.style.left = xpos + "px";
	var repeat = "slide('"+parentBox+"',10)";
	box1.movement = setTimeout(repeat, 10);

}
// reverses slide function. look at merging with slider
function closeBox(parentBox){
	var box1 = $(parentBox);
	var switcher = box1.getElementsByClassName("switch");
	
	if (box1.movement){
		clearTimeout(box1.movement);
	}
	var xpos = parseInt(box1.style.left);
	if (xpos == 245){
		for (var i=0; i<switcher.length; i++){
			switcher[i].style.backgroundPosition = '0px 0px';
		}
		return true;
	}
	if (xpos == 0){ xpos = 1; }
	if (xpos < 245){
		var dist = Math.ceil((245 - xpos) / 5);
		xpos = xpos + dist;
	}
	box1.style.left = xpos + "px";
	var repeat = "closeBox('"+parentBox+"',10)";
	box1.movement = setTimeout(repeat, 10);
		
}