﻿var gallery;
var photos=null;
var galIdx = 0;
var currentAreaContent=null;
var burb;
var xspfPlayer = null;

var serviceUrl = "/modules/listingservice.aspx";

jQuery(document).ready(function() {

    jQuery("a[rel*=facebox]").facebox();

    if (jQuery("#gallery").length > 0) {

        if (jQuery("#photoContainer").length > 0 && photos != null) {
            gallery = new PhotoGallery(photos, window.innerWidth - 12, window.innerHeight - 12, galIdx, "photoContainer", "photoElement", 4000, true);
            gallery.load();

            if (gallery.autoStart)
                gallery.startSlideshow(gallery.interval);
        }
        else if (jQuery("#vtContainer").length > 0 && photos != null) {
            gallery = new VirtualTourGallery(photos, 0, "vtContainer");
            gallery.load();
        }
    }

    if (jQuery("#mapContainer").length > 0) {
        loadMap();
    }

    if (jQuery("#streetViewContainer").length > 0) {
        if (showStreetView) { loadStreetView(); }
    }

    if (jQuery("#areas").length > 0) {
        if (area) {
            if (burb) {
                if (jQuery("#l_" + area)) jQuery("#l_" + area).trigger("click");
            }

            expandTree();
        }
        else {
            jQuery("div.county[expanded=True]").find("span:first").trigger("click");
        }
    }

    MakeBoxesDraggable();

    // nag firing
    if (jQuery("#detailNag").length > 0) {
        jQuery("#nagBody").fadeIn(800);
    }
});

function MakeBoxesDraggable() {
    if (jQuery("#mapCriteriaTitle").length > 0) {
        //RestoreContolBoxPosition();

        jQuery("#controlBox").draggable({ cursor: "move", handle: "#mapCriteriaTitle", opacity: 0.7 });
        jQuery("#areaContainer").draggable({ cursor: "move", handle: "#mapCriteriaTitle", opacity: 0.7 });
        
    }

}

function AddVEUsageTracker(map) {
    tileTrackerSpec = new VETileSourceSpecification("VEUsageTracker", "/modules/veusagetracker.ashx?qkey=%4");
    tileTrackerSpec.Opacity = 0;

    if (map.GetTileLayerByID("VEUsageTracker") == null)
        map.AddTileLayer(tileTrackerSpec);
}

function LoadCompleteAreas(pid, page){
    if(jQuery("#otherareas").html() == "") {
        jQuery("#areastoggle").html(jQuery("#areastoggle").html() + " <img src='/img/wait.gif' alt='Loading...'/>");
        jQuery.get(serviceUrl, { action: "getCompleteAreas", page: page, pid: pid }, function(r) { OnLoadCompleteAreas(r); });
    }
}
function OnLoadCompleteAreas(r) {
    jQuery("#areastoggle").html("Complete Area List");
    
    jQuery("#otherareas").html(r);
    jQuery("#areastoggle").click(function() { jQuery("#otherareas").toggle(); });
    
    expandTree();
}
function LoadFeaturedAreas(pid, page){    
    jQuery.get(serviceUrl, { action: "getFeaturedAreas", page: page, pid: pid }, function(r) { OnLoadFeaturedAreas(r); });
    
}
function OnLoadFeaturedAreas(r){
    jQuery("#areas").html(r);
}
function LoadSuburbs(areaId, parent, page, pid) {
    if (jQuery(parent).find("span:first").attr("loaded")=="1") return;
    jQuery.get(serviceUrl, { action: "getSuburbs", parentId: areaId, page: page, pid: pid }, function(r) { OnLoadSuburbs(r, parent); });
    
}
function OnLoadSuburbs(r, parentEl) {
    jQuery(parentEl).after(r);
    
    jQuery(parentEl).find("span:first").removeClass("areac").addClass("areae");
    jQuery(parentEl).find("span:first").attr("loaded","1");
    jQuery(parentEl).find("span:first").toggle(function(){
                                                   jQuery(parentEl).next("div.subareas").slideUp();
                                                   jQuery(this).removeClass("areae").addClass("areac");
                                               },
                                               function(){
                                                   jQuery(parentEl).next("div.subareas").slideDown();
                                                   jQuery(this).removeClass("areac").addClass("areae");
                                               })
    
    areaContainerId=null;
    expandTree();
}
function LoadSubTypes(pid, typeEleId) {
    jQuery.get(serviceUrl, { action: "subtypes", type: $(typeEleId).value, pid: pid }, function(r) { OnLoadSubTypes(r); });
    
}
function OnLoadSubTypes(r){
    if (jQuery("#subtypeHolder").length != 0) {
        jQuery("#subtypeHolder").html(r);
    }
}

function LoadCriteriaSuburbs(pid, typeEleId) {    
    jQuery.get(serviceUrl, { action: "getCriteriaSuburbs", areaslug: $(typeEleId).value, pid: pid }, function(r) { OnLoadCriteriaSuburbs(r); });    
}
function OnLoadCriteriaSuburbs(r){
    if (jQuery("#suburbsHolder").length != 0) {
        jQuery("#suburbsHolder").html(r);
    }
}
function ForceMapAll() {
    forceMap = 1;
    map.MakeRequest();
}
function RestoreContolBoxPosition() {    
//    var position = Cookie.get("controlBox");
//    if (position == "") return;
//    
//    var apos = position.split(";");
//    $("controlBox").style.left = apos[0];
//    $("controlBox").style.top = apos[1];
}
function SetContolBoxPosition() {
    //Cookie.set("controlBox", $("controlBox").getLeft() + ";" + $("controlBox").getTop(), {duration: 365}); //1 year
}
function UpdateListingsStatus(total, unmapped, area) {    
    if (area.length != 0) area = " in " + area;
    if (burb.length != 0) area = " in " + burb;
    
    jQuery("#statusBox").html(total + " results (" + unmapped + " unmapped)" + area);
}
function SetStatusMessage(msg) {
    jQuery("#statusBox").html(msg);
}

function ShareIt() {
    jQuery("#currentUrl").val(map.GetCurrentLocation());
    jQuery("#shareBox").fadeIn(800);
    jQuery("#currentUrl").focus();
    jQuery("#currentUrl").select();
}
function CloseShareBox() {
    jQuery("#shareBox").hide();
}

function CopyToClipboard() {
    if (jQuery("#currentUrl").val().length != 0) {
        if(window.clipboardData && clipboardData.setData){
		    window.clipboardData.setData("Text", jQuery("#currentUrl").val());
	    }
    }
}

function SendMapInEmail() {
    location.href = "mailto:?subject=Real Estate Map Link&body=" + escape($("currentUrl").value);
}

function CloseNag(nagId) {
    jQuery(nagId).hide();
    jQuery("#xspf_player").remove();
}

function SendLogin(elEmail) {
    jQuery.post(serviceUrl, { action: "forgotlogin", email: jQuery(elEmail).val() }, function(r) { jQuery("<span class='ajaxRes'>" + r + "</span>").insertAfter(jQuery("#forgottitle")).fadeOut(4000); });    
}
