var links; var pics; var slides; var opacity = 1; var stopShow; var items = new Array(); var http = getHTTPObject(); var t1; var t2; var t3;
window.onload = init;
function init() {
	if(!document.getElementById) {return false;}
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	if(document.getElementsByTagName("body")[0].className == "project") {stopShow = document.getElementById("stopShow"); startShow();}
	if((document.getElementsByTagName("body")[0].className == "portfolio") || (document.getElementsByTagName("body")[0].className == "projects")) {doEffects();}
	if(document.getElementsByTagName("body")[0].className == "home") {doFeatured();}
	if(document.getElementsByTagName("body")[0].className == "contact") {emailCloak(); expandForms(); bombForms();}
	if(document.getElementsByTagName("body")[0].className == "prj uibits") {roundUp();}
	doRollovers();
}
function startShow() {
	slides = new slideshow("slides");
	links = document.getElementById("screenshots").getElementsByTagName("a");
	pics = document.getElementById("photo").getElementsByTagName("div");
	for(var q = 0; q < links.length - 1; q++) {slides.add_slide(new slide(links[q].src));}
	slides.transition(1);
	if(links.length > 1) {slides.play();}
}
function slide(src) {this.src = src;}
function slideshow(ssname) { //inspired by Patrick Fitzgerald - www.barelyfitz.com
	this.name = ssname;
	this.slides = new Array();
	this.current = 0;
	this.prev = -1;
	this.timeoutid = 0;
	this.stopped = false;
	this.add_slide = function(slide) {
		var i = this.slides.length;
		this.slides[i] = slide;
	}
	this.play = function() {
		this.pause();
		this.stopped = false;
		stopShow.innerHTML = "Stop";
		stopShow.onclick = function() {slides.fullStop();}
		stopShow.onmouseover = function() {window.status = "parar"; return true;}
		this.timeoutid = setTimeout(this.name + ".loop()", 5000);
	}
	this.pause = function() {
		if(this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
	}
	this.fullStop = function() {
		this.pause();
		this.stopped = true;
		stopShow.innerHTML = "Play";
		stopShow.onmouseover = function() {window.status = "avanzar"; return true;}
		stopShow.onclick = function() {slides.transition(1); slides.play();}
	}
	this.update = function() {
		links[this.current].parentNode.className = "active";
		pics[this.current].className = "active";
		if(!this.stopped) {
			if(this.current > 0) {this.prev = this.current - 1;}
			else {this.prev = this.slides.length - 1;}
		}
		if(this.prev != -1) {links[this.prev].parentNode.className = "";}
		this.transition(1);
	}
	this.transition = function(i) {
		if (typeof i != 'undefined') {opacity = i;}
		if(opacity <= 10) {
			if(this.prev != -1) {pics[this.prev].style.opacity = ((10-opacity)*10)/100; pics[this.prev].style.filter = "alpha(opacity=" + (10-opacity)*10 + ")";}
			else if(this.prev == -1) {pics[this.current].className = "active";}
			pics[this.current].style.opacity = (opacity*10)/100; pics[this.current].style.filter = "alpha(opacity=" + opacity*10 + ")";
			++opacity;
			setTimeout(this.name + ".transition()", 75);
		}
		else {
			if(this.prev != -1) {pics[this.prev].className = "";}
			return true;
		}
	}
	this.next = function(n) {
		if(n < this.slides.length && n >= 0) {this.current = n;}
		this.update();
	}
	this.loop = function() {
		if(this.current < this.slides.length - 1) {next_slide = ++this.current;}
		else {next_slide = 0;}
		this.next(next_slide);
		this.play();
	}
	this.gotoSlide = function(n) {
		if((n < this.slides.length) && (n >= 0)) {
			this.fullStop();
			if(n != this.current) {
				this.prev = this.current;
				this.current = n;
			}
		}
		this.update();
	}
}
function doRollovers() {
	var images = document.getElementsByTagName("img");
	var buttons = document.getElementsByTagName("input");
	preloads = new Object();
	for(var i = 0; i < images.length; i++) {
		var imageOffSrc = images[i].src + "";
		if(imageOffSrc.indexOf("_off") != -1){
			var imageOverSrc = imageOffSrc.replace(/_off/g, "_over");
			preloads['imageOff_' + i] = new Image();
			preloads['imageOff_' + i].src = imageOffSrc;
			preloads['imageOver_' + i] = new Image();
			preloads['imageOver_' + i].src = imageOverSrc;
			images[i].onmouseover = function(){this.src = this.src.replace(/_off/g, "_over");}
			images[i].onmouseout = function(){this.src = this.src.replace(/_over/g, "_off");}
		}
	}
	for (var i = 0; i < buttons.length; i++){
		if(buttons[i].src){
			var imageOffSrc = buttons[i].src + "";
			if(imageOffSrc.indexOf("_off") != -1){
				var imageOverSrc = imageOffSrc.replace(/_off/g, "_over");
				preloads['imageOff_' + i] = new Image();
				preloads['imageOff_' + i].src = imageOffSrc;
				preloads['imageOver_' + i] = new Image();
				preloads['imageOver_' + i].src = imageOverSrc;
				buttons[i].onmouseover = function(){this.src = this.src.replace(/_off/g, "_over");}
				buttons[i].onmouseout = function(){this.src = this.src.replace(/_over/g, "_off");}
			}
		}
	}
}
function gotoNews(obj, n) {
	var links = obj.parentNode.parentNode.getElementsByTagName("li");
	for(var q = 0; q < links.length - 1; q++) {links[q].className = "";}
	obj.parentNode.className = "active";
	var news = document.getElementById("n");
	if(news.style.marginLeft != 0) {var x = parseInt(news.style.marginLeft.replace(/px/g, "")); x = -x;}
	else {var x = 0;}
	--n;
	var newx = n*470;
	slideIt(x, newx);
}
function slideIt(x, newx) { //inspired by Jeremy Keith - www.adactio.com
	if (newx > x) {x += Math.ceil((newx-x)/2);}
	else if (newx < x) {x += Math.floor((newx-x)/2);}
	else if (newx == x) {return true;}
	var obj = document.getElementById("n");
	obj.style.marginLeft = -x + 'px';
	var time = setTimeout('slideIt('+x+', '+newx+')', 75);
}
function doEffects() {
	var links = document.getElementById("portfolio").getElementsByTagName("a");
	for(var q = 0; q < links.length; q++) {
		links[q].onmouseover = function() {this.getElementsByTagName("img")[0].src = this.getElementsByTagName("img")[0].src.replace(/_off/g, "_over");}
		links[q].onmouseout = function() {this.getElementsByTagName("img")[0].src = this.getElementsByTagName("img")[0].src.replace(/_over/g, "_off");}
	}
}
function getHTTPObject() { 
	var xmlhttp; 
	/*@cc_on 
	@if (@_jscript_version >= 5) 
	try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (E) {
			xmlhttp = false; 
		} 
	} 
	@else 
	xmlhttp = false; 
	@end @*/ 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/xml");
		} catch (e) { 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
}
function doFeatured() {
	http.open("GET", "/ui/data/featured.xml", true);
	http.onreadystatechange = startFeatured;
	http.send(null);
}
function startFeatured() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			items = xmlDocument.getElementsByTagName("workitem");
			var preloads = new Object();
			for(var w = 0; w < items.length; w++) {
				preloads[w] = new Image();
				preloads[w].src = items[w].getAttribute("imghref") + "00_off.jpg";
			}
			randomNo = Math.floor(Math.random()*items.length);
			t1 = new featured("t1", 0);
			t2 = new featured("t2", 1);
			t3 = new featured("t3", 2);
			for(var q = 0; q < 3; q++) {
				if(randomNo < items.length-1) {++randomNo;}
				else{randomNo = 0;}
				t1.add_slide(randomNo);
				if(randomNo < items.length-1) {++randomNo;}
				else{randomNo = 0;}
				t2.add_slide(randomNo);
				if(randomNo < items.length-1) {++randomNo;}
				else{randomNo = 0;}
				t3.add_slide(randomNo);
			}
			document.getElementById("portfolio").onmouseover = featuredOn;
			document.getElementById("portfolio").onmouseout = featuredOff;
			playFeatured(1);
		}
	}
}
function featuredOff(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	var tg = (window.event) ? e.srcElement : e.target;
	try {
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while (reltg.id != "portfolio" && reltg.nodeName != 'BODY') {reltg = reltg.parentNode;}
		if (reltg.id == "portfolio") {return;}
	} catch(d) {}
	playFeatured();
}
function featuredOn(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	pauseFeatured();
}
function playFeatured(begin) {
	setTimeout('t1.play('+begin+')', 0);
	setTimeout('t2.play('+begin+')', 250);
	setTimeout('t3.play('+begin+')', 500);
}
function pauseFeatured() {
	t1.pause(); t2.pause(); t3.pause();
}
function featured(fname, pos) {
	this.name = fname;
	this.pos = pos;
	this.slides = new Array();
	this.timeoutid = 0;
	this.current = 0;
	this.prev = -1;
	this.stopped = false;
	this.opct = 1;
	this.add_slide = function(slide) {
		var i = this.slides.length;
		this.slides[i] = slide;
	}
	this.pause = function() {
		if(this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
	}
	this.play = function(i) {
		this.pause();
		this.stopped = false;
		if(typeof i != 'undefined') {this.timeoutid = setTimeout(this.name + ".loop()", 0);}
		else{this.timeoutid = setTimeout(this.name + ".loop()", 7500);}
	}
	this.next = function(n) {
		if(n < this.slides.length && n >= 0) {this.current = n;}
		this.update();
	}
	this.loop = function() {
		if(this.current < this.slides.length - 1) {next_slide = ++this.current;}
		else {next_slide = 0;}
		this.next(next_slide);
		this.play();
	}
	this.update = function() {
		var newImg = document.createElement('img');
		newImg.src = items[this.slides[this.current]].getAttribute("imghref") + "00_off.jpg";
		newImg.alt = items[this.slides[this.current]].getAttribute("title");
		newImg.className = "hidden";
		newImg.onmouseover = function(){
			this.src = this.src.replace(/_off/g, "_over");
			/*this.parentNode.parentNode.getElementsByTagName("span")[0].style.display = "block";*/
		}
		newImg.onmouseout = function(){
			this.src = this.src.replace(/_over/g, "_off");
			/*this.parentNode.parentNode.getElementsByTagName("span")[0].style.display = "none";*/
		}
		var newLink = document.createElement('a');
		newLink.href = items[this.slides[this.current]].getAttribute("href");
		var newTxt = document.createTextNode(items[this.slides[this.current]].getAttribute("title"));
		/*var newSpan = document.createElement('span');
		newSpan.className = "ovr";
		newSpan.onmouseover = function() {this.parentNode.getElementsByTagName("img")[0].src = this.parentNode.getElementsByTagName("img")[0].src.replace(/_off/g, "_over");}
		newSpan.onmouseout = function() {this.parentNode.getElementsByTagName("img")[0].src = this.parentNode.getElementsByTagName("img")[0].src.replace(/_over/g, "_off");}
		newSpan.appendChild(newTxt);*/
		newLink.appendChild(newImg);
		var where = document.getElementById("portfolio").getElementsByTagName("p")[this.pos];
		/*if(where.getElementsByTagName("span")[0]) {where.removeChild(where.getElementsByTagName("span")[0]);}
		where.appendChild(newSpan);*/
		where.appendChild(newLink);
		this.opct = 1;
		this.transition(this.opct);
	}
	this.transition = function(i) {
		if (typeof i != 'undefined') {this.opct = i;}
		var prev = document.getElementById("portfolio").getElementsByTagName("p")[this.pos].getElementsByTagName("img")[0];
		var next = document.getElementById("portfolio").getElementsByTagName("p")[this.pos].getElementsByTagName("img")[1];
		if(this.opct <= 10) {
			prev.style.opacity = ((10-this.opct)*10)/100; prev.style.filter = "alpha(opacity=" + (10-this.opct)*10 + ")";
			next.style.opacity = (this.opct*10)/100; next.style.filter = "alpha(opacity=" + this.opct*10 + ")";
			++this.opct;
			setTimeout(this.name + ".transition()", 50);
		}
		else {
			prev.parentNode.parentNode.removeChild(prev.parentNode);
			return true;
		}
	}
}



///////////////////////////// Basura /////////////////////////



function validateComment() {
	document.getElementById("subject").style.display = "none";
	document.getElementById("comment_form").onsubmit = doCommentValidation;
}
function doCommentValidation() {
	var tmp = document.getElementsByTagName("span");
	for(var q = 0; q < tmp.length; q++) {if(tmp[q].className == "errorMsg") {tmp[q].parentNode.removeChild(tmp[q]);}}
	if(document.getElementById("name").value) {}
	else {
		highlightError("name", "Please enter your name!");
		return false;
	}
	if(document.getElementById("email").value) {
		var str = document.getElementById("email").value;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(str) == false) {
			highlightError("email", "The email address doesn't appear to be valid!");
			return false;
		}
	}
	else {
		highlightError("email", "Please enter your email address!");
		return false;
	}
	if(document.getElementById("comment").value) {}
	else {
		highlightError("comment", "Please enter your comment!");
		return false;
	}
	if(document.getElementById("subject").value) {
		highlightError("subject", "Sorry, it looks like you are an evil robot! Please try again or use the contact page to let me know otherwise.");
		return false;
	}
	return true;
}


