// JavaScript Document $Id: savedAccommodations.js,v 1.1 2010/11/18 16:15:07 andy.f Exp $
savedAccomm ={
		init: function(){
			savedAccomm.noHotelsMsg = '<p class="tac">You have deleted all your saved hotels</p>';
			$('body').append("<div id=\"svdAccomContainer\" class=\"hide\"><div id=\"savedAccomm\"></div></div>");
			$('#svdAccomContainer').append("<a id=\"svdAccomLink\" href=\"#savedAccomm\">Display Saved Accommodation</a>");
			$('#saveAccom a').click(function(e){
				e.preventDefault();
				savedAccomm.saveAccommodation(this);			
				
			}); 
						
			//Email section
			$('.savedAccommodationsEmailForm').hide();
			savedAccomm.showHideEmailForm();			
			savedAccomm.accomTrigger();
			
		},
		accomTrigger: function(){
			$('a.svdAccomLink').click(function(e){
				e.preventDefault();
				savedAccomm.ajaxFBCall($(this));
			});
		},
		ajaxFBCall: function(el){
			//What to pull in via AJAX
			var toLoad = el.attr('href')+' #svdAJAX';
			
			//Show fancy box Waiting...
			$.fancybox.showActivity();
			
			//Put content into hidden container for use...
			$('#savedAccomm').load(toLoad,'',function(){
					savedAccomm.deleteAccommodation();
					savedAccomm.showHideEmailForm();
					$('.savedAccommodationsEmailForm').hide();
				});
			
			
			//$.fancybox.hideActivity()
			
			$('#svdAccomLink').fancybox({
				'centerOnScroll' : true,
				'padding' : 4,
				'autoDimensions' : false,
				'width' : 600,
				'height': 400,
				'overlayColor' : 	'#000',
				'overlayOpacity': 	0.8
				
				
			}).trigger('click');
		},
		saveAccommodation: function(href){
			$.ajax({
				url : href,
				type: 'GET',
				dataType: 'html',
				timeout: 4000,
				error: function(){
				},
				success: function(html){
					if($('#svdAccomm').length === 0){
						if($('#rctSearch').length > 0){
							var appendTo = "#rctSearch";
							tmpCss = " class=\"accommMenuJig\" ";
							$(appendTo).addClass('rctMenuJig');
						} else{
							var appendTo = "#searchForm";
							tmpCss = " ";
						}
						
						$('<div' + tmpCss +'id="svdAccomm"><ul><li>You have <span class="svdAccomCount">1</span> <a href="'+Global.url+'savedAccommodations" title="SavedAccommodations" class="svdAccomLink" id="SavedAccommodations">Saved Hotel</a></li></ul></div>').insertAfter(appendTo);

						$('#savedAccomm').html($('#svdAJAX', html));
						savedAccomm.accomTrigger();
					} else {
							$('.svdAccomCount').html($('#svdACount', html));
							$('#SavedAccommodations').html("Saved Hotels");
					}
					$('#saveAccom').prepend('<span id="savedAccom">accomm saved</span>').addClass("svdUnit");		
					
										
				}
			});
		},
		showHideEmailForm: function(){
			//listener for show hide stuff
			
			$('#emailThisAccomm a,#emailThisAccomm p').click(function(e){
				  e.preventDefault();
				  $("#savedAccommodationsEmailForm").slideToggle("slow");
				});
			
		},
		deleteAccommodation: function(){
			
			$(".savedAccommodations form").bind("submit", function(e) {
				e.preventDefault();
				var $tgtForm = $(e.target);
				
				if($tgtForm.is('form')){
					tgtAction = $tgtForm.attr("action");
					if($tgtForm.attr('id') == "savedAccommodationsForm"){
						var dataString =  'deleteAll=true';
						$.ajax({
							type: "POST",
							url: tgtAction,					 
							data: dataString,
							success: function(html) {
								// hide the one you click on
								$tgtForm.slideUp();
								$('.svdAccom').hide();
								//adjust the value of hotels saved
								
								$('#rctSearch').removeClass('rctMenuJig');
								$('#svdAccomm').slideUp().remove();
								$('#saveAccom span').remove();
								//$('.svdAccomCount').html($('#count', html));
								$('#emailThisAccomm').html(savedAccomm.noHotelsMsg);
							}
						});
					} else if($tgtForm.attr('id') == "savedAccommodationsEmailForm"){
					//	alert("can i get the id of form???" + $tgtForm.attr('id'));
						tmpTag = $tgtForm.attr('id')+" input[name='recipient']";
						errr2 = $("#"+tmpTag);
						
						var dataString =  'recipient='+errr2.val()+'&sendEmail=Send Email';
						$.ajax({
							type: "POST",
							url: tgtAction,					 
							data: dataString,
							success: function(html) {
								// hide the one you click on
								$tgtForm.slideUp();
								//adjust the value of hotels saved
								$('#emailThisAccomm a').html('Your email has been sent');
								
							}
						});
					} else {
						tmpTag = $tgtForm.attr('id')+" .action input[name='savedAccommodationId']";
						tmpMit = $tgtForm.attr('id')+" input[name='savedAccommodationHCode']";
						errr2 = $("#"+tmpTag);
						qMit = $("#"+tmpMit).val();
						var dataString =  'savedAccommodationId='+errr2.val()+'&delete=true';
						$.ajax({
							type: "POST",
							url: tgtAction,					 
							data: dataString,
							success: function(html) {
								// hide the one you click on
								$tgtForm.slideUp('slow', function(){
									// Remove element from DOM, so we can have an accurate count
									$tgtForm.remove();
								});
								//adjust the value of hotels saved
								if(($('#svdAJAX form.svdAccom').size() - 1) == 0){
									$('#svdAccomm').remove();
									//Hide Delete All Form;
									$('#savedAccommodationsForm').fadeOut();
									$('#rctSearch').removeClass('rctMenuJig');
									$('#emailThisAccomm').html(savedAccomm.noHotelsMsg);
								} else {
									if(($('#svdAJAX form.svdAccom').size() - 1) == 1){
										$('#SavedAccommodations').html("Saved Hotel");
									}
									$('.svdAccomCount').html($('#svdACount', html));
								}
								
								//Check to remove saved span
								$('.'+qMit).removeClass("svdUnit");
								$('#saveAccom span').remove();
								
							}
						});
					}
				}
			});
		}
};

//function saveAccommodation(type,hcode,mitSiteCode,aliasRef,link){
//	$.ajax({
//		url : Global.url + "savedAccommodations.vm?saveAccom=true"
//				+"&type=" + type
//				+"&hCode=" + hcode
//				+"&mitSiteCode=" + mitSiteCode
//				+"&aliasRef=" + aliasRef
//				+"&link=" + link,
//		type: 'GET',
//		dataType: 'html',
//		timeout: 4000,
//		error: function(){
//		},
//		success: function(html){
//			$('#saveAccom').html('accomm saved');
//			$('#SavedAccommodations').html($('#count', html));
//		}
//	});
//}
//function saveHotel(isBedbank,hcode,mitSiteCode,aliasRef){
//	if(isBedbank == true){
//		saveAccommodation('hotel',hcode,mitSiteCode,aliasRef,Global.url+"hotel/"+hcode+"-"+mitSiteCode);
//	}else{
//		saveAccommodation('hotel',hcode,mitSiteCode,aliasRef,Global.url+"hotel/"+mitSiteCode);
//	}
//}
