function loginSuccess(data) {
	//Logged in
	jQuery("#login_registration").hide();
	jQuery("#first_name").html(data.user.first_name);
	jQuery("#welcome_back").show();
	jQuery("#logged_in_email").html(data.user.email);
	jQuery("#logged_in").show();
	jQuery(window).data("login", 1);
	
	try  {
		$.fn.fancybox.close();
	}
	catch(err) {
	  //Handle errors here
	}	
}

jQuery(function() {

	jQuery("body").append('<a id="login_or_register" href="/?page=login-or-register" style="display: none;">Login or register</a>');

	$(".contact_us").fancybox({
			frameWidth : 426,
			frameHeight: (jQuery.browser.msie ? 195 : 185),
			hideOnContentClick: false,
			centerOnScroll: false,
			callbackOnShow: function() {
			}
	});

	$("#login_or_register").fancybox({
		frameWidth : 475,
		frameHeight: 300,
		hideOnContentClick: false,
		centerOnScroll: false,
		callbackOnShow: function() {

			$("#fancy_close").addClass("show").bind("click", $.fn.fancybox.close);

			if($(window).data("message")) {
				$("#fancy_content .login_summary").html($(window).data("message"));
				$(window).data("message", null);
			}
			$("#fancy_content form.login").validate({
				rules: {
					email: {
						required: true,
						email: true
					},
					password: {
						required: true
					}
				},
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						beforeSubmit: function() {
							jQuery(form).find(":input").attr("disabled", "disabled");
						},
						dataType:  'json',
						beforeSend: function() {
							jQuery(form).find("[type='image']").attr("src", "images_en/ajax-loader2.gif");
						},
						success: function(data) {
							jQuery(form).find("[type='image']").attr("src", "images_en/login-right-arrow.png");
							if(data.code == 1) {
								loginSuccess(data);
								if(window.location.href.indexOf("?page=image-upload")) {
									$("#btnUpload").click();
									$("#upload-active").click();
									/*var url = '/pages/log_upload_page_activity.php';
									$.ajax({
										type: 'POST',
										url: url,
										data: { varLogin: 'login'},
										success: function(result){
											if(result == true){
											}
										}
									});*/
								}
								else{
									$.fn.fancybox.close();
								}
								//window.location = "/?page=book-size-selection";
								$("form#frmStyleSelectionNew").submit();	
								//if('SC' == data.partner) window.location = "?page=checkout&photoselection="+$("#usePB").val();
								//else $("form#image_selection_sub").submit();

							} else {
								// Error, enable inputs
								jQuery(form).find(":input").removeAttr("disabled");
								$("#fancy_content .login_summary").html(data.message);
							}
						}
					});
				}
			});
		}
   });



	jQuery("#fancy_close").live("click", jQuery.fn.fancybox.close);

	var vLogin = jQuery("#login_registration").validate({

		errorPlacement: function(error, element) {
			// Displays only first encountered error
			if(!jQuery("#login_summary label:visible").length) jQuery("#login_summary").html(error).parents("tr").show();
		},
		rules: {
			email: {
				required: true,
				email: true
			},
			password: {
				required: true
			}
		},
		messages: {
			email: {
				required: "Bitte geben Sie Ihre E-Mail-Adresse ein"
			},
			password: {
				required: "Bitte geben Sie Ihr Passwort"
			}
		},

		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				beforeSubmit: function() {
					jQuery(form).find(":input").attr("disabled", "disabled");
				},
				//target: jQuery(this),
				dataType:  'json',
				beforeSend: function() {
					jQuery("#login_submit").attr("src", "/images_en/ajax-loader2.gif");
				},
				success: function(data) {
					jQuery("#login_submit").attr("src", "/images_en/login-right-arrow.png");
					if(data.code == 1) {
						// if page is not dynamic, redirect to myaccount page
						if(-1 == window.location.href.indexOf("?page=")) {
							/* commented the following two lines. Refer Bug #1911 for further details.
							   if('SC' == data.partner) window.location = "/?page=myaccount";
							   else window.location = "/?page=event-selection"; */
							window.location = "/?page=myaccount";
						}
						else { loginSuccess(data); }
					}
					else {
						// Error, enable inputs
						jQuery(form).find(":input").removeAttr("disabled");
						vLogin.showErrors({email:data.message});
						jQuery("#login_summary").parent("tr").show();
					}

				}
			});
		}
	});

	jQuery("#password-forgot").fancybox({
		hideOnContentClick: false,
		frameHeight: 215,
		callbackOnShow: function() {
			jQuery("#fancy_close").addClass("show").bind("click", jQuery.fn.fancybox.close);

			jQuery("#fancy_ajax #back_security_mail").click(function(e) {
				e.preventDefault();
				jQuery.fn.fancybox.close();
				return false;
			});

			jQuery("#fancy_ajax #back_security_answer").click(function(e) {
				e.preventDefault();
				//jQuery.fn.fancybox.close();
				jQuery("#fancy_ajax #security_email").show();
				jQuery("#fancy_ajax #security_answer").hide();
				return false;
			});


			jQuery("#fancy_content form :input").keypress(function(e){
		      if(e.which == 13){
		       	jQuery(this).parents("form").submit();
		       	return false;
		       }
		    });
			var v3 = jQuery("#fancy_content #security_email").validate({
				submitHandler: function(form) {

					jQuery(form).ajaxSubmit({
						dataType:  'json',
						success: function(data) {
							if('SC' == data.partner) {
								jQuery("#fancy_ajax").html('<h2 style="text-align: center; padding-top:25px;">Ihr neues Passwort wird an die bei der Registrierung angegebene E-Mail-Adresse versandt.</h2>');
							}
							else {
								jQuery("#fancy_content #email_answer").val(jQuery("#fancy_content #email_forgot").val());

								jQuery("#fancy_content #security_email").hide();
								jQuery("#fancy_content #security_question").html(data.question);
								jQuery("#fancy_content #security_answer").show();
							}
						}
					});
				}
			});

			var v4 = jQuery("#fancy_content #security_answer").validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						dataType: 'json',
						success: function(data) {
							if(1 == data.code) {
								jQuery("#fancy_ajax").html('<h2 style="text-align: center; padding-top:25px;">Ihr neues Passwort wird an Ihre bei der Registrierung verwendete E-Mail-Adresse geschickt.</h2>');
							} else {
								v4.showErrors({"answer": "Antwort falsch"});
							}

						}
					});
				}
			});
		}

	});
	jQuery("#register_now").fancybox({
		hideOnContentClick: false,
		centerOnScroll: false,
		frameHeight: jQuery.browser.safari ? 390 : (jQuery.browser.msie ? 390 : 422),
		frameWidth: (jQuery.browser.msie ? 520 : 490),
		callbackOnShow: function() {
			//$("#phone_reg").mask("(999)-999-9999");
			//$("#phone_reg").mask("(999)/999-99999");
		
			$("#first_name_reg").focus();
		
			if(jQuery.browser.msie) jQuery("#fancy_ajax").css("overflow", "hidden");
			jQuery('#fancy_content #form_registration :input').keypress(function(e){
		      if(e.which == 13){
		       	jQuery('#fancy_content #form_registration').submit();
		       }
		    });
			//jQuery("#form_registration").not("#fancy_content #form_registration").remove();
			// Adjust fancy to hide scrollbar
			// jQuery("#form_registration").parents("#fancy_ajax").css("overflow", "hidden");
			//jQuery(window).data("original_fancy_height", jQuery("#fancy_outer").height());
			// show the close stuff
			jQuery("#fancy_content #form_registration").parents("#fancy_inner").find("#fancy_close").addClass("show").bind("click", jQuery.fn.fancybox.close);
			var v2 = jQuery("#fancy_content #form_registration").validate({
				ignoreTitle: true,
				onkeyup: false,
				onfocusout: false,
				onclick: false,
				rules: {
					email: {
						required: true,
						email: true
					},
					password: {
						required: true,
						minlength: 5
					},
					confirm_password: {
						required: true,
						minlength: 5,
						equalTo: "#fancy_content #password_reg" //jQuery(this).find([name='password'])
					}

				},
				messsages: {
					email: "Bitte geben Sie E-Mail",
					password: {
						required: "Bitte geben Sie ein Passwort",
						minlength: "Ihr Passwort muss mindestens 5 Zeichen lang sein"
					},
					confirm_password: {
						required: "Bitte geben Sie ein Passwort",
						minlength: "Ihr Passwort muss mindestens 5 Zeichen lang sein",
						equalTo: "Bitte geben Sie das gleiche Passwort wie oben"
					}
				},

				errorPlacement: function(error, element) {


					if("This field is required." == error.html() || "" == error.html() || "Dieses Feld ist ein Pflichtfeld." == error.html()) {
						error.html("Bitte mit * gekennzeichnete Felder ausfüllen und in die rot umrandeten");
					}

					jQuery("#fancy_ajax #summary").html(error);
					return false;

				},


				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						beforeSend: function() {
							jQuery("#fancy_content #reg_submit").attr("src", "/images_en/ajax-loader3.gif");
						},
						beforeSubmit: function() {
							jQuery(form).find(":input").attr("disabled", "disabled");
						},
						dataType:  'json',
						target: jQuery(this),
						success: function(data) {
							jQuery("#fancy_content #reg_submit").attr("src", "/images_en/next.jpg");

							if(data.code == 0) {
								
								if(-1 == window.location.href.indexOf("?page=")) { 
									jQuery.fn.fancybox.close();
									window.location = "/?page=event-selection"; }
								else {
									loginSuccess(data);
									if(window.location.href.indexOf("?page=image-upload")) {
										$("#btnUpload").click();
										$("#upload-active").click();
										/*var url = '/pages/log_upload_page_activity.php';
										$.ajax({
											type: 'POST',
											url: url,
											data: { varLogin: 'login'},
											success: function(result){
												if(result == true){
												}
											}
										});*/
									}
									else{
										$.fn.fancybox.close();
									}
								}
							} else {
								//If error code is 68 (user already exists), set error as window data (bypass to login popup) then click login popup link to show
								if(68 == data.code) {
									jQuery(window).data("message", "Benutzer ist bereits vorhanden. Bitte melden Sie sich an.");
									jQuery("#login_or_register").click();
								} else {
									jQuery(form).find(":input").removeAttr("disabled");
									jQuery("#fancy_content #summary").html(data.message);
								}
							}
						}
					});
				}
			});
			//This way it works with fancybox
			jQuery("#fancy_content #password_reg").rules("add", {
			 required: true,
			 minlength: 5,
			 messages: {
			   // required: "Please provide a password",
			   minlength: jQuery.format("Ihr Passwort muss mindestens {0} Zeichen lang sein")
			 }
			});
			jQuery("#fancy_content #accept_terms").rules("add", {
			 required: true,
			 messages: {
			   required: "Bitte akzeptiere die Regeln und Bedingungen"
			 }
			});			
		}
	});
});
