//////////////////////////////////////////////////////////
//														//
//  JSLinkMatcherRule									//
//													   	//
//////////////////////////////////////////////////////////

function JSLinkMatcherRule(regexp,cssClassName,negateExpression)
{
    // private members
    this.regexp = regexp;
    this.cssClassName = cssClassName;
    this.negateExpression = negateExpression;
    
    if ( this.negateExpression==null ) {
        this.negateExpression = false;
    }
    
    // public accessors
    this.getCssClassName = function() { return this.cssClassName; }
    this.getRegExp = function() { return this.regexp; }
    this.isNegateExpression = function() { return this.negateExpression; }
}

//////////////////////////////////////////////////////////
//														//
//  JSLinkMatcher										//
//													   	//
//////////////////////////////////////////////////////////

function JSLinkMatcher()
{
	// private members
	this.rules = new Array();
	
	// private methods
	this.matchRules = JSLinkMatcher_matchRules;

	// public methods
	this.attachRule = JSLinkMatcher_attachRule;
	this.process = JSLinkMatcher_process;
}

/////////////////////////////////////////////////////////

function JSLinkMatcher_attachRule(newRule)
{
    this.rules.push(newRule);    
}

/////////////////////////////////////////////////////////

function JSLinkMatcher_matchRules(element,text)
{		    
    var rulesCount = this.rules.length;
    for ( var i=0; i<rulesCount; i++ ) 
    {
        var rule = this.rules[i];
        if ( rule.isNegateExpression() ) 
        {
            if ( !rule.getRegExp().test(text) ) {
                element.className = rule.getCssClassName();
            }         
        }
        else
        {
            if ( rule.getRegExp().test(text) ) {
                element.className = rule.getCssClassName();
            }        
        }
    }
}

/////////////////////////////////////////////////////////

function JSLinkMatcher_process(element)
{
    if ( element==null ) 
    {
        // process entire body if no container element 
        // or link element was sent as argument
        element = document.body;
    }

    if ( element.tagName=="A" ) 
    {
        var linkHref = element.href;
        if ( linkHref!=null ) {
            this.matchRules(element,linkHref);
        }
    }
    else
    {
	    var links = element.getElementsByTagName("A");
	    var linksCount = links.length;
	    
	    for ( var i=0; i<linksCount; i++ )
	    {
			var linkElement = links[i];
			
		    var linkHref = linkElement.href;
		    if ( linkHref==null ) {
			    continue;
		    }
		    
		    if(linkElement.id != "rss-icon" && linkElement.id != "addthis")
		    {
				this.matchRules(linkElement,linkHref);
			}
	    }
    } 
}

/////////////////////////////////////////////////////////