window.onload = function () {
	initAll();
}
function initAll() {
	initToggleLists('faq-list');
	initToggleLists('faq-container');
	initToggleLists('product-information-items', true);
	initCaroussels();
	initLayeredLinks();
	initPopup();
	makePopupLinks();
	if (YAHOO.util.Dom.get('banner-box')) new SlideShow('banner-box', 'active');
	if (YAHOO.util.Dom.get('imagecaroussel')) new SlideShow('imagecaroussel', 'activeimage');
	if (YAHOO.util.Dom.get('product-detail-car')) new ProductCaroussel();
	if (YAHOO.util.Dom.get('site-wide-submenu')) new SitewideSubmenu();
	if (YAHOO.util.Dom.get('nutrilon_subnav')) new NutrilonNav();
}
function initToggleLists(container, oneOpen) {
	var entries = YAHOO.util.Dom.getElementsByClassName(container, 'ul');
	if (entries != null){
	    var len = entries.length;
	    for (var i = 0; i < len; i++) {
		    new ToggleList(entries[i], oneOpen);
	    }
	}
}
function initCaroussels() {
	var entries = YAHOO.util.Dom.getElementsByClassName('js-caroussel', 'div');
	if (entries != null ){
	    var len = entries.length;
	    for (var i = 0; i < len; i++) {
		    new JSCaroussel(entries[i]);
	    }
	}
}
function initLayeredLinks() {
	var entries = YAHOO.util.Dom.getElementsByClassName('layered', 'a');
	if (entries != null ){
	    var len = entries.length;
	    for (var i = 0; i < len; i++) {
		    new LayeredLink(entries[i]);
	    }
	}
}
function initPopup() {
    var popup = new PopupLightbox();
    popup.openLayer();
}
/* -----------------------------
	ToggleList
   ----------------------------- */
function ToggleList(container, oneOpen) {
	this.oneOpen = oneOpen ? true : false;
	this.container = container;
	var o = YAHOO.util.Dom.getElementsByClassName('open', 'li', this.container);
	if (o[0]) this.currentOpen = o[0];
	entries = this.container.getElementsByTagName('a');
	var len = entries.length;
	for (var i = 0; i < len; i++) {
		YAHOO.util.Event.addListener(entries[i], "click", this.toggle, [this, entries[i]], true);
	}
}
ToggleList.prototype.toggle = function (e, args) {
	var base = args[0];
	var link = args[1];
	
	if (!link.parentNode || !link.parentNode.parentNode) return;
	if (base.oneOpen) base.closeCurrent();
	if (link.parentNode.parentNode != base.container) {
		return;
	} else {
		YAHOO.util.Event.preventDefault(e);
		if (YAHOO.util.Dom.hasClass(link.parentNode, 'open')) {
			YAHOO.util.Dom.removeClass(link.parentNode, 'open');
		} else {
			YAHOO.util.Dom.addClass(link.parentNode, 'open');
			base.currentOpen = link.parentNode;
		}
	}
}
ToggleList.prototype.closeCurrent = function () {
	if (this.currentOpen) YAHOO.util.Dom.removeClass(this.currentOpen, 'open');
}

/* -----------------------------
	JSCaroussel
   ----------------------------- */
