followScroll();
fotoZoom();

if (typeof tabArray == 'object' && tabArray.length != 0)
	initTabs();


function buildSearchForm ()
{

	if ($("#zoekopties").length == 0)
		$('#follow').append('<div id="zoekopties" class="popup"></div>');
	
	if ($("#zoekopties").html() == "") {

		$.get('/aanbod/zoekopties', function (data) {

			$('#zoekopties').html(data);
		});

	} else {
	
		$("#zoekopties").empty();
	
	}
	

}


function checkLoadFunction (str)
{

	if ($("#"+str+" iframe").length != 0)
		eval("load"+str+"();");

}


function checkUnloadFunction (str)
{

	if ($("#"+str+" iframe").length != 0)
		eval("unLoad"+str+"();");

}


function closeMailToFriend ()
{

	$("#mailnaarvriend").empty();

}


function closeSearchForm ()
{

	$("#zoekopties").remove();

}


function controlCheck ()
{

	var controlh = $("#controls").height() + $("#head").height();

	$(window).resize(function () {
	
		screenh = $(window).height();

		if (controlh > screenh)
			initControlCollapse();
		else
			killControlCollapse();

	});

	$(window).trigger("resize");

}

function fSetFollowLeft (x, w, s)
{

	var follow = $("#follow");
	
	if (x == 0)
		follow.css("left", w);
	else if (follow.css("left") != "50%")
		follow.css("left", "50%");
	
	if (s != 0)
		follow.css("margin-left", (w * -1) - s); 
	else if (follow.css("margin-left") != w * -1)
		follow.css("margin-left", w * -1);

}



function fSetContainerBackLeft (x)
{

	var container = $("#container");

	if (x == 0)
		container.css("background-position-x", x);
	else
		container.css("background-position-x", "50%");

}


function followScroll ()
{

	var head = $("#head");
	var follow = $("#follow");
	var container = $("#container");
	
	headx = head.offset().left;
	halfw = Math.ceil(container.width() / 2);

//	if (container.offset().left == 0)
//		container.css("background-position-x", 0);

	$(window).scroll(function() {

		containerx = container.offset().left;
		scrollx = $(window).scrollLeft();

		head.css("left", headx - scrollx); 

		fSetFollowLeft(containerx, halfw, scrollx);

	});

	$(window).resize(function() {

		containerx = container.offset().left;
		scrollx = $(window).scrollLeft();

		fSetFollowLeft(containerx, halfw, scrollx);
		fSetContainerBackLeft(containerx);

	});

}


function fotoZoom ()
{

	$("div.fotozoom").mouseenter(function () {

		$(this).parent().css("z-index", "100");
		
		if ($("a.mouseout", this).length != 0)
			return false;
			
		$(this).append('<a class="mouseout"></a>');

		$("a.mouseout", this).click(function () {
		
			$(this).prev("a").trigger("click");
		
		});

		$("a.mouseout", this).mouseout(function () {
		
			// can't untrigger :hover, so getting creative.. :P
			$(this).parent().css("display", "none");
		
		});


	});
	
	$("div.fotozoom").mouseleave(function () {

		// Undo display:none
		$(this).css("display", "block");

		$(this).parent().css("z-index", "10");

		$("a.mouseout", this).remove();

	});

}


function gotoInfo (el)
{

	document.location.href = $("h2 a", el).attr("href");

}


function initControlCollapse ()
{

	$("#controls ul li:not(.current, .nocollapse)").slideUp();

	$("#controls h2:not(.nocollapse)")
		.addClass("cptr")
		.unbind()
		.hover(function () {
	
			$(this).css("text-decoration", "underline");

		})
		.mouseout(function () {
	
			$(this).css("text-decoration", "none");

		})
		.click(function () {
	
			$("li:not(.current, .nocollapse)", $(this).next()).slideToggle();

		})
	;
	

}


function initPlaces ()
{

	els = $("#plaatsen li");

	for (a=0; a<els.length; a++) {
	
		if ($("input", els[a]).attr("checked") == true)
			$(els[a]).addClass("selected");
		else
			$(els[a]).removeClass("selected");

	}

}


function initTabs ()
{

	for (a=0; a<tabArray.length; a++) {

		tc = $("#tabcontrol .tab"+a);

		left = Math.round($("#tabcontrol").offset().left - tc.offset().left);

		if ($.browser.mozilla)
			tc.css("background-position", left+"px 0px");
		else
			tc.css("background-position-x", left+"px");

		if (a != 0)
			$("#"+tabArray[a]).hide();
		else
			$("#tabcontrol .tab"+a).addClass("current");

	}

}


function killControlCollapse ()
{


	$("#controls ul li:not(.current, .nocollapse)").slideDown();

	$("#controls h2:not(.nocollapse)")
		.removeClass("cptr")
		.css("text-decoration", "none")
		.unbind()
	;

}


function mailToFriend ()
{

	if ($("#mailnaarvriend").length == 0)
		$('#follow').append('<div id="mailnaarvriend" class="popup"></div>');
	
	if ($("#mailnaarvriend").html() == "") {

		$.get('/aanbod/informatie/mailnaarvriend/'+mailToFriendGetAddress(), function (data) {

			$('#mailnaarvriend').html(data);

			$("#mtf_nameto").focus();

		});

	} else {
	
		$("#mailnaarvriend").empty();
	
	}
	

}


function mailToFriendGetAddress ()
{

	return document.location.href.replace(new RegExp("^.*informatie\/"), "");

}


function resetPlaces ()
{

	$("#plaatsen input").attr("checked", false);

	initPlaces();

}


function resetSearchForm ()
{

	$("#zoekopties form")[0].reset();
	$("#low").val(-1);
	$("#high").val(-1);
	
	initPriceSlider();

	initPlaces();

}


function resetSearchSettings ()
{

	$.get('/aanbod/zoekopties?reset=alles');

}


function setPlace (el)
{

	$(el).toggleClass("selected");

	cb = $("input", el);

	if (cb.attr("checked") == true)
		cb.attr("checked", false);
	else
		cb.attr("checked", true);

}


function sendMailToFriend()
{

	$.post(
		'/aanbod/informatie/mailnaarvriend/'+mailToFriendGetAddress(),
		{
			email: $("#mtf_mailto").val(),
			to: $("#mtf_nameto").val(),
			from: $("#mtf_namefrom").val(),
			remarks: $("#mtf_remarks").val()
		}
		, function (data) {

			$('#mailnaarvriend').html(data);

		}
	);

}


function triggerParent (el)
{

	$("> a", el.parent()).click();

}


function toggleTab (num)
{

	for (a=0; a<tabArray.length; a++) {

		checkUnloadFunction(tabArray[a]);

		$("#"+tabArray[a]).hide();
		$("#tabcontrol .tab"+a).removeClass("current");

	}

	checkLoadFunction(tabArray[num]);

	$("#"+tabArray[num]).show();
	$("#tabcontrol .tab"+num).addClass("current");

	return false;

}
