(function($) {

    $.nvsMediaPopupOptions = {
        overlayBackground		: 'black',
		overlayTransparancy		: 0.5,
		overlaySpeed			: 500,

		popUpBackground			: 'white',
		popUpRoundedBackground	: false,
		popUpRoundedSize		: '10',
		popUpDetailBackground	: 'white',
		popUpPadding			: '0',
		popUpFade				: true,
		popUpAnimate			: true,
		popUpActive				: false,
		popUpsize				: 'scale', // fixed, scale, full
		popUpWidth				: 250,
		popUpHeight				: 250,

		popUpItems				: [],
		popUpActiveItem			: 0,

		/* DO NOT OVERLOAD THESE, INTERNAL USE ONLY */
		htmlLoaded 				: false,
		jsonLoaded				: false,
		next					: function() { NextFunction(); },
		prev					: function() { PreviousFunction(); },
		kill					: function() { _popUpKill(); }
	}

	$.fn.nvsMediaPopup = function(options) {

		var jQueryMatchedObj = this;

		function _initialize(obj,options) {
			$.nvsMediaPopupOptions.MatchedObj = jQueryMatchedObj;
			$.nvsMediaPopupOptions.ClickedObj = obj;
			$.nvsMediaPopupOptions = $.extend($.nvsMediaPopupOptions, options);
			$.nvsMediaPopupOptions.html = '';
			$.nvsMediaPopupOptions.popUpActiveItem = 0;
			$.nvsMediaPopupOptions.popUpItems = [];

			if($.nvsMediaPopupOptions.popUpsize == 'full') {
				/// set fullscreen size
			}

  			if ( $.nvsMediaPopupOptions.MatchedObj.length == 1 ) {
				$.nvsMediaPopupOptions.popUpItems.push($.nvsMediaPopupOptions.ClickedObj.getAttribute('href'));
			} else {
				// Add an Array with the href
				for ( var i = 0; i < $.nvsMediaPopupOptions.MatchedObj.length; i++ ) {
					$.nvsMediaPopupOptions.popUpItems.push($.nvsMediaPopupOptions.MatchedObj[i].getAttribute('href'));
				}
			}

			while ( $.nvsMediaPopupOptions.popUpItems[$.nvsMediaPopupOptions.popUpActiveItem] != $.nvsMediaPopupOptions.ClickedObj.getAttribute('href')) {
				$.nvsMediaPopupOptions.popUpActiveItem++;
			}

			nvsPopupStart();
		}

		function nvsPopupStart() {
			// show the nvsOverlay
			$.nvsOverlay.show({
				background: $.nvsMediaPopupOptions.overlayBackground,
				transparancy: $.nvsMediaPopupOptions.overlayTransparancy,
				speed: $.nvsMediaPopupOptions.overlaySpeed
			});

			if($.nvsMediaPopupOptions.popUpRoundedBackground) {
				temppopUpBackground = 'none';
			} else {
				temppopUpBackground = $.nvsMediaPopupOptions.popUpBackground;
			}


			//add popup to the DOM
			// old top: $(document).scrollTop() + (($(window).height())/10),


			$('<div></div>').attr({
					id: 'nvs_popup_container'
				}).css({
					position: 'absolute',
					top: $(document).scrollTop() + 30,
					left: '0px',
					width: '100%',
					textAlign: 'center',
					zIndex: '10000'
				}).appendTo('body');


			$('<div></div>').attr({
					id: 'nvs_popup'
				}).css({
					position: 'relative',
					// temp disabled because of IE error
					background: temppopUpBackground,
					width: $.nvsMediaPopupOptions.popUpWidth,
					height: $.nvsMediaPopupOptions.popUpHeight,
					padding: $.nvsMediaPopupOptions.popUpPadding,
					margin: '0 auto'
				}).appendTo('#nvs_popup_container');


			if($.nvsMediaPopupOptions.popUpRoundedBackground) {
				$('<div></div>').addClass('tl').appendTo('#nvs_popup');
				$('<div></div>').addClass('tr').appendTo('#nvs_popup');
				$('<div></div>').addClass('bl').appendTo('#nvs_popup');
				$('<div></div>').addClass('br').appendTo('#nvs_popup');

				$('<div></div>').attr({
						id: 'vbox'
					}).css({
						background:  $.nvsMediaPopupOptions.popUpBackground,
						position: 'absolute',
						top: 0,
						left: $.nvsMediaPopupOptions.popUpRoundedSize+'px',
						width: parseInt($.nvsMediaPopupOptions.popUpWidth) - (2 * $.nvsMediaPopupOptions.popUpRoundedSize),
						height: parseInt($.nvsMediaPopupOptions.popUpHeight)
					}).appendTo('#nvs_popup');
				$('<div></div>').attr({
						id: 'hbox'
					}).css({
						background:  $.nvsMediaPopupOptions.popUpBackground,
						position: 'absolute',
						top: $.nvsMediaPopupOptions.popUpRoundedSize+'px',
						left: 0,
						width: parseInt($.nvsMediaPopupOptions.popUpWidth),
						height: parseInt($.nvsMediaPopupOptions.popUpHeight) - (2 * $.nvsMediaPopupOptions.popUpRoundedSize)
					}).appendTo('#nvs_popup');

				$('#nvs_popup').css({
					width: $.nvsMediaPopupOptions.popUpWidth - (2 * $.nvsMediaPopupOptions.popUpRoundedSize)+"px",
					height: $.nvsMediaPopupOptions.popUpHeight - (2 * $.nvsMediaPopupOptions.popUpRoundedSize)+"px",
					padding:$.nvsMediaPopupOptions.popUpRoundedSize+'px'
				});

			}

			$('<div></div>').attr({	id: 'nvs_popup_content' }).css({ zIndex: '11000', position: 'relative' }).appendTo('#nvs_popup');

			$('#nvs_popup_content').html("");


			$(window).bind('scroll', _popUpScroll);
			_loadContents();
		}

		return this.each(function(){ $(this).click(function(e) {  _initialize(this, options); return false; }); });

	};

	function _loadContents() {

		url = $.nvsMediaPopupOptions.popUpItems[$.nvsMediaPopupOptions.popUpActiveItem];
		ext = url.substr((url.length-3),3);

		if(ext == 'png' || ext == 'gif' || ext == 'jpg' ) {

			$('<img>').attr({ id: 'mediaalbumclose', src :'/img/mediaalbum_btn_close.png' }).css({
				position: 'relative',
				cursor: 'pointer',
				float: 'right',
				margin: '0 0 5px 0'
			}).click(function() { $.nvsMediaPopupOptions.kill() }).appendTo('#nvs_popup_content');

			// add an image to the nvs_popup_content
			$('<img>').attr({ id: 'nvs_media-album-image' }).css({
				position: 'relative',
				visibility: 'hidden'
			}).appendTo('#nvs_popup_content');

			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				$('#nvs_media-album-image').attr('src',$.nvsMediaPopupOptions.popUpItems[$.nvsMediaPopupOptions.popUpActiveItem]);
				$.nvsMediaPopupOptions.json = {};
				$.nvsMediaPopupOptions.json.width = objImagePreloader.width;
				$.nvsMediaPopupOptions.json.height = objImagePreloader.height + 20;
				_popUpScale();
				//	clear onLoad, IE behaves strange with animated gifs otherwise
				objImagePreloader.onload=function(){};
			};
			objImagePreloader.src = $.nvsMediaPopupOptions.popUpItems[$.nvsMediaPopupOptions.popUpActiveItem];

		} else {
			$.nvsMediaPopupOptions.jsonLoaded = false;
			$.nvsMediaPopupOptions.htmlLoaded = false;

			$.get( url, { type: "html"}, function(data){
					_loadedContent('html', data);
				}, 'html' );

			$.getJSON(url, { type: "json"}, function(data){
					_loadedContent('json', data);
				});
		}
	}

	function _loadedContent(type, data) {
		if(type == 'html') {
			$.nvsMediaPopupOptions.html = data;
			$.nvsMediaPopupOptions.htmlLoaded = true;
		}
		if(type == 'json') {
			$.nvsMediaPopupOptions.json = data;
			$.nvsMediaPopupOptions.jsonLoaded = true;
		}

		if($.nvsMediaPopupOptions.htmlLoaded && $.nvsMediaPopupOptions.jsonLoaded) {
			if($.nvsMediaPopupOptions.popUpsize == 'fixed' || $.nvsMediaPopupOptions.popUpsize == 'full') {
				// add content
				$('#nvs_popup_content').html($.nvsMediaPopupOptions.html);
				_popUpBindEvents();
			} else if ($.nvsMediaPopupOptions.popUpsize == 'scale') {
				// resize and add content
				_popUpScale();
			}
		}
	}

	function _popUpScale() {
		if ($.nvsMediaPopupOptions.popUpAnimate && !$.nvsMediaPopupOptions.popUpRoundedSize) {
			$('#nvs_popup').animate({
					width : $.nvsMediaPopupOptions.json.width,
					height: $.nvsMediaPopupOptions.json.height
				}, 1500, function(){
					$('#nvs_popup_content').html($.nvsMediaPopupOptions.html);
					_popUpBindEvents();
				}
			);
		} else {
			if($.nvsMediaPopupOptions.json.width > 0) {
				$('#nvs_popup').css({
					width: parseInt($.nvsMediaPopupOptions.json.width),
					height: parseInt($.nvsMediaPopupOptions.json.height),
					padding: $.nvsMediaPopupOptions.popUpRoundedSize
				});
				$('#vbox').css({
					width: parseInt($.nvsMediaPopupOptions.json.width),
					height: parseInt($.nvsMediaPopupOptions.json.height) + (2 * $.nvsMediaPopupOptions.popUpRoundedSize)
				})
				$('#hbox').css({
					width: parseInt($.nvsMediaPopupOptions.json.width) + (2 * $.nvsMediaPopupOptions.popUpRoundedSize),
					height: parseInt($.nvsMediaPopupOptions.json.height)
				})
			} else {
				$('<div></div>').html($.nvsMediaPopupOptions.html).attr({ id: 'temp' }).css({ position: 'absolute', top:0, left: 0	}).appendTo($('body'));

			 	w = $('#temp').width();
			 	h = $('#temp').height();
				$('#temp').remove();
				$('#nvs_popup').css({ width: w,	height: h });
				$('#vbox').css({
					width: parseInt(w),
					height: parseInt(h) + (2 * $.nvsMediaPopupOptions.popUpRoundedSize)
				});
				$('#hbox').css({
					width: parseInt(w) + (2 * $.nvsMediaPopupOptions.popUpRoundedSize),
					height: parseInt(h)
				});
			}

			_popUpBindEvents();
			if($('#nvs_media-album-image').length > 0) {
				$('#nvs_media-album-image').css({visibility: 'visible'});
			} else {
				$('#nvs_popup_content').html($.nvsMediaPopupOptions.html);
			}
		}

		eval($.nvsMediaPopupOptions.json.onload);
	}


	function _popUpKill() {
		$.nvsOverlay.hide();
		$('#nvs_overlay, #nvs_popup_container').unbind('click', _popUpKill);
		$(document).unbind('keydown', _popUpKeyboardAction);
		$(window).unbind('scroll', _popUpScroll);
		$("#nvs_popup_content object, #nvs_popup_content embed").remove();
		$("#nvs_popup_container, #nvs_popup,, #nvs_popup_content, #nvs_popup_content *").remove();
		return false;
	}

	function _popUpScroll() {
		$('#nvs_popup_container').stop();
		$('#nvs_popup_container').animate({top: $(document).scrollTop() + 30},500);
	}

	function _popUpBindEvents() {
		$('#media-album-close').bind('click', _popUpKill );
		$(document).bind('keydown', _popUpKeyboardAction);
		$('#nvs_overlay, #nvs_popup_container, #nvs-media-album-close').bind('click', _popUpKill);
		$('#nvs_popup').click(function () { return false; });
	}

	function _popUpKeyboardAction(objEvent) {
		switch(objEvent.keyCode) {
		// key ESC
			case 27: 	_popUpKill();
						break;
		// key X
			case 88:	_popUpKill();
						break;
		}
	}

	function NextFunction() {
		if(($.nvsMediaPopupOptions.popUpActiveItem+1) >= $.nvsMediaPopupOptions.MatchedObj.length){
			$.nvsMediaPopupOptions.popUpActiveItem = 0;
		} else {
			$.nvsMediaPopupOptions.popUpActiveItem++;
		}
	}

	function PreviousFunction() {
		if(($.nvsMediaPopupOptions.popUpActiveItem-1) <= 0){
			$.nvsMediaPopupOptions.popUpActiveItem = $.nvsMediaPopupOptions.MatchedObj.length -1;
		} else {
			$.nvsMediaPopupOptions.popUpActiveItem--;
		}
	}

})(jQuery);

 /**
 * NVS_OVERLAY
 *
 * @author		Sebastiaan Smid <sebastiaan@netvlies.nl>
 *
 * Optional parameters:
 * @category		jQuery Plugin
 * @package			Netvlies Overlay
 * @version			1.0
 *
 */

