var isClicked;
var BASE_URL = 'http://www.gcftaskforce.org/docs/';

jQuery(document).ready(function() {
	// signup form
	isClicked = false;    
	jQuery('#email').click(function(srcc) {
		if (!isClicked) {
			jQuery('#email').attr('class', 'clicked');
			jQuery('#email').attr('value', '');
		}
		isClicked = true;
		return false; 
	});
				
	jQuery('#signupform').submit(function(srcc) {
		process_submission();
		return false;
	});
    
	jQuery('#signup').click(function(srcc) {
		process_submission();
		return false;
	});

		// carousel initialization
		$(function() {
				$("#video-carousel").jCarouselLite({
						auto: 800,
						speed: 1500
				});
				$('ul#fader').innerfade({
						speed: 'slow',
						timeout: 5000,
						type: 'random',
						containerheight: '150px'
					});

		});
	
	// document tree
	
	// initialization
	jQuery("#category_list").jstree({
		"types" : {
			"types" : {
				"page_stack" : {
					"icon" : {
						"image" : "/icons/page_white_stack.png"
					}
				},
				"page_pdf" : {
					"icon" : {
						"image" : "/icons/page_white_acrobat.png"
					}
				},
				"page_error" : {
					"icon" : {
						"image" : "/icons/page_white_error.png"
					}
				},
				"page" : {
					"icon" : {
						"image" : "/icons/page_white.png"
					}
				}
			}
		},
		"themes" : {
			"theme" : "default",
			"dots" : false,
			"icons" : true
		},
		"plugins" : [ "themes", "html_data", "types" ]
	});
	
	// Bind the JSTree event. When a node is selected, dislay the associated documents
	jQuery("#category_list").bind("select_node.jstree", function(event, data) {
		if (typeof $(data.args[0]).attr("id") != 'undefined') {
			update_document_list($(data.args[0]).attr("id"));
		}
	});
});

// signup form
function process_submission() {
	var email = jQuery('#email').attr('value');
	// validate e-mail address
	var hasError = false;
	var errMsg = '';
	if (email.length == 0 || !isClicked) {
		hasError = true;
		errMsg = 'Enter e-mail above.';
	}
	else {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(!emailReg.test(email)) {
			hasError = true;
			errMsg = 'Invalid e-mail.';
		}
	}
	if (hasError) {
		jQuery('#message').attr('class', 'error');
		jQuery('#message').html(errMsg);
	}
	else {
		jQuery.post("/signup.php", jQuery("#signupform").serialize(), function callback(data) {
			jQuery('#message').attr('class', 'message');
			jQuery('#message').html('Thanks for signing up.');
			jQuery('#email').attr('value', '');
		});
	}				
}

function update_document_list(category_id) {
	// post data and listen for reply
	var exclude_subcats = 'FALSE';
	if (category_id.substr(category_id.length - 4) == '_doc') {
		// user has clicked on a document stack rather than a folder
		// as indicated by trailing '_doc'
		// remove trailing '_doc'
		category_id = category_id.substr(0, category_id.length - 4);
		// only show the documents in the stack (no subcategories)
		exclude_subcats = 'TRUE';
	}
	// AJAX post
	$.post(BASE_URL + 'get_html_doc_table', {'category_id': category_id, 'exclude_subcats': exclude_subcats}, function(htmlString) {
		var tag = $('#document_list');
		tag.html('');
		tag.html(htmlString);
	});
	return false;
}
