// JavaScript Document
$(document).ready(function(){
						   
	jQuery.fn.center = function () {
		this.css("position","absolute");
		this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
		this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
		return this;
	}

	//$('body').prepend('<div>editor</div>');
	$('.ido-favourite').click(function(event) {
			event.preventDefault();
			if ($(this).html() == '<strong>Add</strong> To Favourites'){
				var action = 'Add'
			} else {
				var action = 'Remove'
			}
			$.ajax({
			  type: "POST",
			  url: "/ajax/setfavourite.asp",
			  data: "l=" + $(this).attr('id') + '&a=' + action,
			  dataType: "html",
			  success: function(data){
					//should check data starts with list-
					
					showTopInfo('1')
					if ($('#' + data).html() == '<strong>Add</strong> To Favourites'){
						//mark as favourite
						$('#' + data).parent().parent().parent().fadeOut("slow", function() { // code to run after the fadeOut is complete
							$('#' + data).parent().parent().parent().next('hr').remove() 
							if ($('.favourites').html() != ''){
								$('.favourites').append('<hr />')												  
							}
							$('#' + data).html('<strong>Remove</strong> Favourite').parent().parent().parent().addClass('favourite-new').appendTo('.favourites')
							var strScrollTo = $(".favourites").offset().top + $(".favourites").height() - 50
							//alert(strScrollTo + ' --- ' + $('html,body').scrollTop())
							if (strScrollTo < $('html,body').scrollTop()){
								$('html,body').animate({
									scrollTop: (strScrollTo)}, 1000,function(){
			
										$('#' + data).parent().parent().parent().fadeIn()
	
									});
							} else {
								$('#' + data).parent().parent().parent().fadeIn()
							}
							
							
							
							
						})

					} else {
						//remove from favourite
						//$('#' + data).html('<strong>Add</strong> To Favourites').parent().parent().parent().removeClass('favourite-new').appendTo('.nonefavourites').fade();
						$('#' + data).parent().parent().parent().fadeOut("slow", function() { // code to run after the fadeOut is complete
							$('#' + data).parent().parent().parent().prev('hr').remove() 
							$('.nonefavourites').prepend('<hr />')													  
							$('#' + data).html('<strong>Add</strong> To Favourites').parent().parent().parent().removeClass('favourite-new').prependTo('.nonefavourites').fadeIn('slow');
						})
					}
					
			  }
			});
		});
	
	
	
	
	$('.ido-voucher').click(function(event) {
		event.preventDefault();
		var listingID = $(this).parent().parent().parent().attr('id')
		$.ajax({
			  type: "POST",
			  url: "/ajax/getvoucher.asp",
			  data: "l=" + listingID ,
			  dataType: "html",
			  success: function(data){
				  $('#voucher').remove();
				  $('body').append('<div id="voucher"></div>')
				  $('#voucher').html(data)
				  $('<div />').addClass('bg-fade').appendTo('body').show()
				  $('#voucher').center()
				  //$('#voucher').css("top", (($(window).height() - $('#voucher').outerHeight()) / 2) + $(window).scrollTop() + "px");
   				  //$('#voucher').css("left", (($(window).width() - $('#voucher').outerWidth()) / 2) + $(window).scrollLeft() + "px");
   				  $('#voucher').prepend('<a class="close-popup" href=""><img src="/_images/generic/close-pop.png" alt="close"/></a>')
				  //$('#outer-wrapper').fadeTo('slow', 0.25)
				  $('#voucher').fadeIn()
				  $('#voucher .close-popup').click(function(event){
						event.preventDefault();
						$('#voucher').remove();
						$('.bg-fade').remove()
						//$('#outer-wrapper').fadeTo('slow', 1)
				  })
			  }
			});
	})
	
	
	$('.listing-image').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	$('.home-image').cycle({
		fx: 'fade',timeout: 10000 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});


	var highlight = unescape(self.document.location.hash.substring(1))
	if (highlight != ''){
		$('#' + highlight).css('background-color','#f58fba')
		$('#' + highlight).animate({ backgroundColor: $('#' + highlight).parent().css('background-color') }, 1500);
	}
	
	$('.choosearea').mouseenter(function(){
				$('.chooseareamenu').fadeIn();			 
			}).mouseleave(function(){
				$('.chooseareamenu').fadeOut();			 
			})
	
	$('#profile-partner').focus(function(){
		$('#profile-partner').data('test',{before:$('#profile-partner').val()});
		if ($(this).val() == '& Partners Name'){
			$(this).val('')
		}
		//$('#profile-partner').val($('#profile-partner').val().replace('& ',''))									 
	}).blur(function(){
		if ($(this).val() == ''){
			$(this).val('Partners Name') 
		}
		if( this.value.indexOf('& ') !== 0 ){
			$('#profile-partner').val('& ' + $('#profile-partner').val())	
		}
	}).keyup(function(){
		if ($('#profile-partner').data('test').before != $('#profile-partner').val()){
			$.ajax({
				  type: "POST",
				  url: "/ajax/setprofile.asp",
				  data: "profile-partner=" + $.trim($('#profile-partner').val().replace('&','')),
				  dataType: "html",
				  success: function(data){
					if (data=='ok'){
						$('#profile-partner').data('test',{before:$('#profile-partner').val()});			
						showTopInfo('1')
					} else {
						//warning	
					}
				  }
				});
		}
	})
	
	$('#profile-name').focus(function(){
		$('#profile-name').data('test',{before:$('#profile-name').val()});
		if ($(this).val() == 'Your Name'){
			$(this).val('')
		}
	}).blur(function(){
		if ($(this).val() == ''){
			$(this).val('Your Name')
		}
	}).keyup(function(){
		if ($('#profile-name').data('test').before != $('#profile-name').val()){
			$.ajax({
				  type: "POST",
				  url: "/ajax/setprofile.asp",
				  data: "profile-name=" + $('#profile-name').val() ,
				  dataType: "html",
				  success: function(data){
					if (data=='ok'){
						$('#profile-name').data('test',{before:$('#profile-name').val()});
						$('#sfn').val($('#profile-name').val())
						showTopInfo('1')
					} else {
						//warning	
					}
				  }
				});	
		}
	})
	
	$('.profile-pic').mouseenter(function(){
			$(this).append('<div class="small-button"><a href="#" class="pinkgreen">Upload <strong>Photo</strong></a></div>')
			$(this).children('div').children('a').click(function(){
				 uploadProfileImage()
			 })
	}).mouseleave(function(){
		$('.profile-pic .small-button').remove();
	})
	function showHelp(theClass,theHeading,theText,theLeft,theTop){
		$(theClass).mouseenter(function(){
			$('.help-container').remove();	
			$(theClass).before('<div class="help-container"><div class="help"><div class="help-message"><h4>' + theHeading + '</h4>' + theText + '</div><div class="help-footer"></div></div></div>')
			$('.help-container .help').css('left',theLeft).css('top',theTop)							  
		}).mouseleave(function(){
			$('.help-container').remove();	
		})
	}
	

	function bigdayCountdown() {
		if ($('#bigdaydate').val() != ''){
			var austDay = new Date();
			austDay = new Date($('#bigdaydate').val());
			$('.bigday-countdown').show().countdown({until: austDay, layout: '{dn} <i>days</i><br />{hn}:{mn}:{s10}{s1}',compact: true});
			//$('#year').text(austDay.getFullYear());
		} else {
			//$('.bigday-countdown').html('Enter<br />date')
			$('.bigday-edit').fadeIn();	
		}
	};
	
	bigdayCountdown();
	
	//$('.bigday-box').mouseenter(function(){
		//$('.bigday-countdown').fadeOut();
		//$('.bigday-edit').fadeIn();	
	//}).mouseleave(function(){
		//$('.bigday-edit').fadeOut();	
		//$('.bigday-countdown').fadeIn();
	//})
	
	
	
	if(userTypeID != '10' && userTypeID != '11' && userTypeID != '12'){
		showHelp('.userbox','Personalise','Simply click the Name you want to change or rollover the image to upload a new photo.','230px','4px')
		showHelp('.forgot-password','Forgotten your password?','No problem, just enter your email address to the left and leave your password empty. We will email you your password.','210px','-40px')
		showHelp('.bigday-edit','Big Day Count Down','Click to select your Wedding Day and <strong>Start the Count Down</strong>.','225px','-83px')
	}
	
	$('.forgot-password').click(function(e){
		e.preventDefault();
		forgotPassword();
		
	})
	//error from here

	 $("#bigdaydate").datepicker({
        buttonImage: '/_images/generic/calendar.jpg',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showOn: 'both',
		dateFormat: 'yy-mm-dd', 
		firstDay: 1,
		onSelect: function(dateText, inst) {
			$.ajax({
			  type: "POST",
			  url: "/ajax/setprofile.asp",
			  data: "bigdaydate=" + dateText + "T10:00:00",
			  dataType: "html",
			  success: function(data){
				if (data=='ok'){
					var d = new Date(dateText.substring(0,4), dateText.substring(5,7)-1, dateText.substring(8,10), '10', '0', '0', '0')
					$('.bigday-countdown').countdown('destroy')
					$('.bigday-countdown').countdown({until: d, layout: '{dn} days <br />{hn}:{mn}:{s10}{s1}',compact: true});
					//$('.bigday-edit').fadeOut();	
					$('.bigday-countdown').fadeIn();
					showTopInfo('1')
				} else {
					//warning	
				}
			 }
			})
	
		}
     });
	 
	 //to here
	
	$('#countdown-start2').click(function(e){
		e.preventDefault()
		$.ajax({
		  type: "POST",
		  url: "/ajax/setprofile.asp",
		  data: "bigdaydate=" + padY($('#bigday-year').val()) + "-" + pad($('#bigday-month').val()) + "-" + pad($('#bigday-day').val()) + "T" + pad($('#bigday-hours').val()) + ":" + pad($('#bigday-minutes').val()) + ":00",
		  dataType: "html",
		  success: function(data){
			if (data=='ok'){
				var d = new Date(padY($('#bigday-year').val()), $('#bigday-month').val()-1, $('#bigday-day').val(), $('#bigday-hours').val(), $('#bigday-minutes').val(), '0', '0')
				$('.bigday-countdown').countdown('destroy')
				$('.bigday-countdown').countdown({until: d, layout: '{dn} days <br />{hn}:{mn}:{s10}{s1}',compact: true});
				//var d = new Date()
				//d.setFullYear($('#bigday-year').val())
				//alert(d.toDateString() + ' ' + d.toTimeString())
				$('.bigday-edit').fadeOut();	
				$('.bigday-countdown').fadeIn();
				showTopInfo('1')
			} else {
				//warning	
			}
		  }
		});		
		
	})
	
	$('.bigday-countdown').click(function(){
		//$('.bigday-countdown').fadeOut();
		//$('.bigday-edit').fadeIn();
		$('.ui-datepicker-trigger').trigger('click')
	}).hover(function() {
		$(this).css('cursor','pointer');
		}, function() {
		$(this).css('cursor','auto');
	});
		
	$('.ui-datepicker-trigger').hover(function() {
		$(this).css('cursor','pointer');
		}, function() {
		$(this).css('cursor','auto');
	});
	
	//$('.box-grey .box-rollover').mouseenter(function(){
	//	$(this).parent().css('background-color','#f4f4f4').children('.login-actions').slideDown()								   
	//})
	//$('.box-grey').mouseleave(function(){
	//	$(this).css('background-color','transparent').children('.login-actions').slideUp()								   
	//})
	
	if($('.topinfo-container').html() != ''){
		$('.topinfo-container').show()
	}
	
	
	$('.c-info').parent().click(function(){
		window.location.href = $(this).children('div').children('strong').children('a').attr('href');				 
	}).hover(function() {
		$(this).css('cursor','pointer');
		}, function() {
		$(this).css('cursor','auto');
	});
	$('.c-fayre').click(function(){
		window.location.href = $(this).children('strong').children('a').attr('href');				 
	}).hover(function() {
		$(this).css('cursor','pointer');
		}, function() {
		$(this).css('cursor','auto');
	});
	
	
	
	$('.logout').click(function(e){
		e.preventDefault();
		logOut();
	})
	
	
	

})

