var barsum;
if (!barsum) barsum = {};

barsum.placeHolder = function(input, value) {
    var val = value || '';
    
    $(input).addClass('placeholder').val(val).attr('defaultValue', val);
    
    $(input).focus(function(){ 
        if ($(this).val() == val) {
            $(this).val('').removeClass('placeholder');
        }
    });
    
    $(input).blur(function(){
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue')).addClass('placeholder');
        }
    });
};

barsum.hyph = {
    tags: '.text p',
    tshy: '&shy;',
    shy: null,
    spec: '[éüú]',
    vovel: '[àå¸èîóûýþÿaeiouy]',
    consonant: '(?:sh|ch|qu|[áâãäæçêëìíïðñòôõö÷øùbcdfghjklmnpqrstvwxz])',
    letters: '[üúéàå¸èîóûýþÿáâãäæçéêëìíïðñòôõö÷øùúüaeiouybcdfghjklmnpqrstvwxz]',
    rules: null,
    elements: [],
    
    getApplyMethod: function() {
        return 'text-align:justify;';
    },
    
    hyphenizeText: function(text) {
        for (var i = this.rules.length - 1; i >= 0; i--) {
            var ru = this.rules[i];
            
            if (!ru) {
                continue;
            }
            
            var re = new RegExp('(' + ru[0] + ')(?=' + ru[1] + ')', 'gi');
            text = text.replace(re, '$1' + this.shy);
        }
        
        return text;
    },
    
    hyphenize: function(node) {
        var n = node;
        var children = n.childNodes;
        var parent = node.parentNode;
        
        if (node.nodeType == 3) {
            if (!node.nodeValue) {
                return;
            }
            
            node.nodeValue = this.hyphenizeText(node.nodeValue);
            $(parent).attr('style', this.getApplyMethod());
        }
        
        for (var i = 0; i < children.length; i++) {
            this.hyphenize(children[i]);
        }
    },
    
    exe: function() {
        this._init();
        
        this.elements = $(this.tags);
        
        if (this.elements.length <= 0) {
            return;
        }
        
        for (var e = 0; e < this.elements.length; e++) {
            this.hyphenize(this.elements[e]);
        }
    },
    
    _init: function() {
        if (!this.shy) {
            var node = document.createElement('span');
            node.innerHTML = this.tshy;
            
            this.shy = node.childNodes[0].nodeValue;
            
            with (this) {
                rules = [[consonant + vovel, vovel + letters],
                         [vovel + consonant, consonant + vovel],
                         [consonant + vovel, consonant + vovel],
                         [vovel + consonant, consonant + consonant + vovel],
                         [vovel + consonant + consonant, consonant + vovel],
                         [vovel + consonant + consonant, consonant + consonant + vovel],
                         [spec, letters + letters],
                         null
                        ];
            }
        }
    }
};