var clickLocked = true,
	actualProduct = false

function getById(id) {
	return document.getElementById(id);
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function introTransition() {
	var intro = getById('product-intro-grafic'),
		prods = getById('product-intro-products'),
		actual = 0, timer = 1000;
	clickLocked = true;
	setAlpha(prods, 0);
	prods.style.display = '';
	for(var i = 0; i < 21; i++) {
		window.setTimeout("setAlpha(getById('product-intro-products'), " + actual + ");", timer);
		actual += 0.05;
		timer += 60;
	}
	window.setTimeout("getById('product-intro').removeChild(getById('product-intro-grafic'));", timer);
	window.setTimeout("clickLocked = false", timer);
	actualProduct = 'product-intro-products';
}

function deleteIntrobox() {
	if(getById('product-intro')) getById('infobox').removeChild(getById('product-intro'));
}

function productTransition(id) {
	var product = getById('product-' + id),
		ref = getById('product-link-' + id),
		els = getElementsByClassName('product-link');
		actual = 0, timer = 0;
	if(!clickLocked) {
		for(var i = 0; i < els.length; i++) {
			els[i].className = 'product-link theme-colored-link';
		}
		ref.className = 'product-link active-theme-colored-link';
		clickLocked = true;
		if(actualProduct) {
			actual = 1;
			for(i = 0; i < 21; i++) {
				window.setTimeout("setAlpha(getById('" + actualProduct + "'), " + actual + ");", timer);
				actual -= 0.05;
				timer += 60;
			}
			window.setTimeout("getById('" + actualProduct + "').style.display = 'none';", timer);
			window.setTimeout("deleteIntrobox();", timer + 10);
		}
		actualProduct = 'product-' + id;
		actual = 0;
		setAlpha(product, 0);
		product.style.display = '';
		for(i = 0; i < 21; i++) {
			window.setTimeout("setAlpha(getById('product-" + id + "'), " + actual + ");", timer);
			actual += 0.05;
			timer += 60;
		}
		window.setTimeout("clickLocked = false", timer);
	}
}

function setAlpha(element, value) {
	element.style.filter = 'alpha(opacity=' + (value * 100) + ')';
	element.style.opacity = value;
}

function preloadImages(arrImages) {
	var arrObj = [];
	for(var i = 0; i < arrImages.length; i++) {
		arrObj[i] = new Image();
		arrObj[i].src = arrImages[i];
	}
}
