﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

if (window.console == undefined) {
    window.console = function () { };
}
if (window.console.log == undefined) {
    window.console.log = function () { };
}


// equal height body columns - http://www.cssnewbie.com/equal-height-columns-with-jquery/.  May need to switch to a CSS approach if this is visually noticeable
$(function () {
    function equalHeight(group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
    // this technique of getting the columns to be equal-height is breaking the text sizing
    // equalHeight($(".body-container > div"));
});


// search box label
$(function () {
    $(".searchbox").hint("searchboxBlur");
});


// stylesheet switching
$(function () {
    $.stylesheetInit();

    $("#txtSmall").bind("click", function () {
        var currStyle = readCookie('style');
        if (currStyle == "XLarge")
            $.stylesheetSwitch("Large")
        else if (currStyle == "Large")
            $.stylesheetSwitch("Small");
    });
    $("#txtLarge").bind("click", function () {
        var currStyle = readCookie('style');
        if (currStyle == "Small")
            $.stylesheetSwitch("Large");
        else if (currStyle == "Large")
            $.stylesheetSwitch("XLarge");
    });
});


// image lightbox
$(function () {
    $(".floorplan-thumbnail a[rel='floorplan'], .community-home-gallery .thumb a").colorbox({
        transition: 'none',
        width: "90%",
        height: "90%",
        scalePhotos: true,
        preloading: true,
        onComplete: function () {
            $("#cboxShare").detach();
            $("#cboxContent").append("<a id='cboxShare' class='share a2a_dd' href='http://www.addtoany.com/share_save' alt='Share'>Share</a>");

            a2a_config.linkurl = document.location.protocol + "//" + document.location.host + document.location.pathname + "?plan=" + $(this).attr("planid");
            a2a_init('page');
            FixAddToAnyEmail();
        }
    });
});

// community pricing disclaimer toggle
$(function () {
    $(".community-pricing").toggle(function () {
        $(this).find(".disclaimer").fadeIn("slow");
    }, function () {
        $(this).find(".disclaimer").fadeOut("slow");
    });
});

// http://www.htmldog.com/articles/suckerfish/dropdowns/ - fix up hover menus for IE 6
sfHover = function () {
    var sfEls = document.getElementById("nav").getElementsByTagName("UL")[0].getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function () {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
jQuery.fn.hint = function (blurClass) {
    if (!blurClass) {
        blurClass = 'blur';
    }

    return this.each(function () {
        // get jQuery version of 'this'
        var $input = jQuery(this),

        // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

        function remove() {
            if ($input.val() === title && $input.hasClass(blurClass)) {
                $input.val('').removeClass(blurClass);
            }
        }

        // only apply logic if the element has the attribute
        if (title) {
            // on blur, set value to title attr if text is blank
            $input.blur(function () {
                if (this.value === '') {
                    $input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title

            // clear the pre-defined text when form is submitted
            $form.submit(remove);
            $win.unload(remove); // handles Firefox's autocomplete
        }
    });
};


// community picker
$(function () {
    $("#community-picker").change(function () {
        var val = $("#community-picker").val();
        if ("0" != val) {
            window.location = $("#community-picker").val();
        }
    });
});


// simple photo gallery
$(function () {
    $(".photo-gallery").each(function () {
        var gallery = $(this);
        gallery.find(".photo-gallery-images").cycle({
            timeout: 4000,
            prev: gallery.find(".photo-gallery-control-prev"),
            next: gallery.find(".photo-gallery-control-next"),
            fit: 1,
            pause: 1
        });
        /*
        var images = gallery.find(".photo-gallery-images > img");
        var activeImage = 0;
        var fadeDelay = 2000;

        images.each(function () {
        $(this).css("position", "absolute");
        $(this).offset(gallery.offset());
        });

        .click(function () { ChangeActiveImageBy(-1); });
        .click(function () { ChangeActiveImageBy(1); });

        function ChangeActiveImageBy(offset) {
        activeImage = (activeImage + offset) % images.size();

        images.each(function (i, img) {
        img = $(img);
        if (i == activeImage) {
        img.show().animate({ opacity: 1 }, fadeDelay);
        } else if (img.filter(":visible").size() > 0) {
        img.animate({ opacity: 0 }, fadeDelay, function () { img.hide(); });
        }
        });

        }
        */
    });
});


// Add-to-Any button re-build
function UpdateAddToAnyButton() {
    var oldShare = $("a.share");
    var newShare = $("<a href='http://www.addtoany.com/share_save' class='share a2a_dd'>Share</a>");
    oldShare.hide();
    newShare.insertBefore(oldShare);
    oldShare.detach();
    a2a_config.linkurl = document.location.href;
    a2a_init('page');
    FixAddToAnyEmail();
}


// Audio Player
var audioPlayerIsSetUp = false;
function SetupAudioPlayer() {
    if (!audioPlayerIsSetUp) {
        AudioPlayer.setup("/assets/player.swf", {
            width: 300,
            transparentpagebg: 'yes'
        });
        audioPlayerIsSetUp = true;
    }
}


// Alerts
$(function () {
    $("#alert .alert-message")
        .css("margin-top", "-10px")
        .animate({
            marginTop: 18
        }, 2500);
    $("#alert .alert-instructions")
        .css("margin-top", "-15px")
        .animate({
            marginTop: 3
        }, 2000);
}); 

/*
// sIFR
var whitney = {
    src: '/assets/whitney.swf', 
    fitExactly: true,
    wmode: 'transparent',
    transparent: true,
    ratios: [8, 1.41, 10, 1.33, 14, 1.31, 16, 1.26, 20, 1.27, 24, 1.26, 25, 1.24, 26, 1.25, 35, 1.24, 49, 1.23, 74, 1.22, 75, 1.21, 79, 1.22, 80, 1.21, 81, 1.22, 1.21]
};
sIFR.activate(whitney);
sIFR.replace(whitney, {
    selector: 'h1',
    css: ['.sIFR-root { background-color: #EEF1F3; color: #3B3B3B; }'],
    filters: {
        DropShadow: {
            distance: 1,
            color: '#CCCCCC',
            strength: 2
        }
    }
});

sIFR.replace(whitney, {
    selector: '#community-home-header h1',
    css: ['.sIFR-root { background-color: #E7ECEF; color: #3B3B3B; }']
});

sIFR.replace(whitney, {
    selector: 'h2',
    css: ['.sIFR-root { background-color: #EEF1F3; color: #333333; }']
});

DoH3Replace({ selector: "h3" });
function DoH3Replace(mergeArgs) {
    var args = {
        css: [
            'a { color: #0077c0; text-decoration: none; }',
            'a:hover { color: #005E97; }',
            '.sIFR-root { background-color: #EEF1F3; color: #1B1B1B; }'
            ]
    };
    for(i in mergeArgs) { args[i] = mergeArgs[i];}
    sIFR.replace(whitney, args);
}
*/

// http://allinthehead.com/retro/338/supersleight-jquery-plugin - IE6 transparent PNG fix
jQuery.fn.supersleight = function (settings) {
    settings = jQuery.extend({
        imgs: true,
        backgrounds: true,
        shim: '/images/transparent.gif',
        apply_positioning: true
    }, settings);

    return this.each(function () {
        if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
            jQuery(this).find('*').andSelf().each(function (i, obj) {
                var self = jQuery(obj);
                // background pngs
                if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
                    var bg = self.css('background-image');
                    var src = bg.substring(5, bg.length - 2);
                    var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
                    var styles = {
                        'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
                        'background-image': 'url(' + settings.shim + ')'
                    };
                    self.css(styles);
                };
                // image elements
                if (settings.imgs && self.is('img[src$=png]')) {
                    var styles = {
                        'width': self.width() + 'px',
                        'height': self.height() + 'px',
                        'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
                    };
                    self.css(styles).attr('src', settings.shim);
                };
                // apply position to 'active' elements
                if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')) {
                    self.css('position', 'relative');
                };
            });
        };
    });
};

// Supersleight
$(function () {
    $(".homepage-resident-story").supersleight();
    $(".community-home-resident-story").supersleight();
});

$(function () {
    $("#a2amail_any_email,#a2apage_email_client").live("click", function () {
        //alert("aldsasdf");
        //return false;
    });
});

function FixAddToAnyEmail() {

    // Demonstration of how overriding the AddToAny email logic would work
    // Since this is not using their public API, it very well could quick working at any time.

    /** 
    $("#a2apage_email_client").attr("customserviceuri", "mailto:?subject=A2A_LINKNAME_ENC&body=Custom text before A2A_LINKURL_ENC and after");
    $("#a2apage_any_email").click(function () {
    $("#a2apage_dropdown").hide();
    var div = $("<div id='thingToShare'/>");
    div.append("<p>some random text</p>");
    div.dialog({ autoOpen: true, modal: true, title: 'E-mail' });

    return false;
    });
    **/
}

$(function () {
    $(".lightbox-popup a").click(function () {
        var e = $(this);
        var id = e.attr("embedid");
        var embed = $("#" + id);
        if (embed.length) {
            var args = { html: embed.text(), open: true, height: "80%", width: "80%" };
            var height = e.attr("popuph");
            if (height) {
                args.height = null;
                args.innerHeight = height + "px";
            }
            var width = e.attr("popupw");
            if (width) {
                args.width = null;
                args.innerWidth = height + "px";
            }
            $.fn.colorbox(args);
        }
        return false;
    });
});

$(function () {
    $(".clickable").click(function () {
        var href = $(this).attr("href");
        if (href) {
            window.location = href;
            return false;
        }
    });
});

// flowplayer configuration
var flowplayerConfig =
{
    key: '#$12394f2160e2e4615d5',
    screen: {
        height: "100pct",
        top: 0
    },
    clip: {
        autoPlay: false,
        autoBuffering: true
    },
    plugins: {
        controls: {
            timeColor: "#ffffff",
            borderRadius: "0px",
            slowForward: true,
            bufferGradient: "none",
            backgroundColor: "rgba(99, 99, 99, 0.2)",
            volumeSliderGradient: "none",
            slowBackward: false,
            timeBorderRadius: 0,
            time: true,
            progressGradient: "none",
            height: 20,
            tooltips: {
                marginBottom: 5,
                scrubber: true,
                volume: true,
                buttons: false
            },
            fastBackward: false,
            opacity: 1,
            volumeSliderColor: "#000000",
            bufferColor: "#0a0a0a",
            border: "0px",
            buttonColor: "#000000",
            mute: false,
            autoHide: {
                enabled: true,
                hideDelay: 500,
                mouseOutDelay: 500,
                hideStyle: "fade",
                hideDuration: 400,
                fullscreenOnly: true
            },
            backgroundGradient: "medium",
            width: "100pct",
            display: "block",
            buttonOverColor: "#FF0000",
            url: "flowplayer.controls-tube-3.2.1.swf",
            fullscreen: false,
            timeBgColor: "rgba(0, 0, 0, 1)",
            borderWidth: 2,
            scrubberBarHeightRatio: "0.22",
            bottom: 0,
            stop: true,
            sliderColor: "#cccccc",
            zIndex: 1,
            scrubberHeightRatio: "0.8",
            tooltipTextColor: "#D00000",
            sliderGradient: "none",
            timeBgHeightRatio: 0,
            volumeSliderHeightRatio: "0.4",
            name: "controls",
            volumeBarHeightRatio: "0.5",
            left: "50pct",
            tooltipColor: "#C9C9C9",
            playlist: false,
            durationColor: "#333333",
            play: true,
            fastForward: true,
            progressColor: "#ffffff",
            timeBorder: "none",
            volumeBorderRadius: 0,
            scrubberBorderRadius: 0,
            volume: true,
            scrubber: true,
            builtIn: false
        },
        gatracker: {
            url: "flowplayer.analytics-3.2.0.swf",
            trackingMode: "AS3",
            googleId: googleAnalyticsId
        }
    }
};
$(function () {
    flowplayer("a.video-player-link", "/assets/flowplayer.commercial-3.2.2.swf", flowplayerConfig);
});