/*
Based on plugin: jQuery OneFBLike v1.0 - http://onerutter.com/open-source/jquery-facebook-like-plugin.html
                 Copyright (c) 2010 Jake Rutter
                 This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
Made modifications to plugin for cross browser support and fixed duplicate calls.
*/

(function ($) {
    $.fn.extend({
        AddFBLike: function (options) {
            //Set the default values, use comma to separate the settings 
            var defaults = {
                appID: '',
                siteTitle: 'Irvine Ranch Natural Landmarks',
                siteName: '',
                siteImage: '',
                buttonWidth: 55,
                buttonHeight: 65,
                showfaces: false,
                font: 'arial',
                layout: 'box_count', //normal, button_count, box_count
                action: 'like',
                colorscheme: 'light',
                FBML: true
            }

            var options = $.extend(defaults, options);

            // Add Meta Tags for additional data - options
            jQuery('head').append('<meta property="og:title" content="' + options.siteTitle + '"/>');
            jQuery('head').append('<meta property="og:site_name" content="' + options.siteName + '"/>');
            jQuery('head').append('<meta property="og:image" content="' + options.siteImage + '"/>');

            this.each(function () {
                var o = options;
                var obj = $(this);

                if (options.FBML) {
                    FBML(obj, o);
                }
                else {
                    iFrame(obj, o);
                }

            });

            function FBML(elem, opt) {
                // Grab the URL and assign it to a variable
                var dynUrl = document.location;

                // Apply the like button to an element on the page and include all available options
                // If no options are passed in from the page, the defaults will be applied

                var attribs = 'href="' + dynUrl + '"';
                if (opt.buttonWidth) {
                    attribs += ' width="' + opt.buttonWidth + '"';
                }
                if (opt.buttonHeight) {
                    attribs += ' height="' + opt.buttonHeight + '"';
                }
                if (opt.showfaces) {
                    attribs += ' show_faces="' + opt.showfaces + '"';
                }
                if (opt.font) {
                    attribs += ' font="' + opt.font + '"';
                }
                if (opt.layout) {
                    attribs += ' layout="' + opt.layout + '"';
                }
                if (opt.action) {
                    attribs += ' action="' + opt.action + '"';
                }
                if (opt.colorscheme) {
                    attribs += ' colorscheme="' + opt.colorscheme + '"';
                }

                jQuery(elem).replaceWith('<fb:like ' + attribs + '></fb:like>');
            }

            function iFrame(elem, opt) {
                // Grab the URL and assign it to a variable
                var dynUrl = document.location;

                // Apply the like button to an element on the page and include all available options
                // If no options are passed in from the page, the defaults will be applied

                var attribs = 'href=' + dynUrl + '&';
                var css = ''
                if (opt.buttonWidth) {
                    attribs += 'width=' + opt.buttonWidth + '&';
                    css += ' width:' + opt.buttonWidth + 'px;';
                }
                if (opt.buttonHeight) {
                    attribs += 'height=' + opt.buttonHeight + '&';
                    css += ' height:' + opt.buttonHeight + 'px;';
                }
                if (opt.showfaces) {
                    attribs += 'show_faces=' + opt.showfaces + '&';
                }
                if (opt.font) {
                    attribs += 'font=' + opt.font + '&';
                }
                if (opt.layout) {
                    attribs += 'layout=' + opt.layout + '&';
                }
                if (opt.action) {
                    attribs += 'action=' + opt.action + '&';
                }
                if (opt.colorscheme) {
                    attribs += 'colorscheme=' + opt.colorscheme + '';
                }

                var url = document.location.protocol + "//www.facebook.com/plugins/like.php?" + attribs;

                var frame = '<iframe src="' + url + '" scrolling="no" frameborder="0" style="border:none; overflow:hidden;' + css + '" allowTransparency="true"></iframe>';
                jQuery(elem).replaceWith(frame);
            }

            // setup FB Developers App Link - do not touch
            window.fbAsyncInit = function () {
                FB.init({ appId: options.appID, status: true, cookie: true, xfbml: true });
            };

            // Add #fb-root div - mandatory - do not remove
            jQuery('body').append('<div id="fb-root"></div>');

            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol +
			'//connect.facebook.net/en_US/all.js#xfbml=1';
            document.getElementById('fb-root').appendChild(e);

            return this;
        }
    });
})(jQuery);
