/**
 * Search Engine Keyword Highlight (http://fucoder.com/code/se-hilite/)
 *
 * This module can be imported by any HTML page, and it would analyse the
 * referrer for search engine keywords, and then highlight those keywords on
 * the page, by wrapping them around <span class="hilite">...</span> tags.
 * Document can then define styles else where to provide visual feedbacks.
 *
 * Usage:
 *
 *   In HTML. Add the following line towards the end of the document.
 *
 *     <script type="text/javascript" src="se_hilite.js"></script>
 *
 *   In CSS, define the following style:
 *
 *     .hilite { background-color: #ff0; }
 *
 *   If Hilite.style_name_suffix is true, then define the follow styles:
 *
 *     .hilite1 { background-color: #ff0; }
 *     .hilite2 { background-color: #f0f; }
 *     .hilite3 { background-color: #0ff; }
 *     .hilite4 ...
 *
 * @author Scott Yang <http://scott.yang.id.au/>
 * @version 1.5
 */

// Configuration:

Hilite = {
    /**
     * Element ID to be highlighted. If set, then only content inside this DOM
     * element will be highlighted, otherwise everything inside document.body
     * will be searched.
     */
    elementid: 'content',
    
    /**
     * Whether we are matching an exact word. For example, searching for
     * "highlight" will only match "highlight" but not "highlighting" if exact
     * is set to true.
     */
    exact: true,

    /**
     * Maximum number of DOM nodes to test, before handing the control back to
     * the GUI thread. This prevents locking up the UI when parsing and
     * replacing inside a large document.
     */
    max_nodes: 1000,

    /**
     * Whether to automatically hilite a section of the HTML document, by
     * binding the "Hilite.hilite()" to window.onload() event. If this
     * attribute is set to false, you can still manually trigger the hilite by
     * calling Hilite.hilite() in Javascript after document has been fully
     * loaded.
     */
    onload: true,

    /**
     * Name of the style to be used. Default to 'hilite'.
     */
    style_name: 'hilite',
    
    /**
     * Whether to use different style names for different search keywords by
     * appending a number starting from 1, i.e. hilite1, hilite2, etc.
     */
    style_name_suffix: false,

    /**
     * Set it to override the document.referrer string. Used for debugging
     * only.
     */
    debug_referrer: '',
    mysearch_list: ''
};

	Hilite.debug_referrer = "Account Manager Sales,Accountant,Accounting Manager,Accounts Payable Supervisor,Accounts Receivable,Administrative Assistant,Advertising Professional,Agriculture,Announcer,Architect,Artist,Automotive Technician,Broadcaster,Business Administration Manager,Certified Nurse Aide,Certified Nurse Assistant,Certified Nurse's Aide,Certified Nurse's Assistant,Chef,Clerk,Communications Manager,Computer Programmer,Computer Repair,Construction,Construction Manager,Construction Worker,Consultant,Cosmetologist,CPA,Customer Service,Decorator,Dental Assistant,Designer,Director,Drafter,Education Administrator,Education Consultant,Emergency Medical Technician,EMT,Engineer,Engineering Manager,Esthetician,Facilities Planner,Fashion Designer,Fashion Merchandiser,Financial Planner,Game Designer,Graphic Designer,Health Care Administrator,Human Resources Manager,Human Resources Professional,HVAC Technician,Information Technology,Insurance Adjuster,Insurance Manager,Interior Designer,IT Specialist,Journalist,Law Enforcement Officer,Legal Secretary,Licensed Vocational Nurse,LVN,Manager,Manufacturing,Manufacturing Manager,Manufacturing Worker,Marketing Manager,Marketing Professional,Massage Therapist,Mechanic,Medical Assistant,Nurse,Nurse Midwife,Nursing Aide,Nutritionist,Occupational Nurse,Occupational Therapist,Occupational Therapy Assistant,Office Manager,Paralegal,Pharmaceutical Sales Representative,Photographer,Practitioner,Project Manager,Psychiatrist,Psychologist,Quality Control Technician,Radiologist,Real Estate Agent,Real Estate Manager,Recruiter,Registered Nurse,Representative,Restaurant Manager,RN,Scientist,Secretary,Security Officer,Shipping & Receiving,Social Worker,Stylist,Supervisor,Teacher,Therapist,Transcriptionist,Warehouse Worker,Welder,Writer,X-ray technician,cna";
	var skipre = /^(script|style|textarea|a|b)/i;
	Hilite.exact = true;
	
function aa(){
	var obj = document.getElementById("dynopix");
	obj.innerHTML = "<img src='http://h.huyilo.com/cgi/p/l?;a=20076195;x=8192;c=203365374;n=203;s=3084;e=i;z=TIMESTAMP;' height='' weight='1' >";
}



/**
 * Highlight a DOM element with a list of keywords.
 */
