/*
 * JavaScript Library Used: jQuery (1.2.6)
 * Written by Andres Gonzalez (c) 2008 - CIESIN, Columbia University
 */


/*
 * NOTE TO SELF:
 * when the menu animation is added, make sure to override default
 * behaviour of anchors that drive dropdowns to toggle submenu.
 */
$(document).ready(function(){
	if($('#data_link').length)
		data_link();
	if($('#site_selector').length)
		site_selector();
	if($('#tabs').length)
		tab_control();
	if($('#mapclient').length)
		mapInit();
});

function site_selector(){
	var sites = $('div.study_site');
	var selectTag = $('#site_selector');
	var curSite = null; //stores current site being displayed
	
	var siteWrap = $('#site_wrapper');
	var height = {}; //stores heights of all sites
	
	//show select box (dislpay: none by default for no js)
	selectTag.css('display', 'block');
	
	sites.each(function(){
		var siteId = $(this).attr('id');
		
		height[siteId] = $(this).height();
		if(siteId != selectTag.val()){
			$(this).hide();
		}else{
			curSite = $('#'+siteId);
		}
	});
	
	selectTag.change(function(){
		//get value attr of option tag which matches with site id attr
		var selSiteId = $(this).val();
		var selSite = $('#'+selSiteId);
		
		//dont attach event to "Select a Site..."
		if (selSiteId != 'null') {
			if (curSite == null) { //only for first time site is selSite
				siteWrap.animate({ height: height[selSiteId]}, {
					duration:500,
					complete: function(){ selSite.fadeIn('normal'); }
				});
				curSite = selSite;
			}else{
				curSite.fadeOut('fast', function(){
					var curSiteHeight = height[curSite.attr('id')];
					
					//if hight is not the same, than animate the transition
					if (curSiteHeight != height[selSiteId]) {
						siteWrap.animate({ height: height[selSiteId]}, {
							complete: function(){ selSite.fadeIn('normal'); }
						});
					}else{
						selSite.fadeIn('normal');
					}
					curSite = selSite;
				});
			}
		}
		
		//draw bounding box around selected area on activated map
		if(window.showMap)
			showMap(selSiteId);
	});
}// site_selector

function tab_control(){
	// switch 'noJs' class to 'yesJs', which enables rendering of tabbed interface.
	$('div.noJs').attr('class', 'yesJs');
	
	var anchors = $('#tabs').find('a');
	var divs = $('div.tabContent');
	var firstDiv;
	
	anchors.each(function(i){
		var divToShow = $(this).attr('href').slice(1);
		
		if (i == 0) {
			firstDiv = divToShow;
			$(this).parent().addClass('curTab');
		}
		
		$(this).click(function(){
			if($('#'+divToShow).is(':hidden')){
								
				//hide all divs, remove class from tabs
				divs.hide();
				anchors.parent().removeClass();
				
				//show div, add class to sel tab
				$('#'+divToShow).show();
				$(this).parent().addClass('curTab');
			}
			
			return false; // prevents default & event bubbling
		});
	});
	
	//hide all divs, except for the first one
	divs.each(function(){
		if($(this).attr('id') != firstDiv)
			$(this).hide();
	});
	
}// tab_control

function data_link(){
	//this does nothing, but preload the image
	var preload = new Image();
		preload.src = "/imgs/arrow_hide.gif";
		
	//hide toggle sections, show default selections
	$('div.toggle_visible,div.toggle_visible_fieldset').hide();
	$('div.toggle_on').show();

	//must define functions before attatching them to events
	var toggle_fields = function(){
		var selectedId = '#'+$(this).val();
		
		$(this).children().each(function(){
			var curId = '#'+$(this).val();
			$(curId+':visible').hide('fast');
			$(curId).find(':input').attr('disabled', 'disabled');
			
		});
		
		$(selectedId).find(':input').removeAttr('disabled');
		$(selectedId).show('normal');
	}// toggle_fields
	
	var toggle_fieldsets = function(){
		var selectedId = $(this).attr('href');
		
		if ($(selectedId).is(':hidden')){
			$('.' + selectedId.slice(1)).hide('fast', function(){ $(selectedId).show('normal'); });
			$(this).find('img').attr('src', '/imgs/arrow_hide.gif');
			$(this).find('img').attr('alt', 'Hide');
		}else{
			$(selectedId).hide('normal', function(){ $('.' + selectedId.slice(1)).show('fast'); });
			$(this).find('img').attr('src', '/imgs/arrow_show.gif');
			$(this).find('img').attr('alt', 'Show');
		}
		
		return false; //prevents default action & event bubbling
	}// toggle_fieldsets
	
	//attach the appropriate toggle function to the desired event for each element
	$('select#geo_type').change(toggle_fields);
	$('select#select_data').change(toggle_fields);
	$('select#program_type').change(toggle_fields);
	$('select#personnel_type').change(toggle_fields);
	$('a.toggle_fieldset').click(toggle_fieldsets);

}// data_link_init

function mapInit(){
	var mapclient = $('#mapclient');
	var text = mapclient.find('h5');
	var maxWidth = $('#portal_content').width()-2;
	var maxHeight;
	var activateMap = function(){};
	
	if(mapclient.hasClass('aProjection'))
		maxHeight = maxWidth*0.85;
	else if(mapclient.hasClass('noRepeat')) {
		maxWidth=512;
		maxHeight=400;
	}
	else
		maxHeight = maxWidth*.78;
		
	if (mapclient.hasClass('oLayers')){
		activateMap = activateOMap;
	}else{
		activateMap = activateGMap;
		$(window).unload(function(){ GUnload(); });
	}
	
	var enable = function(){
		text.css('display', 'none');
		
		mapclient.css({
			backgroundColor: '#fff',
			backgroundImage: 'url(/imgs/mapclient_bg.gif)',
			backgroundPosition: 'center center',
			border: '1px solid #039',
			cursor: 'default'
		});
		
		mapclient.animate({
			width: maxWidth,
			height:maxHeight
		},
		{
			duration:'short',
			complete:activateMap
		});
			
		mapclient.unbind('click',enable);
		
		// Log ip
		document.images["logger_img"].src = "/inc/logger.php?url=" + location.pathname;
	}
	mapclient.click(enable);
}// mapInit


/*
 * THE FOLLOWING CODE IS TEMPORARY AND WILL BE CONVERTED SOON...
 * ITS ONLY NEEDED, AND RUNS FOR IE
 */
sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" submenu";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" submenu\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover); //using window.attachEvent will ensure it only loads on IE

// This is for clearing all the forms and toggling off and on the default drop-downs - Allen
function resetForm(theForm) {
	theForm.reset();
	$('div.toggle_visible').hide('fast');
	$('div.toggle_on').show('fast');
}