$(document).ready(function() {
	
	jQuery.easing.def = "easeInOutQuad";
	
	$('body').addClass('js');
	$('body').append('<div class="junk"></div>');
	
	Cufon.replace('h1, h2, h3', { fontFamily: 'Leitura Display Swashes'});
	Cufon.replace('.menu a, .sub a, p.intro', { fontFamily: 'Lucida Grande', hover: true });
	
	DD_roundies.addRule(".panel", "8px");
	DD_roundies.addRule(".menu a, .product", "8px");
	DD_roundies.addRule("a.button", "8px");
	DD_roundies.addRule(".testimonial blockquote", "8px 8px 0px 0px");
	//DD_roundies.addRule("input.text", "8px");
	//DD_roundies.addRule(".sub", "0px 0px 8px 8px");
	
	//DD_roundies causes some issues with hovers
	// add and remove classes and refresh cufon elements
	$(".menu li, .sub li").hover(
		function () {
			$(this).addClass('over');
			Cufon.refresh(this);
		}, 
		function () {
			$(this).removeClass('over');
			Cufon.refresh(this);
		}
	);
	
	$(".menu li, .sub li").click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});

// ------------------ Commented out due to interfering with Facebook like button iframe ------------------------------------------ //
//	
//	if(location.hash)
//	{
//		var sURL = 'https://online.ebos.co.nz/sub/default.cfm?site_id=1&action=product&product_code=' + location.hash.substring(1);
//		$('iframe').attr('src', sURL);
//	}

	$('.vimeo-links a').click(function (e) {
		e.preventDefault();
		$('.vimeo-links .active').removeClass('active');
		$(this).addClass('active');
		$('.vimeo-videos .active').removeClass('active');
		//$($(this).attr('href')).show();
		$($(this).attr('href')).addClass('active');
	});
	
	$.fn.delay = function(time, callback) {
		// Empty function:
		jQuery.fx.step.delay = function() { };
		// Return meaningless animation, (will be added to queue)
		return this.animate({ delay: 1 }, time, callback);
	}
		
	$('.panel div').append('<a class="close">Close</a>');
	$('.product .specs').before('<a class="close">Close</a>');
	
	$('.panel .close').click(function (e) {
		$(this).parent().parent().fadeOut();
	});
	
	$('.product .close').click(function (e) {
		$(this).parent().parent().fadeOut();
		returnPottle();
	});
	
	$('.spot').click(function (e) {
		$(this).nextAll('.panel:first').fadeIn();
	});
	
	if($('.dot').length){$('.dot').blink({delay:2000});}
	
	var bFirstRun = true;
		
	if(location.hash)
	{
		$(location.hash).addClass('active');
	}
	
	$('.pottle').each(function (){
		if($(this).attr('href') == location.hash)
		{
			$(this).removeClass('blur');
			$(this).addClass('large active');
		}
	});
	
	if(location.hash){location.hash = '';}	

	var bAnimating = false;
	
	$('.pottle').click(function (e) {
		e.preventDefault();
		if(!bAnimating && !$(this).hasClass('.large'))
		{
			bAnimating = true;
			returnPottle();
			//location.hash = $(this).attr('href');
			var o = $(this).clone();
			$('.junk').append(o);
			$(o).removeClass('blur');
			$(o).addClass('large');		
			var h = $(o).css('height');
			var w = $(o).css('width');
			var b = $(o).css('bottom');
			var l = $(o).css('left');
			/*alert('o' +  o + 'h ' + h + ' w ' + w + ' b ' + b + ' l ' + l);*/
			$(this).animate({width:w, height:h, bottom:b, left:l}, 500, 'easeInOutQuad', callProductsIn);		
			$(this).addClass('active');
			$($(this).attr('href')).addClass('active');
			$($(this).attr('href')).fadeIn();
		}
	 });
	
	function returnPottle()
	{
		var o = $('.pottle.large').clone();
		$('.junk').append(o);
		$(o).addClass('blur');
		$(o).removeClass('large');		
		var h = $(o).css('height');
		var w = $(o).css('width');
		var b = $(o).css('bottom');
		var l = $(o).css('left');
		/*alert('o' +  o + 'h ' + h + ' w ' + w + ' b ' + b + ' l ' + l);*/
		$('.pottle.large').removeClass('active');
		$('.pottle.large').animate({width:w, height:h, bottom:b, left:l}, 500, 'easeInOutQuad', callProductsOut);
		$('.product.active').fadeOut();
		$('.product.active').removeClass('active')
	}
	
	function callProductsIn()
	{
		$(this).addClass('large');
		$(this).removeAttr('style');
		$(this).removeClass('blur');		
		$('.junk').empty();
		bAnimating = false;
		if(bFirstRun)
		{
			bFirstRun = false;
		}
	}

	function callProductsOut()
	{
		$('.pottle.large').addClass('blur');
		$('.pottle.large').removeAttr('style');
		$('.pottle.large').removeClass('large');		
		$('.junk').empty();
		bAnimating = false;
	}

	$('.tips.active .panel').show();

	$(".person").click(function (e) {
		e.preventDefault();
		if(!bAnimating && !$(this).hasClass('.large'))
		{
			bAnimating = true;
			$('.hint').fadeOut();
			
			$(this).addClass('active');
			
			$(this).find('.dot').hide();
			/*
			if(bFirstRun)
			{
				$(".person.default").not('.active').each(function (i) {
					var o = $(this).clone();
					$(o).removeClass('default');
					$(o).addClass('blur');
					$('.junk').append(o);
					var h = $(o).css('height');
					var w = $(o).css('width');
					var b = $(o).css('bottom');
					var l = $(o).css('left');
					$(this).animate({width:w, height:h, bottom:b, left:l}, 800, 'easeInOutQuad', callDefault);
					
				});
				
				bFirstRun = false;	
			} 
			*/
			$('.tips').removeClass('active');
			
			var o = $('.person.large').clone();
			$(o).removeClass('large');
			$(o).addClass('blur');
			$('.junk').append(o);
			var h = $(o).css('height');
			var w = $(o).css('width');
			var b = $(o).css('bottom');	
			var l = $(o).css('left');
			$('.person.large').animate({width:w, height:h, bottom:b, left:l}, 600, 'easeInOutQuad', callSmall);
			$('.person.default').removeClass('default').addClass('blur');
			var o = $(this).clone();
			$(o).removeClass('default');
			$(o).addClass('large');
			$('.junk').append(o);
			var h = $(o).css('height');
			var w = $(o).css('width');
			var b = $(o).css('bottom');
			var l = $(o).css('left');
			$(this).animate({width:w, height:h, bottom:b, left:l}, 300, 'easeInOutQuad', callLarge);
		}
    });
	
	
	function callDefault()
	{
		$(this).addClass('blur');
		$(this).removeAttr('style');
		$('.junk').empty();
		bAnimating = false;
	}
	
	function callLarge()
	{
		$($(this).attr('href')).addClass('active');
		$($(this).attr('href')).find('.panel').fadeIn();
		$(this).removeClass('blur').addClass('large');
		$(this).removeAttr('style');
		$('.junk').empty();
		if(bFirstRun)
		{
			bAnimating = false;
			bFirstRun = false;
		}
	}
	function callSmall()
	{
		$(this).removeClass('active');
		$(this).removeClass('large').addClass('blur');	
		$(this).removeAttr('style');
		$(this).find('.dot').show();
		$('.junk').empty();
		bAnimating = false;
	}
		
});
