var submitInProgress = false;
var uploadInProgress = false;

$(document).ready(function(){
	$('#modal').jqm({ajax:'@href',modal:true,trigger:'.modal',onShow:function(h){h.w.fadeIn(500);},ajaxText:'<div class="loading"><img src="images/loading_d9d9d9.gif" /><a href="#" class="jqmClose">Cancel</a></div>'});
	
	var shoutText = $('#shout-text');	
	var defaultShout = "Got something to share? Give a shout and let everyone know!";
	shoutText.val(defaultShout);
	shoutText.focus(function() {
		if ($(this).val() == defaultShout) {
			$(this).val('');
		}
	});
	shoutText.blur(function() {
		if ($(this).val() == '') {
			$(this).val(defaultShout);
		}
	});
	
	$('#specials').supersleight({shim: 'images/x.gif'});
	$('.fancy-gallery').fancybox();
	
	$('div.shout').hover(
		function() {
			$(this).contents().find('.shout-stats').css({'display':'block'});
		},
		function() {
			$(this).contents().find('.shout-stats').css({'display':'none'});
		}
	);
	$('.shout-thumbs-up').click(function(event) {
		var href = $(this).attr("href");
		var me = $(this);
		event.preventDefault();
		$.get(href, function() {
			me.parent().parent().parent().animate({'border':'1px solid #8dc73f'}, 2000);
			me.parent().parent().slideUp('fast');
			me.parent().parent().next('.shout-status').html('Shout rated Thumbs Up!').slideDown('slow');
		});
	});
	$('.shout-thumbs-down').click(function(event) {
		var href = $(this).attr("href");
		var me = $(this);
		event.preventDefault();
		$.get(href, function() {
			me.parent().parent().parent().animate({'border':'1px solid #f26c4f'}, 2000);
			me.parent().parent().slideUp('fast');
			me.parent().parent().next('.shout-status').html('Shout rated Thumbs Down!').css({'color':'#f26c4f'}).slideDown('slow');
		});
	});
	$('#post').click(function(event) {
		event.preventDefault();
		if (submitInProgress == true) {
			var cancelSubmit = confirm("Are you sure you want to cancel your posting?\n\nYou will lose any data which you've already entered.");
			if(cancelSubmit == true) {
				$('#submit-bar .page').slideUp('slow', function() {
					$('#submit-bar .page').html('<span class="loading">Loading...</span>');
					submitInProgress = false;
				});
			}
		} else if (uploadInProgress == true) {
			var cancelSubmit = confirm("Are you sure you want to skip picture uploading?\n\nYour post will be submitted with no pictures.");
			if(cancelSubmit == true) {
				$('#submit-bar .page').slideUp('slow', function() {
					$('#submit-bar .page').html('<span class="loading">Loading...</span>');
					uploadInProgress = false;
				});
			}
		} else {
			$('#submit-bar .page').slideDown('slow', function() {
				$('#submit-content').load('submit.php?slideout=true', function() {
				});
			});
		}
	});
	
	var scroller = new FeaturedScroller();
	var autoScroll = setInterval(scroller.next, 5000);
	$('#specials-nav-next').click(function(event) {
		event.preventDefault();
		scroller.next();
		clearInterval(autoScroll);
	});
	$('#specials-nav-previous').click(function(event) {
		event.preventDefault();
		scroller.prev();
		clearInterval(autoScroll);
	});
	
	$('.success').parent().hide().slideDown('slow').animate({opacity: 1.0}, 1000).slideUp('slow');
	$('.failure').parent().hide().slideDown('slow').animate({opacity: 1.0}, 1000).slideUp('slow');
});

window.onbeforeunload = function() {
	if (submitInProgress == true) {
		return "You will lose any data which you've already entered.";
	} else if (uploadInProgress == true) {
		return "Your post will be submitted with no pictures.";
	}
}; 
