$(document).ready(function(){
	
	
	debug('CFG doc ready');
	
	
	if($('#cfg2').size() > 0){
		
		setupCFG2();
		
	};
	
});


function setupCFG2(){
	debug('setupCFG2');

	setTimeout("$('div.configurationOptions').show()", 1000);
	
	// Product row clickable
	$('#cfg2 div.category div.product').unbind().bind('click', function(){
		$(this).find('a').click();
		
		return false;
		
	});
	
	// IE Alignment patch
	$('#cfg2 a.select,#cfg2 a.selected').css('float','right');
	

	// Select a product
	$('#cfg2 div.category div.product').find('a').unbind().bind('click',function(){
		
		$(this).html('Updating');
		$('#cfg2').load(this.href,setupCFG2);
		return false;

	});

 	// Unbind alternatives
	$('#cfg2 div.category div.alternative').unbind().bind('click', function(){
		window.location = $(this).find('a').attr('href');
	});
	
	// Remove selectedInit class on mouseover of buttons
	$('#cfg2 div.category div.product a.selected').bind('mouseover',function(){
		
		$(this).html('Remove');
		
		$(this).parents('.product').find('.hidden').show();

		$(this).removeClass('selectedInit');
		
	}).bind('mouseout', function(){
		
		if ($(this).html() !== 'Updating'){
			
			$(this).html('Selected');

			$(this).parents('.product').find('.hidden').hide();55
		}
		
	});
	
	// Toggle groups from choose page / hide them first
	$('.choose .group').css('cursor', 'pointer').unbind().bind('click', function(){
		
		var group = $(this);
		
		// Close the other groups
		if (group.attr('class') != 'group active') {

			$('.choose .active').removeClass('active');
			$('.choose .options').slideUp({
				duration: 600,
				easing: 'easeInOutCirc'
			});			
			
		};
			
		// Slide down defaults
		if (group.next('.options').find('.option').size() > 0) {
		
			group.next('.options').slideToggle({
				duration: 800,
				easing: 'easeInOutCirc'
			});
			
			group.toggleClass('active');
		
		// Load group
		} else {
			
			window.location = group.find('.name a').attr('href');
			
		};
		
		return false;
		

	});

	


	// Add config to cart
	$('#cfg2Top a:contains("Add to cart")').unbind().bind('click', function(){
		
		$.get(this.href, function(){
			$.scrollTo('#top',{ duration: 400, easing: 'easeInOutCirc', onAfter: function(){
				speechBox('#minicart div.buttons a.medium:last', 'Thank you, click here to check out', 4500);
				refreshMiniCart();
				setTimeout('cfgOptionsMove(true)', 1000);
				setTimeout('cfgOptionsMove(true)', 2000);
				setTimeout('cfgOptionsMove(true)', 4000);
			}});
		});
		
		return false;
			
			 
	});	
	
	$('div.configurationOptions a:contains("Add to cart")').unbind().bind('click', function(){
		
		var qty = $(this).parents('span.button').find('select[name=qty]').val();
		var url = $(this).attr('href') + '&qty=' + qty;
		
		debug(url);
		
		$.get(url, function(){
			$.scrollTo('#top',{ duration: 400, easing: 'easeInOutCirc', onAfter: function(){
				speechBox('#minicart div.buttons a.medium:last', 'Thank you, click here to check out', 4500);
				refreshMiniCart();
				setTimeout('cfgOptionsMove(true)', 1000);
				setTimeout('cfgOptionsMove(true)', 2000);
				setTimeout('cfgOptionsMove(true)', 4000);
			}});
		});
		
		return false;
			
	});	

	// User clicks email me / Hide the box first
	$('div#emailMe').hide();
	$('div#main a:contains("Email me")').unbind().bind('click', function(){
		
		var emailButton 	= $('div#main a:contains("Email me")');
		var emailDiv 		= $('div#emailMe');
	
		function showEmailMe(){
			emailDiv.show(function(){
				$.scrollTo($('div#footer'),	{
					duration: 400,
					easing: 'easeInOutCirc',
					onAfter: function(){
						pulse(emailDiv);
						emailButton.hide();
					}
				});
			});
		}

		function initEmailMe(){
			
			emailDiv.find('button').hide();
			emailDiv.find('a').show();
			
			emailDiv.find('a').unbind().bind('click', function(){
				
				var button = $(this);
				var input = button.parent().find('input');

				if(button.html() == 'Cancel'){
					emailDiv.fadeOut(500);
					emailButton.show();
				}

				if(button.html() == 'Send'){
					button.html('Sending');
					emailDiv.find('span.response').load(emailDiv.find('form').attr('action'), {to: input.val()}, function(){
						initEmailMe();
					});
				}
				
				return false;
				
			});

			return false;

		}
		
		emailButton.show();
		showEmailMe();
		initEmailMe();		
		
		return false;
		
	});	
	
	
	// CFG Box	
	var configurationOptionsTop = $('#right div.configurationOptions').css('top');	
	if($('#cfg2 div.category').length > 0){
		$('#right .promo').hide();
	}
	
	
	if($('#cfg2 div.configurationOptions').size()){
		var cfgOpts = $('#cfg2 div.configurationOptions');

		// CFG Box	
		var configurationOptionsTop = $('#right div.configurationOptions').css('top');	
		if($('#cfg2 div.category').length > 0){
			$('#right .promo').hide();
		}

		$('#right div.configurationOptions').remove();
		$('#right').append($('#cfg2 div.configurationOptions'));
		
		
		if($('#right').find('div.configurationOptions').size()){
			$('#right').find('div.configurationOptions').css('top', configurationOptionsTop);
			$('#right').find('div.configurationOptions').show();
		}
	};
	

	// Update quantity in sliding box
	$('div.configurationOptions select').unbind().bind('change', function(){
		
		container 	= $('div.configurationOptions');
		select 		= $(this);
		price 			= container.find('.price input[name=price]').val();
		qty 			= select.val();
		
		container.find('.exVAT').html('&pound;' + roundDecimal(price * qty) + '<small>ex.</small>');
		container.find('.incVAT').html('&pound;' + roundDecimal((price * qty) * 1.15) + '<small>inc.</small>');

	});

	
	// Toggle more info boxes
	$('#cfg2 div.category a.moreInfo').unbind().bind('click', function(){
		
		var category 		= $(this).parents('div.category').find('.categoryName').text();
		var categoryID 	= $(this).parents('div.category').attr('id');
		
		if($(this).parents('div.category').find('div.information .more').css('display') == 'none'){
			
			$(this).find('img.arrow').attr('src', '/_img/configurator/arrow_down.gif');
			$.get('?showInfo=' + categoryID);
			
		}else{
			
			$(this).find('img.arrow').attr('src', '/_img/configurator/arrow.gif');
			$.get('?hideInfo=' + categoryID);
			
		}
		
		$(this).parents('div.category').find('div.information .more').slideToggle(300);
		
		return false;
		
	});
	
	// Setup scroll
	$(window).unbind().bind('scroll', function(){
		cfgOptionsMove();
	}); 	
	
};


