$(document).ready(function(){
   // tab changing
   $('div.tab a').click(function(){
      $('div.tab').removeClass('tab_on');
      $(this).parent().parent().parent().addClass('tab_on');
      show_progress();
   });
   $('div.tab').hover(function(){
      $(this).addClass('tab_hover');
   }, function(){
      $(this).removeClass('tab_hover');
   });
   $('#contact_search_btn').click(function(){
      $('#contact_search').css('left', $(this).offset().left);
      $('#contact_search').css('top', $(this).offset().top+25);
      $('#contact_search').fadeIn();
      hide_progress();
   });
   $('#contact_search_close_btn').click(function(){
      $('#contact_search').fadeOut();
   });
   $('div.search_box a').click(function(){
      show_progress();
   });
   // button cliking - show progress
   $('div.sbtn a').click(function(){
      show_progress();
   });
   // link cliking - show progress
   $('tr.odd a,tr.even a').click(function(){
      show_progress();
   });
   $('.progress_checkbox').click(function(){
      show_progress();
   });
   // row highlighting
   $('tr.odd,tr.even').hover(function(){
      $(this).addClass('highlighted_row');
   }, function(){
      $(this).removeClass('highlighted_row');
   });
   // select/deselect all - TODO: will not work with multiple lists on the same page
   $('input[@name=tb]').click(function(){
      if($(this).attr('checked') == true) {
         $('td.tb input[@type=checkbox]').attr('checked', true);
      } else {
         $('td.tb input[@type=checkbox]').attr('checked', false);
      }
   });
   // show/hide timestamps in admin page. Doesnt leave page so hide progress
   $('#show_date_times').toggle(function() {
      $('.datetime').css('display', 'inline');
      $('#show_date_times').text('Hide datetimes');
      this.blur(); // purely aesthetic, removes dotted outline
      hide_progress();
   }, function() {
      $('.datetime').css('display', 'none');
      $('#show_date_times').text('Show datetimes');
      this.blur();
      hide_progress();
   });
   // Select actions
   $('tr.actions select').change(function(){
   	var int_num_selections = 0;
      var any = false;
      $('input[@name^=do_action]').each(function(){
         if($(this).attr('checked') == true) {
            // $(this).parent().parent().css('background-color', 'red');
            any = true;
            int_num_selections++;
         }
      });
      if(any == false) {
         alert('No rows selected');
         $(this).val('None');
         return false;
      }

		// check the maximum select limit
		// set up a limit by adding the attribute 'selectmax="5"' to the
		// select's option tag.
		$('tr.actions select option').each(function() {
			if($(this).attr("selected") == true) {
				if (parseInt($(this).attr("selectmax")) > 0 && int_num_selections > parseInt($(this).attr("selectmax"))) {
					alert("You can only pick a maximum of "+parseInt($(this).attr("selectmax"))+" options");
					// set the select's value back to None - don't continue with the process
					$('tr.actions select').val('None');
				}

				if (parseInt($(this).attr("selectmin")) > 0 && int_num_selections <= parseInt($(this).attr("selectmin"))) {
					alert("You must pick a minimum of "+parseInt($(this).attr("selectmin"))+" options");
					// set the select's value back to None - don't continue with the process
					$('tr.actions select').val('None');
				}

			};
		});

		// make sure that the selectlimit checks succeeded/validated
		if ($(this).val() == 'None') {
			return;
		}

      if(get_confirmation()) {
         $('#do_action_form').attr('action', '/'+$(this).val());
         $('#do_action_form').submit();
      } else {
         $('#do_action_form').attr('action', '');
         $(this).val('None');
         return false;
      }

   });

   $('td.row_actions select.url_picker').change(function(){
      if ($(this).val().length > 0) {
         window.location.href = $(this).val();
      }
   });

   // Allow pressing <enter> in the search field to trigger the search
   $("#str_ocs").keypress(function(e) {
      var int_key_id = (window.event) ? event.keyCode : e.keyCode;
      // if its the <enter> key
      if (int_key_id == 13) {
         $('#btn_').click();
      }
   });

   // Project Ajax Preview
   $('A.preview_project_link').click(function () {
      // Modalise
      if ($('#modalising_div').size() == 0) {
         $('body').append('<div id="modalising_div">');
      }

      $('#modalising_div').css('position','absolute');
      $('#modalising_div').css('top','0px');
      $('#modalising_div').css('left','0px');
      $('#modalising_div').css('width','100%');
      $('#modalising_div').css('height',$(document).height());
      $('#modalising_div').css('opacity','0.3');
      $('#modalising_div').css('background-color','#aaaaaa');
      $('#modalising_div').css('z-index','500');

      // Create Pop-Up
      var window_width = $('#app_body').width();
      var window_height = $(window).height();

      if ($('#ajax_project_preview').size() == 0) {
         $('body').append('<div id="ajax_project_preview" style="background: #FFFFFF; border: 2px solid #A1BDD1; display: none; height: 500px; position: absolute; width: 700px; z-index: 510;"></div>');
      } else {
         $('#ajax_project_preview').empty();
      }

      $('#ajax_project_preview').html('<div id="ajax_project_preview_header"><a href="#" onclick="javascript: close_ajax_project_preview();">CLOSE [X]</a></div><iframe src="' + $(this).attr('href') + '" frameborder="0" scrolling="auto" width="700px" height="475px"></iframe>');
      $('#ajax_project_preview').show();
      var int_width = $('#ajax_project_preview').width();
      var int_height = $('#ajax_project_preview').height();

      $('#ajax_project_preview').css('left',(window_width/2)-(int_width/2));
      $('#ajax_project_preview').css('top',(window_height/2)-(int_height/2));

      hide_progress();

      return false;
   });
});

