$(document).ready(function () {
	// Prepare my deliveries
	var showLink = $('#deliveries a.show-me');
	var showTbl = $('#deliveries table');

	if (showLink && showTbl) {
		// Hide table
		showTbl.hide();

		// Show hide delivery table
		showLink.click(function() {
			showTbl.toggle();
			// Set text of button
			showLink.text((showTbl.is(':hidden')) ? 'show me what\'s coming' : 'hide these details')

			return false;
		});
	}
    
	if ($('.content').is('.new-cust')) {
		var textMain = $('.text-main')[0];
		var items = $('.item');

		items.each(function (i) {
			var item = $($(items[i]));
			item.children('h4, p').addClass('hide');

			// Attach onclick event
			item.click(function() {
				var img = $(item.children('img'));
				var newSrc = img.attr('src').replace('small_', '');

				$('.image-main').children('img').attr('src', newSrc);
				
				$(textMain).children('h3, h4, p').remove();
				$(textMain).append(item.children('h3, h4 ,p').clone().removeClass('hide'));
			});
		});
	}

	$('.product-tabs li').click(function() {
		var lastSelected = $('.tabs').children(':visible');
		
		// Give the last clicked tab the 'closed' class
		var lastLiClass = $('#'+lastSelected.attr('class')).attr('class');

		lastLiClass = lastLiClass.substring(0, lastLiClass.indexOf('open')) + 'closed';
		$('#'+lastSelected.attr('class')).attr('class', lastLiClass);
		// Reset the color of the lastSelected tab.
		$('#'+lastSelected.attr('class')+' a').removeClass('current-active');
		// Give the current tab the 'open' class
		var curLiClass = $('#'+this.id).attr('class');
		curLiClass = curLiClass.substring(0, curLiClass.indexOf('closed')) + 'open';

		$('#'+this.id).attr('class', curLiClass);
		// Make the selected tab's text orange.
		$('#'+this.id+' a').addClass('current-active');
		
		// Hide the current content, and show the content for the clicked item.
		lastSelected.hide();
		$('.'+this.id).show();
	});

	if ($('.reveal')) {
		var openers = $('.reveal').children('dt');
		var openees = $('.reveal').children('dd');
		var open = null;

		openees.each(function (i) {
			$(openers[i]).addClass('hidden');

			$(openers[i]).click(function () {
				if (open !== null) {
					$(openers[open]).addClass('hidden');
					$(openees[open]).hide();
				}

				$(openers[i]).removeClass('hidden');
				$(openees[i]).show();
				open = i;
			});

			$(openees[i]).hide();
		});
	}
	
	$('.btn-back').mouseover(function() { this.src = "images/layout/btn-go-back-over.gif"; });
	$('.btn-back').mouseout(function() { this.src = "images/layout/btn-go-back.gif"; });
	$('.btn-submit').mouseover(function() { this.src = "images/layout/btn-proceed-over.gif"; });
	$('.btn-submit').mouseout(function() { this.src = "images/layout/btn-proceed.gif"; });
	
	$('.different .item').click(function() {
		// Reset all the items.
		for(i=1; i <= 5; i++) {
			$("#r"+i+" h3").removeClass("r"+i+"-current-active");
			$("#r"+i+" h4").removeClass("current-active");
			
			$("#reason-r"+i).addClass("hidden");
		}
		
		$("#"+this.id+" h3").addClass(this.id+"-current-active");
		$("#"+this.id+" h4").addClass("current-active");
			
		$("#reason-"+this.id).removeClass("hidden");
	});
	$("#r1").click();
});

