jQuery.cookie = function (key, value, options) {
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.validator.addMethod("postal", function(value, element, param) {
		var reg = /^[0-9]{4}\s?([a-zA-Z]{2})$/;
		return reg.test(value);
	}, "Geef een geldige postcode op");

jQuery(function($){

	var contents = $(".contents"),
		header = $(".header"),
		body = $("body"),
		sideblock = $(".side-block h1 a"),
		carousel = $("#carousel .jCarouselLite");

//if (false === $("body").hasClass("home")) {
	body.removeClass("expanded");
	header.hover(
		function(){
			body.addClass("expanded");
			$(this).addClass("header-expanded", 1000);
		},
		function(){
			body.removeClass("expanded");
			$(this).removeClass("header-expanded", 1000);
		}
	);
//}
	contents.click(function(ev){
		var t = $(ev.target);
		if (t.is(".slider") || t.is(".close")){
			contents.animate({
				top: (contents.hasClass("collapsed")?"+":"-")+"="+(contents.height()+ 40)
			},
			700, // time
			function(){ // onComplete
				contents.toggleClass("collapsed");
			});
		}
	});

	$(".side-block").each(function(){
		$(this).addClass($.cookie(this.id));
	});

	sideblock.click(function(){
		var sb = $(this).closest(".side-block");
		sb.toggleClass("collapsed");
		$.cookie(sb.get(0).id, sb.hasClass("collapsed")?"collapsed":"", {"expires": 7, "path": "/", "domain": window.location.host });
	});

	if (carousel.size() > 0){
		var countItems = carousel.find("li").size();
			countItems =  countItems > 10 ? 10 : countItems;
		
		carousel.jCarouselLite({
			btnNext: "#carousel .next",
			btnPrev: "#carousel .prev",
			visible: countItems
		});
	}

	$("#carousel").click(function(ev){
		ev.preventDefault();
		var tar = $(ev.target);
		if (tar.is("img")){
			var c_id = tar.parent().get(0).id.substring(5);
			$(".teammembers, .testimonial").hide();
			$("#content_"+c_id).show();
		}		
	});

	/**
	* @author Remy Sharp
	* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
	*/
	(function(c){c.fn.hint=function(b){b||(b="blur");return this.each(function(){function d(){a.val()===e&&a.hasClass(b)&&a.val("").removeClass(b)}var a=c(this),e=a.attr("title"),f=c(this.form),g=c(window);if(e){a.blur(function(){this.value===""&&a.val(e).addClass(b)}).focus(d).blur();f.submit(d);g.unload(d)}})}})(jQuery);

	$(function() {
		$('input[title!=""]').hint();
	});

	// Validator
	var f = $("form");

	if (f.size() > 0){
		$.extend($.validator.messages, {
			required: "Dit is een verplicht veld.",
			email: "Vul geldig e-mailadres in."
		});
		f.each(function() {
			$(this).validate({
				"rules": {
					"naam": "required",
					"email":{
						"required": true,
						"email": true
					}
				}
			});
		});
	}

	// Modal
	var modal = $(".wijn-foto");
	if (modal.size() > 0) {
		modal.fancybox();
	}

	/*
	 * Logo's on Homepage mouse-overs and label text
	 */
	var img = $("#logos img");

	img.each(function(){
		var t = $(this);
		var originalSrc = t.attr('src'); // initial src
		var newSrc = originalSrc.substring(0, originalSrc.lastIndexOf('.'));
		t.hover(function(){
			$(this).attr('src', newSrc+ '-over.' + /[^.]+$/.exec(originalSrc));
		}, function(){
			$(this).attr('src', newSrc + '.' + /[^.]+$/.exec(originalSrc));
		});
	});
	
});
