$('html').removeClass('nojs').addClass('js');

$.fn.floatLabel = function(){
	return $(this).each(function(){
		if ($(this).val() != '') 
			$(this).siblings('label').hide();
			
		$(this)
		.focus(function(){
			$(this).siblings('label').hide();
		})
		.blur(function(){
			if ($(this).val() == '')
				$(this).siblings('label').show();
		});
	})
}

$(function(){
  $(document).bind('live-property-view', function(){
    $("#property-view .link-floor-plan").live("click", function(){
  		$(this).addClass("active");
  		$(this).prev().removeClass("active");
  		$(this).siblings(".main-image, .thumbs").hide();
  		$(this).siblings(".floor-plan").fadeIn();
  	});

  	$("#property-view .link-photos").live("click", function(){
  		$(this).addClass("active");
  		$(this).next().removeClass("active");
  		$(this).siblings(".floor-plan").hide();
  		$(this).siblings(".main-image, .thumbs").fadeIn();
  	});

  	$("#property-view a.close").live('click', function(){
  		$("#property-view, #overlay").fadeOut();
  		return false;
  	});

  	$("#map-description").live('click', function(){
  		$(this).addClass('active');
  		$("#unit-types").removeClass('active').next().hide();
  		$(this).next().fadeIn();
  	});

  	$("#unit-types").live('click', function(){
  		$(this).addClass('active');
  		$("#map-description").removeClass('active').next().hide();
  		$(this).next().fadeIn();
  	});

  	$(document.body).click(function(e){
  		if ( $(e.target).parents('#property-view').length == 0 ) {
  			$("#property-view, #overlay").fadeOut();
  		}
  	});
  });
});

$("#login-info").ready(function(){
	$("#login-info input").floatLabel();
});

$("#renters").ready(function(){
	$("#renters input").floatLabel();
});

$.fn.switchRange = function(fade) {
	return this.each(function(){
		switchid = $(this).attr('id').replace(/Range/, '').toLowerCase();
		if($(this).is(':checked')) {
			$('#'+switchid+' .maximum').show();
			var del = $(this).siblings("input[type=hidden]");
			del.replaceWith('<span id="'+del.attr('name')+'" alt="'+del.val()+'" class="delete"></span>');
		} else {
			$('#'+switchid+' .maximum').hide();
			var del = $(this).siblings('span.delete');
			del.replaceWith('<input type="hidden" name="'+del.attr('id')+'" value="true" />');
		}
	});
}

$(document).ready(function(){
	$("input[id^=Range]")
		.switchRange()
		.bind('click', function(){
			$(this).switchRange(true);
		});
		
	$("button.ui-state-default, a.ui-state-default").hover(
		function(){ $(this).addClass("ui-state-hover"); },
		function(){ $(this).removeClass("ui-state-hover"); }
	);
});

function showErrors(json){
	var ids = [];
	$(json).each(function(i, field){
		ids[i] = field.id;
		if (field.message) {
			input = $("#"+field.id);
			input.parents('div.input:first').addClass('error');
			if (input.siblings('.error-message').length > 0) {
				input.siblings('.error-message').html(field.message);
			} else {
				$('<div class="error-message">' + field.message + '</div>')
					.data('input.id', field.id)
					.insertAfter(input);
			}
			input.change(function(){
				$(this).siblings('.error-message').fadeOut().remove();
			});
		}
	});

	$("div.error-message").each(function(i, errorDiv){
		invalid = $.inArray($(errorDiv).data('input.id'), ids);
		if (invalid < 0) {
			$(errorDiv).parents('div.error:first').removeClass('error');
			$(errorDiv).fadeOut().remove();
		}
	});
	
	if ( $(".error-message:first").size() > 0 ) {
		var errorTop = $(".error-message:first").offset().top;
		$(window).scrollTop(errorTop-20);
	}
}

function imageError($input) {
	var fileName = $input.val();
	var typePos = fileName.lastIndexOf('.') + 1;
	var fileType = fileName.slice(typePos).toLowerCase();
	
	if (fileType.match(/tif|tiff|png|jpeg|jpg|gif/) == null)  {
		alert("Please, upload a valid image file (PNG, JPG, GIF or TIF).");
		return true; // errors return true
	}
	
	return false;
}

function renderMap(el, lat, lng) {
	if (GBrowserIsCompatible()) {
	    var map = new GMap2(el);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lng), 13);
		var point = new GLatLng(lat, lng);

		var baseIcon = new GIcon();
		baseIcon.image = base+"/img/icons/map-pin.png";
		baseIcon.shadow = base+"/img/icons/map-pin-shadow.png";
		baseIcon.iconSize = new GSize(41, 34);
		baseIcon.shadowSize = new GSize(41, 34);
		baseIcon.iconAnchor = new GPoint(15, 32);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		map.addOverlay(new GMarker(point, baseIcon));
	}
}