var current = 0;
var sponsor_url = new Array();
var sponsor_image = new Array();
var sponsor_title = new Array();
$(document).ready(function(){
	$("a.target_blank").attr({"target":"_blank"});
	$("div#sponsor-images img").each(function(){
		sponsor_url.push($(this).attr('alt'));
		sponsor_title.push($(this).attr('title'));
		sponsor_image.push($(this).attr('src'));
	});
	if(sponsor_url.length > 1){
		setInterval("changePhoto()",5000);
	}
});

function changePhoto(){
	current++;
	if(current == sponsor_url.length){
		current = 0;
	}
	$("img#sponsor_image").attr("src",sponsor_image[current]).attr("title",sponsor_title[current]).attr("alt",sponsor_url[current]);
	$("a#sponsor_url").attr("href",sponsor_url[current]).attr("title",sponsor_title[current]);
	$("a#sponsor_url img").attr("alt",sponsor_title[current]);
}

function getObject(id){
	obj = document.getElementById(id);
	if(obj != undefined){
		return obj;
	}
}
function changeBackground(obj, background, left){
	backpos = "center right";
	if(left == false){
		backpos = "center left";
	}
	obj.style.backgroundImage = 'url(/assets/images/' + background + ')';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = backpos;
	obj.onmouseout = function(){
		obj.style.backgroundImage = 'url(/assets/images/spacer.gif)';
		obj.style.backgroundRepeat = 'repeat';
		obj.style.backgroundPosition = 'center center';
	};
}
function redirect(url, target){
	if(target == "_blank"){
		window.open(url, 'new_window');
	}
	else{
		document.location.href = url;
	}
}
function hide_div(id){
	document.getElementById(id).style.display = 'none';
}
function show_div(id){
	document.getElementById(id).style.display = 'block';
}

function show_flash(f_id, f_width, f_height, f_src, f_back, flashvars){	
	var params = {};
	params.quality = "high";
	params.menu = "false";
	params.bgcolor = f_back;
	params.wmode = "opaque";
	
	var attributes = {};
	attributes.id = "uniqueID";
	attributes.name = "uniqueID";

	swfobject.embedSWF(f_src, f_id, f_width, f_height, "8.0.0", "", flashvars, params, attributes);
}

function check_flash(id){
	obj = document.getElementById(id);
	var playerVersion = swfobject.getFlashPlayerVersion();
	if(playerVersion.major < 8){
		if(playerVersion.major == 0){
			obj.innerHTML += "<br /><br /><strong>Let op!</strong> U heeft geen Adobe Flash Player geïnstalleerd.";
		}
		else{
			obj.innerHTML += "<br /><br /><strong>Let op!</strong> U heeft een oude versie van de Adobe Flash Player geïnstalleerd.";
		}
		obj.innerHTML += "<br /><a href=\"http://get.adobe.com/flashplayer/\" target=\"_blank\" alt=\"Get Adobe Flash Player\">Klik hier om de nieuwste versie van de Adobe Flash Player te downloaden en te installeren.</a>";
	}
}

