var hoverMenuCurrentParent;

document.observe('dom:loaded', function (){
	var	productSrc;
	$$("script").findAll( function(s){
		return (s.src && s.src.match(/tescobath_script\.js(\?.*)?$/))}).each( function(s){
		var path = s.src.replace(/tescobath_script\.js(\?.*)?$/,'');
		productSrc = path+'tescobath_product.js';
	});
	if ($('buyForm') && productSrc){
		var prdScript = new Element("script", { type: "text/javascript", src: productSrc });
		document.observe("product:loaded", function(){new Product();});
		$$("head")[0].insert(prdScript);
	}
	
	var consultationCheck = $('zcheck_consultation');
	var fittingFrame = $('basket-fitting')
	if (consultationCheck && fittingFrame){
		fittingFrame.show();
		consultationCheck.observe('click', function() {
			theForm = $('updateBag');
			if (theForm){theForm.submit();}
		});
	}
});
window.onload = function (){ 
	$$('a[rel=popup]').each(function(item){
		item.onclick = function(){
			window.open(this.href, 'popupwindow', 'width=800,height=400,menubar=1,toolbar=1,resizable=1,scrollbars,location=1');
        	return false;
    	}
	});
	var myPrint = $('print'); 	
  	if (myPrint){
    	myPrint.onclick = function(){
      		window.print();
    	}
  	}  	
	var mySer = $('searchTextInput');
	if (mySer){
		mySer.onfocus = function(){
			clearSearch(this);
		}
		mySer.onblur = function(){
			resetSearch(this);
		}
	}
	$$('select.updateBasketQty').invoke('observe', 'change', function(){
        $('updateBag').submit();
    });
    if (document.getElementById('secureCodeRedirect')) {
		document.getElementById('secureCodeRedirect').submit();
	}
	var myHelp = $('cvvhelp');
	if (myHelp) {	
		constructMyHelp();
		var myHelpPopup = $('my-help-popup');
		if (myHelpPopup) {
			myHelp.observe('click', function() {
				myHelpPopup.show();
			});
		}
	}
	$$('.menuHover').each(function(element) {
		var theParent = $('category-' + element.id.substring(5, 10));
		if (theParent) {
			element.style.top = (theParent.positionedOffset().top + 40) + 'px';
			element.style.left = theParent.positionedOffset().left + 'px';
		}
	});	
	$$('.product').invoke('observe', 'mouseover', function() {
		var prodcatID = this.id.substring(9,13);
		var theMenu = $('menu-' + prodcatID);
		if (theMenu) {
			theMenu.show();			
		}
	});	
	$$('.product').invoke('observe', 'mouseout', function() {
		var prodcatID = this.id.substring(9,13);
		var theMenu = $('menu-' + prodcatID);
		if (theMenu) {
			theMenu.hide();
		}
	});	
	$$('.menuHover').invoke('observe', 'mouseover', function() {
		this.show();
	});
	$$('.menuHover').invoke('observe', 'mouseout', function() {
		this.hide();
	});	
	var retailisbox = $('fitting-service-retailis-box');
	if (retailisbox) {
		retailisbox.observe('click', function() {
			$('fitting-service-ikbbi').hide();
			$('fitting-service-retailis').show();
		});
	}
	var ikbbibox = $('fitting-service-ikbbi-box');
	if (ikbbibox) {
		ikbbibox.observe('click', function() {
			$('fitting-service-retailis').hide();	
			$('fitting-service-ikbbi').show();			
		});	
	}
	var fittingserviceretailis = $('fitting-service-retailis');
	if (fittingserviceretailis) {
		fittingserviceretailis.down('span').observe('click', function() {
			fittingserviceretailis.hide();
		});
	}
	var fittingserviceikbbi = $('fitting-service-ikbbi');
	if (fittingserviceikbbi) {	
		fittingserviceikbbi.down('span').observe('click', function() {
			fittingserviceikbbi.hide();
		});	
	}
	
	correctPNG();
}
function constructMyHelp(){
	var paymentFields = $('paymentFields');
	if (paymentFields) {
		paymentFields.appendChild(Builder.node('div', {id:'my-help-popup'}, [
			Builder.node('span', {id:'my-help-popup-close'}, 'Close'),
			Builder.node('strong', 'Electron Visa, Maestro, MasterCard, Visa'),
			Builder.node('p', 'Last 3 Numbers on back of card'),
			Builder.node('img', {src:'/tescobathrooms/ui/icon_cvv.jpg', title:'Last 3 Numbers on back of card', alt:'Last 3 Numbers on back of card'})
		]));	
		$('my-help-popup').hide();
		$('my-help-popup-close').observe('click', function() {
			$('my-help-popup').hide();
		});
		
	}
}
/* Calendar Stuff */
function selectDate(itemRef, newDate){
	var selectedDate = $('selectedDate');
		if (itemRef){
			var flag = false;
			var myCounter = 0;
			var x = 0;
			var myDate;
			selectedDate.value = newDate;
			var myDate = $('stepThreeInfo');
			var myError = $('calError');
			if (myDate){
				myText = document.createTextNode('You have selected ' + newDate.substring(6,8) + '.' + newDate.substring(4,6) + '.' + newDate.substring(0,4) + ' as your delivery date.');
				while(myDate.lastChild){
					myDate.removeChild(myDate.lastChild);
				}
				myDate.appendChild(myText);
				if (myError){
					myError.hide();
				}
			}
			var eltArray = document.getElementsByTagName('td');
			if (eltArray){
				for (x = 0; x < eltArray.length; x++){
					if (eltArray[x].className == 'calCellSelected'){
						myCounter++;
					}
				}
			}
			if (myCounter > 0){
				for (x = 0; x < eltArray.length; x++){
					if (eltArray[x].className == 'calCellSelected'){
						eltArray[x].className = 'calCellLink';
					}
				}
			}
			itemRef.className = 'calCellSelected';
			myCounter = 0;
		}
}
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);
  if ((version >= 5.5 && version < 7) && (document.body.filters)) 
  {
    for(var i=0; i<document.images.length; i++)
    {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
	  var paramLoc = imgName.indexOf("?");	  
	  if (paramLoc != -1)
	  {
		  if (imgName.substring(paramLoc-3, paramLoc) == "PNG"  && img.src.indexOf('google') == -1 && img.className == 'overlay-image')
	      {
	        var imgID = (img.id) ? "id='" + img.id + "' " : "";
	        var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
	        var imgStyle = "display:inline-block;" + img.style.cssText ;
	        var cssClass = img.className;
	        if (img.align == "left") imgStyle = "float:left;" + imgStyle;
	        if (img.align == "right") imgStyle = "float:right;" + imgStyle;
	        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	        var strNewHTML = "<span " + imgID + imgClass + imgTitle
	        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
	        img.outerHTML = strNewHTML;
	        i = i-1;
	      }
	  }
	  else
	  {
		  if (imgName.substring(imgName.length-3, imgName.length) == "PNG"  && img.src.indexOf('google') == -1 || img.className == 'overlay-image')
	      {
	        var imgID = (img.id) ? "id='" + img.id + "' " : "";
	        var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
	        var imgStyle = "display:inline-block;" + img.style.cssText ;
	        var cssClass = img.className;
	        if (img.align == "left") imgStyle = "float:left;" + imgStyle;
	        if (img.align == "right") imgStyle = "float:right;" + imgStyle;
	        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	        var strNewHTML = "<span " + imgID + imgClass + imgTitle
	        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
	        img.outerHTML = strNewHTML;
	        i = i-1;
	      }
	  }
    }
  }
}
function calNextMonth(hx,sx){var h=$(hx);var s=$(sx);if(h){if(s){h.style.display='none';s.style.display='block';}}}
function changeDateText(d){var a =$('calendarInstruction');if(a){n=a.childNodes;for(var i=n.length-1;i>=0;i--){a.removeChild(n[i]);}var t=document.createTextNode('You have selected '+d.substring(6,8)+'/'+ d.substring(4,6)+'/'+d.substring(0,4)+' as your delivery date.');a.appendChild(t);}}
function checkLength(element, length){if (element){if (length > 0){myValue = getValue(element);if (myValue.length > length){myNewValue = myValue.substring(0, length);setValue(element, myNewValue);}}}}
function getValue(element){if (element){if (element.value || element.value == ''){return element.value;}else if (element.lastChild){return element.lastChild.nodeValue;}else{return element.nodeValue;}}}
function setValue(element, value){if (element){if (element.value || element.value == ''){element.value = value;}else if (element.lastChild){element.lastChild.nodeValue = value;}else{element.nodeValue = value;}}}
function clearSearch(ele){if(ele){if(ele.value=='Search our site...'){ele.value='';}}}
function resetSearch(ele){if(ele){if(ele.value==''){ele.value='Search our site...';}}}