function changeCity(sCity)
{
	CallUrl("/?mod=jsgetstreets&city="+sCity);
}

function CallUrl(url)
{
	var tag = document.createElement("script");
	tag.type = "text/javascript";
	tag.src = url;
	document.getElementsByTagName("head")[0].appendChild(tag);
	return true;
}

function showPartnerForm(iID, iPos, sTitle)
{
	iPopinWidth = 400;
	iPopinHeight = 495;

	iDocWidth = document.body.offsetWidth;
	iDocHeight = document.body.offsetHeight;

	iScreenHeight = document.documentElement.clientHeight;

	if(iScreenHeight > iDocHeight)
		iHeight = iScreenHeight;
	else
		iHeight = iDocHeight;

	iPopinLeft = Math.round((iDocWidth - iPopinWidth) / 2);
	iPopinTop  = Math.round((iHeight - iPopinHeight) / 2);

	$("#darkener").css({'width': iDocWidth+'px', 'height': iHeight+'px'});

	$("body").append('<div id="pfpopin"><div class="title">'+sTitle+'</div><div id="pfcontent"></div></div>');
	$("#pfpopin").css({'left': iPopinLeft+'px', 'top': iPopinTop+'px', 'width': iPopinWidth+'px', 'height': iPopinHeight+'px'});
	$("#pfcontent").load("/get_partner_form/?id="+iID+'&pos='+iPos);
}


function switchShowAllImages()
{
	if(bAllPhotosOpen)
	{
		$("#feauteaulist").css("height", "107px");
		bAllPhotosOpen = false;
		$("#morephotosthumb").show();
		$("#showallphotoslink").html("alle foto's tonen");
	}
	else
	{
		$("#feauteaulist").css("height", "auto");
		bAllPhotosOpen = true;
		$("#morephotosthumb").hide();
		$("#showallphotoslink").html("minder foto's");
	}
}

function openTab(sTab)
{
	if(sTab == "photos")
	{
		$("#planmap").hide();
		$("#feauteaulist").show();
		$(".feauteaux .header .photos").removeClass("inactive").addClass("active");
		$(".feauteaux .header .map").removeClass("active").addClass("inactive");
	}
	if(sTab == "planmap")
	{
		$("#feauteaulist").hide();
		$("#planmap").show();
		$(".feauteaux .header .photos").removeClass("active").addClass("inactive");
		$(".feauteaux .header .map").removeClass("inactive").addClass("active");
	}
}

function showPhoto(iPhotoID)
{
	oPhoto = document.getElementById("main_photo");
	oPhoto.style.backgroundImage = "url(/content/objects/images/normal/"+iPhotoID+".jpg)";
	iOpenedPhoto = iPhotoID;
}



function showBigPhoto(iID)
{
	oDarkener = document.getElementById("darkener");

	iDocWidth = document.body.offsetWidth;
	iDocHeight = document.body.offsetHeight;

	iScreenHeight = document.documentElement.clientHeight;

	oDarkener.style.width = iDocWidth + 'px';
	oDarkener.style.height = iDocHeight + 'px';

	var oPopin = document.createElement("div");
	oPopin.id = 'popin';
	document.body.appendChild(oPopin);

	oPopin.style.left = ((iDocWidth - 320) / 2) + 'px';
	oPopin.style.top = (((iScreenHeight - 320) / 2) + getScrollPos()) + 'px';

	if(iOpenedPhoto) iID = iOpenedPhoto;

	oPopin.style.backgroundImage = 'url(/shared/images/custom/loading.gif)';

	loadImage(iID);
}

function checkImageLoaded()
{
	oImg = document.getElementById('newmainimage');
	if(oImg.complete)
	{
		iImageWidth = oImg.width;
		iImageHeight = oImg.height;

		if(iImageWidth < 500) iImageWidth = 500;

		oPopin = document.getElementById("popin");
		iDocWidth = document.body.offsetWidth;
		iScreenHeight = document.documentElement.clientHeight;

		oPopin.style.left = ((iDocWidth - (iImageWidth + 20)) / 2) + 'px';
		oPopin.style.top = (((iScreenHeight - (iImageHeight + 40)) / 2) + getScrollPos()) + 'px';
		oPopin.style.width = (iImageWidth + 20) + 'px';
		oPopin.style.height = (iImageHeight + 40) + 'px';
		oImg.style.visibility = 'visible';
	}
	else
	{
		setTimeout('checkImageLoaded()', 50);
	}
}

function loadImage(iID)
{
	if(!iID) return false;

	oPopin = document.getElementById("popin");

	sHTML = '<img src="/content/objects/images/original/'+iID+'.jpg" alt="" id="newmainimage" style="visibility:hidden;" />';
	sHTML += '<div class="kruizert" onclick="closePhoto();"></div>';

	oPopin.innerHTML = sHTML;

	CallUrl('/?mod=getnextphotos&thisphoto='+iID);

	setTimeout('checkImageLoaded()', 50);
}