function forgotPassword(){
	$.ajax({
	  type: "POST",
	  url: "/ajax/forgotpassword.asp",
	  data: "ue=" + $('#ue').val(),
	  dataType: "html",
	  success: function(data){
		if (data=='found'){
			alert('Password Reminder Sent')
		} else {
			if (data=='not found'){
				alert('Please verify your Email Address and try again')	
			}
			//do nothing	
		}
	  }
	});	
}

function logOut(){
	$.ajax({
	  type: "POST",
	  url: "/ajax/logout.asp",
	  data: "logout=yes",
	  dataType: "html",
	  success: function(data){
		if (data=='ok'){
			window.location.href = window.location.href.split('#')[0]
		}
	  }
	});	
}

function saveProfile(){
	//$('#save-profile-form').submit()
	$.ajax({
	  type: "POST",
	  url: "/ajax/saveprofile.asp",
	  data: "sfn=" + $('#sfn').val() + "&sue=" + $('#sue').val() + "&spw=" + $('#spw').val(),
	  dataType: "html",
	  success: function(data){
		switch(data)
		{
		case 'userexists':
			alert('This email address has already been used to save a profile.\nPlease use the Load Personal Profile or try a different email address.')
			break;
		case 'saved':
			//alert('Profile Saved')
			$('#save-profile').hide();
			$('#outer-wrapper').fadeTo('slow', 1)
			userTypeID = 1;
			$('#profile-name').val($('#sfn').val());
			$('.userbox').append('<div class="small-button"><a href="#" class="green logout">Log-out</a></div>')
			$('.logout').click(function(e){
				e.preventDefault();
				logOut();
			})
			showTopInfo('Profile Saved')
			break;
		case 'incomplete':
			alert('Please ensure you have entered all the information requested')
			break;
		default:
			alert('error')
		}
	  }
	});		
}


