
var maxJobsCount = 5;

function iecheck() {
  if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var iever = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) <= 7 );
  }
  return iever;
}

ZubkaWidget = new function() {
  var BASE_URL = 'http://www.zubka.com/';
  var STYLESHEET = BASE_URL + 'css/widget.php';
  var CONTENT_URL = BASE_URL + 'services/widget/jobs.php';
  var ROOT = 'zubka_widget';

  function addZparam( varname, paramname )
  {
    var retval = '';

    if( varname != '' )
      retval = '&' + paramname + '=' + escape( varname );

    return retval;
  }
  
  function requestStylesheet(stylesheet_url) {
    stylesheet = document.createElement("link");
    stylesheet.rel = "stylesheet";
    stylesheet.type = "text/css";
    var url = stylesheet_url + '?p1=1';
    if( typeof(zubkaWidgetHeight) != 'undefined' ) url += addZparam( zubkaWidgetHeight, 'widgetHeight' );
    if( typeof(zubkaWidgetWidth) != 'undefined' ) url += addZparam( zubkaWidgetWidth, 'widgetWidth' );
    if( typeof(zubkaHeaderImage) != 'undefined' ) url += addZparam( zubkaHeaderImage, 'headerImage' );
    if( typeof(zubkaHeaderHeight) != 'undefined' ) url += addZparam( zubkaHeaderHeight, 'headerHeight' );
    if( typeof(zubkaSubHeaderColor) != 'undefined' ) url += addZparam( zubkaSubHeaderColor, 'subHeaderColor' );
    if( typeof(zubkaFooterImage) != 'undefined' ) url += addZparam( zubkaFooterImage, 'footerImage' );
    if( typeof(zubkaFooterHeight) != 'undefined' ) url += addZparam( zubkaFooterHeight, 'footerHeight' );
    if( typeof(zubkaSubFooterColor) != 'undefined' ) url += addZparam( zubkaSubFooterColor, 'subFooterColor' );
    if( typeof(zubkaTextColor) != 'undefined' ) url += addZparam( zubkaTextColor, 'textColor' );
    if( typeof(zubkaBorderColor) != 'undefined' ) url += addZparam( zubkaBorderColor, 'borderColor' );
    if( typeof(zubkaJobsBackgroundColor) != 'undefined' ) url += addZparam( zubkaJobsBackgroundColor, 'jobsBackgroundColor' );
    if( typeof(zubkaLinkColor) != 'undefined' ) url += addZparam( zubkaLinkColor, 'linkColor' );
    if( typeof(zubkaVisitedLinkColor) != 'undefined' ) url += addZparam( zubkaVisitedLinkColor, 'visitedLinkColor' );
    if( typeof(zubkaHoverLinkColor) != 'undefined' ) url += addZparam( zubkaHoverLinkColor, 'hoverLinkColor' );
    stylesheet.href = url;
    stylesheet.media = "all";
    document.lastChild.firstChild.appendChild(stylesheet);
  }

  function requestContent( local ) {
    var script = document.createElement('script');
    var url = CONTENT_URL + '?max_jobs=' + maxJobsCount;
    var i;
    if( typeof(zubkaUserName) != 'undefined' ) url += addZparam( zubkaUserName, 'referrer' );
    if( typeof(zubkaHirerType) != 'undefined' ) url += addZparam( zubkaHirerType, 'hirer_type' );
    if( typeof(zubkaSector) != 'undefined' ) url += addZparam( zubkaSector, 'sector' );
    if( typeof(zubkaSector0) != 'undefined' ) url += addZparam( zubkaSector0, 'sector0' );
    if( typeof(zubkaSector1) != 'undefined' ) url += addZparam( zubkaSector1, 'sector1' );
    if( typeof(zubkaSector2) != 'undefined' ) url += addZparam( zubkaSector2, 'sector2' );
    if( typeof(zubkaSector3) != 'undefined' ) url += addZparam( zubkaSector3, 'sector3' );
    if( typeof(zubkaSector4) != 'undefined' ) url += addZparam( zubkaSector4, 'sector4' );
    if( typeof(zubkaSector5) != 'undefined' ) url += addZparam( zubkaSector5, 'sector5' );
    if( typeof(zubkaSector6) != 'undefined' ) url += addZparam( zubkaSector6, 'sector6' );
    if( typeof(zubkaSector7) != 'undefined' ) url += addZparam( zubkaSector7, 'sector7' );
    if( typeof(zubkaSector8) != 'undefined' ) url += addZparam( zubkaSector8, 'sector8' );
    if( typeof(zubkaSector9) != 'undefined' ) url += addZparam( zubkaSector9, 'sector9' );
    if( typeof(zubkaSector10) != 'undefined' ) url += addZparam( zubkaSector10, 'sector10' );
    if( typeof(zubkaCountry) != 'undefined' ) url += addZparam( zubkaCountry, 'country' );
    if( typeof(zubkaCountry0) != 'undefined' ) url += addZparam( zubkaCountry0, 'country0' );
    if( typeof(zubkaCountry1) != 'undefined' ) url += addZparam( zubkaCountry1, 'country1' );
    if( typeof(zubkaCountry2) != 'undefined' ) url += addZparam( zubkaCountry2, 'country2' );
    if( typeof(zubkaCountry3) != 'undefined' ) url += addZparam( zubkaCountry3, 'country3' );
    if( typeof(zubkaCountry4) != 'undefined' ) url += addZparam( zubkaCountry4, 'country4' );
    if( typeof(zubkaCountry5) != 'undefined' ) url += addZparam( zubkaCountry5, 'country5' );
    if( typeof(zubkaCountry6) != 'undefined' ) url += addZparam( zubkaCountry6, 'country6' );
    if( typeof(zubkaCountry7) != 'undefined' ) url += addZparam( zubkaCountry7, 'country7' );
    if( typeof(zubkaCountry8) != 'undefined' ) url += addZparam( zubkaCountry8, 'country8' );
    if( typeof(zubkaCountry9) != 'undefined' ) url += addZparam( zubkaCountry9, 'country9' );
    if( typeof(zubkaCountry10) != 'undefined' ) url += addZparam( zubkaCountry10, 'country10' );
    if( typeof(zubkaKeyword) != 'undefined' ) url += addZparam( zubkaKeyword, 'keyword' );
    if( typeof(zubkaKeyword0) != 'undefined' ) url += addZparam( zubkaKeyword0, 'keyword0' );
    if( typeof(zubkaKeyword1) != 'undefined' ) url += addZparam( zubkaKeyword1, 'keyword1' );
    if( typeof(zubkaKeyword2) != 'undefined' ) url += addZparam( zubkaKeyword2, 'keyword2' );
    if( typeof(zubkaKeyword3) != 'undefined' ) url += addZparam( zubkaKeyword3, 'keyword3' );
    if( typeof(zubkaKeyword4) != 'undefined' ) url += addZparam( zubkaKeyword4, 'keyword4' );
    if( typeof(zubkaKeyword5) != 'undefined' ) url += addZparam( zubkaKeyword5, 'keyword5' );
    if( typeof(zubkaKeyword6) != 'undefined' ) url += addZparam( zubkaKeyword6, 'keyword6' );
    if( typeof(zubkaKeyword7) != 'undefined' ) url += addZparam( zubkaKeyword7, 'keyword7' );
    if( typeof(zubkaKeyword8) != 'undefined' ) url += addZparam( zubkaKeyword8, 'keyword8' );
    if( typeof(zubkaKeyword9) != 'undefined' ) url += addZparam( zubkaKeyword9, 'keyword9' );
    if( typeof(zubkaKeyword10) != 'undefined' ) url += addZparam( zubkaKeyword10, 'keyword10' );
    if( typeof(zubkaKeywordOperator) != 'undefined' ) url += addZparam( zubkaKeywordOperator, 'keyword_operator' );
    if( typeof(zubkaContract) != 'undefined' ) url += addZparam( zubkaContract, 'contract' );
    if( typeof(zubkaPermanent) != 'undefined' ) url += addZparam( zubkaPermanent, 'permanent' );
    if( typeof(zubkaSalaryMin) != 'undefined' ) url += addZparam( zubkaSalaryMin, 'salary_min' );
    if( typeof(zubkaSalaryMax) != 'undefined' ) url += addZparam( zubkaSalaryMax, 'salary_max' );
    // How you'd pass the current URL into the request
    // script.src = CONTENT_URL + '&url=' + escape(local || location.href);
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

    this.init = function() {
      this.serverResponse = function( baseurl, jobs, searchLink ) {
        var jobsDiv = document.getElementById('zubkaJobs');
        var txt = "";
        if (jobs.length > 0){
            for (var i = 0; i < jobs.length; ++i) {
              txt += "<div class='zubkaJobContainer'>" +
                         "<div class='zubkaLinkContainer'>" +
                             (zubkaPreview ? "<span>" + (jobs[i].title.length > 0 ? jobs[i].title : '&nbsp;') + "</span>" : "<a href='" + jobs[i].link + "'>" + jobs[i].title + "</a>") +
                         "</div>" +
                         "<div class='zubkaCaptionContainer'>" +
                             "<span id='location'>" + (jobs[i].location.length > 0 ? jobs[i].location : '&nbsp;') + "</span>" +
                         "</div>" +
                         "<div class='zubkaRewardContainer'>" +
                             "Your reward: <span id='rewardFig'>" + jobs[i].reward + "</span>" +
                         "</div>" +
                     "</div>";
            }
        }
        for (var i = 0; i < maxJobsCount-jobs.length; ++i){
          txt += "<div class='zubkaJobContainer'>" +
                     "<div class='zubkaLinkContainer'>&nbsp;</div>" +
                     "<div id='jobContainer" + i + "' class='zubkaCaptionContainer'>&nbsp;</div>" +
                     "<div class='zubkaRewardContainer'>&nbsp;</div>" +
                 "</div>";
        }
        jobsDiv.innerHTML = txt;
        if (jobs.length < 1){
          document.getElementById('jobContainer0').innerHTML = 'No jobs were found';
        }
        document.getElementById('zubkaSearchCaption').innerHTML = zubkaSearchTitle;
        jobsDiv.style.display = 'block';
        var searchLinkDiv = document.getElementById('searchLink');
        searchLinkDiv.innerHTML = zubkaPreview ?
                                  "<span>See more jobs like this</span>" :
                                  ("<a href='" + searchLink + "'>See more jobs like this</a>");
      }
      requestStylesheet(STYLESHEET);
      document.write("<div class='zubkaHeader'>&nbsp;</div>");
      document.write("<div class='zubkaSubHeader'>" +
                       "<div id='zubkaSearchCaption' class='zubkaCaptionContainer'></div>" +
                     "</div>"
                    );
      document.write("<div id='zubkaJobs' style='display: none'></div>");
      document.write("<div class='zubkaSubFooter'>" +
                        "<div class='zubkaLinkContainer'>" +
                           (zubkaPreview ? "<span>Help</span>" : "<a href='http://www.zubka.com/help.php?page=faq'>Help</a>") +
                           "&nbsp;|&nbsp;" +
                           (zubkaPreview ? "<span>Get a widget</span>" : "<a href='http://www.zubka.com/public/what/ref-widget'>Get a widget</a>") +
                       "</div>" +
                       "<div id='searchLink' class='zubkaLinkContainer'>" +
                       "</div>" +
                     "</div>"
                    );
      document.write(zubkaPreview ? "<div class='zubkaFooter'>" : "<a href='http://www.zubka.com'><div class='zubkaFooter'></div></a>");
      requestContent();
      var no_script = document.getElementById('no_script');
      if (no_script) { no_script.style.display = 'none'; }
    }
}
ZubkaWidget.init();
