﻿function obfuscate(name, domain) {
    document.write("<"); document.write("a"); document.write(" "); document.write("h"); document.write("r"); document.write("e"); document.write("f"); document.write("="); document.write("'"); document.write("m"); document.write("a"); document.write("i"); document.write("l"); document.write("t"); document.write("o"); document.write(":");
    document.write(name);
    document.write(String.fromCharCode(64));
    document.write(domain);
    document.write("'"); document.write(">");

    if (arguments.length == 3) {
        document.write(arguments[2]);
    }
    else {
        document.write(name);
        document.write(String.fromCharCode(64));
        document.write(domain);
    }
    document.write("<"); document.write("/"); document.write("a"); document.write(">");
}

function obfuscateLong(email1, email2, subject, cc1, cc2, text) {
    document.write("<a href='mailto:" + email1 + "@" + email2 + "?subject=" + subject + "&cc=" + cc1 + "@" + cc2 + "'>" + text + "</a>");
}

function obfuscateMedium(email1, email2, subject, text) {
    document.write("<a href='mailto:" + email1 + "@" + email2 + "?subject=" + subject + "'>" + text + "</a>");
}

function obfuscateShort(email1, email2, text) {
    document.write("<a href='mailto:" + email1 + "@" + email2 + "'>" + text + "</a>");
}

function loadPicture(index, link) {
    $("#" + photoSrcId).attr("src", photoValues[index][0]);
    $("#" + photoTitleId).html(photoValues[index][1]);

    $("#" + link.id).siblings("a").removeClass('selected');
    $("#" + link.id).addClass('selected');

    return false;
}

var textboxCanClear = true;
function clearTextbox(textbox) {
    if (textboxCanClear) {
        $("#" + textbox.id).val('');
        textboxCanClear = false;
    }
}

var isMapInitialized = false;
function initTabs() {

    var ul = $('#tabs ul').eq(0);
    $('#tabs').children('div.TabTitle').each(function (index) {
        var tabTitle = $(this);
        var tabContent = tabTitle.next('div.TabContent');
        var tabTitleId = tabTitle.attr('id') != null ? tabTitle.attr('id') : '';

        ul.append('<li><a href="#Tabs-' + index + '" id="' + tabTitleId + '">' + tabTitle.html() + '</a></li>');
        tabTitle.remove();
        tabContent.attr('id', 'Tabs-' + index);
    });

    $('#tabs').tabs({
        show: function (event, ui) {
            if (!isMapInitialized && $('#map_canvas').is(":visible")) {
                googleMapInit();
                isMapInitialized = true;
            }
        }
    });
}

function changeSelectedTabs(id) {
    var index = $('#tabs a#' + id).parent().index();
    $('#tabs').tabs('select', index);
}

function addClass() {
    $(".contactLeftInnerTable tr td:first-child").each(function () {
        $(this).addClass("firstCol");
    });
    $(".contactLeftInnerTable td").each(function () {
        $(this).find("div:last").addClass("lastDiv");
    });
    $(".propertyTooltipContent tr").each(function () {
        $(this).find("td:last").addClass("last");
    });

}

function equalize() {
    $(".contactLeftInnerTable td").each(function () {
        $(this).find(".TDInner").css("height", $(this).height() + "px");
    });
}

function initGoogleMap(mapDivId, jsonAddressList) {
    var geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(0, 0);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
    if (jsonAddressList !== null) {
        for (var i = 0; i < jsonAddressList.length; i++) {
            if (jsonAddressList[i].Latitude !== "0" && jsonAddressList[i].Longitude !== "0") {
                geocodeLatLngOnGoogleMap(map, geocoder, jsonAddressList[i].Latitude, jsonAddressList[i].Longitude, jsonAddressList[i].IsCenteredOn, jsonAddressList[i].Title, jsonAddressList[i].InfoWindowContent, jsonAddressList[i].EnableInfoWindow);
            }
            else {
                geocodeAddressOnGoogleMap(map, geocoder, jsonAddressList[i].Address, jsonAddressList[i].IsCenteredOn, jsonAddressList[i].Title, jsonAddressList[i].InfoWindowContent, jsonAddressList[i].EnableInfoWindow);
            }
        }
    }
}

function geocodeLatLngOnGoogleMap(map, geocoder, latitude, longitude, centerMapOnAddress, title, infoWindowContent, enableInfoWindow) {
    var lat = parseFloat(latitude);
    var lng = parseFloat(longitude);
    var latlng = new google.maps.LatLng(lat, lng);

    placeMarkerOnGoogleMapLatLng(map, latlng, title, infoWindowContent, enableInfoWindow, centerMapOnAddress);
}

function geocodeAddressOnGoogleMap(map, geocoder, address, centerMapOnAddress, title, infoWindowContent, enableInfoWindow) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            placeMarkerOnGoogleMap(map, results[0].geometry.location, title, infoWindowContent, enableInfoWindow, centerMapOnAddress);
        }
    });
}

