var imgObj, imgRatio, arrHeader, arrWidth, arrHeight, headerTimeout, textCollapsible, menuCollapsible;
var currentHeader = 0;
var imgIndex = 1;
var imgDirection = 1;
var imgRunning = 1;
var imgTimeout = 5000;

function getWindowWidth() {
	var x = 0;
	if (self.innerHeight) {
		x = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
	} else if (document.body) {
		x = document.body.clientWidth;
	}
	return x;
}

function getWindowHeight() {
	var y = 0;
	if (self.innerHeight) {
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		y = document.body.clientHeight;
	}
	return y;
}

function bgInit() {
	var winWidth = getWindowWidth();
	var winHeight = getWindowHeight();
	setDetailDiv(winHeight);
	winWidth = parseInt(winWidth / 2) - 16;
	winHeight = parseInt(winHeight / 2) - 16;
	winWidth += 'px';
	winHeight += 'px';
	$('bgImg1').style.left = winWidth;
	$('bgImg1').style.top = winHeight;
	arrHeader = $('arrHeader').value;
	arrWidth = $('arrWidth').value;
	arrHeight = $('arrHeight').value;
	if (arrHeader != '') {
		arrHeader = arrHeader.split(',');
		arrWidth = arrWidth.split(',');
		arrHeight = arrHeight.split(',');
		changeBackground(-1);
	}
}

function setDetailDiv(height) {
	if ($('projectDetail')) {
		var h = height - 165;
		h += 'px';
		$('projectDetail').style.maxHeight = h;
	}
	if ($('contentDiv')) {
		var h = height - 125;
		h += 'px';
		$('contentDiv').style.maxHeight = h;
	}
}

function changeBackground(index) {
	var previousHeader = currentHeader;
	if (index == -1) {
		currentHeader += imgDirection;
		if (currentHeader > arrHeader.length) currentHeader = 1;
		if (currentHeader == 0) currentHeader = arrHeader.length;
	} else {
		clearTimeout(headerTimeout);
		currentHeader = index;	
	}
	if (previousHeader == currentHeader) {
		clearTimeout(headerTimeout);
		return;
	}
	var imgObjOld = $('bgImg' + imgIndex);
	imgIndex = (imgIndex == 1)?imgIndex = 2:imgIndex = 1;
	imgObj = $('bgImg' + imgIndex);
	var FX = new Fx.Tween(imgObj,{duration: 500}).set('opacity', 0);
	imgObj.style.zIndex = 1;
	imgObjOld.style.zIndex = 0;
	var images = ['assets/bg/' + arrHeader[currentHeader - 1]];
	var loader = new Asset.images(images, {
		onComplete: function() {
			images.each(function(im) {
				imgObj.src = im;
				var imgWidth = parseInt(arrWidth[currentHeader - 1]);
				var imgHeight = parseInt(arrHeight[currentHeader - 1]);
				imgRatio = imgWidth / imgHeight;
				changeSize();
				FX.start('opacity', 0, 1);
				//FX.set('opacity', 1);
				$('imageBlock' + currentHeader).src = 'assets/images-home/blockImgActive.png';
				if (previousHeader > 0) $('imageBlock' + previousHeader).src = 'assets/images-home/blockImg.png';
				headerTimeout = setTimeout('changeBackground(-1)', imgTimeout);
			});
		}
	});
}

function changeSize() {
	if (imgObj) {
		var winWidth = getWindowWidth();
		var winHeight = getWindowHeight();
		setDetailDiv(winHeight);
		var winRatio = winWidth / winHeight;
		if (winRatio > imgRatio) {
			var imgWidth = winWidth;
			var imgHeight = (imgRatio > 1)?parseInt(imgWidth / imgRatio):parseInt(imgWidth * imgRatio);
		} else {
			var imgHeight = winHeight;
			var imgWidth = (imgRatio < 1)?parseInt(imgHeight / imgRatio):parseInt(imgHeight * imgRatio);
		}
		var imgX = 0;
		var imgY = 0;
		if (imgWidth > winWidth) imgX = 0 - parseInt((imgWidth - winWidth) / 2)
		if (imgHeight > winHeight) imgY = 0 - parseInt((imgHeight - winHeight) / 2)
		var w = imgWidth + 'px';
		var h = imgHeight + 'px';
		var x = imgX + 'px';
		var y = imgY + 'px';
		imgObj.style.left = x;
		imgObj.style.top = y;
		imgObj.style.width = w;
		imgObj.style.height = h;
	}
}