function JSCaroussel (container) {
	this.pages = YAHOO.util.Dom.getElementsByClassName('item', '*', container);
	this.nrOfPages = this.pages.length;
	var p = YAHOO.util.Dom.getElementsByClassName('paging', 'ul', container);
	if (p[0]) {
		this.paging = p[0];
		this.pagingLinks = this.paging.getElementsByTagName('a');
	}
	this.hasPaging = this.pagingLinks.length > 2 ? true : false;
	this.prevPage = this.pagingLinks[0];
	this.nextPage = this.pagingLinks[this.pagingLinks.length - 1];
	YAHOO.util.Event.addListener(this.prevPage, "click", this.prev, this, true);
	YAHOO.util.Event.addListener(this.nextPage, "click", this.next, this, true);
	
	if (this.hasPaging) {
		var len = this.pagingLinks.length;
		for (var i = 1; i < len - 1; i++) {
			YAHOO.util.Event.addListener(this.pagingLinks[i], "click", this.handleSelectPage, [this, i], true);
		}
	}
	this.currentIndex = this.getCurrentActive();
}
JSCaroussel.prototype.getCurrentActive = function () {
	for (var i = 0; i < this.nrOfPages; i++) {
		if (YAHOO.util.Dom.hasClass(this.pages[i], 'item-open')) return i;
	}
	return 0;
}
JSCaroussel.prototype.prev = function (e) {
	YAHOO.util.Event.preventDefault(e);
	this.deSelectPage(this.currentIndex);
	if (this.currentIndex > 0) {
		this.currentIndex--;
	} else {
		this.currentIndex = this.nrOfPages - 1;
	}
	this.selectPage(this.currentIndex);
}
JSCaroussel.prototype.next = function (e) {
	YAHOO.util.Event.preventDefault(e);
	this.deSelectPage(this.currentIndex);
	if (this.currentIndex < this.nrOfPages - 1) {
		this.currentIndex++;
	} else {
		this.currentIndex = 0;
	}
	this.selectPage(this.currentIndex);
}
JSCaroussel.prototype.handleSelectPage = function (e, args) {
	YAHOO.util.Event.preventDefault(e);

	var base = args[0];
	var index = args[1];
	
	base.deSelectPage(base.currentIndex);
	base.currentIndex = index - 1;
	base.selectPage(base.currentIndex);
}
JSCaroussel.prototype.selectPage = function (i) {
	YAHOO.util.Dom.addClass(this.pages[i], 'item-open');
	if (this.hasPaging) YAHOO.util.Dom.addClass(this.pagingLinks[i+1].parentNode, 'active');
}
JSCaroussel.prototype.deSelectPage = function (i) {
	YAHOO.util.Dom.removeClass(this.pages[i], 'item-open');
	if (this.hasPaging) YAHOO.util.Dom.removeClass(this.pagingLinks[i+1].parentNode, 'active');
}

/* -----------------------------
	ProductCaroussel
   ----------------------------- */
function ProductCaroussel() {
	this.container = YAHOO.util.Dom.get('products');
	this.products = YAHOO.util.Dom.getElementsByClassName('product-detail', 'div', this.container);
	this.nrOfProducts = this.products.length;
	if ( this.products != null && this.nrOfProducts > 0 ) {
	    this.PROD_MARGIN = 23;
	    this.PROD_IN_VIEW = 5;
	    this.PROD_WIDTH = this.products[0].offsetWidth + this.PROD_MARGIN;
	    this.container.style.width = (this.nrOfProducts * this.PROD_WIDTH) + 'px';
	    this.currentPos = 0;
	    this.prevBut = YAHOO.util.Dom.get('product-prev');
	    this.nextBut = YAHOO.util.Dom.get('product-next');
	    YAHOO.util.Event.addListener(this.prevBut, "click", this.prev, this, true);
	    YAHOO.util.Event.addListener(this.nextBut, "click", this.next, this, true);
	    this.prevAnim = this.nextAnim = new Object();
	    this.updateNav();
	}
}
ProductCaroussel.prototype.prev = function (e) {
	if(this.prevAnim.animating) return;
	var steps = this.currentPos > this.PROD_IN_VIEW ? this.PROD_IN_VIEW : this.currentPos;
	if (steps > 0) {
		var attributes = {
			left: { by: steps *  this.PROD_WIDTH}
		}
		this.prevAnim = new YAHOO.util.Motion(this.container, attributes,1, YAHOO.util.Easing.easeOut);
		this.prevAnim.animate();
		this.prevAnim.animating = true;
		this.prevAnim.onComplete.subscribe(function () {this.animating = false});
		this.currentPos -= steps;
	}
	this.updateNav();
}
ProductCaroussel.prototype.next = function (e) {
	if(this.nextAnim.animating) return;
	var steps = this.nrOfProducts - (this.currentPos + this.PROD_IN_VIEW) > this.PROD_IN_VIEW ? this.PROD_IN_VIEW : this.nrOfProducts - (this.currentPos + this.PROD_IN_VIEW);
	if (steps > 0) {
		var attributes = {
			left: { by: -1 * steps *  this.PROD_WIDTH}
		}
		this.nextAnim = new YAHOO.util.Motion(this.container, attributes,1, YAHOO.util.Easing.easeOut);
		this.nextAnim.animate();
		this.nextAnim.animating = true;
		this.nextAnim.onComplete.subscribe(function () {this.animating = false});
		this.currentPos += steps;
	}
	this.updateNav();
}
ProductCaroussel.prototype.updateNav = function () {
	if (this.currentPos <= 0) {
		YAHOO.util.Dom.addClass(this.prevBut, 'prev-disabled');
	} else {
		YAHOO.util.Dom.removeClass(this.prevBut, 'prev-disabled');
	}
	if (this.currentPos >= this.nrOfProducts - this.PROD_IN_VIEW) {
		YAHOO.util.Dom.addClass(this.nextBut, 'next-disabled');
	} else {
		YAHOO.util.Dom.removeClass(this.nextBut, 'next-disabled');
	}
}