var defaultMarker;
function placeMarkerOnGoogleMapLatLng(map, latlng, title, infowWindowContent, enableInfoWindow, centerMapOnAddress) {
    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        title: title
    });
    
    if (centerMapOnAddress) {
        marker.icon = '/images/blue-dot.png';
    }

    if (enableInfoWindow) {
        var infowindow = new google.maps.InfoWindow({
            content: infowWindowContent
        });

        // Start of newly added code block
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

        if (centerMapOnAddress) {
            map.setCenter(marker.getPosition());
            defaultMarker = marker;
            setTimeout("google.maps.event.trigger(defaultMarker, 'click')", 500);
        }
    }
}

function placeMarkerOnGoogleMap(map, geoLocation, title, infowWindowContent, enableInfoWindow, centerMapOnAddress) {
    var marker = new google.maps.Marker({
        map: map,
        position: geoLocation,
        title: title
    });

    if (centerMapOnAddress) {
        marker.icon = '/images/blue-dot.png';
    }

    if (enableInfoWindow) {
        var infowindow = new google.maps.InfoWindow({
            content: infowWindowContent
        });

        // Start of newly added code block
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

        if (centerMapOnAddress) {
            map.setCenter(marker.getPosition());
            defaultMarker = marker;
            setTimeout("google.maps.event.trigger(defaultMarker, 'click')", 500);
        }
    }
}

function propertyTooltip() {
    $(".propertyLink").hover(
        function () {
            $(this).parent().find(".propertyTooltip").show();
        },
        function () {
            $(this).parent().find(".propertyTooltip").hide();
        }
    );
}

function tooltip() {
    $('.tooltip').tooltip({
        showURL: false,
        bodyHandler: function () { return $(this).attr('data-tooltip'); }
    });
}


$(document).ready(function () {
    $('.jq_watermark').watermark();
    tooltip()
    addClass();
    equalize();
    if ($("#contactBlock table").hasClass("tab1")) {
        $("#contactFormTabs li").eq(0).addClass("selected");
    } else if ($("#contactBlock table").hasClass("tab2")) {
        $("#contactFormTabs li").eq(1).addClass("selected");
    }
    propertyTooltip();
    $(".propertyImage #thumbs li:nth-child(3n)").addClass("last");


    $(".memberContentLight tr").each(function () {
        $(this).find(".MemberContent:last").addClass("last");
    });
    initTabs();

    $("a[rel^='prettyPhoto']").prettyPhoto({ social_tools: false });

    $('.propertyImageGalley .thumbs li').click(
        function () {
            var index = $(this).index();
            $('.presentation-format').height($('.presentation-format img:visible').height());
            $('.presentation-format img:visible').hide();
            var target = $('.presentation-format img').eq(index);
            $('.presentation-format').animate({ height: target.height() });
            target.show();
        }
    );
    $('.docLink a[href$="pdf"]').attr('target', '_blank');
    searchCreiteriaTree();

    $('.document-list:not(:has(a))').remove();
    $('.docLink, .document-list').linktype();

    $('h1+ul.socialLinks').prev().css('padding-right', '60px');
});

function searchCreiteriaTree() {
    $('.checkbox-tree').checkboxTree({
        initializeChecked: 'expanded',
        initializeUnchecked: 'collapsed',
        onCheck: {
            ancestors: 'checkIfFull',
            descendants: 'check'
        },
        onUncheck: {
            ancestors: 'uncheck'
        }
    });
    $('.checkbox-tree input').click(function () {
        if (!document.checkboxTreeTimmer) { document.checkboxTreeTimmer = null; }
        window.clearTimeout(document.checkboxTreeTimmer);
        window.setTimeout(function () { $('input.checkbox-tree-submit').trigger('click'); }, 1000);
    });
}

/*
file type plugin
*/
(function ($) {
    var settings = {
        pattern: /\.[0-9a-z]+$/i,
        knownFileTypes: ['pdf', 'png', 'jpg', 'gif', 'bmp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'txt', 'pptx', 'zip', 'rar', 'gzip', 'arj', 'wav', 'mp3', 'aif', 'aiff', 'm4a', 'ogg', 'wma', 'psd', 'ai', 'swf', 'fla', 'css', 'js', 'avi', 'mov', 'wmv']
    };
    var methods = {
        init: function (options) {
            jQuery.extend(settings, settings, options);
            return this.each(function () {
                $('a', $(this)).each(function () {
                    var ext = $(this).attr('href').toLowerCase().match(settings.pattern);
                    if (ext.length > 0) { ext = ext[0].slice(1); }
                    if (jQuery.inArray(ext, settings.knownFileTypes) > -1) {
                        $(this).wrapInner('<span class="file-text"></span>').prepend('<span class="file-icon"></span>').addClass(ext).addClass('file-icon');
                    } 
                });
            });
        },
        destroy: function () {
            return this.each(function () { });
        }
    };
    $.fn.linktype = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.linktype');
        }
    };
})(jQuery);
