/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(type) {
  //loads popup only if it is disabled
  if (popupStatus == 0) {
    if (type == "MorePics") {
      $("#popupContact").fadeIn("slow");
    } else if (type == "GoogleMap") {
      $("#popupMap").fadeIn("slow");
    }

    $("#backgroundPopup").css({
      "opacity": "0.7"
    });

    $("#backgroundPopup").fadeIn("slow");

    popupStatus = 1;
  }
}

//disabling popup with jQuery magic!
function disablePopup() {
  //disables popup only if it is enabled
  if (popupStatus == 1) {
    $("#backgroundPopup").fadeOut("slow");

    $("#popupContact").fadeOut("slow");
    $("#popupMap").fadeOut("slow");

    popupStatus = 0;
  }
}

function findPos(obj) {
  var curleft = curtop = 0;

  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;

    } while (obj = obj.offsetParent);
  }

  return curtop;
}

//centering popup
function centerPopup(type, e) {
  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var documentHeight = document.getElementsByTagName('form')[0].clientHeight;

  if (type == "MorePics") {
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();

    //centering    
    $("#popupContact").css({
      "position": "absolute",
      "top": findPos(e) - 245,
      "left": windowWidth / 2 - popupWidth / 2
    });
  } else if (type == "GoogleMap") {
    var popupHeight = $("#popupMap").height();
    var popupWidth = $("#popupMap").width();

    // Center google maps popup
    $("#popupMap").css({
      "position": "absolute",
      "top": findPos(e) - 245,
      "left": windowWidth / 2 - popupWidth / 2
    });
  }
  var height = (document.documentElement.scrollHeight + "px");
  //only need force for IE6
  $("#backgroundPopup").css({
    "height": height,
    "width": windowWidth
  });
}

var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    //map.setCenter(new GLatLng(36.1671428, -94.1316884), 13);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
  } else {
    alert(" browser  is not compatible")
  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(address,
            function(point) {
              if (!point) {
                alert(address + " not found");
              } else {
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                address = address;

                var addressComma = address.indexOf(',', 0);
                var addressLine1 = address.substring(0, addressComma);
                var addressLine2 = address.substring(addressComma + 1, address.length);

                var markerInfo = document.createElement('z');
                markerInfo.innerHTML = "<div style='font-weight: bold'>Property Location</div><br />" + addressLine1 + "<br />" + addressLine2;
                map.openInfoWindow(map.getCenter(), markerInfo);

                document.getElementById('map_container').style.visibility = "visible";
              }
            }
          );
  }
}

function getProperty(propertyId) {
  $.ajax({
    type: "POST",
    async: false,
    url: "PropertyService.asmx/GetPropertyInformation",
    contentType: "application/json; charset=utf-8",
    data: '{"propertyId":' + propertyId + '}',
    dataType: "json",

    success: function(msg) {
      var pictures = msg.d.Pictures;

      var i = 0;
      var liPictures = document.getElementById("liPictures");

      var img = document.getElementById("img2");
      img.src = "";

      img = document.getElementById("img3");
      img.src = "";

      for (var key in pictures) {
        if (i == 0) {
          var mainImg = document.getElementById("imgMain");
          mainImg.src = "/Thumbnails/Thumbnail.ashx?size=400&src=" + pictures[key].Path;

          var imgMainList = document.getElementById("imgMainList");
          imgMainList.src = pictures[key].Path;

          var longDescription = document.getElementById("LongDescription");
          longDescription.innerHTML = pictures[key].LongDescription;

          var prop = document.getElementById("prop");
          switch (msg.d.PropertyType) {
            case 1:
              prop.innerHTML = '<b>Rental</b> -&nbsp;' + msg.d.Address;
            case 2:
              prop.innerHTML = '<b>For</b> Sale -&nbsp;' + msg.d.Address;
            case 3:
              prop.innerHTML = '<b>Leased</b> -&nbsp;' + msg.d.Address;
          }
        } else {
          img = document.getElementById("img" + i);

          if (img != null) {
            img.src = "/Thumbnails/Thumbnail.ashx?size=400&src=" + pictures[key].Path;
            img.src = "/Thumbnails/Thumbnail.ashx?size=150&src=" + pictures[key].Path;
          }
        }

        i++;
      }

      for (i = 2; i < 4; i++) {
        img = document.getElementById("img" + i);
        var div = document.getElementById("div" + i);
        if (img.src.indexOf('Thumbnails') == -1) {
          div.style.visibility = 'hidden';
        } else {
          div.style.visibility = 'visible';
        }
      }
    },

    error: function(msg) {
      alert("We're sorry, this was an error accessing this property.");
    }
  });
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {
  //
  // More Pics
  //
  $(".btn").click(function() {
    //centering with css
    centerPopup('MorePics', this);
    //load popup
    loadPopup('MorePics');
  });

  //CLOSING POPUP
  //Click the x event!
  $("#popupContactClose").click(function() {
    disablePopup();
  });

  //Click out event!
  $("#backgroundPopup").click(function() {
    disablePopup();
  });

  //
  // Google Map
  //
  $(".btnMap").click(function() {
    //centering with css
    centerPopup('GoogleMap', this);
    //load popup
    loadPopup('GoogleMap');
  });

  //CLOSING POPUP
  //Click the x event!
  $("#popupMapClose").click(function() {
    disablePopup();
  });

  //Click out event!
  $("#backgroundPopup").click(function() {
    disablePopup();
  });

  //Press Escape event!
  $(document).keypress(function(e) {
    if (e.keyCode == 27 && popupStatus == 1) {
      disablePopup();
    }
  });
});
