
(function () {
    function escapeHTML(str) {
       var div = document.createElement('div');
       var text = document.createTextNode(str);
       div.appendChild(text);
       return div.innerHTML;
    }

    var templatehtml = "\x0D\x0A\x3Cdiv style=\"border:\x5B\x5Bbordercolor\x5D\x5D 1px solid;padding:2px; margin: 0 0 1em 0;color:#000000;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size: \x5B\x5Bfontsize\x5D\x5D;width:\x5B\x5Bwidth\x5D\x5D\"\x3E\x0D\x0A\x09\x3Ch2 style=\"margin:0;padding:0.2em 0.4em;color:\x5B\x5Btitlecolor\x5D\x5D;background:\x5B\x5Btitlebackgroundcolor\x5D\x5D;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size: \x5B\x5Bfontsize\x5D\x5D;font-weight:normal;\"\x3E\x5B\x5Btitletext\x5D\x5D\x3C/h2\x3E\x0D\x0A\x09\x5B\x5Bjobshtml\x5D\x5D\x0D\x0A\x09\x5B\x5Bsearchboxhtml\x5D\x5D\x0D\x0A\x09\x5B\x5Blogohtml\x5D\x5D\x0D\x0A\x3C/div\x3E\x0D\x0A";

    var jobtemplatehtml = "\x0D\x0A\x3Cp style=\"margin:0 0 0.8em 0;padding:0;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size: \x5B\x5Bfontsize\x5D\x5D\"\x3E\x0D\x0A    \x3Ca href=\"\x5B\x5Blink\x5D\x5D\" style=\"color: \x5B\x5Blinkcolor\x5D\x5D;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size: \x5B\x5Bfontsize\x5D\x5D\"\x3E\x5B\x5Bjobtitle\x5D\x5D\x3C/a\x3E\x3Cbr /\x3E\x0D\x0A    \x3Cspan style=\"color: \x5B\x5Btextcolor\x5D\x5D\"\x3E\x0D\x0A        \x5B\x5Blocation\x5D\x5D, \x5B\x5Bsalary\x5D\x5D\x0D\x0A    \x3C/span\x3E\x0D\x0A\x3C/p\x3E\x0D\x0A";

    var logohtml = "\x0D\x0A\x09\x3Cdiv style=\"padding:0.5em 0 0.2em 0.5em;border-top: 1px solid \x5B\x5Bbordercolor\x5D\x5D;text-align: center\"\x3E\x0D\x0A\x09\x09\x3Ca href=\"\x5B\x5Bredadvertisingurl\x5D\x5D\"\x3E\x3Cimg src=\"\x5B\x5Bredadvertisingurl\x5D\x5D_widgets/powered-by-red-advertising.gif\" alt=\"Powered by Red Advertising\" width=\"100\" height=\"23\" border=\"0\" /\x3E\x3C/a\x3E\x0D\x0A\x09\x3C/div\x3E\x0D\x0A";    

    var searchboxhtml = "\x0D\x0A\x3Cdiv style=\"border-top: 1px solid \x5B\x5Bbordercolor\x5D\x5D;padding:0.3em 0.5em 0.5em 0.5em;margin:0;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size:\x5B\x5Bfontsize\x5D\x5D\"\x3E\x0D\x0A\x09\x3Clabel for=\"\x5B\x5Bsearchinputdomid\x5D\x5D\" style=\"font-size:1.1em;color:\x5B\x5Bbordercolor\x5D\x5D;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size:\x5B\x5Bfontsize\x5D\x5D\"\x3ESearch\x3C/label\x3E\x3Cbr /\x3E\x0D\x0A\x0D\x0A\x09\x3Cinput type=\"text\" id=\"\x5B\x5Bsearchinputdomid\x5D\x5D\" style=\"float:left;width:70%;margin-right:2px;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size:\x5B\x5Bfontsize\x5D\x5D\" /\x3E\x0D\x0A\x09\x3Cinput \x0D\x0A\x09    type=\"button\"\x0D\x0A\x09    value=\"Go\"\x0D\x0A\x09    style=\"float:left;border:1px solid \x5B\x5Btitlebackgroundcolor\x5D\x5D;background:\x5B\x5Btitlebackgroundcolor\x5D\x5D;color:\x5B\x5Btitlecolor\x5D\x5D;font-family:\x26quot;\x5B\x5Bfontfamily\x5D\x5D\x26quot;;font-size:\x5B\x5Bfontsize\x5D\x5D\"\x0D\x0A\x09    onclick=\"location.href='\x5B\x5Bbasesearchurl\x5D\x5D\x26amp;JobTitle=' + escape(document.getElementById('\x5B\x5Bsearchinputdomid\x5D\x5D').value).replace(/ /g, '%20'); return false\"\x0D\x0A\x09/\x3E\x0D\x0A\x09\x3Cdiv style=\"clear:both\"\x3E\x3C!-- --\x3E\x3C/div\x3E\x0D\x0A\x3C/div\x3E\x0D\x0A";
    var params = window.RAJobsWidgetParams || {};
    params.trackingid = params.trackingid || '';
    params.njobs = params.njobs || 5;
    params.width = params.width || '150px';
    params.titletext = params.titletext || 'Top Jobs';
    params.titlecolor = params.titlecolor || '#fff';
    params.titlebackgroundcolor = params.titlebackgroundcolor || '#E01E26';
    params.bordercolor = params.bordercolor || '#E01E26';
    params.linkcolor = params.linkcolor || '#E01E26';
    params.textcolor = params.textcolor || '#646464';
    params.displaysearchbox = !!params.displaysearchbox;
    params.displaylogo = !!params.displaylogo;
    params.fontfamily = params.fontfamily || 'Arial';
    params.fontsize = params.fontsize || '13px';
    params.debug = params.debug || false;

    var redadvertisingurl = "http://www.redadvertising.co.uk/";
    var jobs = [{guid: "09e0c40e-82a5-4f2a-b1a5-e27886db9ed7",jobtitle: "PHP developer",location: "London",salary: "\xA3320 - \xA3350 per day"},{guid: "9580ae4e-c8fa-46b2-b199-ce744c26dc9a",jobtitle: "Software Architect",location: "Ethiopian, Denmark",salary: "\xA30 - \xA30 per day"},{guid: "be55274e-7bed-49c8-ba13-21df03f41ee3",jobtitle: "Software Architect - JAVA/ J2EE/ JBOSS",location: "Switzerland",salary: "\xA385k - \xA385k per annum"},{guid: "984c8c71-1ffa-4116-90c8-1fed29b188ce",jobtitle: "User Experience Architect/Contract/Immediate",location: "London",salary: "\xA3300 - \xA3400 per day"},{guid: "6634de06-fac7-4185-88a0-3dea3c26618c",jobtitle: "Architect - High spec Residential - AutoCAD",location: "UK",salary: "Negotiable"},{guid: "c8a64857-73b9-4e7d-86d1-e998edc300ce",jobtitle: "Architect Assistant - Vectorworks or AutoCAD",location: "UK",salary: "Negotiable"},{guid: "aa34fbe8-d0d4-4037-bf4c-25e09471b818",jobtitle: "CAD Designer - AutoCAD, Photoshop, 3ds Max",location: "UK",salary: "Negotiable"},{guid: "5f470b5c-ee8f-461f-b76e-27ba8be9fb38",jobtitle: "User Experience Architect/Media/London/Perm.",location: "London, Inner London",salary: ''},{guid: "6071f42b-4602-4b0f-a255-12f748213555",jobtitle: "OSS HPOV Consultant.",location: "Reading, Berkshire",salary: ''},{guid: "780b6fd7-d6aa-4c87-b9b6-5799a978cbb4",jobtitle: "User Expereince Architect/Contract/Immediate.",location: "London, Inner London",salary: ''},{guid: "35a95596-0354-4313-8b35-0ec27b6d9883",jobtitle: "Senior ETL/Informatica Powercentre Consultant.",location: "unspecified",salary: ''},{guid: "8471eccc-f343-4a61-aa78-aee90455eb45",jobtitle: "Senior User Experience Architect - Berlin.",location: "unspecified",salary: ''},{guid: "0d362516-db78-456a-840e-883c19885915",jobtitle: "Senior ETL/Informatica Powercentre Consultant.",location: "unspecified",salary: ''},{guid: "d8f41261-9972-4a3a-b49f-29f765074c15",jobtitle: "Consulting Technology - Information Management - Information",location: "unspecified",salary: ''},{guid: "50945ca1-321a-455e-8e6d-fd3d05500fee",jobtitle: "User Expereince Architect/Contract/Immediate.",location: "London, Inner London",salary: ''},{guid: "bbce238f-fed1-4f44-99a5-607c480c684f",jobtitle: "Sr. Data Architect.",location: "unspecified",salary: ''},{guid: "2cd4ddfd-edfa-486d-bcad-9d7d897a8b90",jobtitle: "Senior Thermal Architect.",location: "Cambridge, Cambridgeshire",salary: ''},{guid: "85fcb208-9d7b-4779-8d63-8a3c09fd199e",jobtitle: "Senior ETL/Informatica Powercentre Consultant",location: "Slovak, Holland",salary: "\xA30 - \xA30 per day"},{guid: "687dcaa1-b5b9-4174-ac4a-52817777fc2d",jobtitle: ".Net Architect",location: "Birmingham",salary: "\xA350 - \xA355 per annum"},{guid: "a4f13731-981c-45ec-9321-3dec0f438f3c",jobtitle: "Java Developer.",location: "Abbots Langley, Hertfordshire",salary: ''}];
    var basesearchurl = "http://www.architectsjobs.co.uk/_ppc?t=affs";
    var basejoburl = "http://www.architectsjobs.co.uk/_ppc?t=job";
    if (params.trackingid.length > 0) {
        basejoburl += '&afft=' + params.trackingid;
        basesearchurl += '&afft=' + params.trackingid;
    }    
    
    if (!params.displaysearchbox && jobs.length === 0) {
        // don't display the widget at all if we don't have any jobs and the search box isn't to be displayed
        return;
    }

    var jobshtml = '';

    if (jobs.length > 0) {
        jobshtml = '<div style="padding:0.5em;">';
        for (var i=0; i < jobs.length; i++) {
            var job = jobs[i];
            var tmp = jobtemplatehtml;

            tmp = tmp.replace(/\[\[link\]\]/g, escapeHTML(basejoburl + '&vc=' + job.guid));
            tmp = tmp.replace(/\[\[jobtitle\]\]/g, escapeHTML(job.jobtitle));
            tmp = tmp.replace(/\[\[location\]\]/g, escapeHTML(job.location));
            tmp = tmp.replace(/\[\[salary\]\]/g, escapeHTML(job.salary));
            
            jobshtml += tmp;
            
            if (i+1 >= params.njobs) {
                break;
            }
        }
        jobshtml += '<p style="margin:0 0 0.8em 0;padding:0;"><a href="[[basesearchurl]]" style="color: [[linkcolor]];font-family:&quot;[[fontfamily]]&quot;;font-size:[[fontsize]]">more jobs...</a></p>';
        jobshtml += '</div>';
    }
    
    var html = templatehtml;
    html = html.replace(/\[\[jobshtml\]\]/g, jobshtml);
    if (params.displaysearchbox) {
        searchboxhtml = searchboxhtml.replace(/\[\[searchinputdomid\]\]/g, 'randomid_' + Math.round(Math.random()*1000000));        
        html = html.replace(/\[\[searchboxhtml\]\]/g, searchboxhtml);
    } else {
        html = html.replace(/\[\[searchboxhtml\]\]/g, '');
    }
    if (params.displaylogo) {
        html = html.replace(/\[\[logohtml\]\]/g, logohtml);
    } else {
        html = html.replace(/\[\[logohtml\]\]/g, '');
    }
    html = html.replace(/\[\[basesearchurl\]\]/g, escapeHTML(basesearchurl));
    html = html.replace(/\[\[bordercolor\]\]/g, params.bordercolor);
    html = html.replace(/\[\[titletext\]\]/g, escapeHTML(params.titletext));
    html = html.replace(/\[\[titlebackgroundcolor\]\]/g, params.titlebackgroundcolor);
    html = html.replace(/\[\[titlecolor\]\]/g, params.titlecolor);
    html = html.replace(/\[\[linkcolor\]\]/g, params.linkcolor);
    html = html.replace(/\[\[textcolor\]\]/g, params.textcolor);
    html = html.replace(/\[\[fontfamily\]\]/g, params.fontfamily);
    html = html.replace(/\[\[fontsize\]\]/g, params.fontsize);
    html = html.replace(/\[\[width\]\]/g, params.width);
    html = html.replace(/\[\[redadvertisingurl\]\]/g, redadvertisingurl);
    
    document.write(html);
    
    if (params.debug) {
        setTimeout(function () { // set timeout as the body node may not be ready for DOM manip at this stage yet
            var ta = document.createElement('textarea');
            document.body.appendChild(ta);
            ta.style.width='500px';
            ta.style.height='500px';
            ta.value = html;
        }, 1000);
    }
})();
