
// JS to run on an application page once it's loaded
$(document).ready(function(){
	// set up tabs
	$("#app-tabs").tabs();

	
	// set up the lightbox
	$('div#about-screenshots a').lightBox({
		imageLoading:  '/images/lightbox-ico-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev:  '/images/lightbox-btn-prev.gif',
		imageBtnNext:  '/images/lightbox-btn-next.gif'
	});


	// set up FAQ accordian
	$("#app-faq-accordion").accordion({ header: "h4" });

	
	// set up div as dialog for bug details
	$('#bug-detail').dialog({ autoOpen: false, modal: true, width: "40%", height: "40%" });
	// set bug links to launch dialog 
	$(".bug-info-link").click(function() {  
		// temp static loading
		$('#bug-detail').dialog('open');  
		
		// for loading dialogs via AJAX
		//$(".bug-detail").load("http://url").dialog(); 
	});

	
	// set up div as dialog for bug submission
	$('#bug-submit-form').dialog({ autoOpen: false, modal: true, width: "40%" });
	//
	$("#bugs-submit-link").click( function() {
		// temp static loading
		$('#bug-submit-form').dialog('open');  
	});

	
	// set up the bug tracker form
	$("div#bug-submit-form form input#bug-submit-button").click(function() {  
		// grab the inputs
		var desc    = $("div#bug-submit-form form input#bug_description").val();
		var details = $("div#bug-submit-form form textarea#bug_details" ).val();
		var type    = $("div#bug-submit-form form select#bug_type"      ).val();
	
		// validate form
		
		// disable form elements
		
		// process form
		$.ajax({  
			type: "POST",  
			url: "Applications/APP_NAME/add-bug",  
			data: 'bug_description=' + desc + '&bug_details=' + details + '&bug_type=' + type,  
			success: function() {  
				alert('Successfully submitted bug report.');
			
				// show success message
				$('div#bug-submit-form').html("<div id='bug-submit-message'></div>");  
				$('div#bug-submit-message')
					.html("<h2>Submitted!</h2>")  
					.append("<p>Thanks for your input.</p>");  
				// kill dialog after 2 seconds
				setTimeout("$('#bug-submit-form').dialog( 'close' )", 2000);
				
				return false; 
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert('There was an error submitting your bug report.');
				
				// TODO: something more extensive for errors
				// NOTE: typically only one of textStatus or errorThrown will have info
				
				return false;
			}
		});  
		
		// always return false so the form doesn't post in the browser window
		return false;
	});  
	
});
