/*
** VERSION 1.11
** DATE START: March 13th 2011
****
** Main functions included are:
****
** -> changeframe_galleri(sp, changeType, minCounter, maxCounter)
** -> hideAll(elementId, exception)
** -> addClassToActive(parentId, tagType, newClass, exception)
** -> swapBgImage(elementId, image, path)
** -> toggleBgImage(id, img1, img2, path)
** -> toggleAutoplay(obj, minCounter, maxCounter)
** -> autoplay(obj, minCounter, maxCounter, play)
**
****
** CHANGES:
** -> 1.10: adapted to gallery_class.php
** -> 1.11: IE bugfix
****
*******************
** documentation
** 
*/
function changeframe_galleri(sp, changeType, minCounter, maxCounter)
{
	var newId;
	var obj = sp.currentPanel;
	var oldId = obj.id;
	var counter = Number( oldId.substr(1) );
	if(changeType == "next")
	{
		newId = counter+1;
		if(newId > maxCounter)
			newId = maxCounter;
	}
	if(changeType == "previous")
	{
		newId = counter-1;
		if(newId < minCounter)
			newId = minCounter;
	}
	if(changeType == "first")
		newId = minCounter;
	if(changeType == "last")
		newId = maxCounter;
	if(typeof changeType == "number")
	{
		newId = changeType;
		if(newId > counter)
			changeType = "next";
		if(newId < counter)
			changeType = "previous";
	}
	sp.showPanel("p"+newId);
	hideAll("DIV_galleri", "img_"+newId);

	eval("fadeElement_img_"+newId+".start();");
	if(changeType == "next" || changeType == "previous")
		eval("moveElement_img_"+newId+"_"+changeType+".start();");
	
	if( typeof(toggleAutoplay.value) != "undefined" && toggleAutoplay.value == "stop" && newId == maxCounter)
	{
		toggleAutoplay(sp, minCounter, maxCounter);
		toggleAutoplay.value = "start";
	}
		

	//addClassToActive('contentGroup_sidebar', 'img', 'active', 'img_sidebar_'+newId);
}

function hideAll(elementId, exception)
{
	var length = document.getElementById(elementId).childNodes.length;
	for(var i=0; i<length; i++)
	{
		var newId = document.getElementById(elementId).childNodes.item(i).id;
		if(newId != null)
			document.getElementById(elementId).childNodes.item(i).style.display = "none";
	}	
	document.getElementById(exception).style.opacity = "0.0"; //Firefox
	document.getElementById(exception).style.filter = "alpha(opacity=0)"; //IE
	document.getElementById(exception).style.display = "block";
}

function addClassToActive(parentId, tagType, newClass, exception)
{
	var length = document.getElementById(parentId).getElementsByTagName(tagType).length;
	for(var i=0; i<length; i++)
	{
		var oldClass = document.getElementById(parentId).getElementsByTagName(tagType).item(i).className;
		if(oldClass == newClass)
			document.getElementById(parentId).getElementsByTagName(tagType).item(i).className = "";
	}	
	document.getElementById(exception).className = newClass;
}

function swapBgImage(elementId, image, path)
{
	document.getElementById(elementId).style.backgroundImage = 'url('+path+image+')';
}

function toggleBgImage(id, img1, img2, path)
{
	var current = document.getElementById(id).style.backgroundImage;
	var cur_array = current.split("/");
	var cur_img = cur_array.pop();
	cur_img = cur_img.replace( '")', "");
	cur_img = cur_img.replace( ')', ""); //IE fix
	if(cur_img == img2)
		swapBgImage(id, img1, path); 
	else
		swapBgImage(id, img2, path); 
}

function toggleAutoplay(obj, minCounter, maxCounter)
{
	//abort action if at last img already
	if(typeof(obj) == "string")
		var curId = eval(obj+".currentPanel.id");
	else
		var curId = obj.currentPanel.id;
		
	var curCounter = Number( curId.substr(1) );
	
	if(maxCounter == curCounter)
	{
		swapBgImage('a_play', 'gallery_play_15.png', 'billeder/controls/');
		return;
	}
	
	//setting default value
	toggleBgImage('a_play', 'gallery_play_15.png', 'gallery_stop_15.png', 'billeder/controls/');
	if( typeof(toggleAutoplay.value) == "undefined")
	{
		toggleAutoplay.value = "stop";
		toggleAutoplay.auto = new autoplay(obj, minCounter, maxCounter, "nej");
		toggleAutoplay.auto.start();
	}
	else
	{
		//behaviour based on toggle.value
		if(toggleAutoplay.value == "start")
		{
			toggleAutoplay.value = "stop";
			toggleAutoplay.auto.start();
		}
		else
		{
			toggleAutoplay.value = "start";
			toggleAutoplay.auto.stopTimeout();
			//stopCount();
		}
	}
}

//autoplay objekt
var t;
function autoplay(obj, minCounter, maxCounter, play)
{
	//constructor
	this.play = play;
	this.obj = obj;
	this.minCounter = Number(minCounter);
	this.maxCounter = Number(maxCounter);
	this.speed = 5000;
	
	//bruges ikke som så endnu, men er rare informationer at kunne trække ud
	this.oldId = "";
	this.counter = "";
	
	//starter autoplay
	this.start = function()
	{
		oldId = eval(obj+".currentPanel.id");
		counter = Number(oldId.substr(1));
		this.oldId = oldId;
		this.counter = counter;
		if(counter < maxCounter)
		{
			changeframe_galleri(eval(obj), "next", minCounter, maxCounter);
			if( (maxCounter - counter) !== 1 )
			{
				t=setTimeout("autoplay('"+this.obj+"', '"+this.minCounter+"', '"+this.maxCounter+"', 'ja')", this.speed);
			}
		}
	}
	
	//stopper autoplay
	this.stopTimeout = function()
	{
		clearTimeout(t);
	}
	
	//gentagelsesdelen
	if(this.play == "ja")
	{
		this.start();
	}
}

