// JavaScript Document

var wwidth = 310;
var wheight = 391;

function pageWidth() {
	return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}

function pageHeight() {
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}

function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}

function $(el) {
	return document.getElementById(el);
}

function openInfoWindow(subject) {
	
	var iw = $("infoWindow");
	var ititle = $( "infoTitle" );
	var ibody = $("infoBody");
	
	if( subject == "help" ) {
		ititle.innerHTML = "About This Website";
		ibody.innerHTML = "This website has been designed to give you an overall view of the Westplate Tray range of products. However, it can only be an impression as we are constantly deeloping new products and specialise in making items to customers' own designs and specification. If any of the products you see are of interest or you would like to discuss other products with us, please get in touch (<a href=\"javascript:openInfoWindow(\'contact\')\">View our contact details</a>). We look forward to hearing from you."
	} else if( subject == "contact" ) {
		ititle.innerHTML = "Contact";
		ibody.innerHTML = 'Thank you for visiting our website. We welcome all enquiries from trade customers (wholesalers, retailers) sports clubs, corporate event organizers, overseas importers etc. We also offer a repair &amp; and replating service. Please contact us: <br /><br/>Tel: <span>0114 249 0969</span> or <span>0114 272 3518</span> <br/>Fax: <span>0114 249 0922</span><br />e-mail: <a href="mailto:westplate@chimoholdings.co.uk"><span>westplate@chimoholdings.co.uk</span></a><br />Address: <br /><span>White Rose Works<br/>61 Eyre Lane<br />Sheffield<br />S1 3GF<br />England </span>';
	}
	
	var p = "px";
	//calculate position of infoWindow (aim for center position)
	var tp = posTop() + ((pageHeight() - wheight) / 2);
	var lp = posLeft() + ((pageWidth() - wwidth) / 2);
	
	iw.style.top = (tp < 0 ? 0 : tp) + p;
	iw.style.left = (lp < 0 ? 0 : lp) + p;
	
	iw.style.visibility = "visible";
}

function hideInfoWindow() {
	$("infoWindow").style.visibility = "hidden";
}

//Script for exapnding window
function displayLinks() {
	blankTile();
	var space = $("chimoLinks");
	var xincr = 8;
	var yincr = 12.6;
	var int;
	var swidth = 100;
	var sheight = 35;
	var iLeft = 520;
	
	var expandSpace = function() {
		swidth = swidth + xincr;
		sheight = sheight + yincr;
		
		space.style.width = swidth + "px";
		space.style.left = (iLeft - swidth) + "px";
		space.style.height = sheight + "px";
		
		if( int && (swidth >= 300 || sheight >= 350 )) {
			window.clearInterval( int );
			switchToLinksTile();
		}
	}
	
	int = window.setInterval( expandSpace, 20 );
}

function hideLinks() {
	blankTile();
	var space = $("chimoLinks");
	var xdecr = 8;
	var ydecr = 12.6;
	var int;
	var swidth = 300;
	var sheight = 350;
	var iLeft = 520;
	
	var hideSpace = function() {
		swidth = swidth - xdecr;
		sheight = sheight - ydecr;
		
		space.style.width = swidth + "px";
		space.style.left = (iLeft - swidth) + "px";
		space.style.height = sheight + "px";
		
		if( int && (swidth <= 100 || sheight <= 35 )) {
			window.clearInterval( int );
			switchToLinksButton();
		}
	}
	
	int = window.setInterval( hideSpace, 20 );
}

