﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min-vsdoc.js"/>
/// Using jQuery 1.4.2

// Ensure that the Ms namespace has been defined
if (typeof window.Ms == "undefined") {
    window.Ms = {};
}

Ms.Search = function (id, watermarkColor) {
    // Store the speed of the watermark fade animation
    this.FadeSpeed = 200;

    this.timer = null;

    // Store main control
    this.Control = $('#' + id);

    // Stores the watermark value
    this.WatermarkText = $(this.Control).find('.hpSrc_Textbox').attr('title');
    this.WatermarkColor = watermarkColor;

    // Store the original widths
    this.TextboxWidth = $(this.Control).find('.hpSrc_Textbox').width();
    this.BrandWidth = $(this.Control).find('.hpSrc_InnerBrand').width();

    // Stores value indicating if the brand image should toggle with the watermark
    this.BrandToggle = ($(this.Control).find('.hpSrc_InnerBrand.hpSrc_brandToggle').size() > 0);

    $(this.Control).find('.hpSrc_Button').click($.proxy(this.buttonClick, this));
    /*TODO: use mouse down for non IE browsers*/

    $($.proxy(this.init, this));
};

Ms.Search.prototype = {
    init: function () {
        // Enable the watermark
        if (typeof this.WatermarkText != "undefined" && this.WatermarkText != '') {
            // Add extra textbox for the watermark
            $(this.Control).find('.hpSrc_border').prepend('<input class="hpSrc_waterMark" type="text" readonly="readonly" style="display:none; color:' + this.WatermarkColor + ';" value="' + this.WatermarkText + '" />');

            // Bind show and hide watermark events
            $(this.Control).find('.hpSrc_Textbox')
            .blur($.proxy(this.showWaterMark, this))
            .focus($.proxy(this.hideWaterMark, this));

            // Shift the focus off the watermark textbox to the actual textbox
            $(this.Control).find('.hpSrc_waterMark').focus($.proxy(this.shiftFocus, this));

            if ($(this.Control).find('.hpSrc_Textbox').val().length > 0) {
                // Hide watermark and search brand if textbox is not empty on load
                this.hideWaterMark();
            } else {
                this.applyWaterMark();
            }
        }

        { // TODO: refactor into functions
            var timer;

            var showDropdown = function () {
                if (timer != null) {
                    window.clearTimeout(timer);
                    timer = null;
                }

                var width = $(".hpSrc_border").width();
                $(".hpSrc_Sources").width(width).slideDown(300);
                $(".hpSrc_Arrow").show();
            };

            var hideDropdown = function () {
                timer = window.setTimeout(function () { $(".hpSrc_Arrow").fadeOut(300); $(".hpSrc_Sources").slideUp(300); }, 300);
            };

            $(this.Control).find(".hpSrc_Textbox, .hpSrc_Button, .hpSrc_Sources a").focus(showDropdown).blur(hideDropdown);

            $(this.Control).find("ul a")
            .click(function (e) {
                e.preventDefault();

                $(".hpSrc_Sources li").removeClass("selected");

                var $this = $(this);
                $("#hpSrc_Source").val($this.parent("li").index(".hpSrc_Sources li"));

                var watermarkText = $this.parent("li").addClass("selected").find('a').text();
                $(".hpSrc_waterMark").val(watermarkText);
                $(".hpSrc_Textbox").attr("title", watermarkText);

                return false;
            });
        }
    },

    showWaterMark: function () {
        if (this.timer == null) {
            this.timer = window.setTimeout($.proxy(this.applyWaterMark, this), 1000);
        } else {
            this.applyWaterMark();
        }
    },

    applyWaterMark: function () {
        // Make sure the textbox is empty
        if ($(this.Control).find('.hpSrc_Textbox').val().length == 0) {
            // Show the watermark
            $(this.Control).find('.hpSrc_waterMark').fadeIn(this.FadeSpeed, function () {
                if (this.style && this.style.removeAttribute) {
                    this.style.removeAttribute('filter');
                }
            });

            if (this.BrandToggle) {
                // Show the brand
                $(this.Control).find('.hpSrc_InnerBrand').fadeIn(this.FadeSpeed);

                // Shrink the textbox to allow for the brand
                $(this.Control).find('.hpSrc_Textbox, .src_waterMark').width(this.TextboxWidth);
            } else {
                // Make the watermark textbox the same width as the actual textbox
                $(this.Control).find('.hpSrc_waterMark').width(this.TextboxWidth);
            }
        }
    },

    hideWaterMark: function () {
        if (this.timer != null) {
            window.clearTimeout(this.timer);
            this.timer = null;
        }

        // Hide the watermark
        $(this.Control).find('.hpSrc_waterMark').fadeOut(this.FadeSpeed, function () {
            if (this.style && this.style.removeAttribute) {
                this.style.removeAttribute('filter');
            }
        });

        if (this.BrandToggle) {
            // Hide the brand and enlarge the textbox to fill the brand area
            $(this.Control).find('.hpSrc_InnerBrand').fadeOut(this.FadeSpeed, $.proxy(this.enlargeTextbox, this));
        }
    },

    enlargeTextbox: function () {
        // First check to make sure the watermark is not visible again (due to IE bug)
        if ($(this.Control).find('.hpSrc_waterMark').css('display') == 'none') {
            // Enlarge the textbox to fill the brand area
            $(this.Control).find('.hpSrc_Textbox, .hpSrc_waterMark').width(this.TextboxWidth + this.BrandWidth);
        }
    },

    shiftFocus: function (e) {
        // Timeout ensures the cursor is enabled when the textbox receives focus in Chrome
        window.setTimeout($.proxy(function () { $(this.Control).find('.hpSrc_Textbox').get(0).focus(); }, this), 0);
    },

    buttonClick: function (e) {
        var currentUrl = window.location.href.toLowerCase();
        currentUrl = currentUrl.substr(currentUrl.indexOf('businessproductivity/') + 21);
        var countryCode = currentUrl.substr(0, currentUrl.indexOf('/'));
        var language;
        if (countryCode.indexOf('-') != -1) {
            language = countryCode;
        }
        else {
            language = "en-us";
        }

        var searchQuery = encodeURIComponent(this.Control.find('.hpSrc_Textbox').val());
        var thisSiteQuery = this.Control.find('.hpSrc_Textbox').val() + " " + "site:http://sharepoint.microsoft.com/businessproductivity";

        var thisSiteUrl = "http://www.bing.com/search?mkt=" + language + "&q=" + encodeURIComponent(thisSiteQuery) + "&form=MSSBCU";
        var mscomUrl = "http://search.microsoft.com/results.aspx?mkt=" + language + "&setlang=" + language + "&q=" + searchQuery + "&form=MSSBCUS";
        var bingUrl = "http://www.bing.com/search?mkt=" + language + "&q=" + searchQuery + "&form=MSSBCU";

        var searchType = $("#hpSrc_Source").val();
        if (searchType == 0) {
            window.open(thisSiteUrl);
        }
        else if (searchType == 1) {
            window.open(mscomUrl);
        }
        else {
            window.open(bingUrl);
        }
        return false;
    }
};

