var root = "http://www.stukafest.nl/";
var preloadImgs = new Array();

$(document).ready(function()
{
	$(window).bind("resize", resizeWindow);
	
	setLayout();
	initTweets();
	preloadImages();
	setFocus();
	
	var version = getFlashVersion().split(',').shift();
	
	if (document.getElementById("signpost") && version >= 9)
	{
		showFlash();
	}
	
	if (typeof initMap == "function")
	{
		initMap();
	}
	
	$("#gallery a").lightBox(
	{
		imageLoading: "../../images/lightbox/lightbox-ico-loading.gif",
		imageBtnPrev: "../../images/lightbox/lightbox-btn-prev.gif",
		imageBtnNext: "../../images/lightbox/lightbox-btn-next.gif",
		imageBtnClose: "../../images/lightbox/lightbox-btn-close.gif",
		imageBlank: "../../images/lightbox/lightbox-blank.gif",
		txtImage: "Afbeelding",
		txtOf: "van"
	});
	
	setLinksTarget();
});

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].className != "file")
		{
			document.links[i].removeAttribute("target");
		}
		
		else
		{
			document.links[i].setAttribute("target", "_blank");
		}
	}
}

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");
	}
}

function setFocus()
{
	var thisFocus = document.getElementById("thisFocus");
	
	if (thisFocus)
	{
		thisFocus.focus();
	}
}

function resizeWindow(e)
{
	setLayout();
}

function setLayout()
{
	if ($("#wrapperHome").height() > $(window).height())
	{
		$("#wrapperHome").css("top", "0");
		$("#wrapperHome").css("margin-top", "0");
	}
	
	else
	{
		$("#wrapperHome").css("top", "50%");
		$("#wrapperHome").css("margin-top", "-345px");
	}
}

function initTweets()
{
	$("#tweets").tweet({
		avatar_size: 26,
		count: 3,
		query: "#stukafest2012 OR #stukafest",
		refresh_interval: 30,
		loading_text: "Tweets laden...",
		template: "{avatar}{user}{text}{time}"
	});
}

function getFlashVersion()
{
	// ie
	try
	{
		try
		{
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			
			try
			{
				axo.AllowScriptAccess = 'always';
			}
			
			catch(e) { return '6,0,0'; }
		}
		
		catch(e) {}
		
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
		
	// other browsers
	}
	
	catch(e)
	{
		try
		{
			if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
			{
				return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
			}
		}
		
		catch(e) {}
	}
	
	return '0,0,0';
}

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;
}

function showFlash()
{
	$("#noFlash").hide();
	
	$('#homeNav').flash({
		src: "flash/signpost.swf?id=5",
		width: 341,
		height: 431,
		wmode: "transparent"
	});
}

function checkAllCities()
{
	var cities = document.getElementById("cities");
	var checks = cities.getElementsByTagName("input");
	
	for (var i = 0; i < checks.length; i++)
	{
		if (checks[i].type == "checkbox")
		{
			checks[i].checked = true;
			checks[i].disabled = true;
		}
	}
}

function enableAllCities()
{
	var cities = document.getElementById("cities");
	var checks = cities.getElementsByTagName("input");
	
	for (var i = 0; i < checks.length; i++)
	{
		if (checks[i].type == "checkbox")
		{
			checks[i].disabled = false;
		}
	}
}
