var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $A = YAHOO.util.Anim;
var $M = YAHOO.util.Motion;
var $Ease = YAHOO.util.Easing;
var $ = $D.get;

YAHOO.util.AnimMgr.fps = 500;

YAHOO.widget.vestEffect = function(el) { this.oEl = $(el); };

YAHOO.widget.vestEffect.prototype.selector = function(box, current, timing) {
	var xPos = $D.getX(this.oEl)+(this.oEl.offsetWidth/2)-11;
	var yPos = $D.getY('sarrow');
	var currZ = parseInt($D.getStyle(current, 'z-index'));
	$D.setStyle(box, 'z-index', currZ+1);
	var move = new $M('sarrow', {points: {to: [xPos, yPos]}}, timing, $Ease.easeBoth);
	move.onStart.subscribe(function() {
		var fadeIn = new $A(box, {opacity: {from: 0, to: 1}}, timing, $Ease.easeBoth);
		fadeIn.onComplete.subscribe(function() {
			$D.setStyle(current, 'opacity', '0');
		});
		fadeIn.animate();
	});
	move.animate();
};

YAHOO.widget.vestEffect.prototype.titlePop = function() {
	var xPos = $D.getX(this.oEl)+35;
	var yPos = $D.getY(this.oEl)+this.oEl.offsetHeight+5;
		
	$D.setXY(this.oEl.mTitle, [xPos, yPos]);
		
	$E.addListener(this.oEl, 'mouseout', function() {
		var hide = new YAHOO.widget.vestEffect(this);
		hide.titleHide();
	});

	var appear = new $A(this.oEl.mTitle, {opacity: {to: 0.95}}, 0.1);
	appear.onComplete.subscribe(function() {
		$E.removeListener(this.oEl, 'mouseover');
	});
	appear.animate();
};

YAHOO.widget.vestEffect.prototype.titleHide = function() {
	var disappear = new $A(this.oEl.mTitle, {opacity: {to: 0}}, 0.1);
	disappear.onStart.subscribe(function() {
		$E.removeListener(this.oEl, 'mouseout');
		$E.addListener(this.oEl, 'mouseover', function() {
			var show = new YAHOO.widget.vestEffect(this);
			show.titlePop();
		});
	});
	setTimeout(function () { disappear.animate(); }, 200);
};


YAHOO.thevest = function() {
	return {
		init : function() {
			var pulses = $D.getElementsByClassName('pulse', 'span', 'news');
		    if(pulses.length>0){
		        for(x=0;x<pulses.length;x++){
		            eff = new YAHOO.widget.Effects.Pulse(pulses[x], { delay: true });		
		            eff.animate();            
		        }		        
		    }
			var titles = $D.getElementsByClassName('term', 'a', 'maincontent');
			if(titles.length > 0) {
				for(i = 0; i < titles.length; i++) {
					var titleBox = document.createElement('span');
					$D.generateId(titleBox, 'titleBox');
					titleBox.className = 'titleBox';
					$('container').appendChild(titleBox);
					titleBox.innerHTML = titles[i].getAttribute('title');
					$D.setStyle(titleBox, 'opacity', '0');

					titles[i].setAttribute('title', '');
					titles[i].mTitle = titleBox;
				}
				$E.on(titles, 'mouseover', this.initTitle);
			}
			
			if($('bibliography')) {
				var bibLinks = $('bibliography').getElementsByTagName('a');
				$E.on(bibLinks, 'click', this.bibliography);
			}
			
			var pdfs = $D.getElementsByClassName('pdf', 'a', 'maincontent');
			$E.on(pdfs, 'click', this.newWindow);
			
			$E.on('search', 'focus', function() {
				if(this.value == 'search terms...') { this.value = ''; }
			});
			
			$E.on('search', 'blur', function() {
				if(this.value == '') { this.value = 'search terms...'; }
			});
		},
		
		selector : function() {
			$D.setStyle('sbreathe', 'display', 'none');
			var sarrow = document.createElement('span');
			sarrow.id = 'sarrow';
			$('selholder').insertBefore(sarrow, $('spatients'));
			
			$D.setStyle('sphysicians', 'opacity', '0');
			$D.setStyle('spayors', 'opacity', '0');
			
			var auto = setInterval(function() { YAHOO.thevest.autoSwitch(selectors); }, 4500);
			var selectors = $('selector').getElementsByTagName('li');
			$E.on(selectors, 'mouseover', this.switchSel);
			$E.on('selector', 'mouseover', function() { clearInterval(auto); });
			$E.on('selector', 'mouseout', function() { 
				auto = setInterval(function() { YAHOO.thevest.autoSwitch(selectors); }, 4500); 
			});
		},
		
		switchSel : function(e) {
			var box = $('s'+this.className);
			var that = this;
			if(!$D.hasClass(box, 'current')) {
				var current = $D.getElementsByClassName('current', 'div', 'selector')[0];
				$D.removeClass(current, 'current');
				$D.addClass(box, 'current');

				var change = new YAHOO.widget.vestEffect(that);
				change.selector(box, current, 1);
			}
		},
		
		autoSwitch : function(sels) {
			var current = $D.getElementsByClassName('current', 'div', 'selector')[0];
			var next = 'physicians';
			if(current.id == 'spatients') { next = 'physicians'; } else 
			if(current.id == 'sphysicians') { next = 'payors'; } else 
			if(current.id == 'spayors') { next = 'patients'; }
			var box = $('s'+next);
			var next = $D.getElementsByClassName(next, 'li', 'selector')[0];
			$D.removeClass(current, 'current');
			$D.addClass(box, 'current');
			var change = new YAHOO.widget.vestEffect(next);
			change.selector(box, current, 1.5);
		},

		initTitle : function() {
			var show = new YAHOO.widget.vestEffect(this);
			show.titlePop();
		},
		
		bibliography : function(e) {
			var bibs = $('maincontent').getElementsByTagName('div');
			var current = this.href.split('#')[1];
			if(current != "all") {
				var hide = function(item) { $D.setStyle(item, 'display', 'none'); };
				$D.batch(bibs, hide);
				$D.setStyle(current, 'display', 'block');
			} else {
				var show = function(item) { $D.setStyle(item, 'display', 'block'); };
				$D.batch(bibs, show);
			}
		},
		
		newWindow : function(e) {
			$E.stopEvent(e);
			window.open(this.href, 'blank');
		}
	}
}();
$E.on(window, 'load', YAHOO.thevest.init, YAHOO.thevest, true);
$E.onAvailable('selector', YAHOO.thevest.selector, YAHOO.thevest, true);