var whichinstantdownloads = {
		//Mag 2009/05/08: Function to deal with sliding content show and hide 
		//Arguments: selector of parent of header and content, selector of parent element of header and content, specific header selector, specific content selector
		//Example: whichinstantdownloads.slidingController("#sidebar .promo-5 .wrapper", "li", "h3 a", "div" );
		slidingController:function(controllerContainerSelector, controllerContainerElementSelector, controllerSelector, controllerContentSelector)
		{
			$(controllerContainerSelector).addClass("js-slider");//Adds all buttons for open/close
			$(controllerContainerSelector+" "+controllerContainerElementSelector).each(function () {
				if(!$(this).hasClass("on")) {
					$(this).children(controllerContentSelector).hide();
				}
		    });
			
			$(controllerContainerSelector+" "+controllerContainerElementSelector+" "+controllerSelector).click(function () {
				if($(this).parents(controllerContainerElementSelector).children(controllerContentSelector).is(":visible")) {
					$(this).parents(controllerContainerElementSelector).children(controllerContentSelector).slideUp("slow");
					$(this).parents(controllerContainerElementSelector).removeClass("on");
				}
				else {
					$(this).parents(controllerContainerElementSelector).children(controllerContentSelector).slideDown("slow");
					$(this).parents(controllerContainerElementSelector).addClass("on");
				}
				return false;
		    });
		},
		
		thickbox:function()
		{
			$("#preview").click(function(){
				$("body").addClass("pdf");
			});
			
			$(".close-lightbox").click(function(){
				tb_remove();
				$("body").removeClass("pdf");
				return false;
			});
			
		},
		
		button: {
			personalDetails: {
				src: 'https://www.which.co.uk/assets/images/buttons/btn-edit-your-personal-details.gif',
				height: '25',
				width: '188',
				alt: 'Edit your personal details',
				cssclass: 'editbutton'
			},
			billingAddress: {
				src: 'https://www.which.co.uk/assets/images/buttons/btn-edit-your-billing-address.gif',
				height: '24',
				width: '181',
				alt: 'Edit your billing address',
				cssclass: 'editbutton'
			},
			paymentDetails: {
				src: 'https://www.which.co.uk/assets/images/buttons/btn-edit-your-payment-details.gif',
				height: '24',
				width: '192',
				alt: 'Edit your payment details',
				cssclass: 'editbutton'
			},
			loginDetails: {
				src: 'https://www.which.co.uk/assets/images/buttons/btn-edit-your-login-details.gif',
				height: '24',
				width: '171',
				alt: 'Edit your log in details',
				cssclass: 'editbutton'
			},
			closeEditScreen: {
				src: 'https://www.which.co.uk/assets/images/buttons/btn-close-edit-screen.gif',
				height: '25',
				width: '135',
				alt: 'Close the edit screen'
			}
		},
		
		showEdit: function (id) {
			var button = whichinstantdownloads.button.closeEditScreen;
			$('#' + id + ' img.editbutton').attr({
				src: button.src,
				height: button.height,
				width: button.width,
				alt: button.alt
			});
			$('#' + id + 'Edit').slideDown();
		},
			
		closeEdit: function (id) {
			var button = whichinstantdownloads.button[id];
			$('#' + id + ' img.editbutton').attr({
				src: button.src,
				height: button.height,
				width: button.width,
				alt: button.alt
			});
			$('#' + id + 'Edit').slideUp('fast');
		},
		
		discardChanges: function (id) {
			$('#'+id).parents("form:first").each(function(){
				this.reset();
			});
		},
		
		createEditButton: function (id, button) {
			var html = '<img alt="'+button.alt+'" src="'+button.src+'"'
				+ 'width="'+button.width+'" height="'+button.height+'"'
				+ 'class="'+button.cssclass+'"/>';
			return html;
		}
}
//End of which-instant-downloads closure

$("document").ready(function(){
	whichinstantdownloads.thickbox();
	
	$('div.hide-for-js').hide();
	
	$('div.accountdetailsection').each(function () {
		var id = $(this).attr('id');
		var button;
		switch (id) {
		case 'personalDetails':
			button = whichinstantdownloads.button.personalDetails;
			break;
		case 'billingAddress':
			button = whichinstantdownloads.button.billingAddress;
			break;
		case 'paymentDetails':
			button = whichinstantdownloads.button.paymentDetails;
			break;
		case 'loginDetails':
			button = whichinstantdownloads.button.loginDetails;
			break;
		default:
			button = false;
			break;
		}
		
		if (button != false) {
			var buttonhtml = whichinstantdownloads.createEditButton(id, button);
			$(this).append(buttonhtml);
			$('#' + id + ' .editbutton').toggle(
				function () {
					whichinstantdownloads.showEdit(id);
					return false;
				},
				function () {
					whichinstantdownloads.closeEdit(id);
					return false;
				}
			);
		}
	});

	$('div.editPanel').each(function () {
		var parentId = $(this).attr('id');
		$(this).find('.btnWrapper')
		.prepend('<input type="image" src="https://www.which.co.uk/assets/images/buttons/btn-discard-changes.gif"'
			+ ' onclick="whichinstantdownloads.discardChanges(&quot;' + parentId + '&quot;); return false;"/>');
	});
});