var hideDelay = 10;
var hGap = -1;
$(document).ready(function()
	{ // when html is loaded
	setTall();
	$('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');
		if (typeof(height) == "undefined")
			return;
		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('hov');
					},
				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('hov');
						$('div#'+id).hide();
						}, hideDelay));
					});
			});
		});
	// 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!');
	//			});
	//	});

	// open gallery links on mouseover
	//$('.galcolapse div').click(function(e) { e.stopPropagation(); });
	//var galcolapse = $('.galcolapse');
	//$('.galcolapse').mouseover(function()
	//	{
  //	$(this).find('> div:hidden').slideDown(500);
	//	});
	});

// Flash like animation of face, breast, male, body, nose
$(function()
	{
	var facepost = $('#face-post');
	if ( ! facepost)
		return;
	var facepre = $('#face-pre');
	var breastpost = $('#breast-post');
	var breastpre = $('#breast-pre');
	var malepost = $('#male-post');
	var malepre = $('#male-pre');
	var bodypost = $('#body-post');
	var bodypre = $('#body-pre');
	var nosepost = $('#nose-post');
	var nosepre = $('#nose-pre');
	facepost.css({'opacity': 0.0});
	facepre.css({'opacity': 0.0});
	breastpost.css({'opacity': 0.0});
	breastpre.css({'opacity': 0.0});
	malepost.css({'opacity': 0.0});
	malepre.css({'opacity': 0.0});
	bodypost.css({'opacity': 0.0});
	bodypre.css({'opacity': 0.0});
	nosepost.css({'opacity': 0.0});
	nosepre.css({'opacity': 0.0});
	var animationControl = new Array(
		new Array(facepre,    facepost,     7, 1000),
		new Array(breastpre,  breastpost, 136,    0),
		new Array(malepre,    malepost,   265,    0),
		new Array(bodypre,    bodypost,   394,    0),
		new Array(nosepre,    nosepost,   523,    0),
		new Array(facepost,   facepre,      7, 1000),
		new Array(breastpost, breastpre,  136,    0),
		new Array(malepost,   malepre,    265,    0),
		new Array(bodypost,   bodypre,    394,    0),
		new Array(nosepost,   nosepre,    523,    0)
	);
	$(window).load(function() 
		{
		slideAnimation(animationControl, 0);
		});
	});
function slideAnimation(animationControl, i)
	{
	var imgArray = animationControl[i];
	var img = imgArray[0];
	var imgUnder = imgArray[1];
	var left = imgArray[2];
	var initLeft = left - 20;
	var delay = imgArray[3];
	imgUnder.removeClass('active');
	img
		.css({'opacity': 0.0, 'left': initLeft+'px'})
		.addClass('active')
		.delay(delay)
		.animate({'opacity': 1.0, 'left': left+'px'}, 800, function()
			{
			i++;
			if (i >= animationControl.length)
				i = 0;
			slideAnimation(animationControl, i);
			});
	}

// colapse gallery links
//$(window).load(function() 
//	{
//	if ( ! window.location.href.match(/gallery/i))
//		{
//		setTimeout(function()
//			{
//			$('.galcolapse div:visible').slideUp(2000);
//			}, 1000);
//		}
//	});

$.fn.loadBackgroundImage = function(url, x)
	{
	var t = this;
	var width = this.width();
	var height = this.height();
	var offset = this.offset();
	//create an img so the browser will download the image:
	$('<img />')
		.attr('src', url)
		.load(function()
			{ //attach onload to set background-image
			t.each(function()
				{
				var bg = $('<div></div>');
				bg.css({'position': 'absolute', 'left': offset.left+'px', 'top': offset.top+'px', 'width': width+'px', 'height': height+'px', 'z-index': '-1', 'display': 'none', 'background-color': '#ffffff', 'background-repeat': 'no-repeat', 'backgroundPosition': x+'px 100%'});
				bg.appendTo('body');
				bg.fadeOut('fast', function()
					{
					bg.css('backgroundImage', 'url('+url+')');
					bg.fadeIn(5000);
					});
 				});
			});
		return this;
	}

function setTall()
	{
	var otherOuterHeight = $('#topbar').outerHeight(true) + $('#header').outerHeight(true) + $('#footer').outerHeight(true);

	var greymain = $('#greymain');
	var greysidebarHeight = $('#greysidebar').outerHeight(true);
	if (greymain.height() < greysidebarHeight)
		greymain.height(greysidebarHeight);
	greymainHeight = greymain.outerHeight(true);

	var whitemain = $('#whitemain');
	var whitesidebarHeight = $('#whitesidebar').outerHeight(true);
	if (whitemain.height() < whitesidebarHeight)
		whitemain.height(whitesidebarHeight);
	//whitemainHeight = greymain.outerHeight(true);
	}

function showOrder()
	{
	window.name='origin';
	var w = window.open('','order',
		'width=500,height=400,resizable,scrollbars,status,top=10,left=10');
	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 contact = "click here"
  var email = "enquiry"
  var emailHost = "awaywegotours.com.au"
  document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost + ">'" + contact + "'</a>")
	}

