
$(document).ready(function(){
	init_contact_submit();

	if( $("#property_lat").length > 0 ){
		property_map_load($("#property_lat").val(), $("#property_lng").val(), $("#property_map_icon").val());
	}
});

function init_contact_submit(){
	$('#frm_contact_agent').bind("submit", on_contact_submit);
                $("#btn_contact_agent").bind('click', function(){
                    $("#sec").val('today');
                });
}

function on_contact_submit(){
	$("#btn_contact_agent")[0].disabled = true;
	$.post('/ajax/contact_agent.php',{
			property_pk: $("#frm_contact_agent input[name=property_pk]").val(),
			lang: $("#frm_contact_agent input[name=lang]").val(),
			url: $("#frm_contact_agent input[name=self_url]").val(),
			client_name: $("#client_name").val(),
			client_email: $("#client_email").val(),
			client_phone: $("#client_phone").val(),
			client_message: $("#client_message").val(),
                                                client_sec: $("#sec").val()
	}, function(){
		$("#btn_contact_agent")[0].disabled = false;
		var options = { path: '/', expires: 10 };
		$.cookie('contactagent_name', $("#client_name").val(), options);
		$.cookie('contactagent_email', $("#client_email").val(), options);
		$.cookie('contactagent_phone', $("#client_phone").val(), options);
		$.cookie('contactagent_message', $("#client_message").val(), options);
	}, 'script');
	return false;
}



//------------------------------------------------------------------------------------
// MAP FUNCTIONS
//------------------------------------------------------------------------------------

function property_map_load(lat, lng, icon_index){
	if (!GBrowserIsCompatible()) {
		return;
	}
	var point = new GLatLng(lat, lng);
	var map = new GMap2(document.getElementById("property_map"));
	window.property_map = map;
	map.setCenter(point, 16);
	map.addControl(new GMapTypeControl());
	map.addControl(new GSmallMapControl());
	map.disableInfoWindow();

	var markerManager = new MarkerManager(map);
	var marker = {
		type: 'property'
	};

	map_init_icons();
	var smallMarker = map_create_marker(point,{width:16, height:19, img: window.propertyIcons[icon_index].small}, marker);
	markerManager.addMarker(smallMarker, 0, 15);
	var bigMarker = map_create_marker(point,{width:32, height:38, img: window.propertyIcons[icon_index].big}, marker);
	markerManager.addMarker(bigMarker, 16, 17);

    panoramaOptions = { latlng:point };
    myPano = new GStreetviewPanorama(document.getElementById("property_map_street"), panoramaOptions);
    GEvent.addListener(myPano, "error", handleNoFlash);
}

// ------------------------------------------------------------------------------------
// PHOTO FUNCTIONS
//------------------------------------------------------------------------------------


function property_photo_show(id){
	$("img.detail-big-photo:visible").hide();
	$("img.detail-big-photo[photo_id=" + id + "]").show();

	$("#property_thumbs img.selected").css("border-color", "black").removeClass("selected");
	$("#property_thumbs img[photo_id=" + id + "]").css("border-color", "#EF7D1A").addClass("selected");
}

function property_photo_next(){
	var photo_id = parseInt($("#property_thumbs img.selected").attr("photo_id")) + 1;
	if( $("#property_thumbs img").length <= photo_id ){
		photo_id = 0;
	}
	property_photo_show(photo_id);
}

function property_photo_prev(){
	var photo_id = parseInt($("#property_thumbs img.selected").attr("photo_id")) - 1;
	if( photo_id < 0){
		photo_id = $("#property_thumbs img").length - 1;
	}
	property_photo_show(photo_id);
}

function slide_start(){
	if( window.timerID > 0){
		slide_stop();
		return;
	}
  $("#imgSlidePlay").hide();
	$("#imgSlidePause").show();
  timer_init();
}

function slide_stop(){
  // stop playing
	if( window.timerID > 0){
		$("#imgSlidePlay").show();
		$("#imgSlidePause").hide();
	  clearTimeout(window.timerID);
	}
}

function timer_tick(){
	property_photo_next();
  timer_init();
}

// --------------------------------------------------------------------------------

function timer_init(){
  window.timerID = setTimeout("timer_tick()",3000);
}

function handleNoFlash(errorCode) {
    if (errorCode==603 || errorCode == 600 || errorCode == 500) {
      $("#pano").hide();
      $("#property_map_street").hide();
      //alert("Error: Flash doesn't appear to be supported by your browser");
      return;
    }
}

