function fbs_click(event) {
    u= event.data.url;
    t=event.data.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function twitter_click(event) {
    u= event.data.url;
    //t=decodeURIComponent(event.data.title);
    t = '';
    window.open('http://www.twitter.com/share?url='+u+'&text='+t,'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

(function ($) {
    /**
     * Adds an social share windows to a button
     * @param object settings
     * @return void
     */
    $.fn.socialShareButton = function (options) {
        var defaults = {}, object;
        defaults = {
            bitlyLogin: '',
            bitlyApiKey: ''
        };
        options = $.extend(defaults, options);

        var services = {
            twitter: {
                // Twitter Service Callback
                callback: function(target, icon, url, title) {
                    var link = $('<a></a>'), message, img, shortUrl;

                    if (typeof(url) == "undefined" || url.length <= 0){
                        url = document.location;
                        title=document.title;
                    }

                    link.attr('href', "javascript:void(0);");
                    link.attr('rel', 'nofollow');
                    link.addClass('twitter');
                    link.bind("click", {url: url, title: title}, twitter_click);
                    target
                        .append(link)
                        .append('<br/>');
                },
                icon: ''
            },
            facebook: {
                // Facebook Service Callback
                callback: function(target, icon, url, title) {
                    var link = $('<a></a>'), message, img, shortUrl;

                    if (typeof(url) == "undefined" || url.length <= 0){
                        url = document.location;
                        title=document.title;
                    }
                    link.attr('href', "javascript:void(0);return false;");
                    link.attr('rel', 'nofollow');
                    link.addClass('facebook');
                    link.bind("click", {url: url, title: title}, fbs_click);

                    target
                        .append(link)
                        .append('<br/>');
                },
                icon: ''
            }
        };

        /**
         * Function adds a new Service to the service pool
         * @param string name
         * @param function callback
         * @param string icon
         * @return void
         */
        arguments.callee.addService = function(name, callback, icon) {
            icon = icon || '';
            services[name] = {
                callback: callback,
                icon: icon
            };
            return;
        }

        object = $(this);
        object.attr('href', 'javascript:void(0);');
        object.die('click').live('click', function() {

            if($('.layer-share').length > 0) {
                $('.layer-share').toggle();
            } else {
                var dimensions, container, url;
                object = $(this);

                dimensions = object.offset();
                container = $('<div class="layer-share"></div>');
                object.parent().append(container);

                container.append('<a class="close" href="javascript:void(0);" onclick="$(\'.layer-share\').remove();"><span>Schließen</span></a>')

                url = $(this).attr('rel');
                title = $(this).attr('title');
                // iterate through all services (twitter, facebook, ...)
                for(serviceName in services) {
                    var service = services[serviceName];
                    service.callback(container, service.icon, url, title);
                }
            }
        });
    };
})( jQuery );

$(function() {
    // add buttons to share layer
    $('#main .buttons-share .share').socialShareButton();
    //$('#main .button-row .share').socialShareButton();

    // bind event to footer share buttons in brand pages
    $('#footer .twitter').bind('click', {url: document.location, title: document.title}, twitter_click);
    $('#footer .facebook').bind('click', {url: document.location, title: document.title}, fbs_click);

    $('#main .twitter').bind('click', {url: document.location, title: document.title}, twitter_click);
    $('#main .facebook').bind('click', {url: document.location, title: document.title}, fbs_click);

    $('#contentRight .twitter').bind('click', {url: document.location, title: document.title}, twitter_click);
    $('#contentRight .facebook').bind('click', {url: document.location, title: document.title}, fbs_click);

    $('.shortened .twitter').bind('click', {url: document.location, title: document.title}, twitter_click);
    $('.shortened .facebook').bind('click', {url: document.location, title: document.title}, fbs_click);
})
