
var headSlider = true;
var goHeadChangeTimer = false;
var noNextImage = false;


function InitHeadSlider()
{
if(!headTemplates) {	// Wolf
	headTemplates = new Array("volkswagen", "airbus", "ice", "pipeline", "Heli", "audi", "schweissen", "truck");
}
if(typeof counter == "undefined") {counter = 0;}

laHeadImages = $('HeaderPosition').getElementsByClassName('HeadImg');
SetupHeadNav();

goHeadChangeTimer = setTimeout(changeHeader, 10000);

var HeaderOp = new HelperOpacity(laHeadImages[0]);
HeaderOp.setOpacity(100);

Event.observe('HeaderPosition', 'mouseover', function() {
		window.clearTimeout(goHeadChangeTimer);
});
Event.observe('HeaderPosition', 'mouseout', function() {
	if(!noNextImage)
	goHeadChangeTimer = window.setTimeout(changeHeader, 2000);
});

// Setup Prev/Back
}

/** Opacity Manager **/
function HelperOpacity(id)
{
	this.opacity = 0;
	this.style = id.style;
	this.element = id;
	this.timer = null;
}
HelperOpacity.prototype.setOpacity = function(value)
{
	if(value == 0) {
		this.style.display = 'none';
	} else {
		this.style.display = 'block';
	}
	this.opacity = value;
	this.style.opacity = (value / 100);
	this.style.MozOpacity = (value / 100);
	this.style.KhtmlOpacity = (value / 100);
	this.style.filter = "alpha(opacity=" + value + ")";
}
HelperOpacity.prototype.Stop = function()
{
	clearInterval(this.timer);
	if(this.opacity> 0) {
		this.setOpacity(100);
	}
}
HelperOpacity.prototype.FadeIn = function(click)
{

	var _this = this;
	this.timer = window.setInterval(function() { _this.FadeInTimer(); }, 20);
}
HelperOpacity.prototype.FadeInTimer = function(speed)
{
	if(this.opacity <100) {
		this.opacity = this.opacity + 5;
		this.setOpacity(this.opacity);
		return false;
	} else {
		// TODOS after FAde In
		
		clearInterval(this.timer);
		document.getElementsByClassName('HeadImg')[0].remove();
		SetupHeadNav();
		

		return true;
	}
}

function disableHeadFade()
{
	noNextImage = true;
	window.clearTimeout(goHeadChangeTimer);
}

function enableHeadFade()
{
	noNextImage = false;
	window.setTimeout(changeHeader, 2000);
}


function changeHeader(changeDown, speed) {
	if(changeDown) {
		 if(counter <= 0) {
		 counter =  headTemplates.length -1;
		} else {
			counter = counter -1; 
		}
	} else {
		if (counter >= (headTemplates.length -1))
		{
			counter =  0;
		} else {
			counter++; 
		}
	
	}

		var url = 'app/layout/'+headPath+'/'+ headTemplates[counter] +'.tpl' ;

	new Ajax.Request(url, {
	onSuccess:function changeHead(transport){
		laHeadImages = $('HeaderPosition').getElementsByClassName('HeadImg');
		 
		 //laHeadImages[1].replace();
		 new Insertion.After(laHeadImages[0],transport.responseText);
		 
		 
		// Wolf: $('HeaderPosition').getElementsByClassName('HeadImg')[1]
		var newHeaderOp = new HelperOpacity($('HeaderPosition').getElementsByClassName('HeadImg')[1]);

		var oldHeaderOp = new HelperOpacity($('HeaderPosition').getElementsByClassName('HeadImg')[0]);

		 setHeadinfo();
		 newHeaderOp.setOpacity(0);
		 oldHeaderOp.setOpacity(100);
		 newHeaderOp.FadeIn(speed);

		 if(!noNextImage)
		goHeadChangeTimer = setTimeout(changeHeader, 10000);
		
		
		 //initLightbox();
		}
		
	});
	//$('HeadImg').style.opacity = 0;
}



function ShowNextHeadImage()
{
		disableHeadFade();
		changeHeader(false, 0);
}

function ShowPrevHeadImage()
{
		disableHeadFade();
		changeHeader(true, 0);
}

function SetupHeadNav() {
	HeadLength = document.getElementsByClassName('HeadImg').length - 1;
	
	Head = document.getElementsByClassName('HeadImg')[HeadLength];
	
	HeadNext = Head.getElementsByClassName('HeadNext')[0];
	HeadBack = Head.getElementsByClassName('HeadBack')[0];
	
	HeadBack.style.display = "block";
	HeadNext.style.display = "block";

	Effect.Fade(HeadNext, { duration: 0.7, from: 0.0, to: 0.99 });
	Effect.Fade(HeadBack, { duration: 0.7, from: 0.0, to: 0.99 });

}
