﻿
// Extend Number object with methods for converting degrees/radians
Number.prototype.toRad = function() {  // convert degrees to radians
    return this * Math.PI / 180;
}


// Calculate property's distance in miles from user's location
function milesFromPoint(propertyLat, propertyLng, pointLat, pointLng) {
    var R = 3959; // radius of earth in miles [6371km]
    var dLat = (pointLat - propertyLat).toRad();
    var dLon = (pointLng - propertyLng).toRad();
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                    Math.cos(propertyLat.toRad()) * Math.cos(pointLat.toRad()) *
                    Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return d;
}

function wordCount(s) {
    return s ? s.split(" ").length : 0;
}

$(function() {
    $("a[rel=external]").attr("target", "_blank");
    highlightFade();
});


function highlightFade() {
    $(".highlightFade").animate({ backgroundColor: "white" }, 5000);
}

$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

function setupAjaxVoting() {

    $("a.vote-for-object").unbind().click(function(e) {
        e.preventDefault();

        var url = $(this).attr("href");
        // For ease, the a has a class of a<entityid> eg a123, and the parent div has a class of that name, used for scoping
        var entityId = $(this).attr("id");
        var parentDivSelector = "div." + entityId;

        $.ajax(
                {
                    url: url,
                    success: function(data) {
                        $(parentDivSelector).html(data);
                        //setTimeout($("span.vote-message").animate({ backgroundColor: "#444444" }, 3000), 300);
                        //setTimeout($("span.vote-message").fadeOut(4000), 300);
                        setupAjaxVoting();
                    }
                }
            );
    });
};

function startSlideshow(pause) {

    $("div#treasure-slideshow #slides").cycle(
    {
        fx: 'fade',
        timeout: 5000,
        next: '#slideshow-controls a.next',
        prev: '#slideshow-controls a.prev',
        pause: pause, // pause on mouseover if 1
        /*height: '550px',*/
        cleartype: 1, // enable cleartype corrections
        cleartypeNoBg: false // see http://www.nabble.com/Problems-with-JQuery-Cycle-Plugin-and-IE6-td23452710s27240.html
    });

    $("ul#slideshow-controls a.pause-resume").click(function(e) {
        e.preventDefault();

        var cmd, text;
        if (!$(this).hasClass("paused")) {

            cmd = "pause";
            text = "Play";
        }
        else {
            cmd = "resume";
            text = "Pause";
        }

        $("div#treasure-slideshow #slides").cycle(cmd);

        $(this).text(text);
        $(this).toggleClass("paused");

    });
};


function ClientExceptionLogger() {

    function Log(message, url, line) {
        try {
            var data = "jsmessage=" + encodeURIComponent(message);
            data += "&jsurl=" + encodeURIComponent(url);
            data += "&jsline=" + encodeURIComponent(line);
            data += "&pageurl=" + encodeURIComponent(location.href);
            data += "&useragent=" + encodeURIComponent(navigator.userAgent);

            $.post(
		          "/site/jserror",
		          {
		              jsmessage: encodeURIComponent(message),
		              jsurl: encodeURIComponent(url),
		              jsline: encodeURIComponent(line),
		              pageurl: encodeURIComponent(location.href),
		              useragent: encodeURIComponent(navigator.userAgent)
		          },
                  function(data) { }
            );

        }
        catch (e) {
            // ignore logging errors
        }
    };

    this.Log = Log;

    return this;
};

// Log client browser javascript exceptions to the server
logger = new ClientExceptionLogger();
window.onerror = function(message, url, line) { logger.Log(message, url, line); }



