$(document).ready(function() {
  // user changes order of services, need to reload page
  var url;
  $('.service_order_select').change(function() {
    window.location.href = "/services/?search=true&q=" + window.qName + "&category=" + window.categoryName + "&order=" + $(this).val();
  });
  
  // user changes category, need to get statements from server
  $('#service_category').change(function() {
    var category = $(this).val();
    if(category.length == 0) {
      $('#statement-options').remove();
      return;
    }
    $(this).after(createLoader());
    var id = window.location.href.match(/services\/(.*)\//, '$1');
    if(id)
      id = id[1]    
    
    var url;
    if(id)
      url = '/service_statements/new?category=' + category + '&service_id=' + id
    else
      url = '/service_statements/new?category=' + category
      
    $.get(url, function(data) {
      $('#statement-options').remove();
      $('#service_category').after(data)
      hideLoaders();
    })
  })
})


// http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js
function usePointFromPostcode(postcode, callbackFunction) {
  var localSearch = new GlocalSearch();  
  localSearch.setSearchCompleteCallback(null,
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}



function centerMap(point)
{
  $.googleMap.maps["map_canvas"].setCenter(point);
  
  var icon = new GIcon();
  icon.image = "http://www.google.com/mapfiles/marker.png";
  icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  icon.iconSize = new GSize(20, 34);
  icon.shadowSize = new GSize(37, 34);
  icon.iconAnchor = new GPoint(10, 34);
  
  
	var marker = new GMarker(point,icon);
	$.googleMap.maps["map_canvas"].addOverlay(marker);
}


$(document).ready(function() {
  if(window.service_postcode) {
	  $("#map_canvas").googleMap(37, -122, 13, {
		  controls: ["GSmallMapControl", "GMapTypeControl"],
		  markers: $(".geo")
	  });
	
    usePointFromPostcode(window.service_postcode, centerMap);
  }
  
  service_id = $("#facebox_rate_link").attr("service_id")
  facebox_url = "<a id='facebox_rate_link' href='/services/" + service_id + "/ratings/facebox_new'>Rate this service</a>"

  $("#facebox_rating").html(facebox_url);

  $("#facebox_rate_link").click(function() {
	  
	  
	  //service_id = $(this).attr("service_id")
	
	  url = "/services/" + service_id + "/ratings/facebox_new"  
	 
	  $.facebox(function() {
		  $.get(url, function(data) {
			  $.facebox(data);
		  });
	  });
  	return false
  });

});

function addFormTextField(field_name, human_readable_field_name, insert_after_id, value) {
	$("#" + insert_after_id).after("<p id='service_" + field_name + "'><label for='" + field_name + "'>" + human_readable_field_name + "</label> <br /> <input id='service_" + field_name + "_input' type='text' size='30' name='service[" + field_name + "]' value='" + value + "'/>");	
}

function addFormTextAreaField(field_name, human_readable_field_name, insert_after_id, value) {
	$("#" + insert_after_id).after("<p id='service_" + field_name + "'><label for='" + field_name + "'>" + human_readable_field_name + "</label> <br /> <textarea id='service_" + field_name + "_input' type='text' size='30' name='service[" + field_name + "]'>" + value + "</textarea>");	
}

function removeFormField(field_name) {
	$("#service_" + field_name).remove();
	
}

function setupExtraDetailFields() {
	
	
	addFormTextField("email", "Email", "my_business", $("#hidden_email").attr("value"));
	addFormTextField("phone", "Phone", "service_email", $("#hidden_phone").attr("value"));
	addFormTextField("address1", "Street 1", "service_phone", $("#hidden_address1").attr("value"));
	addFormTextField("address2", "Street 2", "service_address1", $("#hidden_address2").attr("value"));
	
}

// this is only used on the new service page
$(document).ready(function() {
	
	if($("#my_business_input").is(":checked")) {
		setupExtraDetailFields();
	}
	
	$("#my_business_input").click(function(){
		
		if($("#my_business_input").is(":checked")) {
			
			setupExtraDetailFields();			
			
		}
		else{
			
			removeFormField("email");
			removeFormField("phone");
			removeFormField("address1");
			removeFormField("address2");
		}
		
	});
	
});

function setupReviewFields() {
	
	old_comment_content = $("#hidden_conversation").attr("value");
	
	if(old_comment_content.length > 0){
		comment_content = old_comment_content;
	}
	else {
		comment_content = "Write something..."
	}
	
	content = "<div class='service-green-heading' id='add-comment-heading'>"
	+ "<h1> Comments </h1>"
	+ "</div>"
	+ "<p id='add-comment-instruction'>"
	+ "Write a review, ask a question or suggest an idea</p>"
	+ "<input id='conversation[type]' type='hidden' value='Conversation' name='conversation[type]'/>"
	+ "<input id='hidden_conversation_choice' type='hidden' value='1' name='conversation[conversation_type]'/>"
	+ "<div class='conversation-panel'>"
	+ "<div class='conversation-options'>"
	+ "<ul>"
	+ "<li class='review-button-over' title='Write a review' rel='1'>"
	+ "<a> Write a Review </a>"
	+ "</li>"
	+ "<li class='question-button' title='Ask a question about' rel='2'>"
	+ "<a> Ask a Question </a>"
	+ "</li>"
	+ "<li class='idea-button' title='Suggest an idea' rel='3'>"
	+ "<a> Suggest an Idea </a>"
	+ "</li>"
	+ "</ul>"
	+ "</div>"
	+ "<div class='green-background'>"
	+ "<div class='conversation-input'>"
	+ "<textarea id='conversation-content' name='conversation[content]'>" + comment_content + "</textarea>"
	+ "<p>"
	+ "</p><br />"
	+ "</div>"
	+ "</div>"
	+ "</div>"
	$("#review").after(content);
	
	setupConversationLinks();
}

// this is only used on the new service page to allow users to add a review
$(document).ready(function() {
	
	if($("#review_input").is(":checked")) {
		setupReviewFields();
	}
	
	$("#review_input").click(function(){
		
		if($("#review_input").is(":checked")) {
			
			setupReviewFields();			
			
		}
		else{
			
			$(".conversation-panel").remove();
			$("#add-comment-heading").remove();
			$("#add-comment-instruction").remove();
		}
		
	});
	
});