var imageCount = 0;
var currentImage = 0;
//var imageDimensions = { x: 1280, y: 1024 };
var imageDimensions = { x: 1920, y: 1080 };

var minX = 950;
var minY = 800;

var tweeningObject = false;
var tweeningStart = false;
var tweeningInterval = false;

function init(isHomepage)
{
	window.onresize = function()
	{
		var s = getWindowSize();
		if(s.x < minX)
			s.x = minX;
		if(s.y < minY)
			s.y = minY;
		
		var d = { x: s.x, y: s.y };
		var o = document.getElementById("background");

		d.y -= 46;
		d.x = imageDimensions.x / imageDimensions.y * d.y;
		
		if(d.x < s.x)
		{
			d.x = s.x;
			d.y = imageDimensions.y / imageDimensions.x * d.x;
		}
		
		o.style.left = ((s.x - d.x) / 2) + "px";
		o.style.top = (((s.y - d.y - 46) / 2) + 23) + "px";
		o.style.width = Math.ceil(d.x) + "px";
		o.style.height = Math.ceil(d.y) + "px";
	}
	window.onresize();
	
	if(isHomepage)
	{
		minX = 750;
		minY = 350;
		
		addImage("./assets/images/hero1-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg", true);
	
		addOnLoadEvent(function()
		{
			addImage("./assets/images/hero2-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
			setTimeout(function() {
				addImage("./assets/images/hero3-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero4-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero5-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
			}, 1000);
			setTimeout(function() {
				addImage("./assets/images/hero6-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero7-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero8-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
			}, 4000);
			setTimeout(function() {
				addImage("./assets/images/hero9-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero10-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero11-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
			}, 7000);
			setTimeout(function() {
				addImage("./assets/images/hero12-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero13-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero14-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
				addImage("./assets/images/hero15-" + imageDimensions.x + "x" + imageDimensions.y + ".jpg");
			}, 10000);
			
			setInterval(nextImage, 4000);
		});
	}

	addOnLoadEvent(function()
	{
		window.onresize();
		document.p = new Array();
		document.p[0] = new Image;
		document.p[0].src = "/assets/images/img_triangle-bullet-on.gif";
	});
}

function getWindowSize()
{
	var clientWidth = window.innerWidth ? window.innerWidth : 0;
	if(document.documentElement && document.documentElement.clientWidth && (!clientWidth || (clientWidth > document.documentElement.clientWidth)))
		clientWidth = document.documentElement.clientWidth;
	if(document.body && document.body.clientWidth && (!clientWidth || (clientWidth > document.body.clientWidth)))
		clientWidth = document.body.clientWidth;

	var clientHeight = window.innerHeight ? window.innerHeight : 0;
	if(document.documentElement && document.documentElement.clientHeight && (!clientHeight || (clientHeight > document.documentElement.clientHeight)))
		clientHeight = document.documentElement.clientHeight;
	if(document.body && document.body.clientHeight && (!clientHeight || (clientHeight > document.body.clientHeight)))
		clientHeight = document.body.clientHeight;
	
	return { x: clientWidth, y: clientHeight };
}

function addOnLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			func();
			if(oldonload)
				oldonload();
		}
	}
}

function addImage(img, first)
{
	var o = document.getElementById("background");
	var i = document.createElement("img");
	i.id = "image" + imageCount;
	i.src = img;
	i.alt = "";
	i.style.zIndex = 998 - imageCount;
	if(!first)
		i.style.display = "none";
	o.appendChild(i);
	imageCount++;
}

function nextImage()
{
	var d = new Date;
	var o = document.getElementById("image" + currentImage);
	currentImage++;
	if(currentImage == imageCount)
	{
		o.oldzIndex = o.style.zIndex;
		o.style.zIndex = 999;
		currentImage = 0;
	}
	var n = document.getElementById("image" + currentImage);
	if(n.oldzIndex && currentImage == (imageCount - 1))
	{
		n.style.zIndex = n.oldzIndex;
	}	
	n.style.display = "inline";

	tweeningObject = o;
	tweeningStart = d.getTime();
	tweeningInterval = setInterval(opacityTween, 50);
}

function opacityTween()
{
	var d = new Date;
	var o = (d.getTime() - tweeningStart) / 15;
	if(o >= 100)
	{
		clearInterval(tweeningInterval);
		setOpacity(tweeningObject, 100);
		tweeningObject.style.display = "none";
		return;
	}
	setOpacity(tweeningObject, 100 - o);
}

function setOpacity(obj, opacity)
{
	if(obj.style.MozOpacity != null)
		obj.style.MozOpacity = (opacity / 100) - 0.001;
	else if(obj.style.opacity != null)
		obj.style.opacity = (opacity / 100) - 0.001;
	else if(obj.style.filter != null)
		obj.style.filter = "alpha(opacity=" + opacity + ")";
}
