window.addEvent('domready', prep);
function prep(){
	if($('showroom')){
		$$('.section').each(function(e){
			e.addEvents({'mouseenter':expand.pass(e),'mouseleave':contract}).set('tween',{transitions: Fx.Transitions.Quad.easeOut});
		});
		$$('.moreinfo').each(function(e){
			e.addEvent('click',expandinfo.pass(e));
		});
	}
	$('searchbox').addEvent('click',function(){$('searchbox').setProperty('value','').setStyle('color','#000').removeEvents();});	
	if($('ordering')){
			if($('options')){
			$('loading').removeClass('hidden');
			var pfinish = $('pfinish').get('html');
			var highlight = new Element('div',{id:'highlight'});
			$$('.finish').each(function(e){var finishmatch = e.get('html');if(finishmatch == pfinish){e.getParent().getParent().grab(highlight,'top');}} )
			}
			else{$('productimage').setStyle('width',440);$('loading').setStyle('width',440).removeClass('hidden');}
		}
	var wcTips = new Tips('.codepop');
	$$('.codepop').each(function(e){
	var code = e.get('html');
	coden = code.replace(' ','');
	e.store('tip:text', window[coden]);
	}
	);
	if($('addbox')){
		function fadebox(){$('addbox').fade('out');}
		var mc = $('minicart').getCoordinates(document.body);
		$('addbox').setStyles({'top':mc.top+20,'left':mc.right-202}).fade('hide').removeClass('hidden').fade('in');
		fadebox.delay(5000);
	}
	var insetTips = new Tips('.popinset',{'className':'insettip'});
	$$('.popinset').each(function(e){
			var itemnum = e.get('id');
			var img =  '/mm5/images/small/'+itemnum+'.jpg';
			e.store('tip:text', '<div class="insettip"><img src="'+img+'" /><br/><b>'+itemnum+'</b></div>');
		});
	if($('pdst')){
		$$('.cpname').each(function(e){
		var ename = e.get('html');
		var enumber = ename.match(/\d/);
		var sizestart = ename.indexOf(enumber);
		var sizeend = ename.indexOf('"');
		var size = ename.substr(sizestart, (sizeend-sizestart));
		if (size.contains(' ')){
			var newsize = size.replace(' ', '\u00a0');
			var newname = ename.replace(size, newsize);
			e.set('html',newname);
		}
		var second = ename.substr(sizeend+1);
		if (second.contains('"')){
			var first = ename.substr(0,sizeend+1);
			var senumber = second.match(/\d/);
			var ssizestart = second.indexOf(senumber);
			var ssizeend = second.indexOf('"');
			var ssize = second.substr(ssizestart, (ssizeend-ssizestart));
			if (ssize.contains(' ')){
				var snewsize = ssize.replace(' ', '\u00a0');
				var snewname = second.replace(ssize, snewsize);
				snewname = first+snewname;
				e.set('html',snewname);
			}
		}
	});
	}
	if($('sorter')){
		if($('sortJump').children.length<=2){$('sorter').addClass('hidden');}
	}
	function smexpand(e){
	e.set('html','-').removeEvents().addEvent('click',smcontract.pass(e));;
	e.getParent().getNext().removeClass('closed');
	}
	function smcontract(e){
	e.set('html','+').removeEvents().addEvent('click',smexpand.pass(e));
	e.getParent().getNext().addClass('closed');
	}
	if($('sitemap')){
		$$('.sub').each(function(e){e.addClass('closed');});
		$$('.expander').each(function(e){e.addEvent('click',smexpand.pass(e));});
	}
	if($('payshipselect')){
		$('payshipselect').removeClass('hidden');
		var shipcharge = $('pcarttotal').getElement('.charge').get('html').escapeRegExp();
		$('shipmethod').getElements('option').each(function(e){
			var patt=new RegExp(shipcharge);
			var t = e.get('html');
			if (patt.test(t)){
			e.setProperty('selected','selected');
			}
		});
	}
	if($('screenchange')){$('screenchange').set('value','OPAY');}
	if($('cart')){
		$$('.cartimg').each(function(e){
			if(e.complete){dealwci(e);}
			else{e.addEvent('load',dealwci.pass(e));}
			});
			$$('.cartname').each(function(e){
		var ename = e.get('html');
		var enumber = ename.match(/\d/)
		var sizestart = ename.indexOf(enumber);
		var sizeend = ename.indexOf('"');
		var size = ename.substr(sizestart, (sizeend-sizestart));
		if (size.contains(' ')){
			var newsize = size.replace(' ', '\u00a0');
			var newname = ename.replace(size, newsize);
			e.set('html',newname);
		}
		var second = ename.substr(sizeend+1);
		if (second.contains('"')){
			var first = ename.substr(0,sizeend+1);
			var senumber = second.match(/\d/);
			var ssizestart = second.indexOf(senumber);
			var ssizeend = second.indexOf('"');
			var ssize = second.substr(ssizestart, (ssizeend-ssizestart));
			if (ssize.contains(' ')){
				var snewsize = ssize.replace(' ', '\u00a0');
				var snewname = second.replace(ssize, snewsize);
				snewname = first+snewname;
				e.set('html',snewname);
			}
		}
	});
	}
	if($('clearance')){$$('.climg').each(function(e){
		if(e.complete){dealwclearance(e);}
		else{e.addEvent('load', dealwclearance.pass(e));}
		})
		$$('.clpname').each(function(e){
			var ename = e.get('html');
			var enumber = ename.match(/\d/)
			var sizestart = ename.indexOf(enumber);
			var sizeend = ename.indexOf('"');
			var size = ename.substr(sizestart, (sizeend-sizestart));
			if (size.contains(' ')){
				var newsize = size.replace(' ', '\u00a0');
				var newname = ename.replace(size, newsize);
				e.set('html',newname);
			}
			var second = ename.substr(sizeend+1);
			if (second.contains('"')){
				var first = ename.substr(0,sizeend+1);
				var senumber = second.match(/\d/);
				var ssizestart = second.indexOf(senumber);
				var ssizeend = second.indexOf('"');
				var ssize = second.substr(ssizestart, (ssizeend-ssizestart));
				if (ssize.contains(' ')){
					var snewsize = ssize.replace(' ', '\u00a0');
					var snewname = second.replace(ssize, snewsize);
					snewname = first+snewname;
					e.set('html',snewname);
				}
			}
		});	
	}
	function dealwclearance(e){
		var id=e.getSize();
		if (id.y<150){
			e.setStyle('top',(150-id.y)/2);
		}
	}
	if($('subcats')){
		$$('.subcatimage').each(function(e){
		if(e.complete){dealwsubcats(e);}
		else{e.addEvent('load', dealwsubcats.pass(e));}
		})
		}
	function dealwsubcats(e){
		var id=e.getSize();
		if (id.y<120){
			e.setStyle('top',(120-id.y)/2);
		}
	}
	if($('pdst')){
		$$('.cpimg').each(function(e){
		if(e.complete){dealwcpi(e);}
		else{e.addEvent('load', dealwcpi.pass(e));}
	})
	}

	function dealwcpi(e){
		var id=e.getSize();
		if (id.y<150){
			e.setStyle('top',(150-id.y)/2);
		}
		var divd=e.getParent().getParent().getParent().getSize();
		var cpitema = e.getParent().getParent().getParent().getLast();
		cpitema.setStyle('height',divd.y);
	}
	
	function dealwci(img){
		var id = img.getSize();
		if(id.x >= id.y){
			img.setStyle('width',80);
			}
		else{
			img.setStyle('height',80);
			}
	}
	
	
	function dealwpi(){
		$('pi').removeClass('smalling');
		$('loading').dispose();
		var pid = $('pi').getSize();
		var offset = (300-((300*pid.y)/pid.x))/3;
		if(pid.x>=pid.y){
			$('pi').setProperties({'pw':pid.x,'ph':pid.y,'class':'pop'}).setStyles({'width':300,'top':offset}).addEvent('click',pop);
			}
		else{
			$('pi').setProperties({'pw':pid.x,'ph':pid.y,'class':'pop'}).setStyle('height',300).addEvent('click',pop);
			}
	}
	if($('pi')){
		if($('pi').complete){dealwpi();}
		else{$('pi').addEvent('load', dealwpi);}
			
	}
	 
	if ($('map_canvas')){		
		if (GBrowserIsCompatible()) {
			var map = new GMap2($('map_canvas'));
			var wcl = new  GLatLng(43.470454, -110.787996);
			map.setCenter(wcl, 16);
			map.setUIToDefault();
			map.setMapType(G_SATELLITE_MAP);
			map.addOverlay(new GMarker(wcl));
			document.body.setProperty('unload','GUnload();');
		}
	}
}
function expand(e){
	$$('.section').each(function(s){
		if(s.id == e.id){
			s.tween('width', 430);
			} 
		else{
			s.tween('width', 66);
			}
	});
}
function contract(){
	$$('.section').each(function(s){
		s.tween('width', 157);
	});
}

