﻿/* Hexadecimal conversion methods.
* Copyright (c) 2006 by Ali Farhadi.
* released under the terms of the Gnu Public License.
* see the GPL for details.
*
* Email: ali[at]farhadi[dot]ir
* Website: http://farhadi.ir/
*/

//Encodes data to Hex(base16) format
function hexEncode(data) {
  var b16_digits = '0123456789abcdef';
  var b16_map = new Array();
  for (var i = 0; i < 256; i++) {
    b16_map[i] = b16_digits.charAt(i >> 4) + b16_digits.charAt(i & 15);
  }

  var result = new Array();
  for (var i = 0; i < data.length; i++) {
    result[i] = b16_map[data.charCodeAt(i)];
  }

  return result.join('');
}

//Decodes Hex(base16) formated data
function hexDecode(data) {
  var b16_digits = '0123456789abcdef';
  var b16_map = new Array();
  for (var i = 0; i < 256; i++) {
    b16_map[b16_digits.charAt(i >> 4) + b16_digits.charAt(i & 15)] = String.fromCharCode(i);
  }
  if (!data.match(/^[a-f0-9]*$/i)) return false; // return false if input data is not a valid Hex string

  if (data.length % 2) data = '0' + data;

  var result = new Array();
  var j = 0;
  for (var i = 0; i < data.length; i += 2) {
    result[j++] = b16_map[data.substr(i, 2)];
  }

  return result.join('');
}

