jQuery(document).ready(function(){
	RecentlyViewed.init();
	Catalog.init();
	Vacancies.init();
	Carousel.init();
	Spotlight.init();
	Vipcard.init();
	Initialize.init();
});

/**
 * Initialize
 *
 */
var Initialize = function() {

	/**
	 * Initialize.init
	 * Empty focus, corners and print links
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		jQuery("body").addClass('js-enabled');

		jQuery(".empty-focus").each(function(){
			el = jQuery(this);
			el.attr('prefilled', el.val());
			el.focus(function(){
				if (jQuery(this).val() == jQuery(this).attr('prefilled')) {
					jQuery(this).val('');
				}
			});
			el.blur(function(){
				if (!jQuery(this).val()) {
					jQuery(this).val(jQuery(this).attr('prefilled'));
				}
			});
		});

		jQuery("#print-me").click(function(){
			window.print();
			return false;
		});

		jQuery(".custom-corners, .catalog-product-view .col-left").append(
			'<span class="corner corner-tl"></span> \
			<span class="corner corner-tr"></span> \
			<span class="corner corner-bl"></span> \
			<span class="corner corner-br"></span>'
		);

		jQuery("#products-list li.item").click(function(){
			window.location = jQuery(".product-name a", this).attr('href');
			return false;
		});
	}

	return {
		init : function() { init(); }
	}
}();

/**
 * Spotlight
 *
 */
var Spotlight = function() {

	var _interval = false; // used for stopping the current interval
	var running = false; // status of carousel

	var speed = 3; // animation in seconds
	var current = false; // current active element in carousel

	/**
	 * Carousel.init()
	 * Initializes the homepage carousel elements
	 * Triggers the carousel animation
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		start();
	};

	/**
	 * Carousel.start()
	 * Starts the carousel animation from the current active item
	 *
	 * @param void
	 * @return void
	 */
	var start = function()
	{
		if (running) return false;

		current = jQuery("#spotlight .active");
		_interval = setInterval(animate, speed * 2000);

		running = true;
	};

	/**
	 * Carousel.animate()
	 * Carousel animation logic
	 *
	 * @param void
	 * @return void
	 */
	var animate = function()
	{
		current.fadeOut(1000, function(){
			current.removeClass('active');
		});

		if (current.hasClass('last')) {
			current = jQuery('#spotlight .first');
		}
		else {
			current = jQuery(current).next();
		}
		current.fadeIn(1000, function(){
			current.addClass('active');
		});
	};

	/**
	 * Carousel.stop()
	 * Stops the carousel animation
	 *
	 * @param void
	 * @return void
	 */
	var stop = function()
	{
		if (!running) return false;
		clearInterval(_interval);

		running = false;
	};

	return {
		init : function() { init(); },
		start : function() { start(); },
		stop : function() { stop(); }
	};
}();

/**
 * Carousel
 *
 */
var Carousel = function() {

	var carousel; // Carousel element;
	var step = 220; // Carousel element width in pixels
	var speed = 1; // Animation in seconds
	var items = 0; // Amount if items in carousel
	var start = 1; //
	var finish = 1;

	var fetch = function()
	{
		carousel = jQuery("#carousel ul");
		if (!carousel.length) return false;

		jQuery("li", carousel).each(function(){
			if (jQuery("a", this).length) {
				jQuery(this).css({cursor: 'pointer'});

				jQuery(this).click(function(){
					href = jQuery("a.footer-link", this).attr("href");
					window.location = href;
				});
			}
		});

		jQuery("li", carousel).hide();
		items = jQuery("li", carousel).length;

		animate();
		run();
	};

	var run = function()
	{
		setInterval(animate, speed * 6500);
	};

	var animate = function()
	{
		start = finish;
		end = finish + 10;
		counter = 1;
		while(finish < end) {
			jQuery(".carousel-item-"+finish, carousel).addClass('pulse');

			finish++;
			counter++;
			if (finish > jQuery("li", carousel).length) {
				finish = 1;
				end = 10 - counter;
			}
		}

		jQuery(".pulse", carousel).fadeIn(1500,
			function() {
				setTimeout(function() {
				jQuery(".pulse", carousel).fadeOut(1500,
					function() {
						jQuery(this).removeClass('pulse');
					}
				)}, 3000);
			}
		);
	};

	var correction = function()
	{
		childs = jQuery("li", carousel);

		// IE7 throws error when cloning all events..
		first = childs.eq(0).clone();

		childs.eq(0).remove();
		jQuery(first).css({width: '116px', cursor: 'pointer'});

		// Readd events for now, should be done by clone (see clone)
		first.click(function(){
			href = jQuery("a.footer-link", this).attr("href");
			window.location = href;
		});

		carousel.append(first);
		//carousel.css({left: 0});
	};

	return {
		init : function() {
			fetch();
		}
	}
}();


/**
 * Vipcard
 *
 */
var Vipcard = function() {

	/**
	 * Vipcard.init
	 * Re-format birthday to 00-00-0000
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		var patt, result;

		jQuery('#birthday').blur(function(){
			patt = new RegExp(/(\d{1,2}).{0,1}(\d{1,2}).{0,1}(\d{4})/);
			result = patt.exec(jQuery('#birthday').val());
			if (result) {
				result[1] = result[1].length == 1 ? '0'+result[1] : result[1];
				result[2] = result[2].length == 1 ? '0'+result[2] : result[2];
				result[3] = result[3].length == 2 ? '19'+result[3] : result[3];

				jQuery(this).val(result[1] + '-' + result[2] + '-' + result[3]);
			}
		});

		jQuery('#zipcode').blur(function(){
			patt = new RegExp(/(\d{4}).{0,1}(\w{2})/);
			result = patt.exec(jQuery(this).val());
			if (result) {

				result[2] = result[2].toUpperCase();
				jQuery(this).val(result[1] + ' ' + result[2]);
			}
		});
	};

	return {
		init : function() {
			init();
		}
	}
}();


/**
 * Catalog
 *
 */
var Catalog = function() {

	/**
	 * Catalog.init
	 * Adds js interaction to product filter sidebar
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		if (!jQuery(".block-layered-nav").length) return false;

		jQuery(".block-layered-nav dd").hide();
		jQuery(".block-layered-nav dt").addClass('jsified');
		jQuery(".block-layered-nav dt").click(function(){
			if (jQuery(this).hasClass('expanded')) {
				jQuery("#"+jQuery(this).attr('id')+'-content').slideUp('fast');
			}
			else {
				jQuery("#"+jQuery(this).attr('id')+'-content').slideDown('fast');
			}
			jQuery(this).toggleClass('expanded');
		});
	};

	/**
	 * Catalog.zoom
	 * Initiate colorbox zoom for product detail
	 *
	 * @param void
	 * @return void
	 */
	var zoom = function()
	{
		jQuery("#zoom-product").colorbox();
	};

	/**
	 * Catalog.backNav
	 * Create a back to previous page
	 *
	 * @param void
	 * @return void
	 */
	var backNav = function()
	{
		jQuery(".catalog-product-view .main").append(' \
			<p id="back-to-page"><a href="javascript:history.go(-1)">Naar vorige pagina</a></p> \
		');
	};

	return {
		init : function() {
			init();
			backNav();
			zoom();
		}
	}
}();

/**
 * RecentlyViewed
 *
 */
var RecentlyViewed = function() {

	/**
	 * RecentlyViewed.init
	 * JS interaction for collapse/expand recently viewed products
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		var header, el, recent;

		recent = jQuery('.block-recent .block-content');
		recent.css('display', 'none');

		header = jQuery('.block-recent h2');
		header.removeClass('active');
		header.click(function(){
			if (jQuery(this).hasClass('expanded')) {
				jQuery('.block-recent .block-content').css('display', 'none');
			}
			else {
				jQuery('.block-recent .block-content').slideDown('fast',
					jQuery('.block-recent .block-content').css('display', 'block')
				);
			}
			jQuery(this).toggleClass('expanded');
			el = jQuery(this);
			jQuery(this).toggleClass('active');
		});
	}

	return {
		init : function() { init(); }
	}
}();

/**
 * Vacancies
 *
 */
var Vacancies = function() {

	/**
	 * Vacancies.toggleAccordion
	 * Slide toggle interaction for vacancy types headers
	 *
	 * @param rowId integer
	 * @return void
	 */
	var toggleAccordion = function(rowId)
	{
		var contentId = '#vacancy-content_'+rowId;
		var content = jQuery(contentId);
		var headId = '#vacancy-head_'+rowId;
	    var head = jQuery(headId);

	    // collapse all items except the clicked item
	    jQuery('.vacancy-content:not('+contentId+')').slideUp('fast');
	    jQuery('.head:not(' +headId+ ')').removeClass('expanded');

	    if(content.css('display') == 'none') {
	        content.slideDown('fast');
	        head.addClass('expanded');
	    } else {
	        content.slideUp('fast');
	        head.removeClass('expanded');
	    }
	};

	/**
	 * Vacancies.toggleAccordion
	 * Trigger accordion slider upon click on vacancy type header
	 *
	 * @param void
	 * @return void
	 */
	var initIndex = function()
	{
	    jQuery('.vacancy-content:not(#vacancy-content_1)').slideUp('fast');
		jQuery('#vacancy-head_1').addClass('expanded');

		jQuery('#vacancy-list').click(function(e) {
			var target = jQuery(e.target);
		    if (!target.hasClass('head')) {
	            return true;
	        }
		    toggleAccordion(target.attr('id').substring(13));
	        return false;
	    });
	};

	return {
		init : function() {
			initIndex();
		},
		toggleAccordion : function(rowId) {
			toggleAccordion(rowId);
		}
	};
}();
