/* --------------------------------------------------------------------
	Mail a friend (incl. overlay)
-------------------------------------------------------------------- */

$(document).ready(function() {
		
	// vars
	var resizingInterval;
	var beenSubmitted = false;
	
	$('a.mailFriend, a.newsletter')
		.one('click', function(event) {
			$(this).attr('href', recode($(this).attr('href'))); 
		})
		.fancybox({
			'centerOnScroll': false,
			'frameWidth': 425,
			'hideOnContentClick': false,
			'overlayColor': '#50342F',
			'overlayOpacity': .6,
			'padding': 0,
			'callbackOnStart': function() {
				// remove some fancybox features
				$('#fancy_title, #fancy_left, #fancy_right').remove();
				
				// hide (for resize purposes)
				$('#fancy_outer').css('opacity', 0);
				
				if ($.browser.msie) {
					$(".fancy_bg").attr('style', '').fixPNG();
				}
			},
			'callbackOnShow': function() {
				// trigger input help
				inputHelp();
				
				// resize the fancybox
				var boxH = $('#fancy_ajax').height();
				var minT = $('#fancy_overlay').position().top;
				var boxT = Math.max(0,( $(window).height() - boxH ) / 2);
				
				$('#fancy_outer')
				.animate({height: boxH, top: minT + boxT}, 0)
				.animate(
					{opacity: "1"},
					250
				);
				
				// check for height changes
				function resizing() {
					if (boxH !== $('#fancy_ajax').height()) {
						boxH = $('#fancy_ajax').height();
						$('#fancy_outer').animate({
							height: boxH,
							top: $('#fancy_overlay').position().top + Math.max(0,( $(window).height() - boxH ) / 2)
						}, 250);
					};
				}
				resizingInterval  = setInterval(resizing, 50);
				
				// add behavior to the .cancel button
				$('#fancy_ajax a.cancel').live('click',function() {
					$.fn.fancybox.close();
					return false;
				});
				
				// add behavior to the .cancel button
				$('#fancy_ajax a.close').live('click',function() {
					$.fn.fancybox.close();
					return false;
				});
				
				// prevent the form to be submitted more then one time
				$('#fancy_ajax .mailFriend form').one('submit', function(event) {
					$(this).submit(function() {
						return false;
					});
				});
			},
			'callbackOnClose': function() {
				
				clearInterval(resizingInterval);
			}
		});
	
	/**
	 *	Input help
	 *  so the server will return a different view
	 */
	function inputHelp() {
		function switchText() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('').removeClass('hint');
			} else if ($.trim($(this).val()) == '') {
				$(this).addClass('hint').val($(this).attr('title'));
			}
		}
		
		$('#fancy_ajax input[type=text][title!=""]').each(function() {
			if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
			if ($(this).val() == $(this).attr('title')) $(this).addClass('hint');
		}).focus(switchText).blur(switchText);
		
		$('#fancy_ajax form').bind('before-submit', function() {
			$(this).find('input[type=text][title!=""]').each(function() {
				if ($(this).val() == $(this).attr('title')) $(this).val('');
			});
		});
		
		$('#fancy_ajax form').bind('after-submit', function() {
			inputHelp();
		});
	}

	/**
	 *	This method adjusts all URLs to contain a proper ajax parameter
	 *  so the server will return a different view
	 */
	function recode(url) {
		var base, params;

		if (url.indexOf("?") != -1) {
			base = url.substring(0, url.indexOf("?"));
			params = url.substring(url.indexOf("?"));
			
			return base + params + "&view=ajax";
		}
		else {
			return url + "?view=ajax";
		}
	}
});