function highlightError(id, msg) {
	var obj = document.getElementById(id);
	var span = document.createElement("span");
	span.className = "errorMsg";
	span.appendChild(document.createTextNode(" "+msg));
	if(id != "subject") {
		var labels = document.getElementsByTagName("label");
		for(var q = 0; q < labels.length; q++) {if(labels[q].htmlFor == id) {labels[q].appendChild(span);}}
	}
	else if(id == "subject") {
		obj.parentNode.appendChild(span);
	}
}


function validateContactForm(_name, _email, _msg, _subject) {
	var tmp = document.getElementsByTagName("span");
	for(var q = 0; q < tmp.length; q++) {if(tmp[q].className == "errorMsg") {tmp[q].parentNode.removeChild(tmp[q]);}}
	if(document.getElementById(_name).value) {}
	else {
		highlightError(_name, "Please enter your name!");
		return false;
	}
	if(document.getElementById(_email).value) {
		var str = document.getElementById(_email).value;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(str) == false) {
			highlightError(_email, "The email address doesn't appear to be valid!");
			return false;
		}
	}
	else {
		highlightError(_email, "Please enter your email address!");
		return false;
	}
	if(document.getElementById(_msg).value) {}
	else {
		highlightError(_msg, "Please enter your comment!");
		return false;
	}
	if(document.getElementById(_subject).value) {
		highlightError(_subject, "Sorry, it looks like you are an evil robot! Please try again or use the email address on the left.");
		return false;
	}
	return true;
}


