/***
Expo Agenda Creator scripts
***/

// Init stuff onload
jQuery(document).ready(function(){
	jQuery("input#exag_exhibitor_id_check").click(exag_updateCookie);											// Bind updateCookie() function to checkbox
	
	// Bind dialog open to email agenda link
	jQuery("a#exag_email_agenda_link").click(function() {
												jQuery('#exag_email_collector_box').dialog({height: 100, modal:true,title:'Please enter your email address' }); 
												return false;});		
	
	// Bind email collection to email submission box
	jQuery("#exag_email_submit").click(_exag_submitEmail);
	
	_exagInit();																								// Init stuff
});

// Update the cookie array (add/remove item)
function exag_updateCookie() {
	var exag_cookie = _getCookie('exag_exhibitors')					// Get cookie
	var checked = jQuery('#exag_exhibitor_id_check').attr('checked');	// Get 'checked' attribute
	var value = jQuery('#exag_exhibitor_id_check').attr('value');		// Get 'value' attribute
	
	// If check box is checked off
	if( checked == true )
	{
		exag_cookie.ids.push(value);
	}
	else
	{
		exag_cookie.ids = jQuery.grep(exag_cookie.ids, function(v) { return v != value; });
	}

	// Set cookie
	_setCookie('exag_exhibitors', exag_cookie);
	
	// Toggle display of feedback area
	_toggleFeedbackBoxDisplay();
	
	// Toggle checkbox text
	_toggleCheckboxText();
	
	_debug();
	return true;
}

// Inits
function _exagInit() {
	// Init the checkbox (checkoff if id is in the cookie ids array)
	var exag_cookie = _getCookie('exag_exhibitors')					// Get cookie

	// If the ID of this post is already in the array then checkoff the box
	if( jQuery.inArray(jQuery('#exag_exhibitor_id_check').attr('value'),exag_cookie.ids) != -1)
	{
		jQuery('#exag_exhibitor_id_check').attr('checked', 'checked');
	}

// Use styles to unhide components as necessary 
	jQuery('#exag_box').css('display','block');							// (make sure only javascript enabled browsers can see content)
	_toggleFeedbackBoxDisplay();										// Update user's feedback box	
	_toggleCheckboxText();												// Toggle text next to checkbox based on checked state

	return true;
}

// Toggle feedback box display
function _toggleFeedbackBoxDisplay() {
	var exag_cookie = _getCookie('exag_exhibitors')				// Init the checkbox (checkoff if id is in the cookie ids array)
	// If at least 1 id has been defined then unhide feedback box, else hide it	
	if( exag_cookie.ids.length > 0 )
	{
		jQuery('#exag_feedback_box').fadeIn();
	}
	else
	{
		jQuery('#exag_feedback_box').fadeOut();
	}	
	return true;
}

// Toggle checkbox text 
function _toggleCheckboxText() {
	if( jQuery('#exag_exhibitor_id_check').attr('checked') == true )
	{
		jQuery('#exag_check_text').text('Added To Agenda');
		jQuery('#exag_check_text').css('color', '#0a0');
	}
	else
	{
		jQuery('#exag_check_text').text('Add Exhibitor To Your Agenda');
		jQuery('#exag_check_text').css('color', '#000');
	}
}


// Submit email address via ajax
function _exag_submitEmail() {
	var emailInputBox = jQuery('#exag_email_collector_inputs');
	var emailInput = jQuery('#exag_email_input');
	var msgBox = jQuery('#exag_email_collector_messages');
	var exag_cookie = _getCookie('exag_exhibitors');
	
	// Very basic email validation
	if( emailInput.val().match(/.+@.+\..+/) != null )
	{
		// Destroy dialog box
		jQuery('#exag_email_collector_box').dialog('destroy');					
		
		// Redirect to thank you page with post ids and email address arguments
		var qsp = '&email=' + emailInput.val();
		for(var i=0; i<exag_cookie.ids.length; i++)
		{
			qsp += '&exag_id['+i+']='+exag_cookie.ids[i];
		}
		window.location.replace(EXAG_THANK_YOU_REDIRECT + qsp);					
	}
	else
	{
		msgBox.html('Please enter a valid email address.');						// Output msg
		msgBox.css('color', '#aa0000');											// Make msg red
	}
	
	return false;	// prevent submission
}

// Get the cookie
function _getCookie(name) {
	var exag_cookie = jQuery.cookie(name);					// Get cookie containing all ids selected thus far
	
	// If cookie doesn't exist then return object, else return JSON of cookie
	if( exag_cookie == null )
	{
		exag_cookie = new Object();
		exag_cookie.ids = new Array();
	}
	else
	{
		exag_cookie = jQuery.evalJSON(exag_cookie);	
	}

	return exag_cookie;
}

// Set the cookie
function _setCookie(name, value) {
	var exag_cookie = jQuery.toJSON(value);
	jQuery.cookie(name, exag_cookie, {expires: null, path: '/', domain: 'independenceexpo.org'});
}

// Do debug stuff
function _debug() {
	//var exag_cookie = _getCookie('exag_exhibitors');
	//var t = new Date();
	//jQuery('#exag_feedback_box').html( jQuery.toJSON(exag_cookie) + ' ' + t.getTime());
	return true;
}