﻿/* © mySupermarket Limited, 2005-2010. All rights reserved. */

/*=========================
MbiBrandInput
=========================*/
MSBICS.UserControls.HomePage.BrandInput = new Object();

MSBICS.UserControls.HomePage.BrandInput.Select = function()
{
    var wrappedInput = MSBICS.UserControls.HomePage.BrandInput.Obj;
    var value = wrappedInput.attr("value");
    if ((value != "") && (value != MSBICS.UserControls.HomePage.BrandInput.WaterMarkText))
    {
        wrappedInput.search();
    }
    else
    {
        window.location = MSBICS.PageResources.Marketing_Regsiter;
        //  wrappedInput.flushCache();
        //   $(".MbiBrandInput .TextInput").addClass("Error");
        //  $(".MbiBrandInput #ErrorLabel").addClass("Error").html("Please enter a brand name for your free access");
        //  MSBICS.Analytics.GoogleAnalytics.Track("/Events/Errors/BrandInputEmpty.aspx");
    }
}

MSBICS.UserControls.HomePage.BrandInput.ItemSelected = function(event, iItem)
{
    if (iItem)
    {
        var url = MSBICS.PageResources.Marketing_Regsiter + "?" + MSBICS.QueryParamKeys.BrandID + "=" + iItem.id;
        MSBICS.Navigation.Navigate(url, false);
    }
    else
    {
        var wrappedInput = MSBICS.UserControls.HomePage.BrandInput.Obj;
        wrappedInput.flushCache();
        $(".MbiBrandInput .TextInput").addClass("Error");
        $(".MbiBrandInput #ErrorLabel").addClass("Error").html("Unrecognized brand name, please re-enter name");
        var trackingQueryParam = MSBICS.Utils.String.FormatTrackingValue(wrappedInput.val());
        MSBICS.Analytics.GoogleAnalytics.Track("/Events/Errors/InvalidBrand.aspx?Query=" + trackingQueryParam);
    }
}

MSBICS.UserControls.HomePage.BrandInput.OnHover = function()
{
    $(this).toggleClass("Hover");
}

MSBICS.UserControls.HomePage.BrandInput.Init = function(iInitData)
{
    var extraParams = new Object();
    extraParams[MSBICS.QueryParamKeys.BusinessViewContext] = iInitData.BusinessViewContext;

    MSBICS.UserControls.HomePage.BrandInput.WaterMarkText = iInitData.WaterMarkText;
    MSBICS.UserControls.HomePage.BrandInput.Obj = $(".MbiBrandInput .TextInput");
    var wrappedInput = MSBICS.UserControls.HomePage.BrandInput.Obj;

    wrappedInput.autocomplete(iInitData.Url,
            {
                minChars: 0,
                width: iInitData.Width,
                autoFill: false,
                scroll: false,
                extraParams: extraParams,
                matchContains: false,
                matchSubset: false,
                mustMatch: false,
                selectFirst: false,
                delay: 0,
                max: 10,
                parse: function(data)
                {
                    return $.map(eval(data), function(item)
                    {
                        return {
                            data: item,
                            value: item.name,
                            id: item.id,
                            result: item.name
                        }
                    });
                },
                formatItem: function(iItemData, iItemIndex, iListLength, iquery)
                {
                    return iItemData.name;
                },
                highlight: function(value, term)
                {
                    return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/i, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "i"), "<strong>$1</strong>");
                }
            });


    wrappedInput.result(MSBICS.UserControls.HomePage.BrandInput.ItemSelected);

    wrappedInput.keydown(function(event)
    {
        if (event.keyCode == 13)
        {
            MSBICS.UserControls.HomePage.BrandInput.Select();
        }

        if ($(this).hasClass(iInitData.WaterMarkClass))
        {
            $(this).removeClass("WaterMark");
            var value = $(this).attr("value");
            value = value.substring(iInitData.WaterMarkText.length);
            $(this).attr("value", value);
        }

        if ($(this).hasClass("Error"))
        {
            $(this).removeClass("Error");
            $(".MbiBrandInput #ErrorLabel").removeClass("Error").html("");
        }
    });

    wrappedInput.focus(function(event)
    {
        if ($(this).hasClass(iInitData.WaterMarkClass))
        {
            $(this).select();
        }
    });

    wrappedInput.focus();

    wrappedInput.blur(function(event)
    {
        if ($(this).attr("value") == "")
        {
            $(this).addClass(iInitData.WaterMarkClass);
            $(this).attr("value", iInitData.WaterMarkText);
        }
    });

    wrappedInput.hover(MSBICS.UserControls.HomePage.BrandInput.OnHover,
                MSBICS.UserControls.HomePage.BrandInput.OnHover);
}

/*=========================
MbiTellAFriend
=========================*/
MSBICS.UserControls.TellAFriend = new Object();
MSBICS.UserControls.TellAFriend.Init = function()
{
    $(".MbiTellAFriend INPUT").keydown(function(event)
    {
        if (event.keyCode == 13)
        {
            MSBICS.UserControls.TellAFriend.Tell();
        }
    });
}
MSBICS.UserControls.TellAFriend.Tell = function()
{
    var email = $(".MbiTellAFriend INPUT").val();

    if (email == "")
    {
        return;
    }

    var url = MSBICS.PageResources.Ajax_TellAFriend + "?" +
        MSBICS.QueryParamKeys.Email + "=" + email

    $.get(url,
        function(data)
        {
            $(".MbiTellAFriend .EmailSent").css("visibility", "visible");
            $(".MbiTellAFriend INPUT").val("");
            setTimeout(
            function()
            {
                $(".MbiTellAFriend .EmailSent").css("visibility", "hidden");
            },
            5000)
        }
    );
}
