(function($){
    // plugin initialization
    $.fn.share = function(o){
        var share = this;
        var button = null;
        
        // Initialize options for this call
        share.options = $.extend({
            statusPrompt: "",
            facebookStatusPrompt : null,
            
            statusText : "",
            facebookStatusText : null,
            
            title : "",
            facebookTitle : null,
            myspaceTitle : null,
            twitterTitle : null,
            
            titleLink : "",
            facebookTitleLink : null,
            myspaceTitleLink : null,
            
            description : "",
            facebookDescription : null,
            myspaceDescription : null,
            
            linkText : null,
            facebookLinkText : null,
            
            url : "",
            facebookUrl : null,
            
            emailTo : null,
            emailSubject : null,
            emailBody : null,
            
            targetId : null,
            images : [],
            
            callback: function(){},
            appType: 'unspecified',
            
            showTwitter: true,
            showFacebook: true,
            showMyspace: true,
            showEmail: false,
            
            useCustomElements: false,
            facebookElementent: null,
            myspaceElementent: null,
            twitterElementent: null,
            emailElement: null,
            
            useEvents: false,
            tabId: 0
        },o);

        // Exit early if are not sharing anything
        if (!share.options.showTwitter && !share.options.showFacebook && !share.options.showMyspace && !share.options.showEmail) {
            return share;
        }
        
        // Share Template
        if (share.options.useEvents == false) {
            if (share.options.useCustomElements == true) {
                share.options.facebookElement = $(share.options.facebookElement);
                share.options.myspaceElement = $(share.options.myspaceElement);
                share.options.twitterElement = $(share.options.twitterElement);
                share.options.emailElement = $(share.options.emailElement);
            } else if (($.transponder.isFacebookPreview() == true || $.currentPlatform() == 'facebook') && share.options.useCustomElements == false) {
                // Facebook share buttons
                // Empty Div
                share.html('');
        
                // Setup Height
                share.css("height","14px");
        
                // Share Template
                share.options.facebookElement = button = $('<span><img class="ts_clickable" src="http://cdn.transpond.com/buttons/fbsharesprite.png" /></span>');
                
                var shareIcons = button.find('img');
                for (var iq=0;iq<shareIcons.length;iq++) {
                    var iconElem = $(shareIcons.get(iq));
                    var oldUrl = iconElem.attr("src");
                    iconElem.attr("src", "http://cdn.transpond.com/buttons/pixel.gif");
                    iconElem.css({
                        height : "14px",
                        width : "45px",
                        background : "transparent url("+oldUrl+")",
                        verticalAlign : "bottom"
                    });
                    
                    iconElem.bind("mouseover",function(){ $(this).css("backgroundPosition","left -14px"); });
                    iconElem.bind("mouseout",function(){ $(this).css("backgroundPosition","left 0px"); });
                }

            } else if (share.options.useCustomElements == false){
                // Microsite and other share buttons
                // Empty Div
                share.html('');
                
                // Setup Height
                share.css("height","15px");
                
                button = $('<span><img class="ts_clickable" src="http://cdn.transpond.com/buttons/link_facebook16.png" /><img class="ts_clickable" src="http://cdn.transpond.com/buttons/link_myspace16.png" /><img class="ts_clickable" src="http://cdn.transpond.com/buttons/link_twitter16.png" /></span>');
                
                var shareIcons = button.find('img');
                if ($.currentPlatform() == 'facebook' && $.transponder.isFacebookPreview() === false) {
                    shareIcons = shareIcons.nodes;
                }
                
                for (var iq=0;iq<shareIcons.length;iq++) {
                    var iconElem = $(shareIcons[iq]);
                    var oldUrl = iconElem.attr("src");
                    iconElem.attr("src", "http://cdn.transpond.com/buttons/pixel.gif");
                    iconElem.css({
                        height : "16px",
                        width : "16px",
                        background : "transparent url("+oldUrl+")",
                        verticalAlign : "bottom"
                    });
                    
                    iconElem.bind("mouseover",function(){ $(this).css("backgroundPosition","left -16px"); });
                    iconElem.bind("mouseout",function(){ $(this).css("backgroundPosition","left 0px"); });
                }
                
                if ($.currentPlatform() == 'facebook' && $.transponder.isFacebookPreview() === false) {
                    share.options.facebookElement = $(button.find("img").nodes[0]);
                    share.options.myspaceElement = $(button.find("img").nodes[1]);
                    share.options.twitterElement = $(button.find("img").nodes[2]);
                } else {
                    share.options.facebookElement = button.find(":eq(0)");
                    share.options.myspaceElement = button.find(":eq(1)");
                    share.options.twitterElement = button.find(":eq(2)");
                }
                
            }
        }
        
        // Newsfeed Defaults
        var getNewsfeedOptions = function(elem) { 
            return {
                "elem" : $(elem),
                "prompt" : share.options.statusPrompt,
                "userText" : share.options.statusText,
                "title" : share.options.title,
                "titleUrl" : (share.options.titleLink != null) ? share.options.titleLink : share.options.url,
                "description" : share.options.description,
                "linkText" : (share.options.linkText != null) ? share.options.linkText : share.options.url,
                "linkUrl" : share.options.url,
                "images" : share.options.images,
                "emailTo": share.options.emailTo,
                "emailSubject" : share.options.emailSubject,
                "emailBody" : share.options.emailBody,
                "targetId" : share.options.targetId
            };
        };
        
        var bindNewsFeedEvent = function(elem,platform,nfoptions) {
            var postNewsFeed = function(el) {
                nfoptions.elem = el;
                return $.newsfeed(platform,nfoptions, function() {
                    if (share.options.callback) {
                        share.options.callback();
                    }
                    $.galog.share(share.options.appType,platform);
                });
            };
            if (share.options.useEvents) {
                var eventType = "shareFacebook";
                switch (platform) {
                    case 'myspace':
                        eventType = 'shareMyspace';
                    break;
                    case 'twitter':
                        eventType = "shareTwitter";
                    break;
                    case 'email':
                        eventType = 'shareEmail';
                    break;
                }
                $.bindEvent(eventType,postNewsFeed);
            } else if (elem) {
                elem.bind("click",postNewsFeed);
            }
        };
        
        // Facebook Share
        share.bindEvents = function() {
            var fbNewsfeedOptions = getNewsfeedOptions(share.options.facebookElement);
            if (share.options.facebookStatusPrompt) { fbNewsfeedOptions.prompt = share.options.facebookStatusPrompt; }
            if (share.options.facebookStatusText) { fbNewsfeedOptions.userText = share.options.facebookStatusText; }
            if (share.options.facebookTitle) { fbNewsfeedOptions.title = share.options.facebookTitle; }
            if (share.options.facebookTitleLink) { fbNewsfeedOptions.titleUrl = share.options.facebookTitleLink;}
            if (share.options.facebookDescription) { fbNewsfeedOptions.description = share.options.facebookDescription;}
            if (share.options.facebookLinkText) { fbNewsfeedOptions.linkText = share.options.facebookLinkText;}
            if (share.options.facebookUrl) { fbNewsfeedOptions.linkUrl = share.options.facebookUrl;}
            if (share.options.showFacebook) {
                bindNewsFeedEvent(share.options.facebookElement,'facebook',fbNewsfeedOptions);
            } else if (share.options.facebookElement) {
                share.options.facebookElement.hide();
            }
        
            // MySpace
            var msNewsfeedOptions = getNewsfeedOptions(share.options.myspaceElement);
            if (share.options.myspaceTitle) { msNewsfeedOptions.title = share.options.myspaceTitle; }
            if (share.options.myspaceTitleLink) { msNewsfeedOptions.titleUrl = share.options.myspaceTitleLink;}
            if (share.options.myspaceDescription) { msNewsfeedOptions.description = share.options.myspaceDescription;}
            if (share.options.showMyspace) {
                bindNewsFeedEvent(share.options.myspaceElement,'myspace',msNewsfeedOptions);
            } else if (share.options.myspaceElement) {
                share.options.myspaceElement.hide();
            }
        
            // Twitter
            var twNewsfeedOptions = getNewsfeedOptions(share.options.twitterElement);
            if (share.options.twitterTitle) { twNewsfeedOptions.title = share.options.twitterTitle; }
            if (share.options.showTwitter) {
                bindNewsFeedEvent(share.options.twitterElement,'twitter',twNewsfeedOptions);
            } else if (share.options.twitterElement) {
                share.options.twitterElement.hide();
            }
        
            // Email
            var emNewsfeedOptions = getNewsfeedOptions(share.options.emailElement);
            if (share.options.showEmail) {
                bindNewsFeedEvent(share.options.emailElement,'email',emNewsfeedOptions);
            } else if (share.options.emailElement) {
                share.options.emailElement.hide();
            }
        
        };
        
        share.bindEvents();
        
        // Append to DOM if required
        if (share.options.useCustomElements == false && share.options.useEvents == false) { share.append(button); }
        
        return share;
    };
    
})(jQuery);