// JavaScript Document
/* Slide function
---------------------------------------------------------------- */

/* Global variables */
var allSlides;
var nextSlide = 3;
var currentSlide = 1;
var nextSlideHeight;
var slideHeightCounter = 1;
var slideTimer;
var smoothTimer;
var slideRunnerTimer;
var slideRunning;
var itemsContainer;
var items;
var seconds;
var itemsvisible = 2;			// Number of items visible in the container
var itemsmargin = 3;			// The margin to seperate the items
var colorActive = '#d20000';	// The control arrows (Active)
var colorDisabled = '#9e9e9e';	// The control arrows (Inactive)

// loadDynamicSlides(classname of items, items container, items general id, change af x miliseconds)

/* On load, run below functions */
function loadDynamicSlides(object,container,item,second){
	itemsContainer = container;
	items = item;
	seconds = second;
	allSlides = countSlides(object);
	if(allSlides>3){
		setTimeout("runSlide()", seconds);
		slideRunning = true;
	}
}

/* Count the number of slides */
function countSlides(object){
	var slideItems = document.getElementsByTagName('A');
	var slideItemsCount = slideItems.length;
	var numslideItems = 0;
	for(i=0;i<slideItemsCount;i++){
		if(slideItems[i].className==object){
			numslideItems++;
		}
	}
	return numslideItems;
}

/* Control: Stop the auto slide function */
function stopSlide(){
	var stopSlideTimer;
	if(!slideRunning){
		clearTimeout(slideTimer);
		clearTimeout(smoothTimer);
		clearTimeout(stopSlideTimer);
		slideRunning = false;
	}else{
		stopSlideTimer = setTimeout("stopSlide()",100);
	}
}

/* Control: Slide control (controls wether to slide or reset the slide container) */
function runSlide(){
	var wait = false;
	if(nextSlide>allSlides){
		currentSlide = 1;
		nextSlide = currentSlide + itemsvisible;
		document.getElementById(itemsContainer).style.top = '0px';
		wait = true;
		funcControl();
	}else{
		wait = false;
	}
	if(document.getElementById('item'+nextSlide)){
		nextSlideHeight = document.getElementById(items+currentSlide).offsetHeight + itemsmargin;
		if(wait){
			setTimeout("autoSlide()", seconds);
		}else{
			autoSlide();
		}
	}
}

/* Control: Slide function */
function autoSlide(){
	var containerTop = parseInt(document.getElementById(itemsContainer).style.top);
	if(slideHeightCounter==nextSlideHeight){
		slideHeightCounter = 0;
		slideTimer = setTimeout("runSlide()",seconds);
		currentSlide++;
		nextSlide++;
		slideRunning = false;
	}else{
		document.getElementById(itemsContainer).style.top = (containerTop - 1) + 'px';
		slideHeightCounter++;
		smoothTimer = setTimeout("autoSlide()", 30);
		slideRunning = true;
	}
	funcControl();
}

/* Control: Controls (up / down) function */
function funcControl(){
	var slideControlDown = document.getElementById('slideControlDown').style;
	var slideControlUp = document.getElementById('slideControlUp').style;
	if(currentSlide==allSlides-1){slideControlDown.backgroundColor=colorDisabled;}else{slideControlDown.backgroundColor=colorActive;}
	if(currentSlide==1){slideControlUp.backgroundColor=colorDisabled;}else{slideControlUp.backgroundColor=colorActive;}
}

/* Control: Manual slide function */
function slideManual(command){
	var containerTop = parseInt(document.getElementById(itemsContainer).style.top);
	if(command=='down' && !slideRunning){
		if(currentSlide+1!=allSlides){
			currentSlide++;
			nextSlide=currentSlide+1;
			nextSlideHeight = document.getElementById(items+nextSlide);
			document.getElementById(itemsContainer).style.top = (containerTop-(nextSlideHeight.offsetHeight+itemsmargin)) + 'px';
		}
	}
	if(command=='up' && !slideRunning){
		if(containerTop<0){
			currentSlide--;
			nextSlide--;
			nextSlideHeight = document.getElementById(items+nextSlide);
			document.getElementById(itemsContainer).style.top = (containerTop+(nextSlideHeight.offsetHeight+itemsmargin)) + 'px';
		}
	}
	stopSlide();
	funcControl();
}
