/*
 * jQuery Echo Comment Injector 
 *
 * Copyright (c) 2010 Jason Roy and CompareNetworks (jroy@comparenetworks.com)
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * $Version: 0.1
 */
 
(function($)
{
    var _options = {};
    var _commentDom = {};
    var _countDom = {};
    var _echoScriptLocation = '';
    var _countText = '';
    function init()
    {
        injectEchoScript();
		if (_options.countDom !== null && _options.countDom != '') 
		{
			_countDom = $(_options.countDom);
            injectCountScript();
        }
    }
    function injectEchoScript()
    {
        buildEchoScriptLocation();
        _commentDom.html('<div class="js-kit-comments" permalink="' + _options.href + '" uniq="' + _options.uniq + '"><\/div><script src="' + _echoScriptLocation + '"><\/script>');
    }
    function injectCountScript()
    {
        buildCountText();
        _countDom.html('<a href="' + window.location + '#' + _commentDom.attr('id') + '" class="' + _options.countCssClass + '"\>' + _countText + '<\/a><script src="' + _options.commentScriptLocation + '"><\/script>');
    }
    function buildCountText()
    {
        var countSpan = '<span class="js-kit-comments-count" uniq="' + _options.uniq + '">0<\/span>';
        _countText = _options.countText.replace("$", countSpan);
    }
    function buildEchoScriptLocation()
    {
        _echoScriptLocation = _options.mainScriptLocation.replace("$", _options.siteUrl);
    }
    $.fn.injectEcho = function(options)
    {
        _options = $.extend({}, $.fn.injectEcho.defaults, options);
        
		if (_options.siteUrl === '') 
        {
            alert('Site Address Must Be Provided for Echo Injection');
            
            return false;
        }
        _commentDom = $(this);
        init();
        
        return this;
    };
    //Public static options
    $.fn.injectEcho.defaults = 
    {
        siteUrl: '',
        mainScriptLocation: "http://js-kit.com/for/$/comments.js",
        commentScriptLocation: 'http://js-kit.com/comments-count.js',
        uniq: window.location.pathname,
        href: window.location.href,
        countDom: null,
        countCssClass: 'icon comments',
        countText: 'Comment ($)'
    };
})(jQuery);

