/* 1. setup minibasket */
var miniBasket = {
	// show basket:
	show: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
		} else {
			// enter basket
			this.basketSlide.slideIn();
		}
	},
	// start "hide" timer
	hide: function() {
		this.closeTimer = setTimeout('miniBasket.hideTrue()', 200);
	},
	// start "hide" timer for slow hiding
	hideSlow: function() {
		this.closeTimer = setTimeout('miniBasket.hideTrue()', 6000);
	},
	// remove basket from view
	hideTrue: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
			// fade out 
			this.basketSlide.slideOut();
		}
	},
	// intialise
	init: function() {
		// reset positioning by adding class to container
		$('miniBasketContainer').addClass('jsenabled');
		// intiate the Slide
		this.basketSlide = new Fx.Slide('miniBasket', { 'wait': false });
		this.basketSlide.hide();
		// affix events to link
		$('basketLink').addEvents({
			'mouseenter': function() {
				miniBasket.show();
			},
			'focus': function() {
				miniBasket.show();
			},
			'mouseleave': function() {
				miniBasket.hide();
			},
			'blur': function() {
				miniBasket.hide();
			}
		});
		// affix events to minibasket div
		$('miniBasket').addEvents({
			'mouseenter': function() {
				miniBasket.show();
			},
			'focus': function() {
				miniBasket.show();
			},
			'mouseleave': function() {
				miniBasket.hide();
			},
			'blur': function() {
				miniBasket.hide();
			}
		});
		  
					  
		
		if($$('#viewBasket a')[0]!=null){
		// affix rollover to viewBasket btn (browser compatibility - IE6)
		$$('#viewBasket a')[0].addEvents({
			'mouseenter': function() {
				$('viewBasket').addClass('over');
			},
			'mouseleave': function() {
				$('viewBasket').removeClass('over');
			}
		});
		}
		// blank timer var attached
		this.closeTimer = "";
	}
}


/* 2. setup Primary Navigation */

/* Mens Menu */
var menMenuItems = {
	// show  menu:
	show: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
		} else {
			// enter menu
			$$('#menLink').addClass('menuHover');
			$$('#menMenuItemsContainer').addClass('menuShow');
		}
	},
	// start "hide" timer
	hide: function() {
		this.closeTimer = setTimeout('menMenuItems.hideTrue()', 0);
	},
	// remove menu from view
	hideTrue: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
			// fade out 
			$$('#menLink').removeClass('menuHover');
			$$('#menMenuItemsContainer').removeClass('menuShow');
		}
	},
	// intialise
	init: function() {
		// reset positioning by adding class to container
		$('menMenuItemsContainer').addClass('jsenabled');
		// affix events to link
		$('menLink').addEvents({
			'mouseenter': function() {
				menMenuItems.show();
			},
			'focus': function() {
				menMenuItems.show();
			},
			'mouseleave': function() {
				menMenuItems.hide();
			},
			'blur': function() {
				menMenuItems.hide();
			}
		});
		// affix events to div
		$('menMenuItems').addEvents({
			'mouseenter': function() {
				menMenuItems.show();
			},
			'focus': function() {
				menMenuItems.show();
			},
			'mouseleave': function() {
				menMenuItems.hide();
			},
			'blur': function() {
				menMenuItems.hide();
			}
		});
		
		// blank timer var attached
		this.closeTimer = "";
	}
}



/* Womens Menu */
var womenMenuItems = {
	// show  menu:
	show: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
		} else {
			// enter menu
			$$('#womenLink').addClass('menuHover');
			$$('#womenMenuItemsContainer').addClass('menuShow');
		}
	},
	// start "hide" timer
	hide: function() {
		this.closeTimer = setTimeout('womenMenuItems.hideTrue()', 0);
	},
	// remove menu from view
	hideTrue: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
			// fade out 
			$$('#womenLink').removeClass('menuHover');
			$$('#womenMenuItemsContainer').removeClass('menuShow');
		}
	},
	// intialise
	init: function() {
		// reset positioning by adding class to container
		$('womenMenuItemsContainer').addClass('jsenabled');
		// affix events to link
		$('womenLink').addEvents({
			'mouseenter': function() {
				womenMenuItems.show();
			},
			'focus': function() {
				womenMenuItems.show();
			},
			'mouseleave': function() {
				womenMenuItems.hide();
			},
			'blur': function() {
				womenMenuItems.hide();
			}
		});
		// affix events to div
		$('womenMenuItems').addEvents({
			'mouseenter': function() {
				womenMenuItems.show();
			},
			'focus': function() {
				womenMenuItems.show();
			},
			'mouseleave': function() {
				womenMenuItems.hide();
			},
			'blur': function() {
				womenMenuItems.hide();
			}
		});

		// blank timer var attached
		this.closeTimer = "";
	}
}



