function imgSwap(imgId, imgURL) {
	document.getElementById(imgId).src = imgURL;
}

// Consulting Room
function toggleDiv(DivId, linkId) {
	var elm = document.getElementById(DivId);
	var theLink = document.getElementById(linkId);
	var v = elm.style.display;

	var oldText = document.createTextNode("Read full tip ");
	var newText = document.createTextNode("Close tips ");
	var oldImgSrc = "/images/layout/arrows/gray.png";
	var newImgSrc = "/images/layout/arrows/cross.png";
	// use different images for womens and mens
	if (document.getElementsByTagName("div").className == "womens") {
		oldImgSrc = "/images/layout/arrows/pink.png";
		newImgSrc = "/images/layout/arrows/cross_w.png";
	} else if (document.getElementsByTagName("div").className == "mens") {
		oldImgSrc = "/images/layout/arrows/black.png";
		newImgSrc = "/images/layout/arrows/cross_m.png";
	}
//Toggle the read full tips message to close tips
	if (v == "none" || v == "") {
		elm.style.display = "block";
		theLink.replaceChild(newText, theLink.firstChild);
		theLink.lastChild.src = newImgSrc;
	} else {
		elm.style.display = "none";
		theLink.replaceChild(oldText, theLink.firstChild);
		theLink.lastChild.src = oldImgSrc;
	}
}

/* TAB AND PANEL FUNCTIONALITY */

function hideAllTabContent(tabContentToShow) {	
	for(tb=1;tb<=8;tb++) { 
		if ($('tab'+tb)) $('tab'+tb).hide();
	}
	if($('product_content'))
		if($('product_content').select('.tab_box_sub'))
		$('product_content').select('.tab_box_sub').each(Element.hide);

	tabNum = tabContentToShow+1;
	if($('tab'+tabNum))
		Element.show('tab'+tabNum);

	else if($(tabContentToShow))
		Element.show(tabContentToShow);
}

function showBottomPanel(panelname) {
	
	//Hack - if stylesheet shows display:none for buy now, default to showroom
	if(jQuery('#buy_tab').css('display') == 'none')
		panelname = 'showroom_tab';
	
	jQuery('.bottom_panel').hide();
	jQuery('#'+panelname+'_panel').show();
}

function selectBottomTab(tabname) {
	jQuery('ul#bottom-tabs li').removeClass();
	tabname.addClass('on');	
}

var listOfTabs = new Array('A','B','C','D','E','F','G','H');	

function resetTabs(tabToShow) {
	for (i = 0; i < listOfTabs.length; i++) {
		thisTab = 'tab'+listOfTabs[i];
		if ($(thisTab)) {
			$(thisTab).removeClassName('on');
			$(thisTab).removeClassName('semi');				
		}
	}	
		
	thisTab = 'tab'+listOfTabs[tabToShow];
	
	if($(thisTab))
		$(thisTab).addClassName('on');	
		
	if (tabToShow != 0) {		
		prevTab = 'tab'+listOfTabs[tabToShow-1];
		$(prevTab).addClassName('semi');			
	}	
}

Event.observe(document, "dom:loaded", function() {	
	/* Browser detect */
	if (typeof isIE6orPrevious != "undefined" && isIE6orPrevious) {
		Element.addClassName(document.body, "ie6");
		showBottomPanel('buy_tab');
	}

	if (typeof isIE7 != "undefined" && isIE7) {
		Element.addClassName(document.body, "ie7");
		showBottomPanel('buy_tab');
	}
	
	Element.addClassName(document.body, "javascript");	
	
	// product details page, initially hide all tabs except first
	if(typeof( window[ 'tabWanted' ] ) == "undefined")
		tabWanted = 0;
	
	hideAllTabContent(tabWanted);
	resetTabs(tabWanted);	
});

