/*
		Site: 			Infant Toys
		File: 			Master Behaviors JavaScript File
		Author:		Eric Shepherd
		Date:			March 2006
		Requires:	/pages/v4/script/dhtml_lib.js
							/pages/v4/script/ProductHelper.js
							/pages/v4/script/mootools.js
*/


	// onload function

EventHelper.AddEvent(window, 'load', fp_site_infanttoys_start, false);

var fp_site_infanttoys_ph = new ProductHelper();

function fp_site_infanttoys_start() {	
	fp_site_infanttoys_ph.init(); // calls the core prototype methods
	fp_site_infanttoys_ph.swapInit();
	fp_site_infanttoys_ph.awardInit();
	fp_site_infanttoys_ph.addEffects();
}





/*
		Extensions to the ProductHelper class 
*/


ProductHelper.prototype.addEffects = function()
// add moo.fx
{
	//height change on instruction manual box
	var theInstructionToggle = $('instruction-intro');
	
	if (theInstructionToggle) {
		
		var toggle = false;
		var el = $('instructions').getElementsByTagName('dl')[0];
		$(el).setStyle('overflow', 'hidden');

		var elHeight = el.scrollHeight;

		var instructionFx = new Fx.Style(el, 'height', { duration : 400 });
		instructionFx.start(0);
		
		$(theInstructionToggle).addEvent('click', function(e) {
			if (toggle == false) {
				instructionFx.start(0, elHeight);
				toggle = true;
			} else {
				instructionFx.start(elHeight, 0);	
				toggle = false;
			}
		});
		
	}
	//accordion effect
	if (DomHelper.GetElementsByClassName) {
		var stretchers = DomHelper.GetElementsByClassName('expand');
		var toggles = DomHelper.GetElementsByClassName('toggle');
	
		var myAccordion = new Fx.Accordion(toggles, stretchers, {duration: 600, 'start' : 'all-closed'} );

	}
	
	// physical milestones
	/* dropped by fp before launch - keep code for future reference
	if ($('physical-milestones')) {
		$$('li.physical-milestone').each( function(el) { 
			el.addEvent('mouseenter', function(e) { 
				var ul = this.getElementsByTagName('ul')[0];
				var a = this.getElementsByTagName('a')[0];
				$(ul).effect('opacity', { duration : 200 }).custom(0,1);
				$(a).addClass('on');
			}); 
			el.addEvent('mouseleave', function(e) {
				var ul = this.getElementsByTagName('ul')[0];
				var a = this.getElementsByTagName('a')[0];
				$(ul).effect('opacity', { duration : 200 }).custom(1,0);
				$(a).removeClass('on');
			});
		});
	}
	*/
	
}

var ITBehaviors = {}

ITBehaviors.find = function(element, what) {
	element = $(element)[what];
	while (element.nodeType != 1) element = element[what];
	return element;
}



ProductHelper.prototype.toggleInstructionFx = function() 
// for some reason, a separate function is needed to run the effect function
{
	document.instructionFx.toggle();
}

ProductHelper.prototype.swapActive = function(e)
// this switches the active Browse All link so it appears highlighted when selected
{
	var browseLinks = DomHelper.GetElementsByClassName('browse-link');
	for (i=0; i<browseLinks.length; i++) { // first we kill all the on classes on all the links
		browseLinks[i].className = browseLinks[i].className.replace(' on', '');
	}
	this.className += ' on'; // then we add the class only for the selected link
}



/*
		Init Functions
*/



ProductHelper.prototype.swapInit = function() 
// attaches the event to run the function which styles the active Browse All link
{
	if (DomHelper.GetElementsByClassName) {
		var theBrowseLinks = DomHelper.GetElementsByClassName('browse-link');
		for (i=0; i<theBrowseLinks.length; i++) {
			EventHelper.AddEvent(theBrowseLinks[i], 'click', this.swapActive, false);
		}
	}
}



ProductHelper.prototype.awardInit = function() 
// add listener for popping up the award window
{
	theDl = document.getElementById('product-meta');
	if (theDl) {
		theDds = theDl.getElementsByTagName('dd');
		for (i=0; i<theDds.length; i++) {
			if (theDds[i].className.match(/award/)) {
				var thisReference = this;
				EventHelper.AddEvent(theDds[i].getElementsByTagName('a')[0], 'click', popupPtr = function(e) {thisReference.popup(this, 'award_winner', 362, 180, e); }, false);
				if (SupportTest.isSafari) theDds.onclick = EventHelper.CancelClickSafari();
			}
		}
	}
}



