var root = "http://www.eldespacho.org/";
var preloadImgs = new Array();
var increaseHeight = false;
var newHeight;

window.onload = setOnLoad;

function setOnLoad()
{
	setLinksTarget();
	preloadImages();
}

function setLinksTarget()
{
	for (var i = 0; i < document.links.length; i++)
	{
		if (document.links[i].href.indexOf(root.substr(0, root.length - 1)) != -1)
		{
			document.links[i].removeAttribute("target");
		}
	}
}

function preloadImages()
{
	for (var i = 0; i < document.images.length; i++)
	{
		if ((document.images[i].parentNode.tagName == "A" || document.images[i].parentNode.tagName == "SPAN") && document.images[i].src.indexOf("_off") != -1)
		{
			preloadImgs[i]		= new Image();
			preloadImgs[i].src	= document.images[i].src.replace("_off", "_on");
		}
	}
}

function swapImage(imageId)
{
	if (document.getElementById(imageId).src.indexOf("_off") != -1)
	{
		document.getElementById(imageId).src = document.getElementById(imageId).src.replace("_off", "_on");
	}
	
	else
	{
		document.getElementById(imageId).src = document.getElementById(imageId).src.replace("_on", "_off");
	}
}

document.getElementsByClassName = function (c)
{
	
	var r = new Array()
	var j = 0
	var o = document.getElementsByTagName("*");
	
	for(i = 0; i < o.length;i++)
	{
		if (o[i].className == c)
		{
			r[j] = o[i]; j++;
		}
	}
	
	return r;
}

$(document).ready(function()
{
	$(window).bind("resize", checkBackToTop);
	
	checkBackToTop();
	
	if ($("#content").height() < $("#openCall").height())
	{
		newHeight = $("#openCall").height();
		increaseHeight = true;
		
		$("#content").height(newHeight);
	}
	
	$("#closeCall").click(function()
	{
		$("#openCall").hide();
		
		if (increaseHeight)
		{
			newHeight = $("#content").css("min-height");
			
			$("#content").animate({ height: newHeight }, 500);
		}
		
		return false;
	});
	
	$("#photoSlides").carousel();
	$("#videoSlides").carousel({ direction: "vertical", dispItems: 3 });
	
	$("#backToTop").click(function()
	{
		$.scrollTo((0),
		{
			vertical: true,
			axis: 'y',
			speed: 900
		});
	});
	
	$("#showMore").click(function()
	{
		showMoreItems(this.rel, this.rev);
		
		return false;
	});
	
	$(".videoThumb").click(function()
	{
		var thisText = this.rev;
		
		thisText = thisText.replace(/\[ul\]/g, "<ul>");
		thisText = thisText.replace(/\[\/ul\]/g, "</ul>");
		thisText = thisText.replace(/\[li\]/g, "<li>");
		thisText = thisText.replace(/\[\/li\]/g, "</li>");
		thisText = thisText.replace(/\[b\]/g, "<strong>");
		thisText = thisText.replace(/\[\/b\]/g, "</strong>");
		thisText = thisText.replace(/\[br\]/g, "<br />");
		thisText = thisText.replace(/\[url=([\w]+?:\/\/[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/g, "<a href=\"$1\" target=\"blank\">$2</a>");
		
		$("#videoTitle").html(this.title);
		$("#videoText").html(thisText);
		$("#videoFrame").attr("src", this.rel);
		
		return false;
	});
});

function checkBackToTop()
{
	if ($(document).height() > $(window).height())
	{
		$("#backToTop").removeClass("disabled");
		$("#backToTop").css("cursor", "pointer");
	}
	
	else
	{
		$("#backToTop").addClass("disabled");
		$("#backToTop").css("cursor", "");
	}
}

function showGallery()
{
	$("#photoGallery").show();
	$("#photoGalleryBg").show();
	
	$("#photoGalleryBg").click(function()
	{
		$("#photoGallery").hide();
		$("#photoGalleryBg").hide();
	});
	
	$(document).bind("keydown", function(e)
	{
		if (e.which == 27)
		{
			$("#photoGallery").hide();
			$("#photoGalleryBg").hide();
		}
	});
	
	return false;
}

function showMoreItems(current, lang)
{
	var totalItems;
	var allItems = document.getElementById("allItems").innerHTML;
	
	var ajaxRequest; // magic variable
	
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	
	catch(e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		
		catch(e)
		{
			// something went wrong
			//alert ("Your browser broke!");
			return false;
		}
	}
	
	// request data function
	ajaxRequest.onreadystatechange = function()
	{
		if (ajaxRequest.readyState == 4)
		{
			// get id from div to show results in
			var ajaxDisplay = document.getElementById("moreItems");
			
			// put results into the div
			ajaxDisplay.innerHTML += ajaxRequest.responseText;
			
			// remove "show more" button if necessary
			totalItems = document.getElementsByClassName("item").length;
			
			if (totalItems == allItems)
			{
				$("#showMore").hide();
			}
		}
	}
	
	totalItems = document.getElementsByClassName("item").length;
	var query = "?current=" + current + "&lang=" + lang + "&total=" + totalItems;
	
	ajaxRequest.open("GET", root + "db/more_items.php" + query, true);
	ajaxRequest.send(null);
}