function showTopInfo(message){
	if(message == '1'){
		if(userTypeID != '1'){
		message = '<strong>Un-saved Profile</strong> If you would like to access your <b>Favourites</b> and <b>Profile Information</b> in the future, please <a href="#" onclick="showSaveProfile();return false;">Save Your Profile</a>.'	
		} else {
		message = '<strong>Profile Updated</strong>'	
		}
	} 
	
	if(message.toLowerCase() != $('.topinfo-container').html().toLowerCase() || $('.topinfo-container:visible').length == 0){
		$('.topinfo-container').fadeOut()
		$('.topinfo-container').html(message)
		
		if(userTypeID == ''||userTypeID == '0'){
			$('.topinfo-container').slideDown()
		} else {
			$('.topinfo-container').slideDown().delay(2000).slideUp()
		}
	}
}

function showSaveProfile(){
		  $('#save-profile').show()
		  $('#save-profile').css("top", (($(window).height() - $('#save-profile').outerHeight()) / 2) + $(window).scrollTop() + "px");
		  $('#save-profile').css("left", (($(window).width() - $('#save-profile').outerWidth()) / 2) + $(window).scrollLeft() + "px");
		  $('#outer-wrapper').fadeTo('slow', 0.25)
		  $('#save-profile').fadeIn()
		  $('#save-profile .close-popup').click(function(event){
				event.preventDefault();
				$('#save-profile').hide();
				$('#outer-wrapper').fadeTo('slow', 1)
		  })	
	}
	