function setupCFG2AddToCart(url){
	if (url) {
		$('div.productDetail div.price a.large:contains("Add to cart")').attr('href', url);
	}
	
};

function cfgOptionsMove(ignore){
	
	$('#minicart a:contains("View Cart")').bind('click', function(){
		$('div.configurationOptions').hide();
		setTimeout('bindCloseCart()', 1000);
	});
	
	var target 	= $('div.configurationOptions');
	var top 		= $(window).scrollTop();
	var min 		= $('div.topbg').height() + $('#right div.minicart').height() + 72;// + 293 + 150;
	
	// If it's 0 then set it instead of animate it (stops the jump in Safari)
	if (top < (min / 2)) {
	
		target.css('top', min + 3);

	// Too far UP!
	} else if (target.height() > $(window).height()){
		
		botPos = (top - ((target.scrollTop() + target.height())- ($(window).height() - target.scrollTop()))) - 5;
		
		target.stop().animate({top: botPos}, 500)
		
	// Too far down
	} else if (top > (($('#host').height() - target.height()) - 30)) {
		
		// STOP
		
	}else{
		
		if(top > min && ignore != true){
			configurationOptionsTop = top + 3;
			target.stop().animate({top: (top + 3)}, 500);
		}else{
			configurationOptionsTop = min + 3;
			target.stop().animate({top: min + 3}, 500);
		};	

	};	
		
};

function bindCloseCart(){
	$('#cart a:contains("Close Cart")').bind('click', function(){
		setTimeout("$('div.configurationOptions').show()", 800);
		setTimeout('configurationOptionsMove(true)', 1000);
	});
};


function roundDecimal(value){
	
	rounded = (Math.round(value * 100) / 100);
	split = String(rounded).split('.');
	if(split.length < 2){
		rounded = rounded + '.00';
	}else if(split[1].length < 2){
		rounded = rounded + '0';
	};
	
	return rounded;
	
};
