var hideDelay = 10;
var hGap = -1;

$(document).ready(function()
	{ // when html is loaded
	//$('div.popmenu').popMenu();
	// 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;
				});
		});
	$('div.popmenu').popMenu();
	setTall();
	});

function slideSwitch()
	{
	var $active = $('#slideshow IMG.active');
	if ( $active.length == 0 )
		$active = $('#slideshow IMG:last');
	// use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');
	// uncomment the 3 lines below to pull the images in random order
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1500, function()
			{
			$active.removeClass('active last-active');
			});
	}
$(function()
	{
	setInterval( "slideSwitch()", 5000 );
	});

$(function()
	{
	$('a[href*=#]').click(function() 
		{
		//if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
		//	{
		if (this.hash.length == 0)
			{
			var targetOffset = 0;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
			}
		var $target = $(this.hash);
		$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		if ($target.length)
			{
			var targetOffset = $target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
			}
		//	}
		});
	});

(function($)
	{
	$.fn.popMenu = function()
		{
		return this.each(function()
			{
			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();
			//var w = menuEl.parent().width();
			var w = $(document).width();
			if (pos.left + self.outerWidth() > w)
				pos.left = w - self.outerWidth();
			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).unbind('mouseenter').unbind('mouseleave')
				$(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));
						});
				});
			});
		}
	})(jQuery);

function setTall()
	{
	var otherOuterHeight = $('#header').outerHeight(true) + $('#menu').outerHeight(true) + $('#banner').outerHeight(true) + $('#bottommenu').outerHeight(true) + $('#footer').outerHeight(true);

	var content = $('#content');
	var contentOuterHeight = content.outerHeight(true);
	var contentPadEtc = contentOuterHeight - content.height();

	var sidebar = $('#sidebar');
	var sidebarOuterHeight = sidebar.outerHeight(true);
	var sidebarPadEtc = sidebarOuterHeight - sidebar.height();

	var maxHeight = Math.max(contentOuterHeight, Math.max(sidebarOuterHeight, $(window).height()-otherOuterHeight));
	content.height(maxHeight - contentPadEtc);
	}

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 showPic()
  {
	var w = window.open('','gallery',
	    'width=400,height=400,resizable,scrollbars,status,left=0,top=0');
	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 = "info"
  var emailHost = "snowholidays.com.au"
  document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost + ">(Click here)</a>")
	}

