/* 
JavaScript Document for BOLI Full Campaign Site
Author: James Nicol, Glossopteris Web Designs, www.glossopteris.com. July 2006
*/

/*----- Behaviour Registers to assign actions to page elements on load -----*/
var campaign_rules = {

	'#google_search' : function(el) {
		el.onclick = function() { submitGsearch(); return false; }
	},
	'#explore_campaign a' : function(el) {
		el.onmouseover = function(){ page_link_ul_bg (this, 'highlight'); }
		el.onmouseout = function(){ page_link_ul_bg (this, 'off'); }
	},
	'.page_links' : function(el) {
		el.onclick = function() { page_scroll(el); return false; }
	},
	'#text_large' : function(el) {
		el.onclick = function(){ setActiveStyleSheet('large','text'); return false; }
	},
	'#text_small' : function(el) {
		el.onclick = function(){ setActiveStyleSheet('small','text'); return false; }
	},
	'#boli_logo' : function(el) {
		el.onclick = function(){ window.location = 'http://www.dnabarcodes.org'; }		
	}
}

Behaviour.register(campaign_rules);

/*----- Set rotating masthead ------*/
function rotate_masthead()
{
	var num_imgs = 15;
	new_img = Math.round(num_imgs * Math.random());
	new_img = new_img < 1 ? 1 : new_img;
	
	$('masthead').src = 'http://barcoding.si.edu/images/mastheads/masthead_' + new_img + '.jpg';	
}

Behaviour.addLoadEvent(rotate_masthead);


/*----- Sons Of Suckerfish Dropdown (applying sfhover class to IE) -----*/
sfHover = function() 
{
	var sfEls = document.getElementById("banner_nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) 
	{
		sfEls[i].onmouseover=function() { this.className+=" sfhover"; }
		sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); }
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


/*----- Google Site Search code -----*/
function Gsitesearch()
{ 
	document.site_search.q.value="site:http://barcoding.si.edu "+document.site_search.search_text.value; 
}
function submitGsearch() 
{
	document.site_search.q.value="site:http://barcoding.si.edu "+document.site_search.search_text.value; 
	document.site_search.submit();
}
window.onsubmit = function() { Gsitesearch(); }

/*----- Function to show rollover for right nav bar (cause IE doesnt support li:hover!!!) -----*/
function page_link_ul_bg (element, state)
{
	parent_li = element.parentNode;
	if(state=='highlight'){
		parent_li.style.background = 'url(http://barcoding.si.edu/images/ul_arrow.gif) no-repeat left';
	}else{
		parent_li.style.background = 'none';
	}
}

/*----- Some Scriptaculous effects -----*/
function page_scroll(el)
{
	var ref = el.href.split('#');
	new Effect.ScrollTo(ref[1]);
}

/*----- Code for Inserting pngs into IE (sigh.....) -----*/
var objMyImg = null;

function opacity_fix() 
{
	objMyImg = new OpacityObject('boli_logo','http://barcoding.si.edu/images/boli_images/boli_corner_logo_med');
	objMyImg.setBackground();
}

Behaviour.addLoadEvent(opacity_fix);

/*----- Stylesheet switcher -----*/
function set_page_style() 
{
	var cookie = readCookie("style")
	if(cookie||cookie!=null){ setActiveStyleSheet(cookie,'full'); setCookie("style", cookie, 365); }
	else{ setActiveStyleSheet('fixed_small','full'); setCookie("style", 'fixed_small', 365); }
}

function setActiveStyleSheet(title,width) 
{
	var style_data, style_text;
	if (width!='full'){
		style_data = readCookie("style");
		style_text = style_data.split("_");
		if(width=='text'){ title=style_text[0]+'_'+title; }
		if(width=='width'){ title+='_'+style_text[1]; }
	}
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
		  a.disabled = true;
		  if(a.getAttribute("title") == title){ a.disabled = false; setCookie("style", title, 365); }
		}
	}
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

Behaviour.addLoadEvent(set_page_style);

/*-- Add Latest Event --*/
function curr_evnt_link()
{
	html = '<div id="current_event" class="right_nav_div">';
	html += '<h3>Current Event</h3>';
	html += '<ul>';
	html += '<li><a href="http://dnabarcodes2009.org" target="_blank">Visit the Third International Barcode of Life Conference website</a></li>'
	html += '<li><a href="http://barcoding.si.edu/plant_working_group.html">New Developments from the Plant Working Group!</a></li>'
	html += '</ul>';
	html += '</div>';
	
	new Insertion.Before('explore_campaign',html);
	
	html = '<li><a href="http://barcoding.si.edu/member_map.html">View CBOL member locations</li>';
	element = $$('#explore_campaign ul').first();
	new Insertion.Top(element, html);
	
	html = '<li><a href="http://barcoding.si.edu/barcoding_presentations.html">Barcoding Presentations</a></li>';
	element = $$('#banner_nav li ul')[2];
	new Insertion.Bottom(element, html);

}
Behaviour.addLoadEvent(curr_evnt_link);