Cufon.replace('h2 big, #navigation li a, .project .donate, .project .info, .join_us a, div.donate_link a', { hover: true });

(function($) {
	
	$.extend({
		settings: {
			gallery: {
				slide_speed: 6000,
				fade_speed: 1000
			},
			progress_animation_speed: 1800,
			actions: {
				get_movie: 'act/movie.php'
			}
		}
	});
	
	
	$.fn.extend({
		
		
		labelize: function() {
			return $(this).each(function() {
				var input = $(this);
				var label = $('label[for="'+input.attr('id')+'"]');
				
				input.width(input.width() + label.width());
				
				label.hide();
				
				var empty = function() {
					return input.val() == label.text() || input.val().length == 0;
				};
				
				input.focus(function() {
					if(empty())
						input.val('');
				});
				
				input.blur(function() {
					if(empty())
						input.val(label.text());
				}).trigger('blur');
			});
		},
		
		
		animate_progress: function() {
			return $(this).each(function() {
				
				var label = $(this).find('.label');
				var progbar = $(this).find('.progress');
				
				var progress = parseInt(label.text());
				var animinterval;
				
				var speed = $.settings.progress_animation_speed;
				
				label.text('0%');
				animinterval = setInterval(function() {
					label.text(Math.round(parseFloat(label.css('left'))) + '%');
				}, 60);
				
				progbar.width(0).animate({
					width: progress+'%'
				}, speed);
				
				label.css('left', 0).animate({
					left: progress+'%'
				}, speed, function() {
					clearInterval(animinterval);
				});
			});
		},
		
		
		newsletter_ajax: function() {
			return $(this).each(function(e) {
				var nl   = $(this);
				var form = $(this).find('form');
				
				form.submit(function(e) {
					e.preventDefault();
					
					nl.find('.loader').show();
					
					$.ajax({
						url: 		form.attr('action'),
						type: 		'post',
						dataType:	'json',
						data:		form.serializeArray(),
						complete:	function() {
							nl.find('.loader').hide();
						},
						success:	function(response) {
							nl.find('.response').remove();
							var message = $('<p class="response '+response.status+'">'+response.message+'</p>');
							
							if(response.status == 'ok') {
								form.find('fieldset').remove();
							}
							
							nl.prepend(message);
						},
						error:		function(xhr) {
							alert('Oops! An error has occurred. Please try again later.');
						}
					});
				});
			});
		},
		
		
		video_opener: function() {
			return $(this).click(function(e) {
				e.preventDefault();
				var videoc = $(this).siblings('.container');
				var   link = $(this);
				
				if(videoc.is(':hidden')) {
					videoc.css('opacity', 0).show();
					var org_width  	= videoc.width();
					var org_height 	= videoc.height();
					var org_left	= parseInt(videoc.position().left);
					var org_top		= parseInt(videoc.position().top);
					
					videoc.css({
						left: org_left + (org_left * 0.25)+'px',
						top: org_top + (org_top * 0.25)+'px',
						width: org_width * 0.5,
						height: org_height * 0.5
					});
					
					videoc.animate({
						opacity: 1,
						left: org_left+'px',
						top: org_top+'px',
						width: org_width,
						height: org_height
					}, 200, function() {
						if($(this).children().length == 0) {
							$(this).load($.settings.actions.get_movie + '?url=' + escape(link.attr('href')), function() {
								$(this).find('.close').click(function(e) {
									e.preventDefault();
									videoc.fadeOut(300);
								});
							});
						}
					});
				}
			});
		},
		
		// Justifies all elements to the largest height in the collection
		justifyheight: function() {
			var maxheight = 0;
			
			$(this).each(function() {
				var h = $(this).height();
				if(h > maxheight)
					maxheight = h;
			});
			
			$(this).height(maxheight);
			
			return $(this);
		},
		
		
		// Normal fading slideshow with interval
		slideshow: function() {
			return $(this).each(function() {
				var slideinterval;
				var gallery = $(this);
				var images = gallery.find('img');
				
				if(images.length > 1) {
					gallery.append(images.eq(0));
					
					var slide = function() {
						var newimg = gallery.find('img').eq(0);
						newimg.hide();
						gallery.append(newimg.fadeIn($.settings.gallery.fade_speed));
					};
					
					/*images.click(function() {
						clearInterval(slideinterval);
						slide();
					});*/
				
				
					var slideinterval = setInterval(slide, $.settings.gallery.slide_speed);
				}
			});
		},
		
		// make the big donation button (used on the project page)
		createBigDonationButton: function(){
			return $(this).each(function(){
				var submitButton = $(this).find(":submit");
				var submitText = submitButton.val();
				var label = $(this).find("label[for=" + submitButton.attr("id") + "]");
				var labelText = label.text();
				
				var theLinkAndStuff = '<div class="donate_link"><a href="">' + submitText + '</a><span>' + labelText + '</span></div>';
				
				$(this).find("fieldset").append(theLinkAndStuff);
				
				$("div.donate_link").css({
					backgroundImage: "url(/css/images/button_with_divider_442x62.png)",
					width: "442px",
					height: "62px"
				}).find("a").css({
					display: "block",
					height: "100%",
					width: "222px",
					float: "right",
					lineHeight: "62px",
					textAlign: "center",
					fontSize: "1.4em",
					fontWeight: "bold",
					textTransform: "uppercase"
				}).click(function(e){
					e.preventDefault();
					$(this).parents("form").submit();
				});
				
				$("div.donate_link").find("span").css({
					display: "block",
					height: "100%",
					width: "220px",
					float: "right",
					lineHeight: "62px",
					textAlign: "center",
					fontSize: "1.4em"					
				});
				
				label.remove();
				submitButton.remove();
			});
		}
		
	});
	
	
	// Document ready
	$(function() {
	
		// Animate project progress bars
		$('.progress_bar').animate_progress();
		
		// Send newsletter form as an ajax request
		$('.newsletter').newsletter_ajax();
		
		// Labelize inputs
		$('input[type="text"]').labelize();
		
		// Open video
		$('.video .open').video_opener();
		
		// Justify heights of projects to same height
		$('.project .content').justifyheight();
		
		// Article slideshow
		$('.article.gallery').slideshow();
		
		// Temp donate submit
		$('.donate').click(function(e) {
			e.preventDefault();
			$(this).parents('form').submit();
		});
		
		// make the big donation button (used on the project page)
		$("form.big_donation_button").createBigDonationButton();
		
		// all external links should open a new browser window/tab
		$("a[href^='http']").attr({
			target: "_blank"
		});
		
		
	});
	
	
})(jQuery);