/**
 * @author Chris Deemer
 */
/******************************* START :: My Account Landing Page *****************************/
$().ready(function(){
	//individual inline editing events for buttons
		/************** Account Details ***************/
		$('#accountDetails_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
		
		/************** Account Profile ***************/
		$('#accountProfile_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
		
		/************** Orders ***************/
		$('#orders_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
		
		/************** Address Book ***************/
		$('#address_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
		
		/************** Credit Cards ***************/
		$('#creditCard_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
		
		/************** Gift Reminders ***************/
		$('#giftReminders_section a.edit_inline').each(function(){
			var $container = $(this).parents('div.myAccount_section');
			//bind event listener to edit button
			$(this).bind('click',function(){
				showEdit($container); 
				return false;
			});
			//bind event listener to save button
			$container.find('a.myAccount_save').bind('click', function(){
				saveChanges($container); 
				return false;
			});
			//bind event listener to close button
			$container.find('a.myAccount_close').bind('click', function(){
				cancelChanges($container);
				 return false;
			});
		});
	
	//inline editing components
	/*$('#myAccountPage a.edit_inline').each(function(){
		var $container = $(this).parents('div.myAccount_section');
		//bind event listener to edit button
		$(this).bind('click',function(){
			showEdit($container); 
			return false;
		});
		
		//bind event listener to save button
		$container.find('a.myAccount_save').bind('click', function(){
			saveChanges($container); 
			return false;
		});
		//bind event listener to close button
		$container.find('a.myAccount_close').bind('click', function(){
			cancelChanges($container);
			 return false;
		});
	});*/
	

	//email preferences
	$('#myAccountPage a.edit_emailPrefs').each(function(){
		var $container = $(this).parents('div.myAccount_section');
		//bind event listener to edit button
		$(this).bind('click',function(){
			
			$(this).fadeOut(500,function(){
				$container.find('.myAccount_change_buttons').fadeIn(500);
				$container.find('input[type=checkbox]').removeAttr('disabled');
			});
			return false;
		});
		
		//bind event listener to save button
		$container.find('a.myAccount_save').bind('click', function(){
			//TO DO: save changes to checkboxes
			
			$container.find('.myAccount_change_buttons').fadeOut(500,function(){
				$container.find('.edit_emailPrefs').fadeIn(500);
			$container.find('input[@type=checkbox]').attr('disabled', 'disabled');
			});
			return false;
		});
		
		//bind event listener to close button
		$container.find('a.myAccount_close').bind('click', function(){
			//TO DO: revert changes
			
			$container.find('.myAccount_change_buttons').fadeOut(500,function(){
				$container.find('.edit_emailPrefs').fadeIn(500);
				$container.find('input[type=checkbox]').attr('disabled', 'disabled');
			});
			return false;
		});
	});
	
	// catalog request
$('#catalogRequest').find('a').bind('click', function(){
		
		var $container = $('#catalogRequest');
		//switch copy
		$container.find('p.catalog').fadeOut(500,function(){
			$container.find('p.thankYou').fadeIn(500);
		});
		
		return false;
	});
});

function showEdit($container){
	var $sectionContent= $container.find('.section_content');
	var $sectionContentWrapper = $container.find('.section_content_wrapper');
	var $summary = $sectionContent.find('.myAccount_summary');
	var $edit	= $sectionContent.find('.myAccount_change');
	
	////switch buttons
	$container.find('.edit_inline').fadeOut(500,function(){
		$container.find('.myAccount_change_buttons').fadeIn(500);
	});
	////switch panels
	$sectionContent.fadeOut(500,function(){
		$edit.show();
		$summary.hide();
		$sectionContentWrapper.animate({height: $sectionContent.height() },500, function(){
			$sectionContentWrapper.css('height', 'auto');
			$sectionContent.fadeIn(500);
		});
	});
}

function showSummary($container){
	var $sectionContent= $container.find('.section_content');
	var $sectionContentWrapper = $container.find('.section_content_wrapper');
	var $summary = $sectionContent.find('.myAccount_summary');
	var $edit	= $sectionContent.find('.myAccount_change');
	
	//set to static height for animation sake
	$sectionContentWrapper.css('height', $sectionContentWrapper.height());
	////switch buttons
	$container.find('.myAccount_change_buttons').fadeOut(500,function(){
		$container.find('.edit_inline').fadeIn(500);
	});
	////switch panels
	$sectionContent.fadeOut(500,function(){
		$summary.show();
		$edit.hide();
		$sectionContentWrapper.animate({height: '80px' },500, function(){
			$sectionContent.fadeIn(500);
		});
	});
}

//this is for inline editing items only
//this does not include email preferences
function saveChanges($container){
/*TO DO: save funcitonality and updates summary*/

	showSummary($container);
}

function cancelChanges($container){
	showSummary($container);
}
/******************************* END :: My Account Landing Page *****************************/


