jQuery(function($) {
	/*
		Tabs
	*/
	$('ul.tabs a').click(function(event) {
		event.preventDefault();
		var curChildIndex = $(this).parent().prevAll().length + 1;
		$(this).parent().parent().children('.active').removeClass('active');
		$(this).parent().addClass('active');
		$('#content').find('.tab-container').eq(0).children('.active').show('fast', function() {
			$(this).parent().children('div:nth-child(' + curChildIndex + ')').hide('fast', function() {
				$(this).addClass('active');
			});
			$(this).removeClass('active');
		});
		return false;
	});
	
	$('#write-a-review').click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		$('#reviews-tab-handle').click();
	});
	
	// ==============
	// = Hero block =
	// ==============
	var current = 0,
		total = $('#hero div').length,
		colorClass = '',
		timer;
		
	if (total < 2) {
		return;
	}

	// Bulding menu: 
	// 
	// 	<ul id="hero-navigation" class="dark">
	// 		<li>&bull;</li>
	// 		<li class="current">&bull;</li>
	// 		<li>&bull;</li>
	// 		<li>&bull;</li>
	// 	</ul>
	
	var nav = $('<ul id="hero-navigation"></ul>').appendTo($('#hero'));
	
	for (var i=0; i < total; i++) {
		var el = $('<li>&bull;</li>');
		
		if (i == current) {
			el.addClass('current');
		}
		
		el.data('slide-num',i).appendTo(nav).bind('click',function() {
			var num = $(this).data('slide-num');
			
			if (current !== num) {
				switchTo(num);
			}
		});
	}
	
	$('<a id="hero-prev-slide">Previous</a>').insertAfter($('#hero-container')).bind('click',switchPrev);
	$('<a id="hero-next-slide">Next</a>').insertAfter($('#hero-container')).bind('click',switchNext);
	
	function switchTo(next) {
		if (timer) {
			clearTimeout(timer);
		}
		
		if (next == current) {
			timer = setTimeout(switchNext,8000);
			return;
		}
		
		var currenImg = $('#hero div>*').eq(current),
			nextImg = $('#hero div>*').eq(next),
			transition = 'appear',
			// transition = nextImg.parent('div').hasClass('appear') ? 'appear' : 'slide',
			offset = current < next ? -916 : 916,
			diff = current - next;
			
		if ((current == total - 1) && next == 0 || (next == total - 1) && current == 0) {
			offset*= -1;
		}
		
		
		currenImg.delay(20).animate(transition == 'appear' ? {opacity: 0} : {left: offset}, 1000, function() {
			$(this).parent('div').css('display','none');
		});
		$('#hero-navigation li').eq(current).removeClass('current');
		
		current = next;
		
		$('#hero').removeClass(colorClass);
		
		colorClass = $('#hero div>*').eq(current).css(transition == 'appear' ? {opacity: 0, left: 0} : {opacity: 1, left: -(offset)})
			.delay(10)
			.animate(transition == 'appear' ? {opacity: 1} : {left: 0},1000)
			.parent('div').css('display','block').attr('class').replace('slide','');
		
		$('#hero').addClass(colorClass);
		
		// $('#hero div').eq(current).css('display','block');
		$('#hero-navigation li').eq(current).addClass('current');
		timer = setTimeout(switchNext,6000);
	}
	
	function switchNext() {
		var next = current + 1;
		
		if (next >= total) {
			next = 0;
		}
		
		switchTo(next);
	}
	
	function switchPrev() {
		var prev = current - 1;
		
		if (prev < 0) {
			prev = total - 1;
		}
		
		switchTo(prev);
	}
	
	switchTo(current);
});

	// ==============
	// = main menu drop down =
	// ==============

    if ($('#menu-spa-furnishings').length > 0) {
        var itemHovered = false,
            ddHovered = false,
            position = $('#menu-spa-furnishings').position();
        
        $('#dropdown').css({
            top: position.top + 26,
            left: position.left + 17,
            display: 'block'
        }).hide();
        $('#our-range').hover(function(e) {
            itemHovered = true;
            showDD();
        },function(e) {
            itemHovered = false;
            setTimeout(hideDD, 200);
        });
        $('#dropdown').hover(function(e) {
            ddHovered = true;
        },function(e) {
            ddHovered = false;
            setTimeout(hideDD, 200);
        });
        function showDD() {
            $('#dropdown').show(300);
        };
        function hideDD() {
            if (!ddHovered && !itemHovered) {
                $('#dropdown').hide(300);
            }
        };
    }
