// When the DOM is ready
$(document).ready(function() {
	
	/* -- Fade in images -- */
	
	$('.img img, .imgBig img, .imgVertical img').fadeOut(0);	
	
	$('.img img, .imgBig img, .imgVertical img').one('load', function() {
	  $(this).fadeIn(1000); 
	}).each(function() {
	  if(this.complete) $(this).load();
	});
	
	// Fancybox
	$('.img, .imgBig, .imgVertical, .fancybox, .fancy').fancybox({
		openSpeed : 'fast',
		closeSpeed : 'fast',
		prevSpeed : 'normal',
		nextSpeed : 'normal',
		prevEffect : 'fade',
		nextEffect : 'fade',
		closeClick : false,
		helpers : {
    		title : {
    			type : 'inside'
    		}
    	},
		padding: 5
	});

	/* -- Message bar -- */
	
	// When the close button is clicked
	$('#message-close').click(function($event) {
		// Animate messages out of view
		$('#message-bar-wrap').slideUp(100, function() {
			// When done, remove the message bar
			$(this).remove();
		});
		
		// Overwrite default behavior of the link:
		$event.preventDefault();
		return false;
	});
	
	/* -- Input hints -- */
	
	$('input[type="text"][title], textarea[title]').inputHint();
	
	/* -- Popup window for My Account -- */
	
	$('.cart-my-account').click(function($e) {
		// Get currently showing popup window:
		var $currentWindow = $('.cwindow-wrapper');
		
		// If showing:
		if($currentWindow.length > 0) {
			// Then, stop here
			return false;
		}
		
		// Get a reference to the trigger element:
		var $trigger = $(this);
		var $offset  = $trigger.offset();
		
		// Load the popup window:
		$.ajax({
			url : jQueryBaseHref + '/account/popup',
			dataType : 'html',
			success : function(data) {
				// Append it to the page:
				$('body').append(data);
				
				// Position the popup:
				var $popup = $('.cwindow-wrapper');
				$popup.css({
					'position' : 'absolute',
					'top'      : $offset.top + $trigger.height() - 40,
					'left'     : ($offset.left + ($trigger.width() - $popup.width()) / 2),
					'z-index'  : 10000,
					//'opacity'  : 0
				});
				
				// Enable input hints in the popup window:
				$('input[title]', $popup).inputHint({});
				
				// Animate into view:
				$popup.show().animate({
					top      : $offset.top + $trigger.height() + 10
				}, 150, 'easeOutCirc');
			}
		});
		
		// We overwrite the default behavior of the button:
		$e.preventDefault();
	});
		
	// When the window is scrolled:
	$(window).scroll(function() {
		// Remove the currently showing popup window:
		$('.cwindow-wrapper').remove();
	});
	
	// When the window is resized:
	$(window).resize(function() {
		// Remove the currently showing popup window:
		$('.cwindow-wrapper').remove();
	});
	
	// Hide the menu when clicked away from it:
	$(document).mousedown(function(event) {
		// Get the popup window:
		var $popup = $('.cwindow-wrapper');
		
		// If present:
		if($popup) {
			// If clicked away from the popup
			if(! $popup.HitTestXY(event.pageX, event.pageY) && ! $('.cart-my-account').HitTestXY(event.pageX, event.pageY)) {
				// Remove the popup window:
				$('.cwindow-wrapper').remove();
			}
		}
	});
	
	/* -- WISH LIST FUNCTIONALITY -- */
	
//	// Whenever an "add to wishlist" link is clicked
//	$('a.wish-list').click(function() {
//		// Notify the user that this function is still in the works
//		alert('Deze functionaliteit is momenteel nog niet voorzien. Onze excuses voor het ongemak.');
//		
//		// Prevent default link functionality
//		return false;
//	});
});

/**
 * Toggle a html-element
 * 
 * This function will show or hide (animated) a element
 *
 * @param element jQuery element
 * @param bool displayFlag
 *		Set to TRUE in order to show the element, FALSE to hide
 * @return void
 */
function toggleElement(element, displayFlag, speed) {
	if (!$(element).is(':animated')) {
		(displayFlag) ? $(element).stop().slideDown(speed) : $(element).stop().slideUp(speed);
	}
}
