﻿/**
 *  Live Nation UK jQuery plugin
 *  http://www.livenation.co.uk/
 *
 *	by Michal Gondar aka gondo 2010
 *
 *   $('.facebookLike', o).facebookLike({ url: jsLInk.attr('jshref'), omniValue: omniValue });
 */

(function($) {
    config = {
        facebookUrl: '//connect.facebook.net/en_US/all.js',
        twitterWidgetsUrl: '//platform.twitter.com/widgets.js',
        twitterHref: 'http://twitter.com/share',
        twitterClass: 'twitter-share-button'
    }
    function loadFbScript(element) {
        var script = document.createElement('script');
        script.async = true;
        script.type = 'text/javascript';
        script.src = document.location.protocol + config.facebookUrl;
        script.onload = function() {
            if (typeof FB != 'undefined') {
                fbInit(element);
            }
        };
        script.onreadystatechange = function() {
            if (script.readyState == 'loaded' || script.readyState == 'complete') {
                if (typeof FB != 'undefined') {
                    fbInit(element);
                }
            }
        }
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    function fbInit(element) {
        if (typeof FB == 'undefined') {
            loadFbScript(element);
            return;
        }
        var subscribe = function(name, link) {
            if (typeof omni == 'object') {
                omni.facebook.like(name);
            }
        };
        try {
            if (FB.lnInit) {
                FB.Event.clear('edge.create');
                FB.Event.subscribe('edge.create', function(url) {
                    subscribe(element.getAttribute('omniValue'), url);
                });
                FB.XFBML.parse(element);
                return;
            }
            FB.init({
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: false  // parse XFBML
            });
            FB.Event.subscribe('edge.create', function(url) {
                subscribe(element.getAttribute('omniValue'), url);
            });
            FB.XFBML.parse(element);
            FB.lnInit = true;
        } catch (e) { }
    }

    $.fn.facebookLike = function(options) {
        // TODO: this is called more than needed on preopened page
        var defaults = {
            width: 150,
            layout: 'button_count',
            url: null,
            showFaces: false,
            omniValue: '',
            send: true
        };
        var myOptions = $.extend(defaults, options);

        $(this).bind('hideFb', function() {
            //$(this).find('iframe').hide();
            $(this).find('iframe').css('visibility', 'hidden');
        }).bind('showFb', function() {
            //$(this).find('iframe').show();
            $(this).find('iframe').css('visibility', 'visible');
        }).bind('closePopup', function() {
            //$(this).find('iframe').show();
            $(this).find('iframe').css('visibility', 'visible');
        });

        function cleanUrl(url) {
            url = url + '';
            var p = url.indexOf('?');
            if (p > 0) {
                url = url.slice(0, p);
            }
            return url;
        }

        return this.each(function() {
            var w = myOptions.width;
            if (w == 'auto') {
                w = $(this).width();
            }
            var url;
            if (myOptions.url == null) {
                url = cleanUrl(document.location);
            } else {
                url = 'http://' + window.location.hostname + cleanUrl(myOptions.url);
            }
            var html = '<fb:like show_faces="' + myOptions.showFaces + '" href="' + url + '" width="' + w + '" layout="' + myOptions.layout + '"' + 'send="' + myOptions.send + '"></fb:like>';
            $(this).html(html);
            var element = $(this).get()[0];
            element.setAttribute('omniValue', myOptions.omniValue);
            fbInit(element);
        });
    }
})(jQuery);
