var items;
var lastId = '';
var COOKIE_NAME = 'explore_deeper_last_value';
var DIVISION = 'marine';
var cookie_options = { path: '/', expires: 30 }; 

/* Explore Deeper Cookie */
$(function() {   
    if($.cookie(COOKIE_NAME + DIVISION) != null){
      lastId = $.cookie(COOKIE_NAME + DIVISION);
      $('#explore-tab').show();
    } 
});
    
function startExploreDeeper(parentSelector) {
  if(parentSelector == null){
    parentSelector = 'body';
  }
  $('#edItemWrapper', parentSelector).hide();
  $('#applications', parentSelector).unbind('change');
  $('#applications', parentSelector).change(function(){
    processExploreDeeper(parentSelector);
    lastId = $(this).val();
    $.cookie(COOKIE_NAME + DIVISION, lastId, cookie_options);
  });
  if(lastId.length > 0 && lastId != "0"){
      $('#applications', parentSelector).val(lastId).change();
  }
}

function processExploreDeeper(parentSelector){
    if(parentSelector == null){
      parentSelector = 'body';
    }
	$('.edItem', parentSelector).hide();
	var results = '';
	var search = false;
	var val = $('#applications', parentSelector).val();
	$.each(items, function(key, applications){
		if(val != '0' && val.length > 0){
			search = true;
			$.each(applications, function(i, app) {
				if (app == val) {
					$('#edItem-'+ key, parentSelector).show();
				}
			});
		}
	});
	if(search == true){
		$('#edItemWrapper', parentSelector).show()
		$('#edResults', parentSelector).show();
		var count = $('.edItem:visible', parentSelector).length;
		results = count;
		if (count == 1) {
			results += ' Result';
		} else {
			results += ' Results';
		}
		results += ' for ' + val;
		
		$('#edResultsText', parentSelector).html(results);
		initScrollers();
	    resetScrollers();
	} else{
		$('#edItemWrapper', parentSelector).hide();
		$('#edResults', parentSelector).hide();
	}
};