function openWin(url) {
NewWindow = window.open(url,"child","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizeable=yes,width=250,height=525");
} 
function copyfrombilling(){
	$('billto').getElements('input').each(function(e){
		var val=e.get('value');
		var ename=e.get('name');
		ename=ename.replace('Bill','Ship');
		$('shipto').getElement('input[name='+ename+']').set('value',val);
		});
	$('billto').getElements('select').each(function(e){
		var val=e.get('value');
		var ename=e.get('name');
		ename=ename.replace('Bill','Ship');
		$('shipto').getElement('select[name='+ename+']').set('value',val);
		});
}
function copyfromshipping(){
	$('shipto').getElements('input').each(function(e){
		var val=e.get('value');
		var ename=e.get('name');
		ename=ename.replace('Ship','Bill');
		$('billto').getElement('input[name='+ename+']').set('value',val);
		});
	$('shipto').getElements('select').each(function(e){
		var val=e.get('value');
		var ename=e.get('name');
		ename=ename.replace('Ship','Bill');
		$('billto').getElement('select[name='+ename+']').set('value',val);
		});
}
function updatecarttotal(selected){
var shipname;
var shipcost;
if(selected == 'flatrate:UPS Ground'){
shipname = 'UPS Ground';
shipcost = '6.00';
}
else{
var ship = $('shipmethod').getElement('option[value='+selected+']').get('html');
var start = ship.indexOf('$');
shipname = ship.substr(0,start-2);
shipcost = ship.substr(start+1);
shipcost = shipcost.replace(')','');
}
var total = new Number(0.00);
$('pcarttotal').getElement('.charge').getPrevious().set('html','Shipping: '+shipname+':');
$('pcarttotal').getElement('.charge').set('html','$'+shipcost);
$$('.charge').each(function(e){
	var charge = parseFloat(e.get('html').substr(1));
	total=total+charge;
});
var sub = parseFloat($('subtotal').get('html').substr(1));
total=total+sub;
total=total.toFixed(2);
$('pctotal').set('html','$'+total);
}
function closeit(){
$('dark').dispose();
$('iph').dispose();
$('closetab').dispose();
$('dimdiv').dispose();
}
function pop(img){
var ilw=$('pi').getProperty('pw').toInt();
var ilh=$('pi').getProperty('ph').toInt();
var ilsw=$('pi').getSize().x;
var ilsh=$('pi').getSize().y;
var isrc=$('pi').getProperty('src')
var i = $('pi').getCoordinates(document.body);

var darken = new Element('div',{'id':'dark'});
document.body.adopt(darken);
var dimdiv = new Element('div',{'id':'dimdiv'});
var dims = '';
if($('pdim')){
	dims = $('pdim').get('html');
	dimdiv.set('html',dims);
}
var iclose = new Element('div',{'id':'closetab'});
var iph = new Element('img',{'id':'iph','src':isrc});
$('dark').addEvent('click',closeit).set('opacity',0).setStyle('background-image','url(http://www.whitechapel-ltd.com/mm5/images/black.png)').tween('opacity',1);
document.body.adopt(iph);
$('iph').setStyles({'top':i.top,'left':i.left,'width':ilsw,'height':ilsh}).addEvent('click',closeit);
var isize = $('iph').getSize();
var wsize = window.getSize();
var mx = 20;
if (wsize.x>ilw){mx = ((wsize.x-ilw)/2).toInt()};
var imorph = new Fx.Morph('iph',{transition:'quad:in:out',duration:700,onComplete:function(){document.body.adopt(iclose,dimdiv);$('dimdiv').setStyles({'top':51+ilh,'left':mx+1,'width':ilw-10});$('closetab').setStyles({'top':31,'left':mx+ilw-97}).set('html','<a>close window</a>').addEvent('click',closeit);}});
imorph.start({'width':ilw,'height':ilh,'left':mx,'top':50});
}

