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>');
	
	/*initialize contact-us lightbox*/
	$(".contact_us").fancybox({
		frameWidth : 426,
		frameHeight: (jQuery.browser.msie ? 140 : 140),
		hideOnContentClick: false,
		centerOnScroll: false,
		callbackOnShow: function() {

		}
	});
	/*initialize contact-us lightbox ends here*/
	
	/*login-register starts here */
	
	$("#login_or_register").fancybox({
		frameWidth : 475,
		frameHeight: 320,
		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", "./"+PARTNERDIR+"/images/login-register/ajax-loader2.gif");
						},
						success: function(data) {
							jQuery(form).find("[type='image']").attr("src", "./"+PARTNERDIR+"/images/login-register/login-right-arrow.png");
							if(data.code == 1) {
								loginSuccess(data);
								if((-1 != window.location.href.indexOf("image-upload")) || (-1 != window.location.href.indexOf("picasa-process"))) {
									$('#footer_upload').click();
								}
								else{
									$.fn.fancybox.close();
								}
							} else {
								// Error, enable inputs
								jQuery(form).find(":input").removeAttr("disabled");
								$("#fancy_content .login_summary").html(data.message);
							}
						}
					});
				}
			});
		}
   });
	/*login-register ends here */
	
	/*login form validation begins here */
	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", "./"+PARTNERDIR+"/images/login-register/ajax-loader2.gif");
				},
				success: function(data) {
					jQuery("#login_submit").attr("src", "./"+PARTNERDIR+"/images/login-register/login-right-arrow.png");
					if(data.code == 1) {
						// if page is not dynamic, redirect to myaccount page
						if(-1 == window.location.href.indexOf("?page=")) {
							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();
					}

				}
			});
		}
	});
	/*login form validation ends here */
	
	/* Forgot Password Lightbox initialization and validation here */
	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 {
                                                            if (!data.question) {
								v3.showErrors({"email": "Diese e-Mail-Adresse ist nicht gültig"});
								jQuery("#fancy_content #security-email-submit").attr("src", "./"+PARTNERDIR+"/images/login-register/next.png");
                                                                return false;
                                                            } 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"});
							}

						}
					});
				}
			});
		}

	});
	
	/* Forgot Password Lightbox initialization and validation ends here */
	
	/*Registration form validation begins here */
	
	jQuery("#register_now").fancybox({
		hideOnContentClick: false,
		centerOnScroll: false,
		frameHeight: jQuery.browser.safari ? 445 : (jQuery.browser.msie ? 440 : 440),
		frameWidth: (jQuery.browser.msie ? 520 : 490),
		callbackOnShow: function() {

			$("#first_name_reg").focus();
			//$("#phone_reg").mask("(999)-999-9999");

			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
					},
					/*
					street: {
						excludePO: true
					},
					street2: {
						excludePO: true
					},
					*/
					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()) {
						error.html("Bitte füllen Sie die rot umrandeten Felder");
					}

					jQuery("#fancy_ajax #summary").html(error);
					return false;

				},


				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						beforeSend: function() {
							jQuery("#fancy_content #reg_submit").attr("src", "./"+PARTNERDIR+"/images/login-register/ajax-loader3.gif");
						},
						beforeSubmit: function() {
							/*if($("#fancy_ajax #accept_terms").attr("checked") == false) {
								jQuery("#fancy_ajax #summary").html("<label class='error'>Please accept terms and conditions</label>");
								return false;
						  	}*/
							jQuery(form).find(":input").attr("disabled", "disabled");
						},
						dataType:  'json',
						target: jQuery(this),
						success: function(data) {
							jQuery("#fancy_content #reg_submit").attr("src", "./"+PARTNERDIR+"/images/login-register/i-accept.jpg");

							if(data.code == 0) {
								
								if(-1 == window.location.href.indexOf("?page=")) { 
									window.location = "./?page=style-selection"; }
								else {
									loginSuccess(data);
									if((-1 != window.location.href.indexOf("image-upload")) || (-1 != window.location.href.indexOf("picasa-process"))) {
										$('#footer_upload').click();
									}
									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")
			 }
			});
		}
	});
	/*Registration form validation ends here */
	
	//add code beneath this
	
});
