$(function() {	
	$("#destination input").change(function() {
		$("#destination li").removeClass("chosen");
		
		$("body").attr("id", $(this).attr("value").toLowerCase())
	
		$(this).parent().parent().addClass("chosen");
	});
	
	var teamNameFocus = $("input[name=teamname]");
	
	if (teamNameFocus.length) {
		teamNameFocus.focus();
	}
	
	$("a.addDesc").click(function() {
		$("#descForm").toggle();
		return false;
	});
	
	if ($("#msg").length) {
		$("#msg").slideDown();
	}
	
	$("a[rel=external]").bind("click", function() {
		window.open(this.href);
		return false;
	})

	$("#inviteForm a.addMore").live("click", function() {
		var numFields = $("#inviteForm input.emailField").length;
		
		if (numFields < 5) {
			var friendField = $(this).parent().clone();
			$("input", friendField).attr("value", "");
		
			$(this).parent().append(friendField)
		}
		$(this).remove();
		focusFields();
		
		return false;
	});
	
	focusFields();
	
	$("#pickDate").datepicker({
		minDate: new Date(2010, 3, 15),
		closeText: 'Lukk',
        prevText: '&laquo;Forrige',
		nextText: 'Neste&raquo;',
		currentText: 'I dag',
        monthNames: ['Januar','Februar','Mars','April','Mai','Juni',
        'Juli','August','September','Oktober','November','Desember'],
        monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun',
        'Jul','Aug','Sep','Okt','Nov','Des'],
		dayNamesShort: ['S&oslash;n','Man','Tir','Ons','Tor','Fre','L&oslash;r'],
		dayNames: ['S&oslash;ndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','L&oslash;rdag'],
		dayNamesMin: ['S&oslash;','Ma','Ti','On','To','Fr','L&oslash;'],
        dateFormat: 'dd.mm.yy', firstDay: 0
	});
	
	
	$("#inviteForm").validate({
		errorLabelContainer: $("div.errorContainer"),
		rules: {
			email: {
				required: true
			}, 
			msg: "required"
		}, // rules
		messages: {
			email: "Du må skrive inn en gyldig e-postadresse",
			msg: "Skriv inn en personlig hilsen"
		} // messages
	});
	
	$(".signupForm").validate({
		errorLabelContainer: $("div.errorContainer"),
		rules: {
			teamname: "required",
			email: {
				required: true,
				email: true
			}, 
			destination: "required",
			date_of_departure: "required"
		}, // rules
		messages: {
			teamname: "Navn på laget må fylles inn",
			email: "Du må skrive inn en gyldig e-postadresse",
			destination: "Velg en destinasjon",
			date_of_departure: "Velg en dato for avreise"
		} // messages
	}); // signupform, validate
});

function focusFields() {
    $('.clearDefault').addClass("idleField");  
    $('.clearDefault').focus(function() {  
        $(this).removeClass("idleField").addClass("focusField");  
        if (this.value == this.defaultValue){  
            this.value = '';  
        }  
        if(this.value != this.defaultValue){  
            this.select();  
        }  
    });  
    $('.clearDefault').blur(function() {  
        $(this).removeClass("focusField").addClass("idleField");  
        if ($.trim(this.value == '')){  
            this.value = (this.defaultValue ? this.defaultValue : '');  
        }  
    });  		
}
