/*
 * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);


$.fn.moveBanners = function(){
	var numContainers = $('.banner-container').length;
	var numBanners = $('#footer .banner').length;

	if (numContainers > 0){
		
		var i = 1;
		$('#footer .footer').each(function(){
			
			if ((numContainers > 0)&&(i < 4)){
				$('.banner-container:eq(0)').append($(this));
			}
			
		 	if ((numContainers > 1)&&(i > 3)&&(i < 7)){
		 		$('.banner-container:eq(1)').append($(this));
		 	}
		 	
		 	if ((numContainers > 2)&&(i > 6)&&(i <10)){
		 		$('.banner-container:eq(2)').append($(this));
		 	}
		i++;
		});
	
		$('.banner-container .banner:first-child').addClass('first-child');
		$('.banner-container .banner:last-child').addClass('last-child');
	}
}

$.fn.moveHomepageBanners = function(){
	var numContainers = $('.banner-container').length;
	var numBanners = $('#footer .banner').length;

	if (numContainers > 0){
		var i = 1;
		$('#footer .banner').each(function(){
			$('.banner-container:eq(0)').append($(this));
		i++;
		});
		$('.banner-container .banner:first-child').addClass('first-child');
		$('.banner-container .banner:last-child').addClass('last-child');
	}
}

$.fn.searchFieldBehaviour = function (options) {
    var defaults = {
        speed: 250,
        findclass: '.fixvalue',
        closebtn: '.searchreset_standard'
    };
    var options = $.extend(defaults, options);
    $('.searchreset_standard').click(function () {
        var previnputs = $(this).siblings('input');
        previnputs.val($(this).siblings('.fixvalue').data("state").defaulttext);
        $(this).siblings('.fixvalue').data("state").touched = 'no';
        return false;
    });

    function clearField(element) {
        if ($(element).parents('.expand').size() > 0) {
            $(element).parents('.expand.filled').addClass('open');
        }
        $(element).removeClass('default');
        $(element).siblings(options.closebtn).css({
            opacity: 1
        });
        if ($(element).data("state").touched == 'no') {
            $(element).attr('value', '');
            $(element).data("state").touched = 'yes';
        }
    }

    function revertField(element) {
        if ($(element).attr('value') == '') {
            $(element).attr('value', $(element).data("state").defaulttext);
            $(element).addClass('default');
            $(element).data("state").touched = 'no';
        }
        $.doTimeout(150, function () {
            if ($(element).parents('.expand').size() > 0) {
                $(element).parents('.expand.open').removeClass('open');
            }
            $(element).siblings(options.closebtn).css({
                opacity: 0
            });
        });
    }
    $(options.findclass).each(function () {
        var value = $(this).attr('value');
        $(this).data("state", {
            touched: 'no',
            defaulttext: value
        });
        $(this).addClass('default');
        $(this).click(function () {
            clearField(this);
        }).focus(function () {
            clearField(this);
        }).blur(function () {
            revertField(this);
        });
    });
};

$.fn.highlightNewsItems = function(){
	$('.highlightItem').each(function(){
		if ($(this).attr('id').indexOf("news") >= 0){
			var item = $(this).attr('id');
			item = item.replace("news_", "#newsItem_");
			$(item).hide();
		}
	});
}

// Het huis in beeld

$.fn.HibloadSeachBox = function(){
 	$('#leftCol').load('http://'+location.hostname+'/modules/fotomodule/searchForm.php', function(){
 		$.fn.HibInitSeachBox();
 	});
}

$.fn.HibInitSeachBox = function(){
	$.fn.searchFieldBehaviour();

	$('.hibChoice a').live('click', function(){
		var choice = $(this).attr('rel');
		$('#hibSearchForm #dir').val(choice);
		$('#hibSearchForm').submit();
		return false;
	});
	
	$('#dir').change(function(){
		$('#leftCol').load('http://'+location.hostname+'/modules/fotomodule/searchForm.php', { 'dir': $(this).val() }, function(){
 			$.fn.HibInitSeachBox();
 		});
	});
	
}

$.fn.checkInfoForm = function(){
	var error_txt = '';
	var set_error = '';
	$('.checkfield').change(function(){
		$(this).removeClass("invalid");
	});
	
	$('.checkfield').each(function(){
		$(this).removeClass("invalid");
		if ((this.type == 'text') || (this.type == 'textarea')){
			if (($(this).val() == '')||($(this).val() == $(this).attr('alt'))){
				$(this).addClass("invalid");
				set_error = 1;
			}else{
				$(this).removeClass("invalid");
			}
		}
	});
	
	if (set_error == 1){
		error_txt += "Alle rood gemarkeerde velden zijn verplicht.<br>";
		if($('#error-output').length < 1){
			$('<div id="error-output"><p>'+error_txt+'</p></div>').prependTo('body');
			var outputheight = 0;
		}else{
			$('#error-output').slideUp("slow",function(){
				$('#error-output').html('<p>'+error_txt+'</p>')
			});      			
		}
	    
	    $('#error-output').slideDown();
	    		
		$.doTimeout(5000, function(){
			$('#error-output').slideUp();
		});
				      		
		return false;
	}else{
		$('#error-output').slideUp();
		return true;
	}
}

$.fn.HibDetailsPage = function(){
	$(".hibDetailsImages a").fancybox();
	
	$('#infoForm input').each(function(){
		if ((this.type == 'text') || (this.type == 'textarea')){
			if ($(this).val() == ''){
				$(this).val($(this).attr('alt')); 
			};
		}
	});

	$('#infoForm').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
 		beforeSubmit:  $.fn.checkInfoForm,  // pre-submit callback 
        success:       processJson
    });
    
    function processJson(data){
	    if (data.status == 0){
			$('#infoFormHolder').html(data.text);
		}else{
			$('#infoFormHolder').html('<p>Er is iets fout gegaan bij het versuren van uw gegevens. Probeer het a.u.b. later nog een keer.</p>');
		}
	}
    
}


$.fn.initHib = function(){
	$.fn.HibloadSeachBox();
	$.fn.HibDetailsPage();
}

// Einde het huis in beeld

$(function() {
	
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
		window.open(this.href);
		return false;
	});
	
	$.fn.searchFieldBehaviour();
	
	if ($('#highlightNews').length){
		$.fn.highlightNewsItems();
	}
	
	if ($('#hibHolder').length) {
		$.fn.initHib();
	}
	
});
