/****************
ImageView 2.0 | Robin Erixon @ www.robinerixon.se

May be used as long as this comment section is intact
*****************/



/**** Hantera style-attribut ****/
var Style = {
	
	setOpacity : function (element, opacity) {
		
		var Div = document.getElementById(element).style;
	
		Div.opacity			= (opacity / 100);
		Div.MozOpacity		= (opacity / 100);
		Div.KhtmlOpacity	= (opacity / 100);
		Div.filter			= 'alpha(opacity=' + opacity + ')';
	},
	
	setHeight : function (element, height) {
	
		document.getElementById(element).style.height = height + 'px';
	},
	
	setWidth : function (element, width) {
	
		document.getElementById(element).style.width = width + 'px';
	},
	
	setLeft : function (element, left) {
		
		document.getElementById(element).style.marginLeft = left + 'px';
	},
	
	setTop : function (element, top) {
		
		document.getElementById(element).style.marginTop = top + 'px';
	},
	
	setDisplay : function (element, mode) {
		
		document.getElementById(element).style.display = mode;
	}
}


var Element = {
	
	getOpacity : function (element) {
		
		return document.getElementById(element).style.opacity;
	},
	
	getStyle : function (element, getStyle) {
		
		var getDiv = document.getElementById(element);
		var returnStyle;
		
		if (document.defaultView && document.defaultView.getComputedStyle) {
			
			returnStyle = document.defaultView.getComputedStyle(getDiv, null).getPropertyValue(getStyle);
		}
		else if (getDiv.currentStyle) {
			
			getStyle = getStyle.replace(/\-(\w)/g, function (strMatch, p1) {
				return p1.upperCase();
			});
			returnStyle = getDiv.currentStyle(getStyle);
		}
		return returnStyle;
	}
}


/**** Effekter ****/
var Effects = {
	
	Object : null,
	Opacity : null,
	PoN : null,
	State : null,
	
	init : function () {
		
		this.State = true;
	},
	
	Width : function (element, speed, endW) {
		
		var targetDiv	= document.getElementById(element);
		var currWidth	= targetDiv.offsetWidth;
		var PoN			= currWidth < endW ? true : false;
		var diffWidth	= PoN ? endW - currWidth : currWidth - endW;
		
		if (PoN) {
			
			for (var i = 0; i < diffWidth; i++) {
			
				setTimeout("Style.setWidth('" + element + "', " + (parseInt(currWidth) + i) + ")", speed * i);
			}
		}
		else {
		
			for (var i = 0; i < diffWidth; i++) {
			
				setTimeout("Style.setWidth('" + element + "', " + (parseInt(currWidth) - i) + ")", speed * i);
			}
		}
	},
	
	Height : function (element, speed, endH) {
		
		var targetDiv	= document.getElementById(element);
		var currHeight 	= targetDiv.offsetHeight;
		var PoN			= currHeight < endH ? true : false;
		var diffHeight	= PoN ? endH - currHeight : currHeight - endH;
		
		if (PoN) {
		
			for (var i = 0; i < diffHeight; i++) {
			
				setTimeout("Style.setHeight('" + element + "', " + (parseInt(currHeight) + i) + ")", speed * i);
			}
		}
		else {
			for (var i = 0; i < diffHeight; i++) {
			
				setTimeout("Style.setHeight('" + element + "', " + (parseInt(currHeight) - i) + ")", speed * i);
			}
		}
	},
	
	reSize : function (element, speed, width, height) {
		
		this.Width(element, speed, width);
		this.Height(element, speed, height);
	},
	
	Fade : function (element, speed, sOp, eOp) {
		
		if (this.State == null) {
			this.init();
		}
		
		var diffOp		= sOp < eOp ? eOp - sOp : sOp - eOp;
		this.Opacity	= sOp;
		this.PoN		= sOp > eOp ? true : false;
		this.Status		= true;
		
		for (var i = 0; i < diffOp; i++) {
		
			setTimeout("Effects.AnimateFade('" + element + "')", speed * i);
		}
		
		this.Status = false;
	},
	
	AnimateFade : function (element) {
		
		Style.setOpacity(element, this.Opacity);
		
		if (this.PoN) this.Opacity--;
		else this.Opacity++;
	}
}

/****************
SlideShow Vers. 1.0 | Robin Erixon @ www.robinerixon.se
*/
var Settings = {
	
	AutoStart : true,							// Bestämmer om slideshowen ska startas automatiskt vid sidladdning.
	Target : 'SlideShow',						// ID på den img-tagg som slideshowen ska synas i.
	
	Images : new Array(							// En array med dem bilder du vill ska vara med i slideshowen. Sökvägen utgår ifrån denna JavaScript-filen.
				 
				'http://www.vadstenamusikforening.se/bildspel/flash2.jpg', 
				'http://www.vadstenamusikforening.se/bildspel/flash3.jpg', 
				'http://www.vadstenamusikforening.se/bildspel/flash4.jpg',
				'http://www.vadstenamusikforening.se/bildspel/flash5.jpg',
				'http://www.vadstenamusikforening.se/bildspel/flash6.png',
				'http://www.vadstenamusikforening.se/bildspel/flash7.jpg',
				'http://www.vadstenamusikforening.se/bildspel/flash8.jpg',
				'http://www.vadstenamusikforening.se/bildspel/flash9.jpg',
				'http://www.vadstenamusikforening.se/bildspel/flash1.jpg'
	),
	
	Delay : 5,									//  Hur många sekunder det ska dröja innan bilden byts.
	fSpeed : 5,								// Med vilken hastighet bilderna ska tonas in och ut.
	Width : 520,								// Bredd på dem bilderna som ska visas i slideshowen (px).
	Height : 254								// Höjd på dem bilderna som ska visas i slideshowen (px).
}
/*
*****************/



var SlideShow = {
	
	Container : null,
	loadedImg : null,
	Image : null,
	Delay : null,
	Active : null,
	
	init : function () {
		
		this.Container 	= document.getElementById(Settings.Target);
		this.Image		= 1;
		this.Delay		= Settings.Delay * 1000;
		
		for (var i = 0; i < Settings.Images.length; i++) {
			
			this.preLoad(Settings.Images[i]);
		}
	},
	
	execute : function () {
		
		if (!this.Container) {
			this.init();
		}
		
		this.Active = true;
		setTimeout("SlideShow.startSlide();", this.Delay);
	},
	
	startSlide : function () {
		
		if (this.Image >= Settings.Images.length) this.Image = 0;
		if (this.Active) this.fadeOut();
	},
	
	stopSlide : function () {
		
		this.Active = false;
	},
	
	fadeOut : function () {
		
		Effects.Fade(this.Container.id, Settings.fSpeed, 100, 0);
		
		setTimeout("SlideShow.fadeIn();", (Settings.fSpeed * 100));
	},
	
	fadeIn : function () {
		
		this.Container.src = Settings.Images[this.Image];
		
		Effects.Fade(this.Container.id, Settings.fSpeed, 0, 100);
		this.Image++;
		
		setTimeout("SlideShow.startSlide();", (this.Delay + (Settings.fSpeed * 100)));
	},
	
	preLoad : function (image) {
		
		var loadedImg = new Image(Settings.Width, Settings.Height);
		loadedImg.src = image;
	}
}

window.onload = function () { 
	
	if (Settings.AutoStart) SlideShow.execute();
}