(function($) {

	$.nvsOverlay= {

		/**
		 * Show the overlay with default settings or the given params
		 * Also binds window resizing to overlayResize
		 * @param 	Object		background string (cssProperty), speed int (milliseconds), transparancy decimal (between 0 and 1), callback function
		 */

		show : function (options) {
		// default settings
			var defaults = {
				background: 'black',
				speed: 1500,
				height: this.overlayHeight(),
				transparancy: '0.5',
				callback: false
			};
			options = $.extend(defaults, options);

		//hide elements that show through the overlay in IE 6
			if($.browser.msie && $.browser.version <= 6) {
				$('embed, object, select').css({ 'visibility' : 'hidden' });
			}

		//add overlay to the DOM
			$('<div></div>').attr({
							id: 'nvs_overlay'
							})
						.css({
							position: 'absolute',
							top: 0,
							left: 0,
							height: options.height,
							width: '100%',
							zIndex: '9999',
							opacity: '0',
							background: options.background
							})
						.appendTo('body')
						.fadeTo(options.speed, options.transparancy, options.callback);

		// bind resize function on window resize event, this will reposition the overlay when window resizes or scrolls
			$(window).bind('resize', this.overlayResize);
		},

		/**
		* Hides the overlay and unbinds window resize method
		*/
		hide :	function () {
			if($('#nvs_overlay')){
				// first fade and remove on callback
				$('#nvs_overlay').fadeTo('slow', 0, function () {
					$('#nvs_overlay').remove();
					$('embed, object, select').css({ 'visibility' : 'visible' });
				});
			}
			$(window).unbind('resize', this.overlayResize);
		},

		/**
		* Resizes the overlay bound to resize event of the window DOM element
		*/
		overlayResize : function() {
			$('#nvs_overlay').css({ width: 0, height: 0 });
			oheight = $.nvsOverlay.overlayHeight();
			$('#nvs_overlay').css({	width: '100%', height: oheight });
		},

		/**
		 * Calculates the height of the overlay
		 * @returns 	INT		Height of the window or document DOM element
		*/
		overlayHeight : function () {

			if($(document).height() > $(window).height()) {
				oHeight = $(document).height()
			} else {
				oHeight = $(window).height();
			}

			return oHeight;
		}

	}

})(jQuery);