function switchToLinksTile() {
	$("chimoLinks").innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0"><tr><td style="width:15px; height:14px; background-image:url(../images/lt_nw.png);"></td><td style="height:14px; background-image:url(../images/lt_n.png); background-repeat:repeat-x;"></td><td style="width:17px; height:14px; background-image:url(../images/lt_ne.png);"></td></tr><tr><td style="width:15px; background-image:url(../images/lt_w.png);"></td><td style="background-image:url(../images/lt_center.png); background-repeat:no-repeat;" valign="top"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td style="height:15px;"><a href="javascript:hideLinks()"><img src="../images/linksbt_hide.png" width="140" height="14" border="0" /></a><hr /></td></tr><tr><td><p class="linksText">If you require any of the following services provided by Chimo Holdings please click on the link:<br /><table cellpadding="5" cellspacing="0" border="0" class="linksText"><tr><td><a href="http://www.chimoholdings.co.uk" target="_blank"><img src="../images/chimologo.png" width="40" height="46" border="0" /></a></td><td><a href="http://www.chimoholdings.co.uk" target="_blank">Chimo Holdings</a> - group website for Company products &amp; services.</td></tr><tr><td><a href="http://www.cutleryhospital.co.uk" target="_blank"><img src="../images/chlogo.png" width="40" height="44" border="0"/></a></td><td><a href="http://www.cutleryhospital.co.uk" target="_blank">Cutlery Hospital</a> - a comprehensive service for restoring and replating your cutlery. Knife repairs are a speciality.</td></tr><tr><td><a href="http://www.cutlerymate.co.uk" target="_blank"><img src="../images/cmlogo.png" width="40" height="27" border="0"/></a></td><td><a href="http://www.cutlerymate.co.uk" target="_blank">Cutlerymate</a> - a unique system for storing your fine cutlery.</td></tr><tr><td><a href="http://www.chimodirect.co.uk" target="_blank"><img src="../images/cdlogo.png" width="40" height="58" border="0" /></a></td><td><a href="http://www.chimodirect.co.uk" target="_blank">Chimodirect</a> - A webshop for the general public offering a selected range of Chimo products including creamhandled knives and fengshui cufflinks.</td></tr></table></p></td></tr></table></td><td style="width:17px; background-image:url(../images/lt_e.png);"></td></tr><tr><td style="width:15px; height:18px; background-image:url(../images/lt_sw.png);"></td><td style="height:18px; background-image:url(../images/lt_s.png); background-repeat:repeat-x;"></td><td style="width:17px; height:18px; background-image:url(../images/lt_se.png);"></td></tr></table>';
}

function blankTile() {
	$("chimoLinks").innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td style="width:15px; height:14px; background-image:url(../images/lt_nw.png);"></td><td style="height:14px; background-image:url(../images/lt_n.png); background-repeat:repeat-x;"></td><td style="width:17px; height:14px; background-image:url(../images/lt_ne.png);"></td></tr><tr><td style="width:15px; background-image:url(../images/lt_w.png);"></td><td style="background-image:url(../images/lt_center.png); background-repeat:no-repeat;"></td><td style="width:17px; background-image:url(../images/lt_e.png);"></td></tr><tr><td style="width:15px; height:18px; background-image:url(../images/lt_sw.png);"></td><td style="height:18px; background-image:url(../images/lt_s.png); background-repeat:repeat-x;"></td><td style="width:17px; height:18px; background-image:url(../images/lt_se.png);"></td></tr></table>';
}

function switchToLinksButton() {
	$("chimoLinks").innerHTML = '<a href="javascript:displayLinks()"><img src="../images/linksbt.png" width="100" height="35" onmouseover="this.src=\'../images/linksbt_hover.png\'" onmouseout="this.src=\'../images/linksbt.png\'" border="0" /></a>';
}

//Script for dragging an html element 
function dragElem( elemId, event ) {
	elem = document.getElementById( elemId );
	if( elem )
		drag( elem, event );
}

function drag( elementToDrag, event ) {
	var startX = event.clientX, startY = event.clientY;
	var origX = elementToDrag.offsetLeft, origY = elementToDrag.offsetTop;
	
	var deltaX = startX - origX, deltaY = startY - origY;
	
	if( document.addEventListener ) {
		document.addEventListener( "mousemove", moveHandler, true );
		document.addEventListener( "mouseup", upHandler, true );
	}
	else if( window.attachEvent ) {
		elementToDrag.setCapture();
		elementToDrag.attachEvent( "onmousemove", moveHandler );
		elementToDrag.attachEvent( "onmouseup", upHandler );
		elementToDrag.attachEvent( "onlosecapture", upHandler );
	}
	else {
		var oldmovehandler = document.onmousemove;
		var olduphandler = document.onmouseup;
		document.onmousemove = moveHandler;
		document.onmouseup = upHandler;
	}
	
	if( event.stopPropagation ) event.stopPropagation();
	else event.cancelBubble = true;
	
	if( event.preventDefault ) event.preventDefault();
	else event.returnValue = false;
	
	function moveHandler(e) {
		if(!e) e = document.event;
		
		elementToDrag.style.left = (e.clientX - deltaX) + "px";
		elementToDrag.style.top = (e.clientY - deltaY) + "px";
		
		if(e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
	}
	
	function upHandler(e) {
		if(!e) e = window.event;
		
		if( document.removeEventListener) {
			document.removeEventListener( "mousemove", moveHandler, true );
			document.removeEventListener( "mouseup", upHandler, true );
		}
		else if( document.detachEvent ) {
			elementToDrag.detachEvent( "onclosecapture", upHandler );
			elementToDrag.detachEvent( "onmouseup", upHandler );
			elementToDrag.detachEvent( "onmousemove", moveHandler );
			elementToDrag.releaseCapture();
		}
		else {
			document.onmouseup = olduphandler;
			document.onmousemove = oldmovehandler;
		}
		
		if(e.stopPropagation) e.stopPropagation(); 
		else e.cancelBubble = true;
	}
}
