var i;

$(document).ready(function () {
    //Determine which is longer, sidebar or main content and set them so they are equal
    var sidebar = $("#sidebar");
    var content = $("#main_content");

    var sHeight = sidebar.height();
    var cHeight = content.height();


    if (sHeight > cHeight) {
        $("#main_content").height(sHeight);
        $(content).css("min-height", sHeight + "px");
    }
    else {
        $("#sidebar").height(cHeight);
        $(content).css("min-height", cHeight + "px");
    }
    i = 1;

    var slideShow = window.setInterval(slide, 7000);

    //Slide the photo panels on home page depending on which button was clicked
    $(".slide1button").click(function () {
        window.clearInterval(slideShow);
        $(".current").removeClass("current");
        $(this).addClass("current");
        $("#slider").scrollTo($("#slider .slide:nth-child(1)"), 1000);
    });

    $(".slide2button").click(function () {
        window.clearInterval(slideShow);
        $(".current").removeClass("current");
        $(this).addClass("current");
        $("#slider").scrollTo($("#slider .slide:nth-child(2)"), 1000);
    });

    $(".slide3button").click(function () {
        window.clearInterval(slideShow);
        $(".current").removeClass("current");
        $(this).addClass("current");
        $("#slider").scrollTo($("#slider .slide:nth-child(3)"), 1000);
    });

    $(".slide4button").click(function () {
        window.clearInterval(slideShow);
        $(".current").removeClass("current");
        $(this).addClass("current");
        $("#slider").scrollTo($("#slider .slide:nth-child(4)"), 1000);
    });

    //Scroll left and right for Our Properties feature on home page	
    $(".scroll_right").click(function (e) {
        e.preventDefault();
        var children = $(".properties > div").size();
        var width = ((children) * 201) - 6;
        $(".properties").width(width);
        var slider_content = $(".properties");
        var slider_container = $(".our_properties");
        var wDiff = (($(slider_container).width() - $(slider_content).width()));
        var offset = $(slider_content).position();
        if (offset.left > wDiff) {

            var newLeft = (offset.left - 201);
            if (newLeft <= wDiff) {
                newLeft = wDiff;
            }

            $(slider_content).animate({
                left: (newLeft + "px")
            }, 500)
        }
    });

    $(".scroll_left").click(function (e) {
        e.preventDefault();
        var children = $(".properties > div").size();
        var width = ((children) * 201) - 6;
        $(".properties").width(width);
        var slider_content = $(".properties");
        var slider_container = $(".our_properties");
        var wDiff = (($(slider_container).width() - $(slider_content).width()));
        var offset = $(slider_content).position();
        if (offset.left < 0) {

            var newLeft = (offset.left + 201);
            if (newLeft > 0) {
                newLeft = 0;
            }

            $(slider_content).animate({
                left: (newLeft + "px")
            }, 500)
        }
    });

    $(".property_image").mouseenter(function () {
        $(this).children("a").children("img").hide(400);
        $(this).children("a").children("span").css("display", "block");
        var marginTop = ($(this).height() * 0.5) - ($(this).children("a").children("span").height() * 0.5);
        $(this).children("a").children("span").css("margin-top", marginTop + "px");
        $(this).children("a").children("span").show("fast");
    }).mouseleave(function () {
        $(this).children("a").children("span").hide("fast");
        $(this).children("a").children("img").show("fast");
    });

    //clear email textbox
    $("#newsletter_signup .TextBoxField").click(function () {
        $(this).val("");
    });

    $("#newsletter_signup .TextBoxField").blur(function () {
        if ($(this).val() == "") {
            $(this).val("Please enter your e-mail address...");
        }
    });

    //color box for web pages
    $("a.owners_login, .office a.directions, a.reviews_link").colorbox({ opacity: "0.75", width: "75%", height: "75%", iframe: true, title: function () {
        var url = $(this).attr('href');
        return '<a href="' + url + '" target="_blank">Open In New Window</a>';
    }
    });

    $("a.virtual_tour").colorbox({ opacity: "0.75", width: "75%", height: "75%", iframe: true, title: function () {
        var url = $(this).attr('href');
        return '<a href="' + url + '" target="_blank">Open In New Window</a>';
    }
    });

    //colorbox for google map
    $(".office a.map").colorbox({ opacity: "0.75", width: "655px", height: "620px", iframe: true, title: function () {
        var url = $(this).attr('href');
        return '<a href="' + url + '" target="_blank">Open In New Window</a>';
    }
    });


    //override styles from Peter Blum calendar to provide correct rendering. This is a hack but I could not find another way.      
    $(".DES_CalControl").css("padding", "0px");
    $(".DES_CalControl, .DES_CalHeaderRows, .DES_CalHeader").width("214px");
    $("#sidebar .DES_CalControl, #sidebar .DES_CalHeaderRows, #sidebar .DES_CalHeader").width("293px");
    $(".DES_CalWeekRowsTable td").css("border-color", "#DACFBA");

});

//Auto-slide the photo panels on home page
function slide() {
    i = i + 1;
    if (i == 5) {
        i = 1;
    }
    var button = (".slide" + i + "button");
    $(".current").removeClass("current");
    $(button).addClass("current");
    $("#slider").scrollTo($("#slider .slide:nth-child(" + i + ")"), 1000);
}