/* Junior Menu */
var juniorMenuItems = {
	// show  menu:
	show: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
		} else {
			// enter menu
			$$('#juniorLink').addClass('menuHover');
			$$('#juniorMenuItemsContainer').addClass('menuShow');
		}
	},
	// start "hide" timer
	hide: function() {
		this.closeTimer = setTimeout('juniorMenuItems.hideTrue()', 0);
	},
	// remove menu from view
	hideTrue: function() {
		if (this.closeTimer != "") {
			// killtimer
			clearTimeout(this.closeTimer);
			this.closeTimer = "";
			// fade out 
			$$('#juniorLink').removeClass('menuHover');
			$$('#juniorMenuItemsContainer').removeClass('menuShow');
		}
	},
	// intialise
	init: function() {
		// reset positioning by adding class to container
		$('juniorMenuItemsContainer').addClass('jsenabled');
		// affix events to link
		$('juniorLink').addEvents({
			'mouseenter': function() {
				juniorMenuItems.show();			
			},
			'focus': function() {
				juniorMenuItems.show();
			},
			'mouseleave': function() {
				juniorMenuItems.hide();
			},
			'blur': function() {
				juniorMenuItems.hide();
			}
		});
		// affix events to div
		$('juniorMenuItems').addEvents({
			'mouseenter': function() {
				juniorMenuItems.show();
			},
			'focus': function() {
				juniorMenuItems.show();
			},
			'mouseleave': function() {
				juniorMenuItems.hide();
			},
			'blur': function() {
				juniorMenuItems.hide();
			}
		});
// blank timer var attached
		this.closeTimer = "";
	}
}

/* 5. setup product detail page */
var detailPanels = {
	/*
	NOTE:
	the toggle function requires a link to a named div - ie href="#prodDetailsTab"
	the full href is then used as an id css selector via $$(), which drives show/hide, class="selected" etc
	*/
	toggle: function(targEl) {
		this.panelArr.each(function(ref) {
			if (ref == targEl) {
				$$(ref).setStyle('display', 'block');
				$$('#prodNav a[href=' + ref + ']').addClass('selected');
			} else {
				$$(ref).setStyle('display', 'none');
				$$('#prodNav a[href=' + ref + ']').removeClass('selected');
			}
		});
		
	},
	// initialise
	init: function() {
		// create an array for toggling
		this.panelArr = new Array();
		$$('#prodNav li a').each(function(item, index) {
			var ref = item.getProperty('href');
			// append the href (ie "#prodDetailsTab") to the panel array
			detailPanels.panelArr[index] = ref;
			item.addEvent('click', function(e) {
				var e = new Event(e);
				detailPanels.toggle(ref);
				//return false;
				e.stop();
			});
			/* ALSO:
			search the product detail (uses css attribute selector) area for matching links not in the
			tabbed nav (ie a "more detail" link in the summary) via css attribute selector
			*/
			var rel = $$('#productDetailTabsContainer a[href=' + ref + ']');	// matching example would be: href="#prodDetailsTab"
			if (rel.length > 0) {
				rel.each(function(item, index) {
					item.addEvent('click', function(e) {
						var e = new Event(e);
						detailPanels.toggle(ref);
						//return false;
						e.stop();
					});
				});
			}
		});
		// loop through the array that has been assembled, and hide named panels - skipping the first one, which is default selected (ie #prodDetailsTab)
		for (var i = 1; i < this.panelArr.length; i++) {
			$$(this.panelArr[i]).setStyle('display', 'none');
		}
	}
}

