﻿(function (wo$)
{
    var targetControl;

    _getUrl = function (category, parameters)
    {
        var url = '/ContentFragment?type=PhonePickerList&category=' + category;
        if (parameters && parameters != '')
        {
            url += '&' + parameters;
        }
        url += '&t=' + targetControl.id;

        return url;
    };

    var methods =
        {
            init: function ()
            {
                return this.each(function ()
                {
                    targetControl = this;
                    var url = '/ContentFragment?type=PhonePicker&t=' + targetControl.id;
                    wo$(this).attr('href', url).fancybox(
                    {
                        margin: 10,
                        padding: 0,
                        centerOnScroll: true,
                        overlayOpacity: 0.7,
                        overlayColor: '#000'
                    });
                });
            },

            loadSupplier: function (id)
            {
                var url = _getUrl('Supplier', 'id=' + id);
                wo$('#pptabs').tabs('url', 6, url).tabs('load', 6);
                return this;
            },

            loadBrand: function (id)
            {
                var url = _getUrl('Brand', 'id=' + id);
                wo$('#pptabs').tabs('url', 5, url).tabs('load', 5);
                return this;
            },

            initItems: function()
            {
                wo$('#phonePicker .item').click(function ()
                {
                    var data =
                    {
                        id: wo$(this).attr('data-id'),
                        imageUrl: wo$(this).attr('data-image-url'),
                        description: wo$(this).attr('data-description')
                    };
                    wo$(targetControl).trigger('select', data);
                    wo$.fancybox.close();
                    return this;
                });
            }

        };

    wo$.fn.phonePicker = function (method)
    {
        if (methods[method])
        {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || !method)
        {
            return methods.init.apply(this, arguments);
        }
        else
        {
            $.error('Method ' + method + ' does not exist on jQuery.phonePicker');
        }
    };

})(jQuery);

