﻿function menu() {
    $('.level1 li').hover(
         function () {
             $('ul.level2', this).fadeIn('fast');
         },
         function () {
             $('ul.level2', this).fadeOut('fast');
         }
     );
}

function JumpToUrl(element) {
    var ddList = document.getElementById(element);
    var newPage = ddList.options[ddList.selectedIndex].value;
    if (newPage != "") {
        window.open(newPage); 
    };
}


function updateDetails(neighborhood) {
    if ((neighborhood) && (neighborhood != '')) {

        neighborhood = $.trim(neighborhood);

        if (neighborhood == 'Denver') {
            return;
        }

        $.get('/PageTemplates/Template58/mapData.xml', function (data) {

            $(data).find("zone").each(function () {

                name = $(this).attr("Name");
                link = $(this).attr("Link");

                if (name == neighborhood) {
                    window.location.href = link;
                }

            });
        });

        window.onunload = function () {
            window["__flash__removeCallback"] = function (instance, name) { return; };
        }

    }
}


function neighborhood() {
    url = window.location.href;
    url += url.toLowerCase();
    if ((url.indexOf('neighborhood') > -1) && ($('.flash_map').length)) {
        $('.flash_map').flash({
            src: '/pagetemplates/template58/map.swf',
            width: 530,
            height: 530,
            flashvars: { mapData: '/pagetemplates/template58/mapData.xml'}
        });
                
        $(".resource_container").load("/pagetemplates/template58/resources.htm");
    };
}


function checkPageTitles() {

    if ($(".ba_find_agentresults")[0]) {
        $("h1.pagetitle").html("PorchLight Agents");
    }

    if ($(".ba_formgeneral")[0]) {
        $("h1.pagetitle").html("Ask PorchLight a Question?");
    }

    if ($(".ba_customcontent")[0] &&  window.location.href.indexOf('List_Your_Home')!=-1) {
        $("h1.pagetitle").html("List with Porchlight");
    }
}


function GetMailToSpecialOffer() {

    $('.banner_ad').appendTo('.special_offers');

}


function slider() {
    $("#slider").slider({
        value: 100000,
        min: 0,
        max: 1000000,
        step: 10000,
        slide: function (event, ui) {
            $("#amount").val("$" + ui.value);
        }
    });
    $("#amount").val("$" + $("#slider").slider("value"));
}


function slideshow() {
    var $active = $('#slideshow div.active');

    if ($active.length == 0) $active = $('#slideshow div:last');
    var $next = $active.next().length ? $active.next() : $('#slideshow div:first');
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');
        });
}


function featured_slideshow_json() {

    if(typeof (featured_listing_json) != 'undefined' && featured_listing_json)
    {
        var items = [];

        $.each(featured_listing_json.listings, function (item, val) {
            items.push("<div class=\"featured_listing item" + item + "\">"
        + "<a href=\"" + val.propFriendlyURL + "\">"
        + "<img src=\"" + val.propPhotoPath + "\"/></a>"
        + "<span class='prop_price'>" + val.propPrice + "</span>"
        + "<span class='prop_address'>" + val.propAddress + "</span>"
        + "</div>");
        });

        $("<div/>", {
            "id": "slideshow",
            html: items.join("")
        }).appendTo(".propertyfeature .photo");

        $("#slideshow .featured_listing:first").addClass("active");

        setInterval("slideshow()", 4000);
    }

}

//doc ready
$(function () {
    menu();
    uiStateHover();
    neighborhood();
    checkPageTitles();
    GetMailToSpecialOffer();

    if ($(".ba_homepage").length > 0) {

        featured_slideshow_json();

        //homepage rss feed link fix
        $(".title").attr("target", "_self");
    };
});