function uploadProfileImage(){
	  $('#profile-image').show()
	  $('.image-upload-frame').attr('src','/ajax/profileimage.asp')
	  $('#profile-image').css("top", (($(window).height() - $('#profile-image').outerHeight()) / 2) + $(window).scrollTop() + "px");
	  $('#profile-image').css("left", (($(window).width() - $('#profile-image').outerWidth()) / 2) + $(window).scrollLeft() + "px");
	  $('#outer-wrapper').fadeTo('slow', 0.25)
	  $('#profile-image').fadeIn()
	  $('#profile-image .close-popup').click(function(event){
			event.preventDefault();
			$('#profile-image').hide();
			$('#outer-wrapper').fadeTo('slow', 1)
	  })	
}

function profileImageUploaded(imageurl){
		$('.profile-pic img').attr('src',imageurl)
		$('#profile-image').hide();
		$('#outer-wrapper').fadeTo('slow', 1)
}
	
function pad(number) {
    var str = '' + number;
    while (str.length < 2) {
        str = '0' + str;
    }
    return str;
}

function padY(number) {
    var str = '' + number;
    if (str.length == 2) {
        str = '20' + str;
    }
    return str;
}
var tmpTop = 0;
function printVoucher() {
	var tmpTop = $('html, body').scrollTop()
	$('#outer-wrapper').hide();
	$('#control-strip').hide();
	$('.close-popup').hide();
	$('#voucher .long-button').hide();
	$('#voucher').css("top", "5px").css("left", "5px").css("box-shadow", "none");
	window.print();
	setTimeout(function() {
		$('body').bind('mousemove click keypress', function () {
			$('body').unbind('mousemove click keypress')
			$('#outer-wrapper').show();
			$('#control-strip').show();
			window.scrollTo(0, tmpTop);
			$('.close-popup').show();
			$('#voucher').css("box-shadow", "0 0 20px #A6A6A6");
			$('#voucher').center()
			$('#voucher .long-button').show();		});
	}, 200);	
	
	//$('#voucher').remove();
	//$('.bg-fade').remove();
	return false;
}

function hidePrintVoucher(){
	$('#outer-wrapper').show();
	$('#control-strip').show();
	$('.close-popup').show();
	$('#voucher').remove();
	$('.bg-fade').remove();
}
							
