/* ------------------------------------------------- */
/* ==================== YouTube ==================== */
/* ------------------------------------------------- */


jQuery.fn.ytplaylist = function(options) {
 
/* --- Weergave
========================= */
  var options = jQuery.extend( {
    holderId: 'video',
	playerHeight: '190',
	playerWidth: '270',
	addThumbs: false,
	thumbSize: 'small',
	showInline: false,
	autoPlay: false,
	showRelated: false,
	allowFullScreen: false
  },options);
  return this.each(function() {
   		var selector = $(this);
		var autoPlay = "";
		var showRelated = "&rel=0";
		var fullScreen = "";
		if(options.autoPlay) autoPlay = "&autoplay=1"; 
		if(options.showRelated) showRelated = "&rel=1"; 
		if(options.allowFullScreen) fullScreen = "&fs=1"; 
		function play(id)
		{
		   var html  = '';
		   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
		   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
		   html += '<param name="wmode" value="transparent"> </param>';
		   if(options.allowFullScreen) { 
		   		html += '<param name="allowfullscreen" value="true"> </param>'; 
		   }
		   html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
		   if(options.allowFullScreen) { 
		   		html += ' allowfullscreen="true" '; 
		   	}
		   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
		   html += '</object>';
		   return html;
		};
		
		function youtubeid(url) {
			var ytid = url.match("[\\?&]v=([^&#]*)");
			ytid = ytid[1];
			return ytid;
		};

		var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
		$("#"+options.holderId+"").html(play(youtubeid(firstVid)));

		selector.children("li").children("a").click(function() {
			
			if(options.showInline) {
				$("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
			}
			else {
				$("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
				$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo");
			}
			return false;
		}); }); };


/* ------------------------------------------------- */
/* =================== Tooltips ==================== */
/* ------------------------------------------------- */


(function($) {
    $.fn.tipsy = function(options) {
        options = $.extend({}, $.fn.tipsy.defaults, options);
        return this.each(function() {
            var opts = $.fn.tipsy.elementOptions(this, options);
            $(this).hover(function() {
                $.data(this, 'cancel.tipsy', true);
                var tip = $.data(this, 'active.tipsy');
                if (!tip) {
                    tip = $('<div class="tipsy"><div class="tipsy-inner"/></div>');
                    tip.css({position: 'absolute', zIndex: 100000});
                    $.data(this, 'active.tipsy', tip);
                }
                if ($(this).attr('title') || typeof($(this).attr('original-title')) != 'string') {
                    $(this).attr('original-title', $(this).attr('title') || '').removeAttr('title');
                }
                var title;
                if (typeof opts.title == 'string') {
                    title = $(this).attr(opts.title == 'title' ? 'original-title' : opts.title);
                } else if (typeof opts.title == 'function') {
                    title = opts.title.call(this);
                }
                tip.find('.tipsy-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);
                var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                tip.get(0).className = 'tipsy';                 
                tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
                var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
                var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(this) : opts.gravity;

                switch (gravity.charAt(0)) {
                    case 'n':
                        tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                        break;
                    case 's':
                        tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                        break;
                    case 'e':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                        break;
                    case 'w':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                        break;
                }
                if (opts.fade) {
                    tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 0.8});
                } else {
                    tip.css({visibility: 'visible'});
                }
            }, function() {
                $.data(this, 'cancel.tipsy', false);
                var self = this;
                setTimeout(function() {
                    if ($.data(this, 'cancel.tipsy')) return;
                    var tip = $.data(self, 'active.tipsy');
                    if (opts.fade) {
                        tip.stop().fadeOut(function() { $(this).remove(); });
                    } else {
                        tip.remove();
                    }
                }, 100);
            });
        });
    };
    $.fn.tipsy.elementOptions = function(ele, options) {
        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
    };
    $.fn.tipsy.defaults = {
        fade: false,
        fallback: '',
        gravity: 'n',
        html: false,
        title: 'title'
    };
    $.fn.tipsy.autoNS = function() {
        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
    };
    $.fn.tipsy.autoWE = function() {
        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
    };
})(jQuery);


/* --- Weergave
========================= */
$(function() {
    $('#fb').tipsy();
    $('#hv').tipsy();
    $('#yt').tipsy();
    $('#tw').tipsy();
    $('#pf').tipsy();
    $('#follow').tipsy({gravity: 'w'});
    $('#subscribe').tipsy({gravity: 'w'});
});


/* ------------------------------------------------- */
/* ================== Formulieren ================== */
/* ------------------------------------------------- */


(function($) {
  $.fn.example = function(text, args) {
    var isCallback = $.isFunction(text);
    var options = $.extend({}, args, {example: text});
    return this.each(function() {
      var $this = $(this);
      if ($.metadata) {
        var o = $.extend({}, $.fn.example.defaults, $this.metadata(), options);
      } else {
        var o = $.extend({}, $.fn.example.defaults, options);
      }
      if (!$.fn.example.boundClassNames[o.className]) {
        $(window).unload(function() {
          $('.' + o.className).val('');
        });
        $('form').submit(function() {
          $(this).find('.' + o.className).val('');
        });
        $.fn.example.boundClassNames[o.className] = true;
      }
      if (!$this.attr('defaultValue') && (isCallback || $this.val() == o.example))
        $this.val('');
      if ($this.val() == '' && this != document.activeElement) {
        $this.addClass(o.className);
        $this.val(isCallback ? o.example.call(this) : o.example);
      }
      $this.focus(function() {

        if ($(this).is('.' + o.className)) {
          $(this).val('');
          $(this).removeClass(o.className);
        }
      });
      $this.change(function() {
        if ($(this).is('.' + o.className)) {
          $(this).removeClass(o.className);
        }
      });
      $this.blur(function() {
        if ($(this).val() == '') {
          $(this).addClass(o.className);

          $(this).val(isCallback ? o.example.call(this) : o.example);
        }
      });
    });
  };
  $.fn.example.defaults = {
    className: 'example'
  };
  $.fn.example.boundClassNames = [];
})(jQuery);


/* --- Weergave
========================= */
$(function() {
	$('#author').example('Name', { className: 'input_active_text'});
	$('#email').example('E-mail (will not be published)', { className: 'input_active_text'});
	$('#email2').example('E-mail', { className: 'input_active_text'});
	$('#url').example('Website', { className: 'input_active_text'});
	$('#onderwerp').example('Subject...', { className: 'input_active_text'});
	$('#comment').example('Your message...', { className: 'input_active_text'});
	$('#search').example('Search...', { className: 'input_active_search'});
});

/* ----------------------------------------------------------- */
/* ==================== Reactie validatie ==================== */
/* ----------------------------------------------------------- */



jQuery(function($) {
	var errorContainer = $("").appendTo("#commentform").hide();
	var errorLabelContainer = $("<div class='error errorlabels'></div>").appendTo("#commentform").hide();
	$("#commentform").validate({
		rules: {
			author: {required: true},
			email: {
				required: true,
				email: true
			},
			url: "url",
			comment: "required"
		},
		errorContainer: errorContainer,
		errorLabelContainer: errorLabelContainer,
		ignore: ":hidden"
	});
	$.validator.messages.required = "";
	$.validator.messages.email = "&raquo; " + $.validator.messages.email;
	$.validator.messages.url = "&raquo; " + $.validator.messages.url;
});
function selecteer(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
