/*

	Svane Isenkram
	JavaScript functions
	
	Author: Svane Isenkram / www.svaneisenkram.dk
	Copyright: 2011, Svane Isenkram. All rights resevered

-----------------------------------------------------------------------*/

if ( typeof Svane == 'undefined')
{
	var Svane = {};
}

Svane.domready = function (selector)
{
	new Svane.fixHoverButtons('div.formbutton');
	new Svane.fixTopnavHover('div.topnav li.product-menu');
	new Svane.enableImageZoom('div.single-product .image a, .content a.zoom');
	new Svane.enableProductSwitch('div.single-product div.choose');
	new Svane.productInit('.content .add-product');
	new Svane.productAddToBasket('.product-container .buttons a:not(.internal)');
	new Svane.removeDefaultInputValue('input.default-title');
	new Svane.carouselInit();
}

Svane.load = function ()
{
	
}

Svane.fixHoverButtons = function(selector)
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.hover(
		function()
		{
			$(this).addClass('hover');
		}, 
		function()
		{
			$(this).removeClass('hover');
		}
	);

}

Svane.fixTopnavHover = function(selector)
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.hover(
		function()
		{
			$(this)
				.find('a:first').addClass('hover').end()
				.find('div').addClass('hover');
		}, 
		function()
		{
			$(this)
				.find('a:first').removeClass('hover').end()
				.find('div').removeClass('hover');
		}
	);
}

Svane.enableImageZoom = function(selector)
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.fancybox({
		overlayShow: true,
		overlayOpacity: 0.4,
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		overlayColor: '#000'
	});
}

Svane.enableProductSwitch = function(selector)
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.children('input').change(function()
	{
		var id = $(this).attr('id');
		
		$selector
			.parent()
			.find('.price .' + $(this).attr('id'))
			.fadeIn()
			.siblings()
			.fadeOut();
		
		$('div.single-product .image-wrapper span.save')
			.find('.' + $(this).attr('id'))
			.fadeIn()
			.siblings()
			.fadeOut();
	});
}

Svane.productInit = function( selector )
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.closest('form').unbind('submit');
	$selector.closest('form').submit(function(){
		var amount = $('#produkt_antal').val(),
			id = $('#produkt_id').val(),
			size = $('input[name=produkt_storrelse]:checked').val();
		
		Svane.sendProductCall( $selector, id, amount, size );
		
		return false;
	});
}

Svane.productAddToBasket = function( selector )
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.each(function()
	{
		$(this).click(function(event)
		{
			event.preventDefault();
			
			var amount = 1,
				id = $(this).attr('href').split('+')[0],
				size = $(this).attr('size');
			
			Svane.sendProductCall( $(this), id, amount, size );
			
			return false;
		});
	});
}

Svane.sendProductCall = function( button, id, amount, size )
{
	if ( size )
		size = '&size=' + size;
	else
		size = '';
	
	$.ajax({
		type: 'GET',
		cache: false,
		url: '?ajax=true',
		data: 'id=' + id + '&amount=' + amount + size,
		dataType: 'html',
		success: function(result)
		{
			if (result != 'error')
			{
				$('.top .basket').replaceWith(result);
				var text = amount > 1 ? 'Dine varer er nu tilføjet til indkøbskurven' : 'Din vare er nu tilføjet til indkøbskurven';
				
				Svane.showBobble( button, id, text );
				
				$('.top .basket a')
					.append('<em class="animation"></em>')
					.find('em')
					.delay(400)
					.animate({
						top: 13
					}, 1500, 'easeOutQuad', function()
					{
						$(this).remove();
					});
			}
			else
			{
				Svane.showBobble( button, id, 'Der skete en fejl. Kontakt butikken' );
			}
		},
		error: function(result)
		{
			Svane.showBobble( button, id, 'Der skete en fejl. Kontakt butikken' );
		}
	});
}
	
Svane.showBobble = function( button, id, text )
{
	var $oldBobble = $('.main').find('div.bobble.id'+id);
	if ( $oldBobble.length )
	{
		$oldBobble.remove();
	}
	
	var position = button.position(),
		$parentRelative = button.offsetParent().append('<div class="bobble id'+id+'">'+text+'</div>');
	
	if ( $parentRelative.parents('.related').length || $parentRelative.hasClass('last') || ($.browser.msie && $parentRelative.hasClass('product')) )
	{
		var animation = [57, -175, 67, 77];
		$parentRelative.find('div.bobble.id'+id).addClass('bobble-revert');
	}
	else if ( $parentRelative.hasClass('product') )
		var animation = [57, 20, 67, 77];
	else
		var animation = [57, 175, 67, 77];
	
	if ( $.browser.msie )
	{
		$parentRelative.find('div.bobble.id'+id)
			.css({
				top: position.top - animation[0],
				left: position.left + animation[1]
			})
			.show()
			.animate({
				top: position.top - animation[2]
			}, 300)
			.delay(5000)
			.animate({
				top: position.top - animation[3]
			}, 300)
			.queue(function() {
				$(this).remove();
			});
	}
	else
	{
		$parentRelative.find('div.bobble.id'+id)
			.css({
				opacity: 0,
				top: position.top - animation[0],
				left: position.left + animation[1]
			})
			.show()
			.animate({
				opacity: 1,
				top: position.top - animation[2]
			}, 300)
			.delay(5000)
			.animate({
				opacity: 0,
				top: position.top - animation[3]
			}, 300)
			.queue(function() {
				$(this).remove();
			});
	}
}

Svane.carouselInit = function()
{
	jQuery('ul.jcarousel-skin').jcarousel({
		scroll: 4,
		animation: 650,
		easing: 'easeInOutCubic'
		//wrap: 'circular'
	});
}


Svane.removeDefaultInputValue = function( selector )
{
	var $selector = $(selector);
	if ( !$selector.length )
	{
		return;
	}
	
	$selector.each(function()
	{
		Svane.setRemoveAction( $(this) );
	})
}
Svane.setRemoveAction = function( $inputitem )
{
	$('form:not(.no-submit)').submit(function()
	{
		if ($inputitem.val() == $inputitem.attr("title"))
		{
			$inputitem.val("");
		}
		
		return true;
	});
	
	if ( $inputitem.val() == '' )
	{
		$inputitem.val($inputitem.attr("title"));
	}

	$inputitem
		.bind("focus", function(event)
		{
			if ($inputitem.val() == $inputitem.attr("title"))
			{
				$inputitem.val("");
			}
		})
		.bind("blur", function(event)
		{
			if ($inputitem.val() == "")
			{
				$inputitem.val($inputitem.attr("title"));
			}
		});
}

$(document).ready(Svane.domready);
$(window).load(Svane.load);
