/**
 * Create a digg, delicious, StumbleUpon or Technorati button.
 *
 * The Widget inserts a digg, delicios, StumbleUpon or Technorati button into the target element on which the widget is executed on.
 *
 * ### Examples
 * The Widget is executed on a pageelement and is called with passing the chosen services. In this example the digg and the Technorati button is implemented.
 *
 * JavaScript:
 *
 *
 *     jQuery.ready({
 *          plugin: [ 'de_epages.externalcontent.ui.otherbuttons' ],
 *          DOM:    true
 *      }, function($){
 *          var widget = de_epages('<div>').externalcontentUiOtherbuttons({
 *              services : ["digg","technorati"]
 *          });
 *     });
 *
 *
 * @class jQuery.ui.externalcontentUiOtherbuttons
 * @extends jQuery.widget
 *
 * @uses jQuery.ui.widget
 * @uses jQuery.tmpl
 * @uses de_epages.externalcontent.ui.otherbuttons
 * @since 6.15.0
 */

/**
 * @cfg {Array of strings} [services] Specifies which Social Web buttons should be shown. Values: 'digg', 'delicious', 'stumbleupon', 'technorati'
 */

/**
 * See `jQuery.ui.externalcontentUiOtherbuttons` for details.
 *
 * @param {Object} [options] A map of additional options to pass to the method.
 * @param {Array of strings} [services] Specifies which Social Web buttons should be shown. Values: 'digg', 'delicious', 'stumbleupon', 'technorati'
 *
 * @method externalcontentUiOtherbuttons
 * @member jQuery
 *
 * @since 6.15.0
 */

/*
 * @copyright   © Copyright 2006-2012, epages GmbH, All Rights Reserved.
 *
 * @module      de_epages.externalcontent.ui.otherbuttons
 *
 * @revision    $Revision: 1.6 $
 */
/*jslint nomen: true*/
/*global define*/
define('de_epages/externalcontent/ui/otherbuttons', [
    'jquery/ui/widget',
    'ep',
    'de_epages',

    'ep/ajax',
    'jquery/tmpl'
], function ($, ep, de_epages) {
    'use strict';
    // Constants.
    var gClassNames = 'SocialWeb SocialBookmarks',
        // Will be added to *self.element*.
        iconPath = ep.config.storeRoot + '/SF/SocialWeb/ico_bookmark_',
        // Start of icon path of respective services.
        services = {
            digg: {
                url: 'http://digg.com/submit?url=',
                alt: 'digg'
            },
            delicious: {
                url: 'http://www.delicious.com/save?url=',
                alt: 'delicious'
            },
            stumbleupon: {
                url: 'http://www.stumbleupon.com/submit?url=',
                alt: 'StumbleUpon'
            },
            technorati: {
                url: 'http://technorati.com/faves?add=',
                alt: 'Technorati'
            }
        },
        // Service constants.
        templateId = 'de_epages.externalcontent.ui.otherbuttons';

    // Render and cache template.
    $.template(templateId, '<a class="RightMargin" href="${url}" target="_blank"><img src="' + iconPath + '${$item.service}.png" alt="${alt}"/></a>');

    // The actual widget.
    $.widget('ui.externalcontentUiOtherbuttons', {

        options: {
            services: [] // Array of strings with the property names of *services*, see above (Constants).
        },

        _create: function () {
            var self = this,
                o = this.options,
                templateElems = [],
                serviceKey,
                i, // Loop variable.
                servicesLength = o.services.length,
                additionalURLInfo = ep.config.canonicalUrl || ep.config.webUrl,
                urls = [];

            // Collect urls which should be derefered
            for (i = 0; i < servicesLength; i = i + 1) {
                serviceKey = o.services[i];
                urls.push(services[serviceKey].url + additionalURLInfo);
            }

            // Get links for the dereferred ChangeAction
            ep.ajax({
                dataType: 'json',
                data: {
                    'ViewAction': 'JSONDerefer',
                    'DereferURLs': urls.join(',')
                }
            }).done(function (data) {
                for (i = 0; i < servicesLength; i = i + 1) {
                    serviceKey = o.services[i];
                    // Replace with dereferer url
                    services[serviceKey].url = data[i];

                    // Create button using the template and append to *this.element*.
                    $.merge(templateElems, $.tmpl(templateId, services[serviceKey], {
                        service: serviceKey
                    }).get());
                }

                // "Append" *templateElems* and "addClass" *gClassNames* to *this.element*.
                self.renderedTemplates = $(templateElems).appendTo(self.element.addClass(gClassNames));
            });
        },

        /**
         * This method removes the container on which the Social Web buttons plugin has been appended to.
         *
         * @method destroy
         * @member jQuery.ui.externalcontentUiOtherbuttons
         *
         * @since 6.15.0
         */
        destroy: function () {
            // Remove all traces of the widget.
            this.element.removeClass(gClassNames);
            if (this.renderedTemplates) {
                this.renderedTemplates.remove();
            }
            this._superApply(arguments);
        }

    });

    return de_epages;

});