function cartChange(obj){
	
	var price = $(this).parent().next().text();
	price = price.substring(1, price.length);
	
	if (parseInt(price))
	{
		$(this).parent().next().next().text('$' + (price*$(this).val()));
	}
	
	var total = 0;
	
	$('.cartQuantity').each(function(){
		
		
		totalVal = $(this).parent().next().next().text();
		totalVal = totalVal.substring(1, totalVal.length);
		
		
		if (parseInt(totalVal))
		{
			total += parseInt(totalVal);
		}
		
	});
	
	$('#catalogCartTotal').text('$'+total);
	
}

$(document).ready(function(){
	
	$('#contactForm').submit(function(){
		
		var doSubmit = true;
		
		if ($('#contactName').val() == '')
		{
			doSubmit = false;	
		}
		if ($('#contactEmail').val() == '')
		{
			doSubmit = false;	
		}
		if ($('#contactPhone').val() == '')
		{
			doSubmit = false;	
		}
		
		if (doSubmit)
		{
			return true;
		}
		else
		{
			alert('Please fill in all required fields');
			return false;
		}
		
	});

	var activeMenu = $('#columnLeftInner li.active');

	var products = new Array();
   
	// We call this function when the page loads, and pass in the body
	// We call this function when ajax loads and pass in the target
	function Init(obj)
	{
		
		var slideshowAutoClick = false;
		
		if ($(obj+' .jsSwitchActive li').length > 0)
		{
		
		slideshowInt = setInterval(function(){
		
			//$(obj+ ' .slideshowNavigatorRight').click();
			direction = -1;
			
			$('.jsSwitchActive li').each(function(i){
				
				if ($(this).hasClass('active'))
				{
					var goto = i ;
					
					// GO to tlast
					if (i == 0 && direction == 1)
					{
						goto = $('.jsSwitchActive li').length-1
					} else					
					if (i == $('.jsSwitchActive li').length-1 && direction == -1)
					{
						goto = 0;
					} else					
					// Go to beginning
					{
						goto -= direction;	
					}
					
					
					$('.jsSwitchActive li a').each(function(i){
						
						if (i == goto)
						{
							slideshowAutoClick = true;
							$(this).click();	
							slideshowAutoClick = false;
						}
						
					});
					
					return false;
				}
				
				
			});
			
		}, 10000);
		
		}
		
		$(obj+ ' .closeModal').click(function(){
			$('#overlay').click();	
		});
							

		
		$(obj+ ' .cartQuantity').change(cartChange).keyup(cartChange);	 		
	
		
		$(obj+ ' .slideshowNavigatorLeft, '+obj+ ' .slideshowNavigatorRight').click(function(){
			
			if (slideshowInt)
			{
				clearInterval(slideshowInt);	
			}
			
			var direction = 1;
			
			if ($(this).hasClass('slideshowNavigatorRight'))
			{
				direction = -1;
			}
			
			$('.jsSwitchActive li').each(function(i){
				
				if ($(this).hasClass('active'))
				{
					var goto = i ;
					
					// GO to tlast
					if (i == 0 && direction == 1)
					{
						goto = $('.jsSwitchActive li').length-1
					} else					
					if (i == $('.jsSwitchActive li').length-1 && direction == -1)
					{
						goto = 0;
					} else					
					// Go to beginning
					{
						goto -= direction;	
					}
					
					
					$('.jsSwitchActive li a').each(function(i){
						
						if (i == goto)
						{
							$(this).click();	
						}
						
					});
					
					return false;
				}
				
				
			});
			
		});
		
		$(obj+' .slideshowShowMore').click(function(){
			//$(this).parent().css('display', 'none');
					
			$(this).parent().hide();	
			$(this).parent().next().hide();
			$(this).parent().next().next().show();
			
			var height = 319 - $('#slideshowAjaxContent').height();
			
			
			
			$(this).parent().parent().parent().animate({
				top: height+'px'	
			});
			
			return false;

		});
	
		$(obj+' .slideshowNavigation').click(function(){
			
			var href = $(this).attr('href');
			
			if (slideshowInt && !slideshowAutoClick)
			{
				clearInterval(slideshowInt);	
			}
			
			$('#slideshowAjaxContent').animate({top: '280px'});
			
			if ($('#overlay3').length < 1)
			{	
				// Load the href
				$('#slideshowAjaxBackground').prepend('<div id="overlay3"></div>');
				
				// Add the overlay
				$('#overlay3').css({
					width: '982px',
					height: '336px',
					zIndex: '1',
					position: 'absolute',
					display: 'none',
					opacity: '0.8'
				});	
				
				$('#overlay3').fadeIn(function(){
					
					$.ajax({
						url: href+'&render=component&tpl=slideshow',
						success: function(data) {
							
							$('body').prepend('<div id="loader" style="display:none;"></div>');
							$('#loader').append(data);	
							$('#loader').find('img').load(function(response){
														
								$('#slideshowAjaxBackground').css('background','url('+$('#loader').find('img').attr('src')+')');
								$('#loader').find('img').remove();
								$('#slideshowAjaxContent').html($('#loader').html());
								$('#loader').remove();
								$('#overlay3').fadeOut(function(){
									$('#overlay3').remove();
								});
								
								Init('#slideshowAjaxContent');
								
							}).error(function(){
							
								//$('#slideshowAjaxBackground').css('background','url('+$('#loader').find('img').attr('src')+')');
								$('#loader').find('img').remove();
								$('#slideshowAjaxContent').html($('#loader').html());
								$('#loader').remove();
								$('#overlay3').fadeOut(function(){
									$('#overlay3').remove();
								});
								
							});				
						}
					});			
					
				});
				
			}
			
			
			
			

		});
		
		$(obj+ ' #slide').click(function(){
			
			/*
			$('#slide').animate({
				marginLeft: '-=982px'	
			}, 1200);
			*/
			
			
			// Load the next data,
			
			
			// Load the image
			
			// Insert
			
		});	 
			 
			 
			 
		$(obj+ ' #primaryNavigation li.active .part1').addClass('active-hover');
		
		var navTimeout;
		
		$(obj+ ' #primaryNavigation a').mouseover(function(){
			clearTimeout(navTimeout);
			$(obj+ ' #primaryNavigation .part1').removeClass('active-hover');
			$(this).find('.part1').addClass('active-hover');
		}).mouseout(function(){
			
			navTimeout = setTimeout(function(){
				$(obj+ ' #primaryNavigation .part1').removeClass('active-hover');
				$(obj+ ' #primaryNavigation li.active .part1').addClass('active-hover');
			}, 300);
			
		});
		
		$(obj+ ' .jsSwitchActive a').click(function(){
			$(obj+ ' .jsSwitchActive li').removeClass('active');
			
			$(this).parent().addClass('active');
			
			return false;	
		});
	
		$(obj+ ' .product-image-large').click(function(){
			return false;	
		});
	
		$(obj+ ' .product-image-small').click(function(){
			
			var itemClickHref 	= $(this).attr('href');				
			var ItemClickSrc 	= $(this).find('img').attr('src');	
			
			var itemLargeHref 	= $(obj).find('.product-image-large').attr('href');
			var itemLargeSrc 	= $(obj).find('.product-image-large img').attr('src');	
			
			
			$(this).find('img').attr('src', itemLargeHref);
			$(this).attr('href', itemLargeSrc);
			
			$(obj).find('.product-image-large img').attr('src', itemClickHref);
			$(obj).find('.product-image-large').attr('href', ItemClickSrc);
			
			return false;
			
		});
	
		
		$(obj+' .colorSelector').click(function(){
			
			var target 	= $(this).attr('target');
			var id		= $(this).attr('title');
			
			
			$('.'+target+'a a').removeClass('colorSelectorActive');
			
			$(this).addClass('colorSelectorActive');
			
			$('#'+target).val(id);
			
			return false;
			
		});
		
		$(obj+' .product-button-add').click(function(){
									
			var pId 		= $(this).next().next().val();
			var quantity 	= $(this).next().val();
			var price 		= $(this).next().next().next().val();
			var code		= $(this).next().next().next().next();
			var obj 		= this;
			
			if (!code.val() && !code.hasClass('si0'))
			{
				alert('Please select a "colour" or "size", change quantity (if required) and select "Add"');
				return false;
			}
			
			
			$(this).prev().prev().css('display', 'block');
						
			$.ajax({
				type: "POST",
				url: 'index.php?component=catalog&action=add_cart',
				data: "productId="+pId+"&quantity="+quantity+"&productCode="+code.val(),
				success: function(data) {
				
					$(obj).prev().prev().css('display', 'none');
					$(obj).prev().css('display', 'block');
					$(obj).prev().fadeOut('slow');
					$('.productQuantity'+pId).text('Item(s): '+data+'');
					$('.productQuantity'+pId).css('display', 'block');
					$('.productQuantityInput'+pId).val(data);
					$('.productQuantityInput'+pId).change();
				}
			});			
			/*
			if (products[pId])
			{}
			else
			{
				products[pId] = { count: 0, price: 0 };	
			}
			
			products[pId].count += parseInt(quantity);
			products[pId].price = parseInt(price);
			
			var cartTotal = 0;
			var itemTotal = 0;
			
			for(var i in products)
			{
				cartTotal += products[i].count * products[i].price;
				itemTotal += products[i].count;		
			}
			
			$('#cartTotal').text(cartTotal);
			$('#cartGst').text(cartTotal/10);
			$('#cartItems').text(itemTotal);
			*/
	
		});
	
	
		$(obj+' .modal').click(function(){
						
			if ($('#modal').length > 0)
			{
			}
			else
			{
			
				$('body').prepend('<div id="modal"></div>');
				$('#modal').before('<div id="overlay"></div>');
				$('#modal').css({
					zIndex: '10001',
					position: 'absolute',
					display: 'block'
				});	
				$('#modal').center();
			
			}
			
			
			var width = $(document).width();
			var height = $(document).height();

			// Add the overlay
			$('#overlay').css({
				width: width+'px',
				height: height+'px',
				zIndex: '10000',
				position: 'absolute',
				backgroundColor: 'black',
				left:'0px',
				top:'0px',
				display: 'block',
				opacity: '0.8'
			});	
							
			$('#modal').load($(this).attr('href')+'&render=component', function(response, status, xhr){
					
				if (status == 'error')
				{
					location.replace(href);						
				}
				else
				{
					// Re Init the component area
					Init('#modal');
					
				}
				
			});
			
			
			$('#overlay').click(function(){
				$('#modal').remove();
				$(this).remove();
			});
			
			
			return false;
			
		});
	
			
		// If we click an object that targets the component area
		$(obj+' .ajax').click(function(){
			
			//$('#columnLeftInner li').removeClass('active');
			var target;
			
			target = '#columnCenterInner';
			
			if ($(this).attr('target'))
			{
				target = $(this).attr('target');	
			}
			
			
			// Get the width and height of the current component area
			var width = $(target).width();
			var height = $(target).height();
			//var target = $(this).attr('target');
			var href = $(this).attr('href');
			
			var clickObj = this;
			var removeOverlay = false;
					
			if ($('#overlay2').length < 1)
			{	

				// Load the href
				$(target).prepend('<div id="overlay2"></div>');
				
				// Add the overlay
				$('#overlay2').css({
					width: width+'px',
					height: height+'px',
					zIndex: '10001',
					position: 'absolute',
					display: 'block',
					opacity: '0.8'
				});	
				
				// Fade in the overlay
				$('#overlay2').fadeIn();
				
				// 
				removeOverlay = true;

			}
			
			
			//$('#overlay').animate({ opacity: 1 });
			
			
			// Load the component into the target area
			$(target).load(href+'&render=component', function(response, status, xhr){
					
				if (status == 'error')
				{
					
					location.replace(href);						

				}
				else
				{
					
					// Hide the new stuff
					$(target).css('display', 'none');
					
					// Fade it in
					//$(target).fadeIn();
					$(target).css('display', 'block');
					
					// Re Init the component area
					Init(target);
					
					if (removeOverlay)
					{
						$('#overlay2').remove();
					}
				
				}
				
			});
			
			return false;
		});
		
		
		$(obj+' .product-bottom').mouseover(function(){
			
			$(this).addClass('product-full');
			$(this).find('.hidden').show();
			
		}).mouseout(function(){
			
			$(this).removeClass('product-full');
			$(this).find('.hidden').hide();
		
		});
		
		
		// Init any ajax href's
		$(obj+' .ajaxHref').confirm({
			'ajaxHref':true,
			'width':'auto'
		});
		
		$(obj+' li a.level1').click(function(){
		
			if ($(activeMenu).length > 0 && $(activeMenu)[0] != $(this).parent()[0])
			{
				$(activeMenu).removeClass('active');
				
				$(activeMenu).find('ul').animate({ height: 'hide' });
			}
							
			obj = $(this).parent();
			
			activeMenu = obj;
							
			$(obj).addClass('active');
			
			
			$(obj).find('ul').animate({ height: 'show' }, function(){
				
				if ($(this).is(':visible') != true)
				{
					//$(obj).removeClass('active');	
				}
					
			}); 
			
			if ($(obj).find('ul').length > 0)
			{
				return false;
			}
			else
			{
				return false;
			}
							  
		});
		
	}
	
	Init('body');
	
});

