// JavaScript Document

$(document).ready(function() {
	
	// map/photo toggle (listing page)
	$(".butPhotos").click(function() {
		$("#mapWrap").hide();
		$("#photoWrap").show();
			return false;
	});
	$(".butMap,#linkShowMap").click(function() {
		$("#photoWrap").hide();
		$("#mapWrap").show();
			return false;
	});
	
	//event to update models list depending on make
	$('#SearchMakeId').change(function() {
		var make_id = $('#SearchMakeId').val();
		updateModelList(make_id);
	});
	
	/*if($('#SearchMakeId').val() > 0) {
		var make_id = $('#SearchMakeId').val();
		updateModelList(make_id);
	}*/
						   
	// form fields
	function clearField(targetField) {
	  var defText = $(targetField).attr('title');
	  if ($(targetField).val() == defText) {
		$(targetField).val(""); 
		$(targetField).css({'color' : '#000'});
	  }
	}
	function fillField(targetField) {
	  var defText = $(targetField).attr('title');
	  if ($(targetField).val() == "") {
		$(targetField).val(defText); 
		$(targetField).css({'color' : '#ccc'});
	  }
	}
	
	
	$(".boxContactOwner .name,.boxContactOwner .email,.boxEmailAlerts .name,.boxEmailAlerts .email,.boxMoreInfo .name,.boxMoreInfo .email,.boxAlerts .name,.boxAlerts .email,.boxContactOwner .msg,.boxMoreInfo .msg").focus(function() {
      clearField(this);
    });
	$(".boxContactOwner .name,.boxContactOwner .email,.boxEmailAlerts .name,.boxEmailAlerts .email,.boxMoreInfo .name,.boxMoreInfo .email,.boxAlerts .name,.boxAlerts .email,.boxContactOwner .msg,.boxMoreInfo .msg").blur(function() {
      fillField(this);
    });

});

function updateModelList(make_id) {
	var action = $.cake.http_root+'listings/update_model_list/'+make_id+'/search';
	$.get(
		action,
		null,
		function(data) {
			$('#SearchCarModelId').replaceWith(data);
		},
		'html'
	);
}	
