




// 2009-12-05 - this function is from here: 
// http://phpjs.org/functions/time:562
function time () {
    // Return current UNIX timestamp  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/time
    // +   original by: GeekFG (http://geekfg.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: metjay
    // +   improved by: HKM
    // *     example 1: timeStamp = time();
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
    
    return Math.floor(new Date().getTime()/1000);
}




// borrowed from here: http://phpjs.org/functions/strpos:545
function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}




// borrowed from here: http://phpjs.org/functions/substr:558
function substr (str, start, len) {
    // Returns part of a string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // +      revised by: Theriault
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: false
// Add: (?) Use unicode.semantics and/or unicode.runtime_encoding (e.g., with string wrapped in "binary" or "Binary" class) to
// allow access of binary (see file_get_contents()) by: charCodeAt(x) & 0xFF (see https://developer.mozilla.org/En/Using_XMLHttpRequest )
// Fix: Handle 4-byte characters
 
    str += '';
    var end = str.length;
    if (start < 0) {
        start += end;
    }
    end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
    // PHP returns false if start does not fall within the string.
    // PHP returns false if the calculated end comes before the calculated start.
    // PHP returns an empty string if start and end are the same.
    // Otherwise, PHP returns the portion of the string from start to end.
    return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
}









function isPrizeAtLeastTheMinimumAllowedAmount() {
  var step3PrizeAmount = $('#step3PrizeAmount').val(); 
  step3PrizeAmount = parseInt(step3PrizeAmount);
  if (step3PrizeAmount < 4) {   
    alert("The prize must be at least $4. You have only offered $" + step3PrizeAmount + "."); 
    return false;
  }
}










function getRemainingTime(endtimeAsUnixTimestamp) {
  var currentTime = time(); 
  var timeRemainingInSeconds = endtimeAsUnixTimestamp - currentTime; 

  if (timeRemainingInSeconds > 86400) {
    var howMuchMoreBeyondToday = timeRemainingInSeconds - 86400;
    var howManyDays = Math.floor(timeRemainingInSeconds / 86400);
  }

  var hoursAndMinutesOfTheLastDay = timeRemainingInSeconds % 86400;
  var howManyHours = "";

  if (hoursAndMinutesOfTheLastDay > 3600) {
    howManyHours = Math.floor(hoursAndMinutesOfTheLastDay / 3600);
  }

  var howManyMinutesAsSeconds = hoursAndMinutesOfTheLastDay % 3600;
  var howManyMinutes = Math.floor(howManyMinutesAsSeconds / 60);
  if (howManyMinutes < 10) howManyMinutes = "0" + howManyMinutes;
  if (howManyMinutes.length < 2) howManyMinutes = "0";

  var howManySeconds = howManyMinutesAsSeconds % 60;
  if (howManySeconds >= 0  && howManySeconds < 10) howManySeconds = "0" + howManySeconds;

  var days = "";
  var hours = "";
  var minutes = "";
  
  if (howManyDays > 0) days = howManyDays + " days, ";
  if (howManyHours > 0) hours = howManyHours; 
  if (howManyMinutes > 0) minutes = howManyMinutes; 

  if (minutes == "" || minutes < 1) minutes = "00";
  if (hours == "" || hours < 1) hours = "00";

  if (parseInt(howManySeconds) < 0) {
    var finalTime = "00:00:00 (Expired)";
  } else {
    var finalTime = days + hours + ":" + minutes + ":" + howManySeconds + "";
  }

  return finalTime;
}









function showTimeRemainingOnIndividualQuestionPage() {
  $('#time-remaining').each(function() {
    var endtimeAsUnixTimestamp = this.title;
    var finalTime = getRemainingTime(endtimeAsUnixTimestamp);
    this.firstChild.nodeValue = finalTime; 
  });
}









function textCounter(field) {
  var referenceToField = document.getElementById(field);
  document.getElementById('characters_remaining').innerHTML = 65 - referenceToField.value.length;
  if (referenceToField.value.length > 65) $('#characters_remaining').addClass('too_many_characters'); 
  if (referenceToField.value.length < 66) $('#characters_remaining').removeClass('too_many_characters'); 
}





$(document).ready(function() {


  // Create the tooltips on document load
  $('.question a').qtip();

  $("a#trigger").click(function(){
          $("div.pulldown").slideToggle("slow,");
  });

  $('#short-title').keydown(function() {
    textCounter('short-title'); 
  });
  $('#short-title').keyup(function() {
    textCounter('short-title'); 
  });
  
  $('#step3Form').submit(function() {
    return isPrizeAtLeastTheMinimumAllowedAmount();
  });

  $('#step4Submit').click(function() {
    var refToForm = document.getElementById('step4Form');
    alert(refToForm); 
    refToForm.submit(); 
  });

  $('#pickAWinnerOptionalPaymentsLink').click(function() {
    $('#pickAWinnerOptionalPayments').toggle('slow'); 
    $('#pickAWinnerMainPrize').toggle('slow'); 
  });

  setInterval("showTimeRemainingOnIndividualQuestionPage()", 1000);

  $('div.answer a.answer_thread_reply_link').each(function() {
    $(this).click(function() {
      var relId = $(this).attr('rel'); 
      var idOfHiddenDiv = "answerExpanded-section" + relId;
      $('#' + idOfHiddenDiv).toggle("slow");
      var currentUrl = location.href; 
      // remove the anchor attached the last time someone clicked on something
      var locationOfAnchor = strpos (currentUrl, "#"); 
      if (locationOfAnchor > 0) currentUrl = substr(currentUrl, 0, locationOfAnchor);
      var urlWithAnchor = currentUrl + "#" + idOfHiddenDiv;
      location.href = urlWithAnchor;
      return false; 
    });
  });
});









