﻿BSW = {

    Init: function () {
        this.FadeInPage();
        this.LoadCareers();
        this.HomeSlideshow();
        this.InitLoginForm();
        this.LabelInit();
        this.LoadMap();
        this.EmailForm();
    },
    EmailForm: function () {
        $("#contact-form input.text, #contact-form textarea").focus(function () {

            var initValue = $(this).val();

            if ($(this).val() == "Your Name" || $(this).val() == "Your Email Address" || $(this).val() == "Your Message") {
                $(this).val('');
            }

            $(this).focusout(function () {
                if ($(this).val() == '') {
                    $(this).val(initValue);
                }
            });
        });

        $(".email-button").click(function () {
            var me = $(this).parent().next("#contact-form");

            $(me).toggle('blind', 'fast');
            // To Do:
            // Toggle Only Open #contact-form(s)
        });

        $("#contact-form .button").click(function () {
            var valid = BSW.ValidateForm($(this).parent("#contact-form"));

            if (valid) {
                BSW.SubmitForm($(this).parent("#contact-form"));
            }
        });
    },
    ValidateForm: function (form) {

        // Update Checkpoints and add conditions when more required fields are added to the form.

        var output = false;
        var checkPoints = 2;
        var cleared = 0;

        var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if ($(form).find("#full-name").val().trim().length > 0 && $(form).find("#full-name").val().trim() != "Your Name") {
            $(form).find("span#name-required").hide();
            cleared++;
        } else {
            $(form).find("span#name-required").fadeIn('slow');
        }

        if ($(form).find("#email-address").val().trim().length > 0 && $(form).find("#email-address").val().trim() != "Your Email Address") {
            if (emailFilter.test($(form).find("#email-address").val().trim())) {
                $(form).find("span#email-required").hide();
                $(form).find("span#email-validator").hide();
                cleared++;
            } else {
                $(form).find("span#email-required").hide();
                $(form).find("span#email-validator").fadeIn('slow');
            }
        } else {
            $(form).find("span#email-required").fadeIn('slow');
        }

        if (cleared == checkPoints) {
            output = true;
        }

        return output;
    },
    SubmitForm: function (form) {
        $.ajax({
            type: 'post',
            url: '/CMSTemplates/bswing/Handlers/ContactFormHandler.ashx',
            data: {
                name: $(form).find("#full-name").val().trim(),
                email: $(form).find("#email-address").val().trim(),
                message: $(form).find("#message").val().trim(),
                recipient: $(form).find(".button").attr("data-value")
            },
            success: function (valid) {
                if (valid == "True") {
                    $(form).html("<p class='result-message'>Your message to " + $(form).find(".button").attr("data-value") + " has been sent successfully!</p>");
                    $(form).delay(5000).fadeOut('slow');
                } else {
                    $(form).html("<p class='result-message'>An error has occurred. Please try again later.</p>");
                }
            },
            error: function (msg) {
                $(form).html("<p class='result-message'>An error has occurred. Please try again later.</p>");
            }
        });
    },
    FadeInPage: function () {
        if ($(".content").length > 0) {
            $(".content").hide();
            $(".content").fadeIn(200);
        }
    },
    LoadMap: function () {
        if ($("#map-canvas").length > 0) {
            var latlng = new google.maps.LatLng(44.98799, -93.276671);
            var center = new google.maps.LatLng(44.99100, -93.266000);
            var marker;

            var myOptions = {
                scrollwheel: false,
                zoom: 14,
                center: center,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

            marker = new google.maps.Marker({
                map: map,
                draggable: false,
                animation: google.maps.Animation.DROP,
                position: latlng,
                icon: '/CMSTemplates/bswing/Images/maps-icon.png'
            });

            var label = new BSW.Label({ map: map });
            label.bindTo('position', marker, 'position');
            label.bindTo('text', marker, 'position');

            setTimeout(function () { label.show(); }, 1500);

            google.maps.event.addListener(marker, 'click', function () {
                label.show();
            });
        }
    },
    Label: function (options) {
        this.setValues(options);
        this.labeldiv = $('<div class="map-overlay" style="position:absolute;display:none;"></div>');
    },
    LabelInit: function () {
        if ($("#map-canvas").length > 0) {
            BSW.Label.prototype = new google.maps.OverlayView;

            BSW.Label.prototype.onAdd = function () {
                var pane = this.getPanes().overlayLayer;
                var _this = this;

                $(pane).append(this.labeldiv);
                $(pane).css('z-index', '1000');

                $(this.labeldiv).html($("#map-overlay").html());
                $(this.labeldiv).find('.map-close').click(function () { _this.hide(); }).css('cursor', 'pointer');

                var me = this;
                this.listeners_ = [
                    google.maps.event.addListener(this, 'position_changed', function () { me.draw(); }),
                    google.maps.event.addListener(this, 'text_changed', function () { me.draw(); })
                ];
            };

            BSW.Label.prototype.onRemove = function () {
                this.labeldiv.parentNode.removeChild(this.labeldiv);

                for (var i = 0, I = this.listeners_.length; i < I; ++i) {
                    google.maps.event.removeListener(this.listeners_[i]);
                }
            };

            BSW.Label.prototype.draw = function () {
                var projection = this.getProjection();
                var position = projection.fromLatLngToDivPixel(this.get('position'));
                var divheight = $(this.labeldiv).outerHeight();

                $(this.labeldiv).css('left', (position.x - 152) + 'px');
                $(this.labeldiv).css('top', (position.y - divheight - 40) + 'px');

            };

            BSW.Label.prototype.show = function () {
                $(this.labeldiv).css('display', 'block');
                $(this.labeldiv).css('opacity', '0');
                $(this.labeldiv).animate({ 'opacity': '1' }, 1000);
            };

            BSW.Label.prototype.hide = function () {
                $(this.labeldiv).animate({ 'opacity': '0' }, 500);
                setTimeout(function () { $(this.labeldiv).css('display', 'none'); }, 500);
            };
        }
    },
    LoadCareers: function () {

        $(".career").each(function () {
            $(this).find(".career-more").click(function () {

                var description = $(this).prev();

                if ($(description).css("display") == "none") {
                    $(this).html("Hide Description");
                    $(this).prev().slideDown();
                } else {
                    $(this).html("Read More");
                    $(this).prev().slideUp();
                }
            });
        });

    },

    HomeSlideshowSlides: []
    ,
    HomeSlideshowDots: []
    ,
    HomeSlideInterval: null
    ,
    HomeSlideshowCS: 0
    ,
    HomeSlideshow: function () {

        if ($("#home-slide-show").length > 0) {
            console.log("#home-slide-show");
        } else {
            return;
        }

        var _rSlidesSrc = [];


        var s1 = {
            path: "/CMSTemplates/bswing/Images/slide-mne.png",
            title: "Case Study : MN Energy Challenge",
            href: "/Work/MN-Energy-Challenge",
            teaser1: "Inspiring energy conservation. bswing worked with CEE, Center for Energy and Environment,  ",
            teaser2: "to transform a grassroots initiative into a targeted effort to change ",
            teaser3: "how Minnesotans think about and consume energy."
        };
        var s2 = {
            path: "/CMSTemplates/bswing/Images/slide-rb.jpg",
            title: "Case Study : Room & Board ",
            href: "/Work/Room-and-Board",
            teaser1: "Creating an e-commerce experience that matches high customer expectations.",
            teaser2: "",
            teaser3: ""
        };
        var s3 = {
            path: "/CMSTemplates/bswing/Images/slide-bmt.jpg",
            title: "Case Study : Pediatric Bone Marrow Transplant Center",
            href: "/Work/BMT",
            teaser1: "Helping parents and families understand the path to recovery. An immersive",
            teaser2: "photo documentary features real stories of nine families and their kids",
            teaser3: "going through the transplant journey."
        };
        var s4 = {
            path: "/CMSTemplates/bswing/Images/slide-drsears.jpg",
            title: "Case Study : Dr. Sears Family Essentials",
            href: "/Work/Dr--Sears-Family-Essentials",
            teaser1: "Inspired by and designed for today’s kids and parents. bswing moved",
            teaser2: "the Dr. Sears' brand identity from an author personality to a health food brand",
            teaser3: "focused on lifestyle and nurturing healthy families."
        };
        _rSlidesSrc.push(s1, s2, s3, s4);


        var _rSlides = [];

        var _r = Raphael("home-slide-show", 570, 290);


        for (var s = 0; s < _rSlidesSrc.length; s++) {
            var _x = s * 570;
            var _img = _r.image(_rSlidesSrc[s].path, _x, 0, 570, 183).attr({ "cursor": "pointer" }).click(function () { document.location = this._href; });
            _img._href = _rSlidesSrc[s].href;
            var opac = (s == 0) ? 1 : 0;

            var _txt1 = _r.text(0, 202, _rSlidesSrc[s].title).attr({ "opacity": opac, "text-anchor": "start", "font-family": "Open Sans", "font-size": "14px", "fill": "#E07400", "font-weight": "600" });
            var _txt2 = _r.text(0, 224, _rSlidesSrc[s].teaser1).attr({ "opacity": opac, "text-anchor": "start", "font-family": "Open Sans", "font-size": "13px", "fill": "#9a9a9a", "font-weight": "400" });
            var _txt3 = _r.text(0, 241, _rSlidesSrc[s].teaser2).attr({ "opacity": opac, "text-anchor": "start", "font-family": "Open Sans", "font-size": "13px", "fill": "#9a9a9a", "font-weight": "400" });
            var _txt4 = _r.text(0, 258, _rSlidesSrc[s].teaser3).attr({ "opacity": opac, "text-anchor": "start", "font-family": "Open Sans", "font-size": "13px", "fill": "#9a9a9a", "font-weight": "400" });

            BSW.HomeSlideshowSlides.push({ img: _img, txt1: _txt1, txt2: _txt2, txt3: _txt3, txt4: _txt4 });
        }

        _navs = [];


        var offset = 585 - (_rSlidesSrc.length * 20);

        for (var s = 0; s < _rSlidesSrc.length; s++) {

            var opac = (s == 0) ? 1 : .3;
            var n = _r.circle(offset + (s * 20), 200, 5).attr({ stroke: null, fill: "#666", opacity: opac, cursor: "pointer" });
            n._index = s;
            n.click(BSW.HomeSlideshowGo);
            BSW.HomeSlideshowDots.push(n);
        }

        BSW.HomeSlideInterval = setInterval(BSW.HomeSlideshowGo, 10000, "auto");

    }
    ,
    HomeSlideshowGo: function (auto) {


        if (auto != "auto" || auto != undefined) {
            clearInterval(BSW.HomeSlideInterval);
            BSW.HomeSlideInterval = setInterval(BSW.HomeSlideshowGo, 10000, "auto");
        }
        if (auto == "auto" || auto == undefined) {

            BSW.HomeSlideshowCS++;

            if (BSW.HomeSlideshowCS >= BSW.HomeSlideshowSlides.length) {
                BSW.HomeSlideshowCS = 0;
            }

        } else {
            BSW.HomeSlideshowCS = this._index;
        }

        var cs = BSW.HomeSlideshowCS;

        var b = cs * 570 * -1;
        for (var s = 0; s < BSW.HomeSlideshowSlides.length; s++) {

            var _x = b + (570 * s);
            BSW.HomeSlideshowSlides[s].img.animate({ x: _x }, 1000, "<>");

            var opactxt = (s == cs) ? 1 : 0;
            BSW.HomeSlideshowSlides[s].txt1.animate({ opacity: opactxt }, 500, "<>");
            BSW.HomeSlideshowSlides[s].txt2.animate({ opacity: opactxt }, 500, "<>");
            BSW.HomeSlideshowSlides[s].txt3.animate({ opacity: opactxt }, 500, "<>");
            BSW.HomeSlideshowSlides[s].txt4.animate({ opacity: opactxt }, 500, "<>");

            var opac = (s == cs) ? 1 : .3;
            BSW.HomeSlideshowDots[s].animate({ opacity: opac }, 1000, "<>");
        }

    }
    ,
    InitLoginForm: function () {
        if ($("label.inlined").length > 0) {
            $("label.inlined + .input-text").each(function (type) {
                if ($(this).val().length > 0) {
                    $(this).prev("label.inlined").addClass("has-text");
                }
                $(this).focus(function () {
                    $(this).prev("label.inlined").addClass("focus");
                });
                $(this).keypress(function () {
                    $(this).prev("label.inlined").addClass("has-text").removeClass("focus");
                });
                $(this).blur(function () {
                    if ($(this).val() == "") {
                        $(this).prev("label.inlined").removeClass("has-text").removeClass("focus");
                    }
                });
            });
        }
    }
}

$(document).ready(function () {
    BSW.Init();
});
