submitName = '';

$(function() {
	initUI ();
});

function initUI () {
		
	/* select all */
		$("#select_all a").click( function (e) {
			e.preventDefault();
			$("input[type=checkbox]").attr('checked', true);
		});
		
	/* usertab */
		$("#user").click( function (e) {
			e.preventDefault();
			e.stopPropagation();
			$(this).toggleClass("useropt_open").next().slideToggle();
		});
		
		$("body").click(function () {
			$("#user").removeClass("useropt_open").next().slideUp();
		});
	
	/* tablesorter */
		$("#tablesorter").tablesorter();

	/* default_value */
		$("input[type=text], textarea").each(function () {

			labelTxt = $("label[for="+$(this).attr("id")+"]").text();
			
			try {
				labelClass = $("label[for="+$(this).attr("id")+"]").attr("class");
				$(this).attr("title",labelClass+' - '+labelTxt);
			} catch(err) {  }
			
			if ($(this).val()=='') { 
				$(this).val(labelTxt);	
			}
			
		}).focusin(function () {
			if ($(this).val() == $("label[for="+$(this).attr("id")+"]").text()) $(this).val("");
		}).focusout(function () {
			if ($(this).val() == '') $(this).val($("label[for="+$(this).attr("id")+"]").text());
		});
			
		$("select").each(function () {
			$(this).find("option:first").text($("label[for="+$(this).attr("id")+"]").text());	
		});

		$("form").submit(function (e) {
			$(this).find("input[type=text], textarea").each(function () {
				if ($(this).val() == $("label[for="+$(this).attr("id")+"]").text()) $(this).val("");
			});
		});
		
	/* select boxes */
		$("select").selectbox({
			effect: "slide"
		});	
		
	/* datepicker */
		var dates = $( "#date_start, #date_end" ).datepicker({
			minDate: 0,
			defaultDate: "+1M",
			changeMonth: true,
			onSelect: function( selectedDate ) {
				var option = this.id == "date_start" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
	
	/* tooltips - tipsy */
		$('a[title], img[title], #content .active').tipsy({gravity: $.fn.tipsy.autoNS});
		$('input[title], textarea[title]').tipsy({trigger: 'focus', gravity: $.fn.tipsy.autoWE});
		
	/* accordion */
		$( "div.accordion" ).accordion({ 
			autoHeight: false
		});
			
	/* carousel */
		$('#carousel').jcarousel({
			visible: 4,
			scroll: 4
		}); 
		
		$('#carousel img').click( function () {
			pos = $(this).parent().prevAll().length;
			$('#carousel img').removeClass("current");
			$(this).addClass("current");
			$("#big_preview a").removeClass("current");
			$("#big_preview a:eq("+pos+")").addClass("current");
		}).dblclick( function (e) {
			$("#big_preview a.current").click();
		}); 

	/* image zoom */
		$('#zoom').click( function (e) {
			$("#big_preview a.current").click();
		}); 
			
		$("a.fancybox").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200
		});
		
	
	/* validation */
	  
	  $("#listing_contact").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "listing_contact_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,subject,Please enter a subject.",
		  "required,message,Please enter your a message."
		]
	  });	  	  
	  
	  $("#listing_create.listing_cr_location").RSV({
		customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "listing_create_valRes",
		errorFieldClass: "errorField",
		rules: [
			"required,location[address],You must enter an Address for this listing",
			"required,location[city],You must enter a City for this listing",
			"required,location[state],You must enter a State/Provence for this listing",
			"required,location[zip],You must enter a Zip Code for this listing",
			"required,location[country],You must enter a Country for this listing",
			"required,location[visa_required],You must enter if a Visa is required to stay at this listing",
			"required,house[listing_type], You must enter the Type of House",
			"required,house[bedrooms],You must enter the number of bedrooms at this listing",
			"digits_only,house[bedrooms],Please use digits only",
			"required,house[max_people],You must enter the maximum number of people that could start at this listing",
			"digits_only,house[max_people],Please use digits only"
		]
	  }); 
	  
	  $("#listing_create.listing_cr_surrounding").RSV({
		customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "listing_create_valRes",
		errorFieldClass: "errorField",
		rules: [
			"required,transportation[airport_name],You must enter the name of the nearest Airport to this listing",
			"required,transportation[airport_distance],You must enter the approximate distance to the nearest Airport",
			"required,transportation[airport_unit],You must select the units for the distance to the nearest Airport",
			"required,town_base[town_name],You must enter the name of the nearest Town to this listing",
			"required,town_base[town_distance],You must enter the approximate distance to the nearest Town",
			"required,town_base[town_unit],You must select the units for the distance to the nearest Town"
		]
	  });	
	  
	  $("#listing_create.listing_cr_desc").RSV({
		customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "listing_create_valRes",
		errorFieldClass: "errorField",
		rules: [
			"required,description_house,You must enter a description of the House"
		]
	  });	
	  
	  $("#listing_create.listing_cr_dates").RSV({
		customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "listing_create_valRes",
		errorFieldClass: "errorField",
		rules: [
			"required,date_start,You must enter a start date",
			"required,date_end,You must enter an end date"
		]
	  });
	  
	  $("#login").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "login_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,username,Please enter your username.",
		  "required,password,Please enter your password."
		]
	  });		
	  
	  $("#forgotPassword").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "forgotPassword_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,email,Please enter your email address.",
		  "valid_email,email,Please enter a valid email address."
		]
	  });		
	  
	  $("#changePassword").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "changePassword_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,old_password,Please enter your old password.",
		  "required,new_password,Please enter your new password.",
		  "length>=8,new_password,Password minimum length is 8 characters.",
		  "same_as,new_password,new_password2,Passwords don't match"
		]
	  });	  
	  
	  $("#changeEmail").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "changeEmail_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,email,Please enter your email address.",
		  "valid_email,email,Please enter a valid email address."
		]
	  });	  
	  
	  $("#scholarshipInvite").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "scholarshipInvite_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,firstname,Please enter a first name.",
		  "required,lastname,Please enter a last name.",
		  "required,email,Please enter an email address.",
		  "valid_email,email,Please enter a valid email address."
		]
	  });		
	  
	  $("#scholarshipSignup").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "scholarshipSignup_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,firstname,Please enter a first name.",
		  "required,lastname,Please enter a last name.",
		  "required,username_reg,Please enter a user name.",
		  "length>=8,username_reg,User name minimum length is 8 characters.",
		  "required,new_password,Please enter your new password.",
		  "length>=8,new_password,Password minimum length is 8 characters.",
		  "same_as,new_password,new_password2,Passwords don't match"
		]
	  });	 
 	  
	  $("#personalInformation").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "personalInformation_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,personal[firstname],Please enter a first name.",
		  "required,personal[lastname],Please enter a last name.",
		  "required,dodds[school],Please enter a school.",
		  "required,dodds[last_year_of_work],Please enter the last year of work.",
		  "required,dodds[job_title],Please enter a job title.",
		]
	  }); 	  
	  
	  $("#contact").RSV({
	  customErrorHandler: myReturnFunction,
		displayType: "display-html",
		errorTargetElementId: "contact_valRes",
		errorFieldClass: "errorField",
		rules: [
		  "required,name,Please enter a name.",
		  "required,email,Please enter an email address.",
		  "valid_email,email,Please enter a valid email address.",
		  "required,message,Please enter your a message."
		]
	  });	

	  
	$("form :submit").live('click', function(event) {
	  submitName = this.name;
	});	  
	
	$("#massmail #send").click(function() {
	  return confirm("Are you sure you want to send this mail to all users?");
	});	
	
	$("#deleteThreads").click(function() {
	  return confirm("Are you sure you want to delete the selected conversations?");
	});
	
	/* image removal confirmation */
		$("#listing_images a").click( function () {
			return confirm("Are you sure you want to delete this image?");
		});
}

function myReturnFunction(f, errorInfo) {

	if (submitName!='back') {
		errorInfoMsg = "";
		
		// de-highlight all fields
			$(f).find("input, select, label, .sbHolder").removeClass("errorField");
			
		// highlight newly checked fields
			for (var i=0; i<errorInfo.length; i++)
			{
			  curEl = $(errorInfo[i][0]);
			  curEl.addClass("errorField");
			  if (curEl.is("select")) curEl.next().addClass("errorField");
			  if (curEl.is("input[type=radio]")) curEl.parent().addClass("errorField");
			  errorInfoMsg += "<li>"+errorInfo[i][1]+"</li>";
			}
			
			$(f).find("input").focusout();
		
		
		// display error message
			if (errorInfoMsg != "") {
				$(f).find("div.valRes").html("<ul>"+errorInfoMsg+"</ul>").css("display","block").focus();
				return false;
			}
		
		// empty default input value generated from label text before submit
			$(f).find("input[type=text], textarea").each(function () {
				if ($(this).val() == $("label[for="+$(this).attr("id")+"]").text()) $(this).val("");
			});
	}
}

