var timer = 0;
var jobIndex = 0;
var numJobs = 0;
var xposition = 0;
var zmagsThumbnailsWidth = 0;
var zmagsThumbnailsTotalWidth = 0;
var numZmagsThumbnails = 0;
var zmagsThumbnailsPosition = 0;


function clearField(field) {
  field.value = "";
}


function toggleElement(element) {
  var e = document.getElementById(element);

  if (e.style.display != "block") {
    e.style.display = "block";
  }
  else {
    e.style.display = "none";  
  }
}


function toggleClassWithId(id, class_one, class_two) {
  var element = document.getElementById(id);
  toggleClass(element, class_one, class_two);
}


function toggleClass(element, class_one, class_two) {
  if (element.className != class_one) {
    element.className = class_one;
  }
  else {
    element.className = class_two;  
  }
}


function jobCarousel(speed) {
  toggleElement("job_0");  
  timer = setInterval("swapJobArticle()", speed);
}


function addJobArticle() {
  numJobs++;
}


function swapJobArticle() {
  toggleElement("job_" + jobIndex);  
  jobIndex++;
  if(jobIndex == numJobs) {
    jobIndex = 0;
  }
  toggleElement("job_" + jobIndex);
}


function updateDateFields() {

  if(document.getElementById('cal_0')) {
    var fromObj = cal_0.getDate();  
    var fromDate = fromObj.getDate() + "-" + (fromObj.getMonth() + 1) + "-" + fromObj.getFullYear();
    document.getElementById("minDate").value = fromDate;
    document.getElementById("searc_from").innerHTML = fromDate;
    document.getElementById("search_from_text").style.display = "none";
  }
  
  if(document.getElementById('cal_1')) {
    var toObj = cal_1.getDate();
    var toDate = toObj.getDate() + "-" + (toObj.getMonth() + 1) + "-" + toObj.getFullYear();  
    document.getElementById("maxDate").value = toDate;
    document.getElementById("searc_to").innerHTML = toDate;  
    document.getElementById("search_to_text").style.display = "none";
  }
}

function updateDateFields(calid) {

if (calid == 'cal_0') {
  if(document.getElementById('cal_0')) {
    var fromObj = cal_0.getDate();  
    var fromDate = fromObj.getDate() + "-" + (fromObj.getMonth() + 1) + "-" + fromObj.getFullYear();
    document.getElementById("minDate").value = fromDate;
    document.getElementById("searc_from").innerHTML = fromDate;
    document.getElementById("search_from_text").style.display = "none";
  }
}

if (calid == 'cal_1') { 
  if(document.getElementById('cal_1')) {
    var toObj = cal_1.getDate();
    var toDate = toObj.getDate() + "-" + (toObj.getMonth() + 1) + "-" + toObj.getFullYear();  
    document.getElementById("maxDate").value = toDate;
    document.getElementById("searc_to").innerHTML = toDate;  
    document.getElementById("search_to_text").style.display = "none";
  }
}
}


function delicious() {
  toggleElement("share_article");
  window.open('http://del.icio.us/post?v=4;url=' + 
              encodeURIComponent(location.href) + 
              '; title=' + 
              encodeURIComponent(document.title),'');
}


function facebook() {
  toggleElement("share_article");
  window.open('http://www.facebook.com/sharer.php?u=' + 
              encodeURIComponent(location.href) + 
              '&t=' + 
              encodeURIComponent(document.title), 'sharer', 'toolbar=0, status=0, width=626, height=436');
}


function googleBookmarks() {
  toggleElement("share_article");
  window.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=" + 
              encodeURIComponent(document.location) + 
              "&title=" + 
              encodeURIComponent(document.title), "bkmk_popup", "left=" + 
              ((window.screenX || window.screenLeft) + 10) + 
              ";top=" + 
              ((window.screenY || window.screenTop) + 10) + 
              ", height=420px, width=550px, resizable=1, alwaysRaised=1");
}


function mySpace() {
  toggleElement("share_article");
  window.open('http://www.myspace.com/index.cfm?fuseaction=postto&' + 
              't=' + 
              encodeURIComponent(document.title) + 
              '&c=""&u=' + 
              encodeURIComponent(location.href) + 
              '&l=2');
}


function digg() {
  window.open('http://digg.com/submit?url=' + 
              encodeURIComponent(location.href) + 
              '&title=' + 
              encodeURIComponent(document.title) + 
              '&bodytext=&media=news&topic=business_finance', '');
}


function hideDuplicate(articleId) {
  if(document.getElementById("article" + articleId)) {
    document.getElementById("article" + articleId).style.display = "none";
  }
  else {
    document.getElementById("last_story").style.display = "none";    
  }  
}


function scrollingJobs(speed) {
  timer = setInterval("scrollJobs()", speed);
}


function scrollJobs() {
  xposition += 161;
  if (xposition > (161 * (numJobs - 2))) {
    xposition = 0;
  }
  document.getElementById("jobs_container").style.right = xposition + "px";
}


function stopScrolling() {
  clearInterval(timer);
}


function initZmagsThumbnails(thumbnailWidth, numThumbnails) {
  zmagsThumbnailsWidth = thumbnailWidth;
  numZmagsThumbnails = numThumbnails;
  zmagsThumbnailsTotalWidth = numZmagsThumbnails * zmagsThumbnailsWidth; 
  document.getElementById('zmags_thumbnails').style.width = zmagsThumbnailsTotalWidth + "px";    
}


function scrollZmagsThumbnails(direction) {

  if(numZmagsThumbnails > 6) {
    if(direction == "left") {
      var limit = -(zmagsThumbnailsTotalWidth - (6 * zmagsThumbnailsWidth));
    
      zmagsThumbnailsPosition -= zmagsThumbnailsWidth;
      if(zmagsThumbnailsPosition < limit) {
        zmagsThumbnailsPosition = limit;
      }
    }
    
    if(direction == "right") {
      zmagsThumbnailsPosition += zmagsThumbnailsWidth;  
      if(zmagsThumbnailsPosition > 0) {
        zmagsThumbnailsPosition = 0;
      }    
    }
  }
  
  document.getElementById('zmags_thumbnails').style.left = zmagsThumbnailsPosition + "px";  
}


function centerZmagsThumbnails() {
  var element = document.getElementById('zmags_thumbnails_container');
  element.style.cssFloat = "none";
  element.style.margin = "10px auto 10px auto";
  element.style.width = (zmagsThumbnailsTotalWidth - 35) + "px";    
}


function togglePopUps(e) {

  var clickedElement = e.element().identify();

  if(document.getElementById('email_form_div')) {
    if(!$(clickedElement).descendantOf('email_form_div') &&
       document.getElementById('email_form_div').style.display == "block") {
      toggleElement('email_form_div');
    }
  }

  if(document.getElementById('share_article')) {  
    if(!$(clickedElement).descendantOf('share_article') &&
            document.getElementById('share_article').style.display == "block") {
      toggleElement('share_article');         
    }
  }
  /*
  if(!$(clickedElement).descendantOf('login_box') &&
          document.getElementById('login_box').style.display == "block") {
    toggleElement('login_box');
    toggleClassWithId('login_link', 'active', 'inactive');
  }
  */
}