// JobDiva Feed stream js:
function GetJobDivaFeed( elem, clear ) {

  elem = jQuery( elem );

  if ( clear ) {
    jQuery( '.twDisplay', elem ).empty();
    elem.removeData( 'tweet' );
  }

  searchTerm = jQuery( '.twSearch > input' ).val();

  if ( !elem.data( 'tweet' ) ) {
    elem.data( 'tweet', { lastId: 0, total: 12, timeout: null } );
  }

  //***************************************************************//
  ConsultNet.Modules.TopJobs.ConsultNet_TopJobs_Services
    .GetJobDivaFeed_JSON
    ( "", searchCount, "", searchTerm, "", ""
    , function (json) {

      json = json.replace(/#cdata-section/g, "text");
      jsonObj = jQuery.parseJSON(json);

      var _itemCount = 0;
      if (jsonObj.rss.channel.item != undefined ) {
        _itemCount = searchCount < jsonObj.rss.channel.item.length
                   ? searchCount
                   : jsonObj.rss.channel.item.length; 
      }

      for (var _itemIndex = 0; _itemIndex < _itemCount; _itemIndex++) {

        var _key = jsonObj.rss.channel.item[_itemIndex].key;

        if (jQuery('#tw' + _key, elem).length == 0) {
          elem.data('tweet').total++;

          var _divString = ''
              + '<div id="tw' + _key + '" class="tweet">'
              + '<div>'
              + '<li>' + jsonObj.rss.channel.item[ _itemIndex ].title.text + '&nbsp;&nbsp;&nbsp;'
              + '<a href="#" target="_blank">'
              + 'View'
              + '</a>'
              + '</li>'
              + '</div>'
              + '</div>';

          jQuery('.twDisplay', elem).append( _divString );
          jQuery('#tw' + _key, elem).hide();
          jQuery('#tw' + _key, elem).fadeIn('slow');

        }

      }

      searchTerm = escape(searchTerm);
      searchCount = 1;
      elem.data('tweet').timeout = setTimeout(function () { GetJobDivaFeedDetail(elem, false) }, 60000);

    });
    //***************************************************************//      
}

function GetJobDivaFeedDetail(elem, clear) {

  elem = jQuery(elem);

  if (clear) {
    jQuery('.twDisplay', elem).empty();
    elem.removeData('tweet');
  }

  searchTerm = jQuery('.twSearch > input').val();
  
  if (!elem.data('tweet')) {
    elem.data('tweet', { lastId: 0, total: 12, timeout: null });
  }

  //***************************************************************//
  ConsultNet.Modules.TopJobs.ConsultNet_TopJobs_Services
    .GetJobDivaFeedDetail_JSON
     ("", searchCount, "", searchTerm, "", ""
     , function (json) {

       json = json.replace(/#cdata-section/g, "text");
       json = json.replace(/\<\!\[CDATA\[/g, "");
       json = json.replace(/\]\]/g, "");

       jsonObj = jQuery.parseJSON(json);

       var _itemCount = 0;
       if (jsonObj.outertag.jobs.job != undefined) {
         _itemCount = searchCount < jsonObj.outertag.jobs.job.length
                       ? searchCount
                       : jsonObj.outertag.jobs.job.length;
       }

       for (var _itemIndex = 0; _itemIndex < _itemCount; _itemIndex++) {

         var _key = jsonObj.outertag.jobs.job[_itemIndex].key;

         if (jQuery('#tw' + _key, elem).length == 0) {
           elem.data('tweet').total++;

           var _divString = ''
             + '<div id="tw' + _key.toString() + '" class="tweet">'
             + '<div>'
             + '<li id="dialogOpener' + _key.toString() + '" style="cursor:pointer;" >'
             + jsonObj.outertag.jobs.job[_itemIndex].title + ' '
             + '(' + jsonObj.outertag.jobs.job[_itemIndex].jobdiva_no + ') '
             + '- ' + jsonObj.outertag.jobs.job[_itemIndex].location.replace(" - ", ", ")
             + '</li>'
             + '</div>'
             + '</div>'

             + '<div id="dialog' + _key.toString() + '">'
             + '<div style="height:65px;width:100%;" >'
             + '<img id="dialogLogo" '
             + 'height="76px" '
             + 'src="/Portals/0/ConsultNet_logo.jpg" '
             + 'style="border:0;" '
             + 'title="ConsultNet, LLC" '
             + 'width="298px" />'
             + '</div>'
             + '<div style="background-color:#000;height:11px;margin-bottom:20px;width:100%;" ></div>'
             + '<div style="clear:both;float:right;">'
             + '<img id="dialogForwardOpener' + _key.toString() + '" '
             + 'height="40px" '
             + 'src="/DesktopModules/ConsultNet_TopJobs/Images/icon_forward_32x40.png" '
             + 'style="border:0;padding-right:20px;" '
             + 'title="Forward this to a friend" '
             + 'width="32px" />'
             + '<img id="dialogApplyEmailOpener' + _key.toString() + '" '
             + 'height="40px" '
             + 'src="/DesktopModules/ConsultNet_TopJobs/Images/icon_email_32x40.png" '
             + 'style="border:0;padding-right:20px;" '
             + 'title="Apply by email" '
             + 'width="32px" />'
             + '</div>'
             + '<div>'
             + jsonObj.outertag.jobs.job[_itemIndex].jobdescription.text.replace(/[^\u000A\u0020-\u007E]/g, ''); 
             + '</div>'
             + '</div>'

             + '<div id="dialogApplyEmail' + _key.toString() + '">'
             + '<iframe '
             + 'frameborder="0" '
             + 'height="100%" '
             + 'src="http://jobs.consultnet.com/private/myjobs/applybyemail_outside.jsp'
             + '?a=' + escape("wljdnw1bn5zh7zauc3ep6lxa6sezar010fevfyba4n5ugq9o3v3pknkew7e2su1q") + ''
             + '&jobid=' + escape(jsonObj.outertag.jobs.job[_itemIndex].jobdivaid)
             + '&time=' + (new Date()).getTime()
             + '&subject='
             + hexEncode("Application for "
                             + jsonObj.outertag.jobs.job[_itemIndex].jobdiva_no + ' - '
                             + jsonObj.outertag.jobs.job[_itemIndex].title + ' - '
                             + jsonObj.outertag.jobs.job[_itemIndex].location.replace(" - ", ", ")
                             + " (ConsultNet.com)"
                             )
             + '&compose=1" '
             + 'width="100%" />'
             + '</div>'

             + '<div id="dialogForward' + _key.toString() + '">'
             + '<iframe '
             + 'frameborder=0 '
             + 'height="100%" '
             + 'src="http://jobs.consultnet.com/private/myjobs/emailjob.jsp'
             + '?a=' + escape("wljdnw1bn5zh7zauc3ep6lxa6sezar010fevfyba4n5ugq9o3v3pknkew7e2su1q")
             + '&jobid=' + escape(jsonObj.outertag.jobs.job[_itemIndex].jobdivaid) + '" '
             + 'width="100%" />'
             + '</div>';

           jQuery('.twDisplay', elem).append(_divString);
           jQuery('#tw' + _key, elem).hide();
           jQuery('#tw' + _key, elem).fadeIn('slow');

           jQuery(
             function () {
               jQuery('#dialog' + _key.toString())
                 .dialog({
                   autoOpen: false
                 , height: '600'
                 , hide: 'explode'
                 , minHeight: '320'
                 , minWidth: '320'
                 , modal: true
                 , show: 'blind'
                 , title: jsonObj.outertag.jobs.job[_itemIndex].title + ' '
                        + '(' + jsonObj.outertag.jobs.job[_itemIndex].jobdiva_no + ') '
                        + '- ' + jsonObj.outertag.jobs.job[_itemIndex].location.replace(" - ", ", ")
                 , width: '75%'
                 });

               jQuery('#dialogOpener' + _key.toString())
                 .click(
                   function () {
                     jQuery('#' + this.id.replace('Opener', ''))
                       .dialog('open');
                     return false;
                   }
                 );

             }
           );

           jQuery(
             function () {
               jQuery('#dialogApplyEmail' + _key.toString())
                 .dialog({
                   autoOpen: false
                 , height: '450'
                 , hide: 'explode'
                 , minHeight: '450'
                 , minWidth: '600'
                 , modal: true
                 , show: 'blind'
                 , title: 'Appy: '
                        + jsonObj.outertag.jobs.job[_itemIndex].title + ' '
                        + '(' + jsonObj.outertag.jobs.job[_itemIndex].jobdiva_no + ') '
                        + '- ' + jsonObj.outertag.jobs.job[_itemIndex].location.replace(" - ", ", ")
                 , width: '600'
                 });

               jQuery('#dialogApplyEmailOpener' + _key.toString())
                 .click(
                   function () {
                     jQuery('#' + this.id.replace('Opener', ''))
                       .dialog('open');
                     return false;
                   }
                 );

             }
           );

           jQuery(
             function () {
               jQuery('#dialogForward' + _key.toString())
                 .dialog({
                   autoOpen: false
                 , height: '475'
                 , hide: 'explode'
                 , minHeight: '475'
                 , minWidth: '400'
                 , modal: true
                 , show: 'blind'
                 , title: 'Forward: '
                        + jsonObj.outertag.jobs.job[_itemIndex].title + ' '
                        + '(' + jsonObj.outertag.jobs.job[_itemIndex].jobdiva_no + ') '
                        + '- ' + jsonObj.outertag.jobs.job[_itemIndex].location.replace(" - ", ", ")
                 , width: '400'
                 });

               jQuery('#dialogForwardOpener' + _key.toString())
                 .click(
                   function () {
                     jQuery('#' + this.id.replace('Opener', ''))
                       .dialog('open');
                     return false;
                   }
                 );

             }
           );

         }

       }

       searchTerm = escape(searchTerm);
       searchCount = 1;
       elem.data('tweet').timeout = setTimeout(function () { GetJobDivaFeedDetail(elem, false) }, 60000);

     }
     );
     //***************************************************************//      
}

function searchInputKeyDown( clientID ) {
  if (event.keyCode == 13) {
    document.getElementById(clientID).click;
    return false;
  }

}

function submitSearch() {
  elem = jQuery(this).parents('.monitter');

  if (!clearOnSearch) {
    jQuery('.twDisplay', elem).empty();
    elem.removeData('tweet');
  }

  if (elem.data('tweet')) { clearTimeout(elem.data('tweet').timeout); }
  elem.attr('title', jQuery('.twSearch > input', elem).val());

  searchCount = dnn.getVar('SearchCount');
  GetJobDivaFeedDetail( elem, clearOnSearch );
  return false;
}

jQuery(document).ready(
  function () {
    clearOnSearch = (dnn.getVar('ClearOnSearch') == 'True');
    searchCount   = dnn.getVar('SearchCount');
    searchDisplay = (dnn.getVar('SearchDisplay') == 'True');
    searchTerm    = dnn.getVar('SearchTerm');

    var timeouts = {};

    jQuery('.monitter')
      .find('.twSearch > input')
      .addClass('searchbox')
      .end()
      .find('.twSearch > a')
      .click(submitSearch)
      .end()
      .each(
         function (e) {
           GetJobDivaFeedDetail(this, true);
         }
       );
  }
);