function show_img(id, image){
	document.getElementById(id).src = image;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function checkInput(obj, val){
	if(obj.value == val){
		obj.value = '';
		obj.onblur = function() { blurInput(this, val); };
		obj.style.color = '#000000';
	}
}

function blurInput(obj, val){
	if(obj.value == ''){
		obj.style.color = '#d7d7d7';
		obj.value = val;
	}
}

function checkPassword(obj, val) {
	if(obj.value != val) return true;
	var parentElem = obj.parentNode;
	try {
		// For well-behaved browsers
		obj.type = "password";
		obj.value = "";
		obj.style.color = "#000000";
		obj.onblur = function() { blurPassword(this, val); };
	} catch(error) {
		// IE chokes, so catch it and swap out the elements
		var newId = obj.id;
		var newName = obj.name;
		var newStyle = obj.style;
		var textnode = document.createElement("INPUT");
		textnode.type = "password";
		textnode.onblur = function() { blurPassword(this, val); };
		parentElem.appendChild(textnode);
		parentElem.removeChild(obj);
		textnode.id = newId;
		textnode.name = newName;
		textnode.focus();
		textnode.style.color = '#000000';
	}
}

function blurPassword(obj, val) {
	if(obj.value != "") 	return true;
	var parentElem = obj.parentNode;
	try {
		// For well-behaved browsers
		obj.type = "text";
		obj.style.color = "#d7d7d7";
		obj.value = val;
	} catch(error) {
		// IE chokes, so catch it and swap out the elements
		var newId = obj.id;
		var newName = obj.name;
		var newStyle = obj.style;
		var textnode = document.createElement("INPUT");
		textnode.type = "text";
		textnode.style.color = '#d7d7d7';
		textnode.value = val;
		textnode.onfocus = function() { checkPassword(this, val); };
		parentElem.appendChild(textnode);
		parentElem.removeChild(obj);
		textnode.id = newId;
		textnode.name = newName;
		textnode.blur();
	}
}

function showPopup(id, url){
	pos = findPos(getObject(id));
	getObject('dialog').style.top = (pos[1] - 259) + 'px';
	showBackground();
	show_div("dialog");
	updateDiv('dialog', url);
}

function closePopup(){
	hide_div("background");
	hide_div("errorFloater");
	getObject('dialog').innerHTML = '';
	hide_div("dialog");
}

function updateDiv(obj, url){
	$("#" + obj).load(url);
}

function showBackground(){
	obj = getObject('background');
	show_div('background');
	var tmp_height = obj.offsetHeight;
	objs = document.getElementsByTagName('div');
	for(var i = 0; i < objs.length; i++){
		objpos = findPos(objs[i]);
		objheight = objs[i].offsetHeight;
		objtemp = objpos[1] + objheight;
		if(objtemp > tmp_height){
			tmp_height = objtemp;
		}
	}
	obj.style.height = tmp_height + 'px';
}

function show_activity(id){
	var content_height = $("#activity-content" + id).height();
	var box_height = $("#activity" + id).height();
	if(box_height == 67){
		$("#activity" + id).animate({height: content_height + "px"}, 1000, "", function(){ getObject("readmore" + id).innerHTML = "Sluiten <<"; });
	}
	else{
		$("#activity" + id).animate({height: "67px"}, 1000, "", function(){ getObject("readmore" + id).innerHTML = "Lees verder >>"; });
	}
}

function showAlbum(obj, url){
	showBackground();
	show_div(obj);
	updateDiv(obj, url);
}
function hideAlbum(obj){
	getObject(obj).innerHTML = '';
	hide_div('background');
	hide_div(obj);
}
function nextPhoto(obj,start,end,slide){
	var marginleft = parseInt($("div#" + obj).css("marginLeft"));
	if(marginleft != -end){
		if( (marginleft - slide) < -end ){
			templeft = -end;
		}
		else{
			templeft = marginleft - slide;
		}
	}
	else{
		templeft = 0;
	}
	$("div#" + obj).animate( { "marginLeft":templeft+"px"}, 1000 );
}
function prevPhoto(obj,start,end,slide){
	var marginleft = parseInt($("div#" + obj).css("marginLeft"));
	if(marginleft != start){
		if( (marginleft + slide) > start ){
			templeft = start;
		}
		else{
			templeft = marginleft + slide;
		}
	}
	else{
		templeft = -end;
	}
	$("div#" + obj).animate( { "marginLeft":templeft+"px"}, 1000 );
}
function showPhoto(rc,name,url,obj,max,slide,maxslide){
	var marginleft = parseInt($("div#" + obj).css("marginLeft"));
	if(rc > 2 && rc < (max-1)){
		templeft = -(rc - 3)*slide;
		$("div#" + obj).animate( { "marginLeft":templeft+"px"}, 1000 );
	}
	else if(rc < 3 && marginleft != 0){
		$("div#" + obj).animate( { "marginLeft":"0px"}, 1000 );
	}
	else if(rc > (max - 2) && marginleft != -maxslide){
		$("div#" + obj).animate( { "marginLeft":-maxslide+"px"}, 1000 );
	}
	$("img#photo-detail-src").attr({"src":url});
	$("span#photo-detail-name").html(name);
}

/*
 * google map functions
*/

var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
	if (GBrowserIsCompatible()){
		map = new GMap2(document.getElementById("map_canvas"));
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		setDirections("Etten-Leur");
	}
}

function setDirections(fromAddress){
	gdir.load("from: " + fromAddress + " to: Frederik van Eedenstraat 23 4873AW Etten-Leur", { "locale": "nl_NL" });
}

function handleErrors(){
	if(gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	}
	else if(gdir.getStatus().code == G_GEO_SERVER_ERROR){
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	}
	else if(gdir.getStatus().code == G_GEO_MISSING_QUERY){
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	}
	//else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong{
		//alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	//}
	else if (gdir.getStatus().code == G_GEO_BAD_KEY){
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	}
	else{
		alert("An unknown error occurred.");
	}
}

function onGDirectionsLoad(){
	// Use this function to access information about the latest load()
	// results.
	// e.g.
	// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	// and yada yada yada...
}
