var hideDelay = 10;
var hGap = 0;
$(document).ready(function()
	{ // when html is loaded
	// setup compact form
	$('label.m00').css('display', 'none');
	$('input.m00').each(function()
		{
		var self = $(this);
		var lab = self.prev();
		var def = lab.text();
		if (lab.hasClass('req'))
			def = '*'+def;
		if (self.val() == def)
			self.addClass('inactive')
		self
			.data('default', def)
			.focus(function()
				{
				self.removeClass('inactive');
				if (self.val() == self.data('default') || '')
					self.val('');
				})
			.blur(function()
				{
				var default_val = self.data('default');
				if (self.val() == '')
					{
					self.addClass('inactive');
					self.val(self.data('default'));
					}
				})
			.closest('form').submit(function(event)
				{
				if (event.isDefaultPrevented())
					{
					if (self.val() == self.data('default'))
						alert('already failed so wont remove '+self.val());
					return false;
					}
				if (self.val() == self.data('default'))
					self.val('');
				return true;
				});
		});
	// form helpers and validators
		$('.datepicker').datepick({dateFormat: 'D d M yy'});
		$('.timeentry').timeEntry({spinnerImage: 'js/img/spinnerOrange.png', show24Hours: true});
		$('.integerentry').format({precision: 0,autofix:true});
		//$('.emailentry').each(function()
		//	{
		//	var self = $(this);
		//	var selfthis = this;
		//	self
		//		.format({type:"email"},function()
		//			{
		//			if (self.val()=='')
		//				alert('Please enter email!');
		//			else
		//				alert('Wrong Email format!');
		//			});
		//	});
	// extend all columns to full window height
	setTall();
	// drop down menu
	$('div.popmenu').each(function()
		{ // iterate dropdown menu divs
		var self = $(this);
		self.data('hiding', false); // init timeout id
		var id = self.attr('id');
		var menuEl = $('a.'+id); // menu link class = dropdown id
		// position under menu link; using offsetHeight because height() seems to give the link's non block height
		pos = menuEl.offset();
		height = menuEl.attr('offsetHeight');
		self.css({'left': (pos.left+hGap)+'px', 'top': (pos.top + height)+'px'});
		// apply hover (mouseover, mouseout) to both menu link and dropdown.
		$.each([self, menuEl], function()
			{
			$(this).hover(
				function()
					{
					if (self.data('hiding'))
						{
						clearTimeout(self.data('hiding'));
						self.data('hiding', false);
						}
					$('div#'+id).show();
					menuEl.addClass('current');
					},
				function()
					{ // delay hide so if the pointer passes between the menu and dropdown the hide can be cancelled
					self.data('hiding', setTimeout(function()
						{
						menuEl.removeClass('current');
						$('div#'+id).hide();
						}, hideDelay));
					});
			});
		});
	});

$(function()
	{
	$('.moreinfo').each(function()
		{
		var self = $(this);
		var lnk = $('<a class="minfo" href="#">More info</a>');
		lnk.click(function()
			{
		  self.slideToggle('slow', function()
				{
				if ($(this).is(":visible"))
					lnk.text('Hide info');
		    else
					lnk.text('More info');
		  	});
			return false;
			});
		var p = $('<p/>');
		p.append(lnk);
		p.insertAfter(self);
		self.hide();
		});
	});

function setTall()
	{
	var otherOuterHeight = $('#header').outerHeight(true) + $('#menu').outerHeight(true) + $('#main').outerHeight(true) - $('#main').outerHeight(true) + $('#footer').outerHeight(true) + 1;

	var content = $('#content');
	var contentOuterHeight = content.outerHeight(true);
	var contentPadEtc = contentOuterHeight - content.height();

	var leftbar = $('#leftbar');
	var leftbarOuterHeight = leftbar.outerHeight(true);
	var leftbarPadEtc = leftbarOuterHeight - leftbar.height();

	var submenu = $('#submenu');
	var submenuOuterHeight = submenu.outerHeight(true);
	var submenuPadEtc = submenuOuterHeight - submenu.height();

	var rightbar = $('#rightbar');
	var rightbarOuterHeight = rightbar.outerHeight(true);
	var rightbarPadEtc = rightbarOuterHeight - rightbar.height();

	var maxHeight = Math.max(contentOuterHeight, Math.max(leftbarOuterHeight, Math.max(rightbarOuterHeight, $(window).height()-otherOuterHeight)));
	content.height(maxHeight - contentPadEtc);
	rightbar.height(maxHeight - rightbarPadEtc);
	submenu.height(maxHeight + submenuOuterHeight - leftbarOuterHeight - submenuPadEtc);
	}

function showOrder()
	{
	window.name='origin';
	var w = window.open('','order',
		'width=600,height=600,resizable,scrollbars,status,top=0,left=0');
	w.document.clear();
	w.focus();
	return true;
	}
function showProduct(pic)
	{
	var w = window.open('', 'product', 'width=350,height=400,resizable,scrollbars,top=20,left=30');
	var d = w.document;
	d.write('<html><head><title>Simplycrafts close up</title></head><body bgcolor="white" leftmargin="6" topmargin="6">');
	d.write('<center><table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td align="center">');
	d.write('<img src="/products/largeimages/' + pic + '">');
	d.write('</td></tr><tr><td align="center"><form>');
	d.write('<input type="button" value="Close this window" onClick="window.close();">');
	d.write('</form></td></tr></table></center></body></html>');
	d.close();
	w.focus();
	return false;
	}
function showWin()
  {
  var w = window.open('','popup',
      'width=500,height=400,resizable,scrollbars,status,left=80,top=80');
  w.document.clear();
  w.focus();
  return true;
  }
function showSearch()
	{
	window.name='origin';
	var w = window.open('','search',
		'width=250,height=550,resizable,scrollbars,status,top=0,left=0');
	w.document.clear();
	w.focus();
	return true;
	}

function mailIt()
	{
  var email = "enquiries"
  var emailHost = "simmark.com.au"
  document.write("(<a href=" + "mail" + "to:" + email + "@" + emailHost + ">click here</a>)")
	}

addDOMLoadEvent = (function()
	{
	// create event function stack
	var load_events = [],
		load_timer,
		script,
		done,
		exec,
		old_onload,
		init = function ()
			{
			done = true;
			// kill the timer
			clearInterval(load_timer);
			// execute each function in the stack in the order they were added
			while (exec = load_events.shift())
				exec();
			if (script)
				script.onreadystatechange = '';
			};
	return function (func)
		{
		// if the init function was already ran, just run this function now and stop
		if (done)
			return func();
		if (!load_events[0])
			{
			// for Mozilla/Opera9
			if (document.addEventListener)
				document.addEventListener("DOMContentLoaded", init, false);
			// for Internet Explorer
			/*@cc_on @*/
			/*@if (@_win32)
			document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
			script = document.getElementById("__ie_onload");
			script.onreadystatechange = function()
				{
				if (this.readyState == "complete")
					init(); // call the onload handler
				};
			/*@end @*/
			// for Safari
			if (/WebKit/i.test(navigator.userAgent))
				{ // sniff
				load_timer = setInterval(function()
					{
					if (/loaded|complete/.test(document.readyState))
						init(); // call the onload handler
					}, 10);
				}
			// for other browsers set the window.onload, but also execute the old window.onload
			old_onload = window.onload;
			window.onload = function()
				{
				init();
				if (old_onload)
					old_onload();
				};
			}

		load_events.push(func);
		}
	})();