Hilite.hiliteElement = function(elm, query) { 
    if (!query || elm.childNodes.length == 0)
	return;

    var qre = new Array();
    for (var i = 0; i < query.length; i ++) {
        query[i] = query[i].toLowerCase();
        if (Hilite.exact)
            qre.push('\\b'+query[i]+'\\b');
        else
            qre.push(query[i]);
    }

    qre = new RegExp(qre.join("|"), "i");

    var stylemapper = {};
    for (var i = 0; i < query.length; i ++) {
        if (Hilite.style_name_suffix)
            stylemapper[query[i]] = Hilite.style_name+(i+1);
        else
            stylemapper[query[i]] = Hilite.style_name;
    }
    
    var overlib_val = "return overlib('pop_content', WIDTH, 380, STICKY, MOUSEOFF);";

    temp_attribute = new Array();
    var textproc = function(node) {
        var match = qre.exec(node.data);
        if (match) { 
			aa();
            var val = match[0];
            var k = '';
            var node2 = node.splitText(match.index);
            var node3 = node2.splitText(val.length);
            //var span = node.ownerDocument.createElement('SPAN');
            temp_attribute["class"] = stylemapper[val.toLowerCase()];
            //temp_attribute["href"] = 'http://www.allaboutlawns.com/lawn-info/form.html?form=184&rsrc=forum&rkw='+node2.data.toLowerCase();
			temp_attribute["href"] = 'http://e.ertile.com/cgi/r?;n=203;c=365372;s=3084;x=7936;f=200802111335470;u=j;z=TIMESTAMP;strCrit=rawWords%3d'+node2.data.toLowerCase()+'%3b';
			temp_attribute["style"]= 'color:#0000ff; cursor:pointer; border-bottom: 1px solid #0000ff; font-weight: normal;';
			temp_attribute["title"]= 'Find '+node2.data.toLowerCase()+' jobs';
			//temp_attribute["onmouseover"] = overlib_val;
            //temp_attribute["onmouseout"] = "return nd()";
            var span = makeElement("a", temp_attribute);
			
            node.parentNode.replaceChild(span, node2);
            span.appendChild(node2); 
            return span;
        } else {
            return node;
        }
    };
    Hilite.walkElements(elm.childNodes[0], 1, textproc);
};

function makeElement(elementType, elementAttributeArray){
	// IE DOM BUG
	var element = Object;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var estring = String;
		var estring = "<"+elementType+" ";
		for(a in elementAttributeArray){
			estring += a + '="'+ elementAttributeArray[a] +'" ';
		}
		estring += ">";
		element = document.createElement(estring);
		
	}else{
		element = document.createElement(elementType);
		for(a in elementAttributeArray){
			element.setAttribute(a, elementAttributeArray[a]);
		}
	} 
	 
	return element;
}


/**
 * Highlight a HTML document using keywords extracted from document.referrer.
 * This is the main function to be called to perform search engine highlight
 * on a document.
 *
 * Currently it would check for DOM element 'content', element 'container' and
 * then document.body in that order, so it only highlights appropriate section
 * on WordPress and Movable Type pages.
 */
Hilite.hilite = function() { 
//document.write(Hilite.match_found);
    // If 'debug_referrer' then we will use that as our referrer string
    // instead.
    var q = Hilite.debug_referrer ? Hilite.debug_referrer : document.referrer;
    var e = null;
    //q = Hilite.decodeReferrer(q);
    q = q.split(/[,]+/);

    if (q && ((Hilite.elementid && 
               (e = document.getElementById(Hilite.elementid))) || 
              (e = document.body)))
    {
	Hilite.hiliteElement(e, q);
    }
	
};

Hilite.walkElements = function(node, depth, textproc) {
    //var skipre = /^(script|style|textarea|a)/i;
    var count = 0;
    while (node && depth > 0) {
        count ++;
        if (count >= Hilite.max_nodes) {
            var handler = function() {
                Hilite.walkElements(node, depth, textproc);
            };
            setTimeout(handler, 50);
            return;
        }

        if (node.nodeType == 1) { // ELEMENT_NODE
            if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
                node = node.childNodes[0];
                depth ++;
                continue;
            }
        } else if (node.nodeType == 3) { // TEXT_NODE
            node = textproc(node);
        }

        if (node.nextSibling) {
            node = node.nextSibling;
        } else {
            while (depth > 0) {
                node = node.parentNode;
                depth --;
                if (node.nextSibling) {
                    node = node.nextSibling;
                    break;
                }
            }
        }
    }
};

// Trigger the highlight using the onload handler.
if (Hilite.onload) {
    if (window.attachEvent) {
        window.attachEvent('onload', Hilite.hilite);
    } else if (window.addEventListener) {
        window.addEventListener('load', Hilite.hilite, false);
    } else {
        var __onload = window.onload;
        window.onload = function() {
            Hilite.hilite();
            __onload();
        };
    }
}
