// JavaScript Document

jq(document).ready(function(){
		jq('#ProductsLink').bind('mouseenter mouseleave', function() {
		jq("#ProductsLinkTag").toggleClass('selected');
  	jq("#ProductsNavList").animate({
			height: 'toggle'
		}, {
			duration: 300, 
			specialEasing: {
				width: 'linear',
				height: 'easeOutBounce'
			}
		});
	});
	/*
	jq('#ProductsList').bind('mouseleave', function() {
  	jq(this).toggle('slow');
	});
	*/
});

function switchClass(element_id, new_class){
	var element = document.getElementById(element_id);
	element.className = new_class;
}

function addRollOver(element, element_class, element_roll_class){
	//var element = document.getElementById(element.id);
	obj = element;
	element.onfocus = function() {
		switchClass(element, element);
	}
	element.onmouseover = function() {
		switchClass(element, element);
	}
	element.onblur = function() {
		switchClass(element, element);
	}
	element.onmouseout = function() {
		switchClass(element, element);
	}
}

function validateRegForm(){
	
}

function openFootnote(footnote_num){
	new Effect.ScrollTo('footanchor', {duration: 0.7});
	var footnotes = $$('.footnote');
	for(i=0; i<footnotes.length; i++){
		new Effect.BlindUp(footnotes[i], { queue: 'end', duration: 0.2 });
		//footnotes[i].style.display = "none";
	}
	var footnote = $('footnote-'+footnote_num);
	//new Effect.BlindDown($('Footnotes'), { queue: 'end' });
	new Effect.BlindDown(footnote, { queue: 'end', duration: 0.4 });
	//new Effect.Pulsate('footnote-'+footnote_num, {pulses:2, duration:0.5, queue: 'end'});
	new Effect.Morph('footnote-'+footnote_num, {style: 'color:#BE9014', queue: 'end'});
		
}

function openQuoteBox(){
	new Effect.BlindDown('QuoteBox', {duration: 0.4});
}

function mran(ma,mi)
   {return(Math.round(Math.random()*(ma-mi))+mi)}

	var Slideshow = Class.create({
		 initialize: function(delay, totalElmt, elmtName) {
			this.delay = delay;
			this.totalElmt = totalElmt;
			this.paused = 0;
			this.arrSlideElmt = [totalElmt];
			this.curElmtNum = mran(totalElmt, 1); //randomize first element
		 	//preload all elements into array and preload all images within element
			for (i = 1; i <= totalElmt; i++) {
				this.arrSlideElmt[i] = $(elmtName + i);
				this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this));  //pause on mouseover
				this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this));  //resume on mouseout
				$$(this.arrSlideElmt[i].img).each(function(img) {	
					img = new Image();
				});
			}
			$$('img.arrow').each(function(node) {  //looks for all arrows (pointing right) and handles click event
				node.observe('click', function(s){
					this.arrSlideElmt[this.curElmtNum].setStyle({
					  display: 'none'
					});
					this.checkSlide();
					this.arrSlideElmt[this.curElmtNum].setStyle({
					  display: 'block'
					});
					s.stop();
				}.bind(this));
			}.bind(this));			
		 },
		start: function() {
			//show first element without effect
			this.arrSlideElmt[this.curElmtNum].setStyle({
			  display: 'block'
			});
			this.executor = new PeriodicalExecuter(function() { 
				this.next(); //start slidehow
			}.bind(this), this.delay);		
		},
		
		next: function(){
			if (!this.paused) {
				this.update();
			}
		},
		update: function() {
			new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
				this.checkSlide();
				new Effect.Appear(this.arrSlideElmt[this.curElmtNum]);
			}.bind(this)});
		},
		checkSlide: function() {
			if (this.curElmtNum == this.totalElmt) { this.curElmtNum = 1; }
			else { this.curElmtNum ++; }		
		}
	});

var tabOpen = null;

function openBodyTab(tab, focusTo){
	focusTo = (focusTo!='') ? focusTo : null;
	//alert("tab: "+tab +" - tabOpen: "+tabOpen);
	if(tabOpen==tab){
		var tabclose = $(tabOpen);
		new Effect.BlindUp(tabclose, { queue: 'end', duration: 0.8 });
		tabOpen = null;
	} else {
		if(tabOpen!=null){
			var tabclose = $(tabOpen);
			new Effect.BlindUp(tabclose, { queue: 'end', duration: 0.8 });
		}
		new Effect.BlindDown(tab, { queue: 'end', duration: 0.6 });
		tabOpen = tab;
	}
	if(focusTo) { $(focusTo).focus(); }
	return true;
}

function hideAllButtons(buttons){
	$(buttons).invoke('hide');
}

function openAllTabs(tabs){
	$(tabs).invoke('show');
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function enterCode(){
	jq("#CouponCodeLink").toggle("fade");
	jq("#CouponCodeField").toggle("fade");
}