

function ElsApp(){

    // App public contents:
    this.init = init;
    this.updateStyles = updateStyles;
    // App Private contents:

    function updateStyles () {
	$('a').filter("[href$=.pdf]").each(
	    function () {
		$(this).hover(
		    function() { $(this).addClass('with-icon'); }, 
		    function() { $(this).removeClass('with-icon'); }
		);
	    }
	);
	$('.sponsor-logo').parent().attr('target', '_blank');

	// Things that must be done after certain content is loaded:
	setupTotalUpdates(); //registration page
	setupAjaxForms();    //registration page


	setupFadingLink("a.trip-link", "./content-trip.html");
	setupBio("pitman");
	setupBio("cornez");
	setupBio("costanza");
	setupBio("levine");
	setupBio("felleisen");
	setupBio("demo");
	setupBack();
    }

    function setupFadingLink(id, target, prehooks) {
	$(id).click(function (){
			if (prehooks){
			    prehooks(this);			
			}
			$("#content").fadeOut("normal", function () {
						  $("#content").load(target,
								     null, 
								     function () {
									 updateStyles();
									 $("#content").fadeIn("normal", null);
								     }
								    );
						  return false;
					      });
			return false;
		    });
	return false;
    }

    function setupMenuButton(name, notatab) {
	setupFadingLink((notatab? "#right-sidebar div#" : "#tabs div#") + name, 
			"./" + name + ".html",
			function(thing){
			    // remove all selected-tab styles
			    $("div.selected-tab").each( function() { $(this).removeClass('selected-tab'); });
			    // make me a selected-tab
			    $(thing).addClass('selected-tab');
			});
    }


    function setupBio(name) {
	setupFadingLink("#"+name, "./content-" + name + ".html");
    }

    function setupBack() {
	$("a.back-link").each(function() {
				  var name = $(this).attr("href");
				  var name = name.replace(/\.\/content-/g, "");
				  var name = name.replace(/\-full.html/g, "");
				  setupFadingLink(this, "./content-" + name + ".html");
			      });
    }

    // pre-submit callback 
    function showRequest(formData, jqForm, options) { 
	// jqForm is a jQuery object encapsulating the form element.  To access the 
	// DOM element for the form do this: 
	// var formElement = jqForm[0]; 

	
	if (fieldsAreNotEmpty(jqForm[0])){

	    if (!isValidEmail(jqForm[0].email.value)) {

		$("#error-email-message").show();
		$("#personal-data input").each(function(){ $(this).change(function(){ $("#error-email-message").hide();}); });
		return false;
	    }

	    return true;

	} else {

	    $("#error-message").show();
	    $("#personal-data input").each(function(){ $(this).change(function(){ $("#error-message").hide(); }); });
	    $("span.required").each(function(){$(this).addClass("error-red");});
	    return false;

	}
    }

    function isValidEmail(str) {
	return (str.indexOf("@") > 0);
    }

    function fieldsAreNotEmpty(formObj) {
	//check required fields.
	return formObj.name.value != "" &&
	    formObj.surname.value != "" &&
	    formObj.email.value != "" &&
	    formObj.phone.value != "" &&
	    formObj.organization.value != "" &&
	    formObj.mailling_address.value != "";
    }



    function getTotalRegistrationPrice() {
	var total = 0;

	var isStudent      = $('#student').is(':checked');
	var bringsGuest    = $('#guest').is(':checked');
	var wantsTrip      = $('#trip').is(':checked');
	var guestWantsTrip = $('#tripguest').is(':checked');
	var viaTransfer    = $('#payment').val() == 'transfer';
	
	var limitDate=new Date();
	// 22 April 2010 (month is zero-indexed...)
	limitDate.setFullYear(2010,3,22);
	var today = new Date();
	var isLate = today>limitDate;

	if (isStudent) {
	    total += (isLate? 100 : 60);
	} else {
	    total += (isLate? 200 : 120);
	}

	if (bringsGuest) {
	    total += (isLate? 70 : 40);
	}

	if (wantsTrip) {
	    total += 90;
	}
	if (guestWantsTrip) {
	    total += 90;
	}

	if (viaTransfer) {
	    total += 10;
	}

	return '' + total;
    }

    function updateTotalRegistrationPrice() {
	var label = $('span#total-registration-cost');
	if (label.length > 0) {
	    // we are on the registration page, so proceed:
	    label.text(getTotalRegistrationPrice());
	}
    }

    function setupTotalUpdates() {
	$('.check').each(function () {
			     $(this).change(updateTotalRegistrationPrice);
			 });
	updateTotalRegistrationPrice();
    }

    function toggleCheckElement(element) {
	if(element.is(':checked')) {
	    $('#tripguest').removeAttr('disabled'); 
	    $('#tripguestrow').removeClass('quiet');
	} else {
	    $('#tripguest').attr('disabled', 'disabled');
	    $('#tripguestrow').addClass('quiet');
	    
	}
    }

    function setupDisabledFields () {
	toggleCheckElement($('#guest'));
	$('#guest').change(function () { toggleCheckElement($(this)); });
    }

    function setupAjaxForms() {
	var registrationForm = $('#register-form');
	// bind form using 'ajaxForm', when in registration page:
	if (registrationForm.length > 0){
	    registrationForm.ajaxForm({ target:       '#content-registration',   // target element(s) to be updated with server response 
					beforeSubmit: showRequest                // pre-submit callback 
				      });
	    $('#already-registered-form').ajaxForm({target: "#content-registration" });

	    setupDisabledFields();
	}
    }


    function init () {
	// run when DOM is ready
	$(function(){
	      updateStyles();

	      // setup link actions on menuX to replace page-contents
	      setupMenuButton("content-scope", false);
	      setupMenuButton("content-organization", false);
	      setupMenuButton("content-location", false);
	      setupMenuButton("content-programme", false);
	      setupMenuButton("content-registration", true);
	  });
    }


    
}



// load frameworks with google ajax api loader:
// google.load("jquery", "1.4.1");
google.load("jqueryui", "1");

var app = new ElsApp();

google.setOnLoadCallback(
    function () {
	app.updateStyles();
	$(document).ready(app.init);
    });