var Code1='<b>Code 1:</b> All English made and solid brass. The antique finish on our Code 1 hardware is the result of controlled oxidation. This darkened surface is highlighted to replicate the effect of use. No lacquer or other finish is applied to seal this surface so time will add to the patina. If a hand polished finish is preferred, this patina can be knocked back or removed with any brass polish. Many Code 1 items also come in Polished Brass (<b>P</b>), and plated finishes: Satin Nickel (<b>SN</b>), Polished Chrome (<b>PC</b>) ';
var Code2='<b>Code 2:</b> Refers to a sanded finish. Throughout most of the 18th and 19th  centuries the common butt hinge was expected to do its job without drawing attention to itself. Functional elements such as hinges and locks were not meant to be in competition with designed elements for the viewer’s attention. These days, perhaps because we are starved of contact with quality materials, we like to display them to best effect. If you are reproducing period cabinetwork your choice should be the standard finish hinges. These can be finished along with the surrounding woodwork and will blend unobtrusively.';
var Code3='English made highest quality locks, in some cases a traditional brushed extruded finish and other cases highly polished.';
var Code4='<b>Code 4:</b> No other reproduction hardware can compare to our Code 4 fittings. Each is a clone of an original, reproduced by the time consuming and demanding lost wax process. The result is a completely convincing piece in all respects. No attempt is made to “improve” upon the original either visually or functionally. Each item is finished with a marvelous ammonia patina that can be removed with common brass polish for a “cared for” effect. If you want to elevate your work to the highest possible standards of authenticity this range should be your choice. Truly “Restoration” grade.';
var Code5='<b>Code 5:</b> Made of solid cast brass and hand finished in Spain.';
var Code6='<b>Code 6:</b> A blacksmith shapes iron by first softening it in a very hot fire-usually produced with coal. A skilled blacksmith can judge just how hot and malleable a piece of metal is by its color-white, yellow, orange, red or purple. The softened iron is shaped or turned into wrought iron by pounding it against a heavy steel anvil. A variety of tongs and hammers are used for different jobs.';
var Code7='<b>Code 7:</b> These iron hardware pieces are sand cast and then hand finished with a final application of beeswax for protection. They look centuries old when new and will only become more beautiful with time and use.All of the Rustix range is available using the traditional method of applying bees-wax to the fittings which give a stunning effect of a rich dark brown/black colour tinged here and there with the deep red/ochre from the forging process. A regular application of bees-wax should be applied to any fittings, which are used externally, or where they may become exposed to the weather such an open window where stays and fasteners might experience occasional damp conditions. A soft shoe brush is ideal for polishing the fittings.';
var Code8='<b>Code 8:</b> Well formed and beautifully polished stainless steel and brass bring a contemporary functional elegance to this range of Japanese hardware.';
var Code9='<b>Code 9:</b> Our wooden knobs & pulls are available in a wide array of useful styles and sizes. Nearly all of our wood knobs and pulls are made in North America from a variety of hardwoods and pine.';
var Code10='<b>Code 10:</b> Our wooden knobs & pulls are available in a wide array of useful styles and sizes. Nearly all of our wood knobs and pulls are made in North America from a variety of hardwoods and pine.';
var Code11='<b>Code 11:</b> American Made in Polished Brass to the highest specifications.';
var Code13='<b>Code 13:</b> Patinated Iron. These iron fittings have been chemically aged and rusted, then tumbled, waxed and rubbed to achieve an authentic antique patina. They are not sealed or plated, so they will continue to age with use.';
var Code14='<b>Code 14:</b> These precision made items are solid brass and finished in a fine satin with no protective coating. ';
var Code16='<b>Code 16:</b> These items all high quality American made. Many Code 16 items come in Polished Brass (<b>P</b>), US10B (<b>A</b>) and plated finishes: Satin Nickel (<b>SN</b>), Polished Chrome (<b>PC</b>)';
var Code19='<b>Code 19:</b> Beautiful black iron of a rustic quality.';
var Code26='<b>Code 16:</b> Rusted Iron with excellent anti-corrosion properties. The quality of these finishes is generally regarded to be the very best.';
var Code28='<b>Code 28:</b> The black finish on these knobs and pulls is created by the carbon build up on the surface when they are heated at the forge. Some surfaces are then hammered to produce the texture of hand wrought iron. Then the pieces are waxed and rubbed, resulting in a dark iron surface with pewter highlights.';
var Code31='<b>Code 31:</b> French Cast Brass Fiche Hinges These hinges and escutcheons are sand cast and hand finished. This method and standard of manufacture ensures a fully authentic product that has no equal. The bowed face and bevelled edges set these authentic escutcheons apart from any mass produced item.';


function quote(){
var width = $('width').get('value').toInt();
var length = $('length').get('value').toInt();
width = Math.ceil(width/4);
length = Math.ceil(length/4);
var price = (width*length*8.97).round(2);
price = price.toFixed(2);
$('quotearea').set('html',  '$<b>'+price+'</b> is an estimated price for your order.');
}