function getScrollPos()
{
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
		    ScrollTop = window.pageYOffset;
		else
		    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}

function closePhoto()
{
	$("#darkener").css({'width': '0px', 'height': '0px'});
	$("#popin").remove();
	$("#pfpopin").remove();
}


function addEvent(hoo, wot, fun)
{
	if(document.addEventListener)
		hoo.addEventListener(wot, fun, null);
	else if(document.attachEvent)
		hoo.attachEvent('on' + wot, fun);
	else hoo['on' + wot] = fun;        
}

function catchEscape(e)
{
	e=(window.event)?window.event:e;
	if(e.keyCode == 27)
	{ 
		closePhoto();
	}

	if(e.keyCode == 39)
	{
		loadImage(sNextPhoto);
	}

	if(e.keyCode == 37)
	{
		loadImage(sPrevPhoto);
	}

	return true;
}

function startcatching()
{
	addEvent(document, 'keyup', catchEscape);
}

function adjustDescriptionHeight(iMaxHeight)
{
	oText = document.getElementById("description_text");
	if(oText)
	{
		oReadMore = document.getElementById("read_more");
		if(oText.offsetHeight > iMaxHeight)
		{
			oText.style.height = (iMaxHeight - 20) + 'px';
			oReadMore.style.display = 'block';
		}
	}
}

function expandDescription()
{
	oReadMore = document.getElementById("read_more");
	oText = document.getElementById("description_text");

	oReadMore.style.display = 'none';
	oText.style.height = 'auto';
}

function createCookie(name, value, days)
{
	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 86400000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value+expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while(c.charAt(0) == ' ') c = c.substring(1, c.length);
		if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}


function saveMyLocation(sLocationName)
{
	createCookie('mylocation', sLocationName, 30);
}

function saveNewLocation(sLat, sLng)
{
	streetViewLat = sLat;
	streetViewLng = sLng;
}

function showFeedbackButton()
{
	oFeedbackButton = document.getElementById("feedbackbutton");
	iScreenHeight = document.documentElement.clientHeight;
	oFeedbackButton.style.top = ((iScreenHeight / 2) - 57) + 'px';
	oFeedbackButton.style.display = 'block';
}

function showFeedbackForm()
{
	oWhitener = document.getElementById("whitener");

	iDocWidth = document.body.offsetWidth;
	iDocHeight = document.body.offsetHeight;

	oWhitener.style.width = iDocWidth + 'px';
	oWhitener.style.height = iDocHeight + 'px';


	iScreenHeight = document.documentElement.clientHeight;

	var oPopin = document.createElement("div");
	oPopin.id = 'feedbackpopin';
	document.body.appendChild(oPopin);

	oPopin.style.left = ((iDocWidth - 300) / 2) + 'px';
	oPopin.style.top = (((iScreenHeight - 300) / 2) + getScrollPos()) + 'px';

	var oClose = document.createElement("div");
	oClose.id = 'feedbackclose';
	document.body.appendChild(oClose);

	oClose.style.left = ((iDocWidth / 2) + 156) + 'px';
	oClose.style.top = ((iScreenHeight / 2) + getScrollPos() - 168) + 'px';
	oClose.onclick = closeFeedbackForm;
	oClose.style.display = 'block';

	CallUrl('/shared/templates/custom/feedback_popin.js');
}

function closeFeedbackForm()
{
	oWhitener = document.getElementById("whitener");
	oPopin = document.getElementById("feedbackpopin");
	oClose = document.getElementById("feedbackclose");

	oPopin.parentNode.removeChild(oPopin);
	oClose.parentNode.removeChild(oClose);
	oWhitener.style.width = '0px';
	oWhitener.style.height = '0px';
}


function init()
{
	fotoFader = new fotoFader();
	fotoFader.initFotoFader('carousel', 'carousel_buttons');

	startcatching();
	bAllPhotosOpen = false;
	iOpenedPhoto = false;
	sNextPhoto = sPrevPhoto = oDefenestrated = false;

	if(bLoadBothMaps == true) loadBothMaps();
	if(bLoadObjectMap == true) loadObjectMap();
	if(bLoadVestigingenMap == true) loadVestigingenMap();

	showFeedbackButton();

	$("#header .login").toggle(
		function()
		{
			$("#loginbg").slideDown(500, function()
				{
					$("#login").fadeIn(250);
				});
		},
		function()
		{
			$("#login").fadeOut(250, function()
				{
					$("#loginbg").slideUp(500);
				});
		});
}

window.onload = init;

