// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* need something to trigger the hide, prototype equivalent here... */

// input#user_photo.click <-- check to see if the radio button for 'upload my own is selected' and change it if it's not
// input#user_photo_from_upload checked == checked


$(document).ready(function(e) {
  metricSelector();
  setupLabelables();
  openModal();
  setInterval('rotateOurImpact()', 3000);
  setTimeout('hideFlashMessages()',4300);
  
  $(function() {
    $("#activity_created_at").datepicker({ dateFormat: 'mm/dd/yy' });
  });

  $('#name_sort').click(function(e) {
    e.preventDefault();
    $.get('/public/organizations/sort_by_name/', {}, null, "script");	
  })

  $('#popularity_sort').click(function(e) {
    e.preventDefault();
    $.get('/public/organizations/sort_by_popularity/', {}, null, "script");	
  })
  
  $('.photo-thumbnail').click(function(e) {
    var imgTesting = new Image();
    var photoDiv = "photo-"+ this.id
    function CreateDelegate(contextObject, delegateMethod) {
      return function() {
        return delegateMethod.apply(contextObject, arguments);
      }
    }
    function imgTesting_onload() {
      var $dialogContent = $('#'+photoDiv);
      adjustedWidth = this.width + 50
      adjustedHeight = this.height + 50
      $dialogContent.dialog({
         modal: true,
         title: "Picture",
         width: adjustedWidth,
         height: adjustedHeight,
         close: function() {},
      }).show();
    }
    var picUrl = $('#pic-'+this.id).attr('src')
    imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
    imgTesting.src = picUrl
  });

  $("#register-button").click(function(e){
       if(!$("#terms").is(':checked'))
       {
         alert("Please accept the myImpact Terms of Use and Privacy Policy");
         e.preventDefault();
       }
   });

   $('.org-listing').click(function() {
     window.location = "/organizations/" + this.id
   })
   
  // function this. This could be a hover versus a click in which case keep the second function too
  $('.short-description').click(
    function (e) {
    e.preventDefault();
	  e.stopPropagation();
      var fullText = $('#full-desc-' + this.id).html()
      $('#short-desc-' + this.id).append('<span>' + fullText + '<span>')
      $(this).remove();
    },
    function () {
      $('#short-desc-' + this.id).find("span").remove();
    }
  );
  
  $('.join-org').click(function(e) {
    $.get('/organizations/join/' + this.id, {}, null, "script");
  });
  
  $('.suggest-org').click(function(e) {
    e.preventDefault();
    var $dialogContent = $("#suggest-org");

    $dialogContent.dialog({
       modal: true,
       title: "Suggest a new organization",
       width: 500,
       close: function() {},
    }).show();
  });
  
  $('.flag-org').click(function(e) {
    e.preventDefault();
    var $dialogContent = $("#flag-org");

    $dialogContent.dialog({
       modal: true,
       title: "Flag this organization for faulty data",
       width: 500,
       close: function() {},
    }).show();
  });
  
  $('#organization-select').click(function(e) {
    if ($('#organization-select option').size() == 1)
    {
      alert('click the "add an organization" link to affiliate yourself with organizations so you can track your impact with your favorite organizations')
    }
  })
  
});


function openModal() {
  $('#add-button, #add-link').click(function(e) {
    e.preventDefault();
    var $dialogContent = $("#create-panel");

    $dialogContent.dialog({
       modal: true,
       title: "Create a new activity",
       width: 500,
       close: function() {},
    }).show();
    $("#ui-datepicker-div").css("z-index", "9999");

  });
}

function hideFlashMessages() {
  $('.flash').each(function(e) {
    $('.flash').fadeOut('slow');
  });
}
function metricSelector() {
  $('#activity_activity_type_id').change(function(e) {
    // alert("check this out!")
    $.get('/activities/get_metrics', {activity_type_id: $('#activity_activity_type_id').val()}, null, "script");
  });
}

function setupLabelables() {
   $('input.labelable, textarea.labelable').focus(function(e) {
     $(this).prev('label').hide();
   });

   $('input.labelable, textarea.labelable').blur(function(e) {
      if($(this).val().length == 0) {
        $(this).prev('label').show();
      }

  });

  $('input.labelable, textarea.labelable').each(function(i) {
    if($(this).val().length > 0) {
      $(this).prev('label').hide();
    }
  });

  $('div.fieldWithErrors').each(function(e) {
     $(this).children('label').hide();
   });


  $('label').click(function(e) {
    $.get('/activities/get_metrics', {activity_type_id: $('#activity_activity_type_id').val()}, null, "script");
    $(this).next('input').focus();
  })
}
function rotateOurImpact() {
     $('#metric-rotator span.current').slideUp('slow', function() {
        var nextStat = $('#metric-rotator span.current').next()
        if(nextStat.length == 0) {
          nextStat = $('#metric-rotator span:first')
        }
        $('#metric-rotator span.current').removeClass('current')
        nextStat.addClass('current');
        nextStat.show();

     });
}
function grabActivity() {
  var activityId = $("table#recent-activity tr:first").attr("id")
  $.get('/grab_activity', {activity_id: activityId}, null, "script");
}


  
  
  // setTimeout(function() { $('#metric-rotator span#0').slideUp('slow'); }, 3000);
  // var metricSize = $('#metric-rotator span').size()
  
  // we want to start by counting the number of spans in this selector $('#our-impact span')
  // That's the length of the loop which we run through over and over again
  // set a 5 second timeout, then we:
    // hide current one and show the next one, repeat.
  // $('#our-impact span#2').show()

