// Common JavaScript code across your application goes here.

var active_color = '#574930'; // Colour of user provided text
var inactive_color = '#888'; // Colour of default text
var up = '<div class="up">Hide</div>'; // meeting hide toggle
var down = '<div class="down">More</div>'; // meeting show toggle

// default value JS
$(function() {
  var default_values = new Array();
  $('input.default-value').css('color', inactive_color);
  $('input.default-value').focus(function() {    
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });
});

function ajaxRequest(url, data_params, javascript) {
  if (!javascript) {javascript = '';}

  $.ajax({
    type: "POST",
    url: url,
    data: data_params,
    dataType: 'script',
    success: function(js){js; eval(javascript);}
  });
};

function bindDialog(buttonId, dialogId, submitCaption, submitFunction) {
  $(buttonId).click(function(){
		$(dialogId).dialog('open');
		return false;
	});
 
  $(dialogId).dialog({
		autoOpen: false,
		width: 600,
		buttons: {
			'Ok': function() { 
			  dialog = this;
			  $(this).find('form').submit(submitFunction);
				return false; 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	}); 
}

$(function bindSubmitLinks() {
  $('.submit-link').bind('click', function() {
    $(this).parents('form').submit();
  })
});

$(function bindButtons() {
  $('.ui-button, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
});

function reloadListing() {
  meetings = document.getElementsByClassName('meeting-item');
  ids = new Array(); 
  for (var i=0; i<meetings.length; i++) {
    ids[i] = meetings[i].id;
  }
  ajaxRequest('/meetings/reload_list', {'ids[]': ids, 'authenticity_token': $('#authenticity_token').attr('value')});
  return true;
};

function fadeLauncher() {
  $(".launcher").fadeOut("slow");
  setTimeout(refreshLauncher, 1000)
}

function refreshLauncher() {
  $(".launcher").html('');
  $(".launcher").show();
}

function toggleAllDetails(display, toggle) {
  var details = document.getElementsByClassName('result-details');
  var toggles = document.getElementsByClassName('view-details');
  
  $.each(details, function(i, detail) {
    if (display == 'show') {
      $(detail).show();
      $(toggles[i]).html(up);
    } else {
      $(detail).hide();
      $(toggles[i]).html(down);
    }
  })
  
  if (display == 'show') {
    toggle.setAttribute('onclick', "toggleAllDetails('hide', this)");
    $(toggle).html(up + ' Hide Details');
  } else {
    toggle.setAttribute('onclick', "toggleAllDetails('show', this)");
    $(toggle).html(down + ' View Details');
  }
}

function toggleDetails(toggle, meeting_id) {
  var item = document.getElementById('meeting-item-' + meeting_id);
  var item_title = document.getElementById('meeting-title-' + meeting_id);
  var details = document.getElementById('meeting-details-' + meeting_id);
  var date_details_open = document.getElementById('meeting-date-details-' + meeting_id + '-open');
  var date_details_closed = document.getElementById('meeting-date-details-' + meeting_id + '-closed');

  if ($(details).css('display') == 'none') {
    $(item).addClass('open');
    $(item_title).addClass('open');
    $(details).show();
    $(toggle).html(up);
    $(date_details_open).show();
    $(date_details_closed).hide();
  } else {
    $(item).removeClass('open');
    $(item_title).removeClass('open');
    $(details).hide();
    $(toggle).html(down);
    $(date_details_open).hide();
    $(date_details_closed).show();
  }
}