/* 5. setup product detail page */
var crossSellsPanels = {
	/*
	NOTE:
	the toggle function requires a link to a named div - ie href="#prodDetailsTab"
	the full href is then used as an id css selector via $$(), which drives show/hide, class="selected" etc
	*/
	toggle: function(targEl) {
		this.panelArr.each(function(ref) {
			if (ref == targEl) {
				$$(ref).setStyle('display', 'block');
				$$('#crossSellsNav a[href=' + ref + ']').addClass('selected');
			} else {
				$$(ref).setStyle('display', 'none');
				$$('#crossSellsNav a[href=' + ref + ']').removeClass('selected');
			}
		});
		
	},
	// initialise
	init: function() {
		// create an array for toggling
		this.panelArr = new Array();
		$$('#crossSellsNav li a').each(function(item, index) {
			var ref = item.getProperty('href');
			// append the href (ie "#prodDetailsTab") to the panel array
			crossSellsPanels.panelArr[index] = ref;
			item.addEvent('click', function(e) {
				var e = new Event(e);
				crossSellsPanels.toggle(ref);
				//return false;
				e.stop();
			});
			/* ALSO:
			search the product detail (uses css attribute selector) area for matching links not in the
			tabbed nav (ie a "more detail" link in the summary) via css attribute selector
			*/
			var rel = $$('#productCrossSellsTabsContainer a[href=' + ref + ']');	// matching example would be: href="#prodCrossSellsTab"
			if (rel.length > 0) {
				rel.each(function(item, index) {
					item.addEvent('click', function(e) {
						var e = new Event(e);
						crossSellsPanels.toggle(ref);
						
						//return false;
						e.stop();
					});
				});
			}
		});
		// loop through the array that has been assembled, and hide named panels - skipping the first one, which is default selected (ie #prodDetailsTab)
		for (var i = 1; i < this.panelArr.length; i++) {
			$$(this.panelArr[i]).setStyle('display', 'none');
		}
	}
}




/* 6. product attributes form */
var addForm = {
	// display "selected" state for a radio - search for selected item
	showSelected: function(pass) {
		$$("input[name=" + pass + "]").each(function(item, index) {
			var parEl = item.getParent();
			if (item.checked == true) {
				parEl.addClass('selected');
			} else {
				parEl.removeClass('selected');
			}
		});
	},
	init: function() {
		// set up classnames
		$('productAttributes').addClass('jsenabled');
		// affix focus and blur events to radio items (assistive)
		$$('#productAttributes label input').each(function(item, index) {
			item.passed = item.getAttribute('name');
			item.parEl = item.getParent();
			item.addEvents({
				'focus': function() {
					item.parEl.addClass('focus');
				},
				'blur': function() {
					item.parEl.removeClass('focus');
				},
				'click': function() {
					addForm.showSelected(item.passed);
				}
			});
		});
	}
}


/* DomReady Event makes initial function calls */
window.addEvent('domready', function() { 
	// 1. initialise minibasket
	if ($('miniBasket')) {
			miniBasket.init();
	}
	// 2. initialise menMenuItems
	if ($('menMenuItems')) {
		menMenuItems.init();
	}
	// 3. initialise womenMenuItems
	if ($('womenMenuItems')) {
		womenMenuItems.init();
	}
	// 4. initialise juniorMenuItems
	if ($('juniorMenuItems')) {
		juniorMenuItems.init();
	}
	// 5. initialise product detail panel if present
	if ($('productDetailTabsContainer')) {
		detailPanels.init();
		addForm.init();
	}
	// 5. initialise product detail panel if present
	if ($('productCrossSellsTabsContainer')) {
		crossSellsPanels.init();
		addForm.init();
		// this is the logic for selecting appropriate tabs depending upon the cookie set Jira -416
		var results = document.cookie.match ( '(^|;) ? tabNumber=([^;]*)(;|$)' );
		if(results)
		{
		if ( unescape ( results[2] ) == '2')
			crossSellsPanels.toggle('#prodCrossSellsContent2');
		}
	}

	if ($('showBag')) {
		miniBasket.show();
		miniBasket.hideSlow();
	}
	
	
});