function get_confirmation() {
	return confirm('Please confirm');
}

function confirm_and_redirect(str_target_location) {
	if (get_confirmation()) {
		window.location.href = str_target_location;
	} else {
		hide_progress();
	}
}

function show_progress() {
   // $('div.tab_body').hide();
   $('#progress').show();
}

function hide_progress() {
	$('#progress').hide();
}

function show_results_filters() {
   hide_progress();

   // Modalise
   $('body').append('<div id="modalising_div">');
   $('#modalising_div').css('position','absolute');
   $('#modalising_div').css('top','0px');
   $('#modalising_div').css('left','0px');
   $('#modalising_div').css('width','100%');
   $('#modalising_div').css('height',$(document).height());
   $('#modalising_div').css('opacity','0.3');
   $('#modalising_div').css('background-color','#aaaaaa');
   $('#modalising_div').css('z-index','500');

   // Show pop-up
   var window_width = $('#app_body').width();
   var window_height = $(window).height();
   $('#results_filters').show();
   var filters_width = $('#results_filters').width();
   var filters_height = $('#results_filters').height();

   $('#results_filters').css('left',(window_width/2)-(filters_width/2));
   $('#results_filters').css('top',(window_height/2)-(filters_height/2));
}

function apply_results_filters(int_project_id, int_report_type_id) {
   var str_location = '/AdvancedReportPage/?int_project_id=' + int_project_id + '&int_report_type_id=' + int_report_type_id + '&int_results_per_page=9999';

   var obj_field_filter = $('#str_results_filter_field_name').get(0);
   if (obj_field_filter.selectedIndex == 0) {
      alert('You must select a field to filter on.');
      return false;
   }

   if ($('#str_results_filter_comparison_value').val().length < 2) {
      alert('Please enter a filter value at least 2 characters long.');
      return false;
   }

   str_location = str_location + '&str_filter_field=' + $('#str_results_filter_field_name').val();
   str_location = str_location + '&str_filter_value=' + $('#str_results_filter_comparison_value').val();

   window.location = str_location;
}

function cancel_apply_results_filters() {
   $('#results_filters').hide();
   $('#modalising_div').remove();
   hide_progress();
}

function clear_result_filters(int_project_id, int_report_type_id) {
   var str_location = '/AdvancedReportPage/?int_project_id=' + int_project_id + '&int_report_type_id=' + int_report_type_id + '&int_results_per_page=15';
   window.location = str_location;
}

function close_ajax_project_preview() {
   $('#ajax_project_preview').hide();
   $('#modalising_div').remove();
}

