var bgSwapping = false;
var LoftTools = 
{
	initInternalButtons : function(type)
	{
		var t = this;
		$(type).click(function()
		{
			if ($(type + '.curr_link').html() == $(this).html())
			{
				return false;
			}
			t.showLoader();
			$('.curr_link').removeClass('curr_link');
			$(type + '.active').removeClass('active');
			$(this).addClass('active curr_link');
			
			var href = $(this).attr('href');
			if (href.indexOf('ajax.true') <= 0)
			{
				href = href + '/ajax.true/';
				$(this).attr({	'href' : href,
								'target' : 'ajax_target'});
			}
		});
	},
	
	changeTitle : function(new_title)
	{
		$('.popup_title').html('<img src="static/images/title.php?text=' + new_title + '" />');
	},
	
	resizePopup : function(w, h)
	{
		/*if ($('div.popup').height() == h && $('div.popup').width() == w)
		{
			return;
		}*/
		$('div.popup_content').stop(true, true);
		$('div.popup').stop(true, true);
		
		// scrollpane verwijderen
		$('div.popup_content').jScrollPaneRemove();
		
		// popup resizen en verplaatsen
		var margin_top = (($(window).height() - (h + 20) - $('.footer').height() - $('img#logo').height()) / 3);
		$('div.popup').animate({'margin-top' : margin_top+'px','width' : w + 'px', 'height' : h + 'px'}, 500);
		
		// popup content resizen en vervolgens de scrollpane activeren
		$('div.popup_content').css({'overflow' : 'hidden', 'padding-right' : '10px', 'opacity': 0});
		$('div.popup_title').css({'opacity': 0});
		$('div.popup_content').animate({'width' : (w-35) + 'px', 'height' : (h-80) + 'px'}, 500, function()
		{
			//$('div.popup_content').css('overflow', 'auto');
			$(this).css({'overflow' : 'auto', 'width' : w-20});
			$(this).animate({'opacity': 1}, 300);
			$('div.popup_title').animate({'opacity': 1}, 300);
			$('div.popup_content').jScrollPaneRemove();
			$(this).jScrollPane();
		});
	},
	
	updateContent : function(classname, new_content)
	{
		$('.' + classname).html(new_content);
	},
	
	initPhotoStrip : function(nr_items)
	{
		var t = this;

		$('a.photo').click(function() 
		{
			if ($('div.photo_strip a.active').html() == $(this).html())
			{
				return false;
			}
			$('div.photo_strip a.active').removeClass('active');
			$(this).addClass('active');
			t.changeBackground('upload/large/' + $(this).attr('rel'));
			return false;
		});
		
		var carousel_width = Math.min(5, nr_items) * 90;
		if (nr_items > 5) {
			jQuery("#carousel").jcarousel({ scroll: 4 });
			$(".jcarousel-container-horizontal").width(carousel_width);
		} else {
			jQuery("#carousel").jcarousel({buttonNextHTML: null, buttonPrevHTML: null });
			$(".jcarousel-container-horizontal").width(carousel_width);
		}
	},
	
	showElement : function(class_name)
	{
		$('.' + class_name).fadeIn(200);
	},
	
	hideElement : function(class_name)
	{
		$('.' + class_name).fadeOut(200);
	},
	
	changeBackground : function(url)
	{
		var t = this;
		var old_bg = $("img#bg");
		
		if (!bgSwapping && url != old_bg.attr('src'))
		{
			t.showLoader();
			bgSwapping = true;

			old_bg.stop(true);
			var img = new Image();
			$(img).delay(200).load(function () 
			{
				t.hideLoader();
				
				old_bg.after($(this));
				flexBG.positionBg($(this));
				$(this).attr('id', 'bg');
				$(this).css({	'z-index' 	: '-1', 
								'display' 	: 'none'});
				
				old_bg.fadeOut(300);
				$(this).fadeIn(300, function()
				{
					$(this).css({'z-index' : '-5'});
					bgSwapping = false;
					old_bg.remove();
				});
			})
			.error(function () 
			{
				alert("error loading background " + url);
			})
			.attr('src', url);
		}
		else
		{
			t.hideLoader();
		}
		return false;
	},
	
	showLoader : function()
	{
		$("#loader").stop(true);
		$("#loader").delay(500).fadeIn(200);
	},
	
	hideLoader : function()
	{
		$("#loader").stop(true);
		if (!$("#loader").is(':hidden'))
		{
			$("#loader").fadeOut(200);
		}
	},
	
	initContactForm : function()
	{
		var t = this;
		
		$("input.contact").click(function()
		{
			$('span.output').html('');
			t.showLoader();
			$.ajax({
				type: "POST",
				url: "Contact",
				data: $('#contactform').serialize(),
				dataType : "json",
				success: function(response)
				{
					if (response.success == 'true')
					{
						$('span.output').html('<strong>Je bericht is verstuurd</strong>');
						$('span.output').fadeIn(300);
						$("input#name").val('');
						$("input#email").val('');
						$("textarea#comments").val('');
					}
					else if (response.success == 'false')
					{
						$('span.output').html('<strong style="color:#FF0000;">Foutje: ' + response.msg + '</strong>');
					}
					t.hideLoader();
				},
				error : function(msg)
				{
					$('span.output').html('<strong style="color:#FF0000;">Foutje. Probeer het nog eens.</strong>');
					t.hideLoader();
				}
			});
			return false;
		});
	}
};
