// break out of frames
if (window != top) top.location.href = location.href;

var attending = null,
	errObject = null;

$(function() {
	// adjust body height so page height fills page
	pbte.adjustBodyHeight();

	// adjust height of submenu to match content box
	Array.max = function(array) {
		return Math.max.apply(Math, array);
	};
	$("#submenu_box").height(Math.max($("#submenu_box").height(), $("#content_box").height(), $("#sponsors_box").height()));

	// registration form
	var $registration_form = $("#registration_form");
	if ($registration_form.length) {
		$("#submit_btn").removeAttr('disabled');

		$registration_form.find('input, select')
			.focus( function() { $(this).addClass('focused'); } )
			.blur( function() { $(this).removeClass('focused'); });

		$registration_form.submit(function() {
			$(this).validate(pbte.options);
			var valid = $(this).valid();
			if (valid) {
				attending = $("#q_attend").val() == 'Yes' ? true : false;
				$("#submit_btn").attr('disabled','disabled').blur();
				// fail if process takes more than 6 seconds
				errObject = setTimeout(function() {
					alert('Error: The form could not be submitted. Please try again.\nIf it still doesn\'t work after several tries, please contact us.');
					$("#submit_btn").removeAttr('disabled');
				}, 6000);
				return true;
			}
			return false;
		});
	}

	// feedback form
	var $feedback_form = $("#feedback_form");
	if ($feedback_form.length) {
		$("#submit_btn").removeAttr('disabled');

		$feedback_form.find('input, textarea')
			.focus( function() { $(this).addClass('focused'); } )
			.blur( function() { $(this).removeClass('focused'); });

		// special actions to display/hide extra questions
		$(".extra-question").hide();
		$(".extraaction").click(function() {
			var $extra = $(this).parent().parent().next().next();
			if ($(this).val() === 'No') {
				$extra.show();
				$extra.find('.textinput').focus();
			}
			else {
				$extra.hide();
			}
		});

		$feedback_form.submit(function() {
			$("#submit_btn").attr('disabled','disabled').blur();
			return true;
		});
	}

	// past events gallery
	if ($("#past-events-gallery").length) {
		$("a.events-photo").attr('rel', 'gallery').fancybox({
			padding: 0,
			centerOnScroll: true,
			type: 'image',
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			changeFade: 0,
			titlePosition: 'over',
			titleFormat: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
			}
		});
	}
});

$.validator.messages.required = '';
$.validator.messages.email = '';

var pbte = {
	thankyou: function() {
		clearTimeout(errObject);
		$("#registration_form_fields").animate({height:'toggle'}, 1000, 'swing', function() {
			var confDiv = attending ? '#registration_form_confirmation' : '#registration_form_confirmation_no';
			$(confDiv).fadeIn();
		});
	},

	thankyouFeedback: function() {
		clearTimeout(errObject);
		$("#feedback_form_fields").animate({height:'toggle'}, 1000, 'swing', function() {
			$("#feedback_form_confirmation").fadeIn();
		});
	},

	adjustBodyHeight: function(){
		var wHeight = $(window).height(),
			pHeight = $("#wrapper").outerHeight();
		if (pHeight < wHeight) {
			var bHeight = wHeight - $("#header").outerHeight() - $("#footer").outerHeight();
			$("#main").height(bHeight);
		}
	},

	options: {
		errorContainer: 'div.registrationform_validationErrorMsg',
		rules: {
			'c_firstname': { required: true },
			'c_lastname': { required: true },
			'c_title': { required: true },
			'c_email': { required: true, email: true },
			'c_phone': { required: true },
			'c_company': { required: true },
			'c_address1': { required: true },
			'c_city': { required: true },
			'c_state': { required: true },
			'c_zip': { required: true },
			'q_attend' : { required: true }
		}
	},

	readCookie: function(name) {
		var nameEQ = name + '=',
			ca = document.cookie.split(';');
		for (var i=0; i<ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	}
};

if (typeof console === 'undefined') var console = { log: function() {} };