Event.observe(window, 'load', function() {

	selectBottomTab(jQuery('#bottom-tabs li:first'));
	if(!isIE7 && !isIE6orPrevious)
		showBottomPanel('buy_tab');

	
	// productdetails - change the product image
	//set the onmouseover event to swap different images.
	$$('div.gallery a').each(function(item, index) {
		//change the index to start from ONE.
		Event.observe(item, 'mouseover', function(event) {
			element = Event.element(event);
			linkEl = (element.match('img')) ? element.up() : element;
			$('mainPic').src = linkEl.href;
		});
	});		
	
	//set the click event to toggle tabs.
	$$('ul.tabs li').each(function(item, index) {
		Event.observe(item, 'click', function(event) {
			hideAllTabContent(index);
			resetTabs(index);
		});
	});		
	
	// Event to toggle bottom tabs.
	jQuery('ul#bottom-tabs li').click(function() {
		selectBottomTab(jQuery(this));
		showBottomPanel(jQuery(this).attr('id'));
		return false;
	});	
	
	if ($('find_out_more')) {
		Event.observe($('find_out_more'), 'click', function(event) {
			 document.getElementById('tabA').addClassName('off');
			 Element.hide('tab1');
			 Element.show('tab2');
		 });
	}

	var v = {
		hideProductSelector: function(event) {
			var bounds = boundingBox($('product_selector_wrapper'));
			if (Event.isLeftClick(event) && !withinBounds(event, bounds)) {
				if (!isIE7) {
					Effect.Fade('product_selector_wrapper', {
						duration: 0.5,
						queue: {position: 'end', scope: 'pselector'},
						beforeStart: cancelQueues,
						afterFinish: function() {
							v.changeButtonStatus("click", "off");
						}
					});
				} else {
					Element.hide('product_selector_wrapper');
					v.changeButtonStatus("click", "off");
				}
				Event.stopObserving(document, 'mousedown', hps);
				if (Event.element(event).title != "Product Selector") ps_open = false;
			}
		},
		showProductSelector: function(event) {
			if (!ps_open) {
				ps_open = true;
				if (!isIE7) {
					Effect.Appear('product_selector_wrapper', {
						duration: 0.35,
						queue: {position: 'end', scope: 'pselector'},
						beforeStart: cancelQueues,
						afterFinish: function() {
							setPSHeights();
							v.changeButtonStatus("click", "on");
						}
					});
				} else {
					Element.show('product_selector_wrapper');
					v.changeButtonStatus("click", "on");
				}
			} else {
				this.forceHideProductSelector(event);
			}
			Event.observe(document, 'mousedown', hps);
		},
		forceHideProductSelector: function(event) {
			Effect.Fade('product_selector_wrapper', {
				duration: 0.5,
				queue: {position: 'end', scope: 'pselector'},
				beforeStart: cancelQueues,
				afterFinish: function() {
					v.changeButtonStatus("click", "off");
				}
			});
			Event.stopObserving(document, 'mousedown', hps);
			ps_open = false;
		},
		changeButtonStatus: function(status, direction) {
			if (direction == "on") {
				productSelectorButton.addClassName(element_class + '_' + status);
			}
			else if (direction == "off") {
				productSelectorButton.removeClassName(element_class + '_' + status);
			}
		}
	};

	var hps = v.hideProductSelector.bindAsEventListener(v);
	var sps = v.showProductSelector.bindAsEventListener(v);
	var ps_open = false;

	var productSelectorButton = $("product_selector_button");

	if (productSelectorButton) {
		productSelectorButton.observe("click", sps);
		productSelectorButton.observe("mouseover", function() {
			v.changeButtonStatus("hover", "on");
		});
		productSelectorButton.observe("mouseout", function() {
			v.changeButtonStatus("hover", "off");
		});
	}


	if (typeof s != "undefined") {
		var s_code = s.t();
		if (s_code) {
			document.write(s_code);
		}
	}

});

function boundingBox(element) {
	alert('here');
	var pos = $(element).cumulativeOffset();
	var dim = $(element).getDimensions();

	var new_pos = {
		leftBorder: pos.left,
		topBorder: pos.top,
		rightBorder: pos.left + dim.width,
		bottomBorder: pos.top + dim.height
	};

	return new_pos;
}

function withinBounds(event, bounds) {
	var isWithin = false;
	pX = Event.pointerX(event);
	pY = Event.pointerY(event);
	if (pX > bounds.leftBorder && pX < bounds.rightBorder
			&& pY > bounds.topBorder && pY < bounds.bottomBorder) isWithin = true;

	return isWithin;
}

function cancelQueues() {
	queue = Effect.Queues.get('pselector');
	queue.each(function(e) {
		e.cancel();
	});
}

function setPSHeights() {
	els = $$('div#product_selector ul.categories');
	mensEls = els[0].childElements();
	womensEls = els[1].childElements();

	for (i = 0; i < mensEls.length; i++) {
		mHeight = mensEls[i].getHeight();
		wHeight = womensEls[i].getHeight();
		if (mHeight > wHeight) womensEls[i].setStyle({height: mHeight + 'px'});
		if (mHeight < wHeight) mensEls[i].setStyle({height: wHeight + 'px'});
	}
}

function link(url) {
	location.href = url;
	return false;
}
function loadScript(scriptURL) {
	var scriptElem = document.createElement("script");
	scriptElem.setAttribute("type", "text/javascript");
	scriptElem.setAttribute("src", scriptURL);
	document.body.appendChild(scriptElem);
	return false;
}