/** 
 * JavaScript method array.filter(filter_string)
 *
 * http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
 *
 */
if (!Array.prototype.filter) {
	Array.prototype.filter = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function") throw new TypeError();
		var res = new Array();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) {
				var val = this[i]; // in case fun mutates this
				if (fun.call(thisp, val, i, this))
				res.push(val);
			}
		}
		return res;
	};
}





/* ================================================================================================ */
/**
 * BEGIN GENERAL JQUERY 
 *
 */

$(function() {	


	/* ================================================================================================ */
	/**
	 * ENABLE GENERAL PAGE BEHAVIORS 
	 *
	 */

	function enable_lightbox() {
		$('a.lightbox').lightBox();
	};
	enable_lightbox();

	function initialise_scroll_pane() {
		$('.scroll-pane').jScrollPane();
	};

	function catch_dynamic_links() {
		$('a.local').click(function(event) {
			event.preventDefault();
			window.location.href = event.target.href;
			var hash_request = window.location.hash.substr(1);
			if (hash_request) {
				updateTopicHandler(window.location.protocol + "//" + window.location.hostname + hash_request);
			} else {
				window.location = event.target.href;
			};
		
		});
	};


	function enable_page_behaviors() {
		enable_lightbox();
		initialise_scroll_pane();
		catch_dynamic_links();

	
	};




	/* ================================================================================================ */

	scrollPaneShowLoading = function() {
		$('.scroll-pane').fadeOut(500, function() {
			$('.scroll-pane').html('<p><img src="/_assets/img/ajax-loader.gif" /> <br /><br />Loading...</p>');
			$('.scroll-pane').show();
		});
	};
	scrollPaneHideLoading = function() {
		$('.scroll-pane').fadeIn(500, enable_page_behaviors);
	};




	
	/** 
	 * Resize Google Maps map on load to fill available height and bind to window resize event
	 *
	 */

	function resize_map() {
		availableHeight = $(window).height() - $('#navigation').height();
		$('#map-only').height(availableHeight);
		$('#map').height(availableHeight-70);
	};
	resize_map();
	$(window).bind("resize", resize_map);

	function resize_infopanel() {
		availableHeight = $(window).height() - $('#navigation').height();
		//$('#infopanel-only').height(availableHeight);
		//$('#map-infopanel .scroll-pane').height(availableHeight-70);
		$('#map-infopanel').height(availableHeight-70);
	};
	resize_infopanel();
	$(window).bind("resize", resize_infopanel);


	/** 
	 * Resize image on static pages
	 *
	 */
	function resize_static_image() {
		availableHeight = $(window).height() - $('#navigation').height();
		availableWidth = $('#graphic').width();
		graphicWidth = $('#graphic img').width();
		graphicHeight = $('#graphic img').height();
		widthRatio = availableWidth/graphicWidth;
		heightRatio = availableHeight/graphicHeight;
		if (widthRatio > heightRatio) {
			$('#graphic img').width(graphicWidth*widthRatio).height(graphicHeight*widthRatio);
		} else {
			$('#graphic img').width(graphicWidth*heightRatio).height(graphicHeight*heightRatio);
		};
	};
	resize_static_image();
	$(window).bind("resize", resize_static_image);


	
	

}); //END JQUERY



function updateTopicHandler(title_permalink) {
	// split the url, get the title, which is the only useful part
	var url_array = title_permalink.split('/');
	var title_permalink_short = "/" + url_array[3] + "/" + url_array[4] + "/" + url_array[5];
	//location.href = location.href.replace(/\d+$/, this.value)
	window.location.href = "#" + title_permalink_short;

	//scrollPaneShowLoading();										// fade out and load "loading icon"
	$(".scroll-pane").load(title_permalink, scrollPaneHideLoading);	// load content and fade back in

	// update map with KML, if it exists
	var myUrl = 'http://www.deltanationalpark.org/topics/topic_kml/' +  url_array[5];
	//myUrl += '&cachedisable='+(new Date()).valueOf(); // uncomment this line to disable Google Maps KML cache
	gmap_load_kml(myUrl);
}


function updateTagDescriptionHandler(tag_name) {
	var tag_url = "/includes/tag_view/tag/" + tag_name;
//	scrollPaneShowLoading();
	$(".scroll-pane").load(tag_url, scrollPaneHideLoading);

	var title_permalink_short = "/tag/" + tag_name;
	window.location.href = "#" + title_permalink_short;
}






/* QUESTIONABLE ========================== */

function isempty(x){
	if(x!=="")
    return true;
}
