/**
 *                    SEO Counter
 *                ==================
 *
 * Copyright 2009 Harvey Kane <code@ragepank.com>
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Harvey Kane <code@ragepank.com>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.ragepank.com/seocounter/
 *
 */

function seocounter(suffix)
{
	var gg_objSpan;
	
	
    if (!suffix) {var suffix = '';}
    var seocounter_title = 70;
    var seocounter_meta = 155;
    $('input, textarea').each(function(){
        var s = false;
        var c = $(this).attr('class');
        var a = c.split(' ');
        var r = /seocounter_(.*)/i;
        for(i=0; i<a.length; i++) {
            var m = r.exec(a[i]);
            if (m != null && m.length > 1) {
            	s = m[1];
            }
        }
        if (s == 'title') {s=seocounter_title-suffix.length;}
        if (s == 'meta') {s=seocounter_meta;}
        if ((s != false) && (!$('#seocounter_'+$(this).attr('name')).length)) {
            var l = s - $(this).val().length;
//            $('body').prepend(' <span class="seocounter" id="seocounter_'+$(this).attr('name')+'">'+l+'</span>');
						gg_objSpan = $('<span class="seocounter" id="seocounter_'+$(this).attr('name')+'">'+l+'</span>');
						gg_objSpan.prependTo($('body'));
/*
						gg_objSpan.css('height','20px');
						gg_objSpan.css('width','50px');
*/						
						gg_objSpan.css('top',parseInt(($(this).offset().top)) + $(this).height() + + parseInt($(this).css('padding-top').replace('px', '')) + parseInt($(this).css('padding-bottom').replace('px', '')) - 16 );
						gg_objSpan.css('left',(parseInt(($(this).offset().left)) + $(this).width() + parseInt($(this).css('padding-left').replace('px', '')) + parseInt($(this).css('padding-right').replace('px', ''))) + 'px');
            $(this).keyup(function(){
                var l = s - $(this).val().length;                     
                $('#seocounter_'+$(this).attr('name')).html(l + '');
                if (l >= 0)
                	$('#seocounter_'+$(this).attr('name')).css('color','#666666');
                else
                	$('#seocounter_'+$(this).attr('name')).css('color','red');
            });
        }
    });
}
