//Perform Strip Function
function StripString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//Perform Clock Operation
function calctime() {
  var currenttime = new Date();
  var hours = currenttime.getHours();
  var minutes = currenttime.getMinutes();
  var seconds = currenttime.getSeconds();
  var timesuffix = "AM";
  if (hours > 11) {
    timesuffix = "PM";
    hours = hours - 12;
  }
  if (hours == 0) {
    hours = 12;
  }
  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (seconds < 10) {
    seconds = "0" + seconds;
  }
  $('#alarmclock').text(hours + ":" + minutes + ":" + seconds + " " + timesuffix);
  if ( $('#hidden_alarm_time').text() ) {
    if( ((currenttime.getTime() / 1000) | 0) == parseInt($('#hidden_alarm_time').text()) ) {
      window.location.href="/videos/show";
    } 
  }
  setTimeout("calctime()", 1000);
}

function showWorking() {
  $('#working_indicator').show();
}
function hideWorking() {
  $('#working_indicator').hide();
}

////
// Behaviors
$(document).ready(function() {
  
  //Alarm Submit
  //var options = { 
    //target:        '#alarm_set_for',   // target element(s) to be updated with server response 
    //beforeSubmit:  showRequest,  // pre-submit callback 
    //success:       showResponse  // post-submit callback 
    // other available options: 
    //url:       url         // override for form's 'action' attribute 
    //type:      type        // 'get' or 'post', override for form's 'method' attribute 
    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
    //clearForm: true        // clear all form fields after successful submit 
    //resetForm: true        // reset the form after successful submit 
    // $.ajax options can be used here too, for example: 
    //timeout:   3000 
  //}; 
  // New Alarm Submit
  $('#new_alarm_form').ajaxForm({target: '#alarm_set_for'});
  
  //Get Featured Videos Link
  $('#featured_videos_link').click(function() {
    $(this).ajaxSubmit({
        target: '#video_list',
        url: '/videos',
        type: 'get',
        beforeSubmit: showWorking,
        success: hideWorking
    });
  })
  
  //Get Video Tag
  $('#search_videos_form').ajaxForm({target: '#video_list', beforeSubmit: showWorking, success: hideWorking});
  
  //Change Video Form
  $('#page_forward_form').ajaxForm({target: '#video_list', beforeSubmit: showWorking, success: hideWorking});
  $('#page_backward_form').ajaxForm({target: '#video_list', beforeSubmit: showWorking, success: hideWorking});
  
  //Set new video
  $('.new_video_form').ajaxForm({target: '#video_selected'}); 
  
  //Set the timezone value
  var currenttime = new Date()
  $('#tz_offset').val(currenttime.getTimezoneOffset())
  //
  calctime();
});