var timeouts = {
};

$(document).ready(function()
{
 
	if( typeof(DD_belatedPNG) !== 'undefined' )
	{
		DD_belatedPNG.fix('.browseArrow, .iePng');
	}
	fixHeightBox();

//	fixErrorBox();

		$('div.kailisOtherProductsScrollable').scrollable(
			{
				items: ".kailisOtherProductsItems",
				clickable: false,
				circular: true,
				speed:800,
				easing: "swing"
			}
			);
		var scrollable = $("div.kailisOtherProductsScrollable").data("scrollable");
		// use the event "onSeek", which fires every time you scroll an item
		//scrollable.onBeforeSeek(function(){});

	//$('#languageChooser li a').width(parseInt($('#languageChooser li a').width())).css('top', parseInt($('#languageChooser li a').height()));
	$('.drop_ul li.current a')
		.click(function(e)
			{
				e.preventDefault();
				id = $(this).parents('div.drop_ul').attr('id');
				
				if( $('#'+id+' li:hidden').length == 0 )
				{
					e.stopPropagation();
					//var id = "languageChooser";
					if( typeof(timeouts[id]) == 'undefined' )
					{
						timeouts[id] = {
							'open': null,
							'close': null
							};
					}
					if(	timeouts[id].close != null )
					{
						clearTimeout(timeouts[id].open);
					}

					$('#' + id).stop().clearQueue();

					$('#' + id + ' li:not(.current)').hide();
					$('#' + id + '').removeClass('selectActive');
				}
			});

	$('.drop_ul').mouseover(function()
		{
			var id = $(this).attr('id');
			if( typeof(timeouts[id]) == 'undefined' )
			{
				timeouts[id] = {
					'open': null,
					'close': null
					};
			}
			if(	timeouts[id].close != null )
			{
				clearTimeout(timeouts[id].close);
			}

			$(this).stop().clearQueue();
			if(id == 'ProductCollectionSelect') $('#select_p').hide();
			var cmd = "$('#" + id + "').addClass('selectActive'); $('#" + id + " li:not(.current)').show();";
			timeouts[id].open = setTimeout(cmd, 50);
		})
	.hover(
		function()
		{
			var id = $(this).attr('id');
			if( typeof(timeouts[id]) == 'undefined' )
			{
				timeouts[id] = {
					'open': null,
					'close': null
					};
			}
			if(	timeouts[id].close != null )
			{
				clearTimeout(timeouts[id].close);
			}

			$(this).stop().clearQueue();
		},
		function()
		{
			var id = $(this).attr('id');
			if( typeof(timeouts[id]) == 'undefined' )
			{
				timeouts[id] = {
					'open': null,
					'close': null
					};
			}
			if(	timeouts[id].close != null )
			{
				clearTimeout(timeouts[id].open);
			}

			$(this).stop().clearQueue();

			var cmd = "$('#" + id + " li:not(.current)').hide(); $('#" + id + "').removeClass('selectActive');";
			
			timeouts[id].close = setTimeout(cmd, 200);
			if(id == 'ProductCollectionSelect') $('#select_p').fadeIn('slow');
		});

	$('.kailisMainMenu > li').hover(
			function()
			{
				$(this).addClass('active');
				var images = $(this).find('a > img.level1Image');
				if( images.length > 0 )
				{
					var newSrc = images.attr('src').replace('.gif', '_rollover.gif');
					images.attr('src', newSrc);
				}
				var subNav = $(this).children('ul');
				if( subNav.length > 0 )
				{
					var edge = $(this).offset().left + subNav.width();
					if( edge > 1024 )
					{
						subNav.addClass('rightMenu');
					}

					var $link = $(this).children('a:first');
					var newTop = -10
						+ parseInt($link.height())
						+ parseInt($link.css('padding-top').replace('px', ''))
						+ parseInt($link.css('padding-bottom').replace('px', ''));
					var newLeft = 1;
					var newWidth = $link.width()
						+ parseInt($link.css('padding-left').replace('px', ''))
						+ parseInt($link.css('padding-right').replace('px', ''));

					$(this).children(".level1MenuBlocker")
						.height(22)
						.width(newWidth)
						.css({
							'top': newTop + "px",
							'left': newLeft + "px"
							})
						.show();
				}
			},
			function()
			{
				var images = $(this).find('a > img.level1Image');
				if( images.length > 0 )
				{
					var newSrc = images.attr('src').replace('_rollover.gif', '.gif');
					images.attr('src', newSrc);
				}
				$(this).removeClass('active');
				$(this).children(".level1MenuBlocker").hide();
			});

	$('ul.level2 > li, ul.level3 > li').hover(
			function()
			{
				$(this).addClass('active');
				$(this).children('li > a').addClass('active');
				$(this).children('ul > li > a').addClass('active');
				$(this).prev('li').children('a:first').addClass('hiddenBorder');
				var subNav = $(this).children('ul');
				if( subNav.length > 0 )
				{
					var $link = $(this).children('a:first');
					var newTop = 0;
					var newLeft = 44
						+ $link.width()
						+ parseInt($link.css('padding-left').replace('px', ''))
						+ parseInt($link.css('padding-right').replace('px', ''));
					var newHeight = 1 + parseInt($link.height())
						+ parseInt($link.css('padding-top').replace('px', ''))
						+ parseInt($link.css('padding-bottom').replace('px', ''));

					$(this).children(".level2MenuBlocker")
						.height(newHeight)
						.width(2)
						.css({
							'top': newTop + "px",
							'left': newLeft + "px"
							})
						.show();
				}
			},
			function()
			{
				$(this).removeClass('active');
				$(this).children('li > a').removeClass('active');
				$(this).prev('li').children('a:first').removeClass('hiddenBorder');
				$(this).children(".level2MenuBlocker").hide();
			});

/*	function fixErrorBox()
	{
		$('.element span.error').css(
		{
			position: 'relative',
			top: -18,
			marginBottom: -18
		}).click( function()
		{
			var obj = $(this);
			obj.prev().focus();
			obj.parents('.error').removeClass('error');
			obj.remove();
		});
		$('.element.textarea span.error').css(
		{
			position: 'relative',
			top: -100,
			height: 90,
			marginBottom: -90,
			backgroundColor: 'white'
		});
		$('li.error .element input, li.error .element textarea, td.error input').focus( function()
		{
			var obj = $(this);
			obj.parent().parent().removeClass('error');
			obj.parent().children('span.error').remove();
		});
	}*/

	function fixHeightBox()
	{
		var maxHeight = 0;

		$('div.equalHeightBox').each(function()
			{
				if( $(this).height() > maxHeight )
				{
					maxHeight = $(this).height();
				}
			});

		$('div.equalHeightBox').height(maxHeight);
		$('div.equalHeightBox.heroBox img.largeLogo').css(
			{
				bottom: 6,
				left: 6
			});
	}

	$('.scrollable').scrollable({
				items: ".items",
				circular: true
			});

	if( $('#collectionScrollable').length > 0 )
	{
		$('#collectionScrollable').data("scrollable").seekTo(2, 0);
	}
	if( $('#productScrollable').length > 0 )
	{
		$('#productScrollable').data("scrollable").seekTo(4, 0);
	}
	//this is the home page fader
	if( $('div.kailisHpHeroBox .hero').length > 0 )
	{
		var fadeSpeed = 1500;
		var cycleSpeed = 10000;
		var heroCurrent = 1;
		var heroInterval = null;

		var heroFader = function()
		{
			
			$('div.kailisHpHeroBox div.hero' + heroCurrent + ' .heroCopy').fadeOut(1000);
			$('div.kailisHpHeroBox div.hero' + heroCurrent).fadeOut(fadeSpeed);
			//$('div.kailisHpHeroBox div.thumbnail' + heroCurrent + ' .inactive').fadeIn(fadeSpeed);
			heroCurrent++;
		
			if( heroCurrent > 3 )
			{
				heroCurrent = 1;
			}
			$('div.kailisHpHeroBox div.hero' + heroCurrent + ' .heroCopy').fadeIn(1500);
			$('div.kailisHpHeroBox div.hero' + heroCurrent).fadeIn(fadeSpeed);
			//$('div.kailisHpHeroBox div.thumbnail' + heroCurrent + ' .inactive').fadeOut(fadeSpeed);

			heroInterval = setTimeout(heroFader, cycleSpeed);
		}
		$('div.kailisHpHeroBox div.hero' + heroCurrent).fadeIn(fadeSpeed);
		//$('div.kailisHpHeroBox div.thumbnail' + heroCurrent + ' .inactive').fadeOut(fadeSpeed);

		heroInterval = setTimeout(heroFader, cycleSpeed);

	}

	$('.boxset .shopJewelleryBox .copy').hover(
		function()
		{
			$(this).find('ul.level2').slideDown();
		},
		function()
		{
			$(this).find('ul.level2').slideUp();
		});

	$('.boxset .threeColumnBordered .copy').each(function(index)
		{
			var lineHeight = parseInt($(this).css('lineHeight').replace('px', ''));
			var newLineHeight = lineHeight;

			if( $(this).height() > lineHeight )
			{
				//newLineHeight = Math.floor( lineHeight / Math.ceil( $(this).height() / lineHeight ) );

				//$(this).css('lineHeight', newLineHeight + 'px');
			}
		});

	if( $('a[href=http://ultimategift.kailisjewellery.com.au/]').length > 0 )
	{
		var selectorOverlay = $('#ultimategiftOverlay');
		selectorOverlay.overlay({
			top: '40%',
			mask: {
				color: '#ffffff',
				loadSpeed: 200,
				opacity: 0.75
			},
			closeOnClick: true,
			load: false
		});

		$('a[href=http://ultimategift.kailisjewellery.com.au/]').click(
			function( e )
			{
				selectorOverlay.data('overlay').load();
				e.preventDefault();
			}
		);
	}

	$('.kailisFooter ul.right a').hover(
		function()
		{
			var img = $(this).find('img');

			var newSrc = img.attr('src').replace(/.gif/, '_over.gif');
			img.attr('src', newSrc);
		},
		function()
		{
			var img = $(this).find('img');

			var newSrc = img.attr('src').replace(/_over.gif/, '.gif');
			img.attr('src', newSrc);
		});
});



