var ToggleSearch = Behavior.create({     
	onfocus: function(e) {
		if($$(text_box).value == txt) $$(text_box).value = '';
	},
	onblur: function(e) {
		if($$(text_box).value == txt || $$(text_box).value == '') $$(text_box).value = txt;
	}
});

if(document.observe) {
	document.observe('dom:loaded', function(e) {
		//  original values -- TL
		//$('text_default').observe('click', function(event){resizeText('100%');event.stop();});
		//$('text_large1').observe('click', function(event){resizeText('108%');event.stop();});
		//$('text_large2').observe('click', function(event){resizeText('116%');event.stop();});

		$('text_default').observe('click', function(event){resizeText('85%');event.stop();});
		$('text_large1').observe('click', function(event){resizeText('95%');event.stop();});
		$('text_large2').observe('click', function(event){resizeText('105%');event.stop();});
		
		// Create DropDowns
		new DropDown("nav_hou", $("nav_hou").down("div"));
		new DropDown("nav_lib", $("nav_lib").down("div"));
		new DropDown("nav_asklib", $("nav_asklib").down("div"));
		new DropDown("nav_myacct", $("nav_myacct").down("div"));
		new DropDown("nav_probrept", $("nav_probrept").down("div"));
		
		$$('ul.tabs').each(function(e){
			new TabSet(e, {stopEvent: 'true'});
		});


		searchBoxes();
		
		if($('open_chat')) {
			$('open_chat').hide();
			$('open_chat_icon').observe('click', function(event){$('help_center').fade();$('open_chat').appear();event.stop();});
			$('cancel_icon').observe('click', function(event){$('open_chat').fade();$('help_center').appear();event.stop();});
		}
	})
}

function searchBoxes() {
	var txt = 'Enter Keywords';
	$$('input.search_box').each(function(e){
		e.observe('focus', function(event){if(e.value == txt) e.value = '';});
		e.observe('blur', function(event){if(e.value == txt || e.value == '') e.value = txt;});
	});
}

function resizeText(val) {
	$('wrapper').style.fontSize = val;
}

/**
 * Handy for making a link work as a toggling device for a selector;
 * 		ie: new Toggler('some_link', 'ul.pages');
 * 		The above would make an <a> tag with an id of #some_link 
 * 		show and hide all the ul.pages when it is clicked.
 */
var Toggler = Class.create();
Toggler.Version = '0.1';
Toggler.prototype = {
	initialize: function(element, selector, options) {
		
		this.options = $H({
			altText: 'Hide Advanced Options'
		}).merge(options);
		
		this.element 	= $(element);
		this.togglers = $$(selector);
		this.element.initialText = this.element.innerHTML;
		this.element.altText = this.options.altText;
		this.hide();
		
		this.element.observe('click', this.toggle.bindAsEventListener(this));
	},
	
	toggle: function(event) {
		(this.status == 'showing') ? this.hide() : this.show();
		Event.stop(event);
	},
	
	show: function() {
		this.status = 'showing';
		this.swapLinkText();
		this.togglers.each(function(el) {
			el.show();
		});
	},
	
	hide: function() {
		this.status = 'hiding';
		this.swapLinkText();
		this.togglers.each(function(el) {
			el.hide();
		});
	},
	
	swapLinkText: function() {
		this.element.innerHTML = (this.status == 'showing') ? this.element.altText : this.element.initialText;
	}
}

/************************************************************************
 * Helpful for managing cookies with javascript
 * Objectified by John Nunemaker, original functions by ppk
 */
var Cookie = {
	set: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	get: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	remove: function(name) {
		Cookie.set(name,"",-1);
	}
}

/************************************************************************
 * Creates a toggleable section whose display value is stored in a cookie
 * Requires the cookie.js class in same repository
 * By John Nunemaker
 */