/* -----------------------------
	SlideShow
   ----------------------------- */
function SlideShow(container, activeID) {
	this.active = YAHOO.util.Dom.get(activeID);
	this.entries = YAHOO.util.Dom.getChildren(container);
	this.current = 0;
	var scope = this;
	setInterval(function(){scope.showNext()}, 5000);
}
SlideShow.prototype.showNext = function () {
	var attributes = {
		opacity: { to: 0 }
	}
	var anim = new YAHOO.util.Motion(this.active, attributes,.8, YAHOO.util.Easing.easeOut);
	anim.animate();

	this.current++;
	this.current = this.current == this.entries.length ? 0 : this.current;
	this.active = this.entries[this.current];
	var attributes = {
		opacity: { to: 1 }
	}
	anim = new YAHOO.util.Motion(this.active, attributes,.8, YAHOO.util.Easing.easeOut);
	anim.animate();
}

function GetLayerHeight( contentheight ) {
    var windowSizeHeight = 0;
	if (typeof window.innerHeight != 'undefined')
	{
	     windowSizeHeight = window.innerHeight;
	}
	else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.scrollHeight !=
	     'undefined' && document.documentElement.scrollHeight != 0)
	{
	      windowSizeHeight = document.documentElement.scrollHeight;
	}
	else
	{
	      windowSizeHeight = document.getElementsByTagName('body')[0].clientHeight;
	}
	return Math.max( contentheight, windowSizeHeight );
}

/* -----------------------------
	LayeredLink
   ----------------------------- */
function LayeredLink(lnk) {
	this.link = lnk;
	this.lightbox = YAHOO.util.Dom.get('layer-container');
	this.positioner = YAHOO.util.Dom.get('layer-positioner');
	this.content = YAHOO.util.Dom.get('layer-content');
	this.closeBut = YAHOO.util.Dom.get('close-layer');
	if (!this.lightbox || !this.positioner || !this.content) return;
	YAHOO.util.Event.addListener(this.link, "click", this.openLayer, this, true);
	YAHOO.util.Event.addListener(this.closeBut, "click", this.closeLayer, this, true);
}
LayeredLink.prototype.openLayer = function (e) {
	YAHOO.util.Event.preventDefault(e);
	this.lightbox.style.display = 'block';
	this.positioner.style.display = 'block';
	scroll(0, 0);
	var callback = { 
		success: this.fillData, 
		scope: this
	};
	YAHOO.util.Connect.asyncRequest('GET', this.link.href + '?layer=true', callback, null);
}
LayeredLink.prototype.closeLayer = function (e) {
	YAHOO.util.Event.preventDefault(e);
	this.lightbox.style.display = 'none';
	this.positioner.style.display = 'none';
}
LayeredLink.prototype.fillData = function (o) {
	this.content.innerHTML = o.responseText;
	if ( this.content.offsetHeight ){
	    this.lightbox.style.height = GetLayerHeight(this.content.offsetHeight);
	} else {
	    this.lightbox.style.height = GetLayerHeight(0);
	}
}
