/**
 * Gets things started. This is the "onload" event of the page.
 */
var form_window;
$(document).ready(function() {
	$('#event-tabs').tabs();

	// Thumnail the maps/plots tabs
	thumbnail("map");
	thumbnail("plot");

	/*$.tablesorter.addParser({
			id: 'longint',
			is: function(s) {
				return false;
			},
			format: function(s) {
				return s.replace(/,/, '');
			},
			type: 'numeric'
		});*/

	$("#resp_by_zip").tablesorter({
		widgets: ['zebra'],
		headers: {
			//3: { sorter: 'longint' },
			6: { sorter: false },
			7: { sorter: false}
		}
	});

	$("#resp_by_city").tablesorter({
		widgets: ['zebra'],
		headers: {
			//3: { sorter: 'longint' },
			6: { sorter: false },
			7: { sorter: false }
		}
	});

	$("#resp_by_geo").tablesorter({widgets: ['zebra']});

	// Remove the old DYFI link
	var linkTarget= $('#dyfi-link a').attr('href');
	var display = $('#dyfi-link a').text();
	$('#dyfi-link').remove();

	// Create a new DYFI link next to the tabs.
	$('#event-tabs-menu').append('<li id="dyfi-link"><a href="' + linkTarget +
			'"><b>' + display + '</b></a></li>');

	// Bind a click handler to the new link
	$('#dyfi-link a').click(function(_event) {
		var url = $(this).attr('href');
		url = url.replace(/disabled/, 'enabled');
		var options = "width=500px,height=350px,resizable,scrollbars";
		if (!form_window || form_window.closed || !form_window.open) {
			form_window = window.open(url, 'ciim_form', options);
		} else {
			form_window.focus();
		}
		return preventDefault(_event);
	});

});

/**
 * "Thumbinizes" the images on the give "_tabname". The images are converted to
 * thumbnail images and set to swap with a larger image when clicked.
 */
thumbnail = function(_tabname) {
	var parent_div_id = "#"+_tabname+"-section";
	var main_image_id = "dyfi_main_" + _tabname;

	var class_to_remove  = "dyfi_image";

	// Thuminize all the original images
	if( $("img", $(parent_div_id)).length <= 1) {
		// Don't thumbnail single images
		return;
	}

	$("img", $(parent_div_id)).each(function() {
		$(this).removeClass(class_to_remove);

		var innerHtml = '<div class="thumb">';
		innerHtml += $(this).parent().html();
		innerHtml += '<br />';
		innerHtml += '<span>' + $(this).parent().attr("title") + '</span>';

		$(this).parent().replaceWith(innerHtml);
	});
	
	// Bind the click events
	$("div.thumb", $(parent_div_id)).each(function() {
		var image = $("img", $(this));
		$(this).bind("click", function() {setMainImage(_tabname, image)} );
	});

	// Store away the first image before prepending the new image
	var _first_image = $("img:first", $(parent_div_id));

	// Create the "main" image and append it to the parent div
	var main_image_str = '<div id="dyfi_main_map_wrapper">';
	
	// We only put a <a> link tag around the plots. --EMM 11/19/2007
	if(_tabname == "plot") { main_image_str += '<a href="" title="">'; }
	
	main_image_str += '<img id="'+main_image_id+'" useMap="#does_not_exist" />';
	
	// We only put a <a> link around the plots. --EMM 11/19/2007
	if(_tabname == "plot") { main_image_str += '</a>'; }
	
	main_image_str += '</div>';
	
	$(main_image_str).prependTo($(parent_div_id));

	// Set the default image values for main image
	setMainImage(_tabname, _first_image);
};

/**
 * Sets the "main" image for the "_tabname" tab.
 */
setMainImage = function(_tabname, _thumb_img) {

	var current       = "current";
	var parent_div_id = "#"+_tabname+"-section";
	var main_img_id   = "dyfi_main_" + _tabname;
	
	var new_thumb_div = _thumb_img.parent();
	var old_thumb_div = $("div."+current, $(parent_div_id) );

	var src  = _thumb_img.attr("src");
	var alt  = _thumb_img.attr("alt");
	var map  = $(_thumb_img).attr("useMap");
	if(!map) { map = "#does_not_exist"; }
	
	if(src != $("#"+main_img_id).attr("src"))  {
				
		// Remove the current image
		if(old_thumb_div) {
			old_thumb_div.removeClass(current);
		}

		swapImage(main_img_id,  src, alt, map);

		// Set the new current image
		new_thumb_div.addClass(current);
	}
};

/**
 * Swaps image information and re-displays the image.
 */
swapImage = function(_main_img_id, src, alt, map) {
	// We only put a <a> link around the plots. --EMM 11/19/2007
	var tabname = _main_img_id.substr(10);
	
	$("#"+_main_img_id).attr({
			"src"    : src,
			"alt"    : alt,
			"useMap" : map
		});
	
	// We only put a <a> link around the plots. --EMM 11/19/2007
	if(tabname == "plot") {
		$("#"+_main_img_id).parent().attr("href", src);
		$("#"+_main_img_id).parent().attr("title", alt);
	}

	// $("#"+_main_img_id).slideDown("normal");
};

printResponses = function(_network, _code) {
	var url = '/eqcenter/dyfi/inc/print_responses.php?' +
	          'network=' + _network + '&' + 
			  'code=' + _code;
	var response_view = 
		window.open(url, 'response_view', "width=750,height=950");
	response_view.focus();
};