function emailCloak() {
	var alltags = document.all? document.all : document.getElementsByTagName("*");
	for (i=0; i < alltags.length; i++) {
	  	if (alltags[i].className == "emailCloak") {
			var oldText = alltags[i].firstChild;
			var emailAddress = alltags[i].firstChild.nodeValue;
			var user = emailAddress.substring(0, emailAddress.indexOf("("));
			var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
			var newText = user+"@"+website;
			var a = document.createElement("a");
			a.href = "mailto:"+newText;
			var address = document.createTextNode(newText);
			a.appendChild(address);
			alltags[i].replaceChild(a,oldText);
		}
	}
}

function bombForms() {
	document.getElementById("subject").style.display = "none";
	document.getElementById("p_subject").style.display = "none";
}
function expandForms() {
	var business = document.getElementById("business"); var businessF = document.getElementById("business_form");
	var personal = document.getElementById("personal"); var personalF = document.getElementById("personal_form");
	business.onclick = function() {
		if((businessF.style.display == "none") || (!businessF.style.display)) {
			businessF.style.display = "block";
			business.getElementsByTagName("img")[0].src = "/ui/media/images/contact/business_active.gif";
			personalF.style.display = "none";
			personal.getElementsByTagName("img")[0].src = "/ui/media/images/contact/personal_off.gif";
		}
		else {
			businessF.style.display = "none";
			business.getElementsByTagName("img")[0].src = "/ui/media/images/contact/business_off.gif";
		}
	}
	personal.onclick = function() {
		if((personalF.style.display == "none") || (!personalF.style.display)) {
			personalF.style.display = "block";
			personal.getElementsByTagName("img")[0].src = "/ui/media/images/contact/personal_active.gif";
			businessF.style.display = "none";
			business.getElementsByTagName("img")[0].src = "/ui/media/images/contact/business_off.gif";
		}
		else {
			personalF.style.display = "none";
			personal.getElementsByTagName("img")[0].src = "/ui/media/images/contact/personal_off.gif";
		}
	}
}
function roundUp() {
	var divs = document.getElementById("uibits").getElementsByTagName("div");
	for(var q = 0; q < divs.length; q++) {
		if(divs[q].offsetWidth <= 240) {divs[q].style.width = "240px";}
		else if((divs[q].offsetWidth > 240) && (divs[q].offsetWidth <= 485)) {divs[q].style.width = "485px";}
		else if(divs[q].offsetWidth > 485) {divs[q].style.width = "730px";}
	}
}