var ToggledSection = Class.create();
ToggledSection.Version = '0.2.1';
ToggledSection.prototype = {
	initialize: function(section, options) {
		// the options you can change
		this.options = $H({
			imgOpen: '/images/icons/toggle_minus.gif',
			imgClosed: '/images/icons/toggle_plus.gif'
		}).merge(options);
		
		this.section_id 			= section;
		var section_link_id 	= this.section_id + '_link';
		var section_image_id 	= this.section_id + '_image';		
		this.section 					= $(this.section_id);
		this.link 						= $(section_link_id);
		this.image						= $(section_image_id);
		
		// set cookie if it doesn't exist
		if (Cookie.get(this.section_id) == null) { Cookie.set(this.section_id, 'hide'); }
		
		// if cookie says to hide
		(Cookie.get(this.section_id) == 'hide') ? this.hideSection() : this.showSection();
		
		this.link.observe('click', this.toggleSection.bindAsEventListener(this));
	},
	
	toggleSection: function(event) {
		(Cookie.get(this.section_id) == 'hide') ? this.showSection() : this.hideSection();
		Event.stop(event);
	},
	
	showSection: function() {
		this.section.show();
		Cookie.set(this.section_id, 'show');
		if (this.image) { this.image.src = this.options.imgOpen; }
	},
	
	hideSection: function() {
		this.section.hide();
		Cookie.set(this.section_id, 'hide');
		if (this.image) { this.image.src = this.options.imgClosed; }
	}
}

/**
* Unobtrusive TabSet
* Author: John Nunemaker
* Website: Addicted To New (http://addictedtonew.com)
* Requires: Prototype 1.6+
*
* - Checks url to see if it should activate based on hash (ie: http://somedomain.com/#team)
* - Ff no hash in url or hash in url is for a different tab set, it activates the first tab
* in the set
* - Tabs can be bookmarked because of the url checking
* - Sets a class of 'active' on the active tab (li), can be changed with activeClass option
*
* Usage:
* <ul id="some_id">
* <li><a href="#about">About</a></li>
* <li><a href="#team">Team</a></li>
* <li><a href="#contact">Contact</a></li>
* </ul>
*
* <div id="about">Some text...</div>
* <div id="team">Some text...</div>
* <div id="contact">Some text...</div>
*
* <script type="text/javascript">
* new TabSet('some_id');
* // new TabSet('some_id', {activeClass: 'current'}); // would change the active class to current
*    // new TabSet('some_id', {stopEvent:true}); // stops the click event when tab link is clicked
* </script>
*/
var TabSet = Class.create({
  /* element is the id of the tab set ul */
    initialize: function(element, options) {
    this.opts = options;
    this.options = $H({ activeClass: 'active', stopEvent: true }).merge(options);
    this.element = $(element);
    this.tabs    = this.element.select('a');
    this.areas    = $A([]);
    this.tabs.each(function(tab) {
    	var area = this.areaForTab(tab);
      if (area) { this.areas.push(area); }
    }.bind(this));
    this.areas.invoke('hide');
    this.addObservers();
    this.setInitialTab();
 },
 
  addObservers: function() { 
				this.tabs.each(
					function(tab) { 
  						var diffurl = tab.readAttribute('href').split('#')[0];
						if(!diffurl){
							tab.observe('click', this.onClick.bindAsEventListener(this));
						} 
			}.bind(this));
  },
 
  setInitialTab: function() {
    var initialTab = this.tabs.first();
    if (window.location.hash) {
      this.tabs.detect(function(tab) {
        if (this.parseHash(tab.href) == this.parseHash(window.location.hash)) {
          initialTab = tab;
        }
      }.bind(this));
    }
    this.showTab(initialTab);
  },
 
  showTab: function(tab) {
    var tab = $(tab);
    this.areas.invoke('hide');
    this.setActiveListElement(tab);
    this.areaForTab(tab).show();
    var tab_id_text = tab.id;
    var tab_sans_s_regex = /(.+)?s/;
    tab_sans_s_regex.exec(tab_id_text);
    var match_tab = RegExp.$1;
    var tab_element = document.getElementById(match_tab);
    tab_element.focus();
  },
 
  setActiveListElement: function(tab) {
    this.removeActiveClass();
    $(tab).up().addClassName(this.options.get('activeClass'));
  },
 
  removeActiveClass: function() {
    this.tabs.each(function(tab) {
      tab.up().removeClassName(this.options.get('activeClass'));
    }.bind(this));
  },
 
  isTab: function(el) {
    return (this.tabs.include($(el))) ? true : false;
  },
 
  isArea: function(el) {
    return (this.areas.include($(el))) ? true : false;
  },
 
  onClick: function(event) {
    if (this.options.get('stopEvent')) { event.stop(); }
    var el = Event.findElement(event, 'a');
    this.showTab(el);
  },
  
  areaForTab: function(tab) {
   var tab = $(tab);
   var area_id = tab.readAttribute('href').split('#')[1];
   return $(area_id);
  },
  
  parseHash: function(href) {
    var str = href.split('#').last();
   return str;
  }
});
