/***************************/
// @implementing and optimizing for ACA: Alex Baskov, 2010
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusJoinArtist = 0;


//loading popup with jQuery magic!
function loadPopupJoinArtist()
{
	//loads popup only if it is disabled
	if(popupStatusJoinArtist == 0)
	{
		$("#popupJoinArtistBackground").css({
			"opacity": "0.4" // set to zero, if you don't want to have a background
		});
		$("#popupJoinArtistBackground").fadeIn("slow");
		$("#popupJoinArtist").fadeIn("slow");
		popupStatusJoinArtist = 1;
	}
	
	$.validator.addMethod('customEmail', function(value, element) {
		return this.optional(element) || /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(element.value);
		},"Enter a valid email"
	);
	$.validator.addMethod('customPhone', function(value, element) {
		return this.optional(element) || /^[0-9\-\(\)+ ]*$/i.test(element.value);
		},"Enter a valid phone number"
	);
	$.validator.addMethod( 
	  "customRequired", 
	  function(value, element) { 
		if (element.value == element.title) { return false; } 
		else return true; 
	  }, 
	  "Please input something." 
	);
	ja_validator = $("#form_join_artist").validate({
		errorElement: "div",
		rules: {
			name: {
				customRequired: true,
				maxlength: 200
			},
			email: {
				customRequired: true,
				customEmail: true,
				maxlength: 200
			},
			phone: {
				customRequired: true,
				customPhone: true,
				maxlength: 50
			},
			resume: {
				accept:'docx?|txt|pdf',
				minlength: 2,
				maxlength: 400
			},
			photo: {
				accept: 'jpe?g|gif|png|bmp|tiff?',
				minlength: 2,
				maxlength: 400
			},
			agree: {
				required: true
			}
		},
		messages: {
			name: {
				customRequired: "<b>Full Name</b> is missing",
				maxlength: jQuery.format("<b>Your Name</b>: Enter less than {0} characters")
			},
			email: {
				customEmail: "<b>Email</b> address appears to be invalid. Please check. ",
				customRequired: "<b>Email</b> address is missing",
				maxlength: jQuery.format("<b>Email</b>: Enter less than {0} characters")
			},
			phone: {
				customRequired: "<b>Phone</b> is missing",
				customPhone: "<b>Phone</b> appears to be invalid. Please check.",
				maxlength: jQuery.format("<b>Phone</b>: Enter less than {0} characters")
			},
			resume: {
				accept: "Unsupportable <b>Resume</b> file extension",
				minlength: jQuery.format("<b>Resume</b>: Enter at least {0} characters"),
				maxlength: jQuery.format("<b>Resume</b>: Enter less than {0} characters")
			},
			photo: {
				accept: "Unsupportable <b>Photo</b> file extension",
				minlength: jQuery.format("<b>Photo</b>: Enter at least {0} characters"),
				maxlength: jQuery.format("<b>Photo</b>: Enter less than {0} characters")
			},
			agree: {
				required: "You need to <b>agree</b> if you want to join us!"
			}
		},
		errorPlacement: function(error, element) {
			if ($("#form_join_artist .popup_error").length == 0) {
				$("#form_join_artist .popup_error_holder").append("<div class='popup_error'></div>").show();
			}
			$("#form_join_artist .popup_error").show().append(error);
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
			label.hide();

			if ($("#form_join_artist .popup_error .checked").length == $("#form_join_artist .popup_error .error").length) {
				$("#form_join_artist .popup_error").hide();
			}
			else {
				$("#form_join_artist .popup_error").show();
			}
		},
		highlight: function(element, errorClass) {
		},
		invalidHandler: function() {
		},
		submitHandler: function() {
		}
	});
	$("#form_join_artist input[class^='it_popup_join']").keyup(function(){
		ja_validator.element("#" + $(this).attr('id'));
	});
	$("#form_join_artist input.it_popup_join").change(function(){
		ja_validator.element("#" + $(this).attr('id'));
	});
	$("#form_join_artist .popup_message_inprogress").hide();
	$("#form_join_artist .popup_error_holder").empty();
	$("#form_join_artist .popup_lbl").show();
	$("#form_join_artist .popup_fld").show();
	$("#form_join_artist .popup_join_tos").show();
	$("#form_join_artist .popup_submit_btn").show();
}

//disabling popup with jQuery magic!
function disablePopupJoinArtist()
{
	//disables popup only if it is enabled
	if(popupStatusJoinArtist == 1)
	{
		$("#popupJoinArtistBackground").fadeOut("slow");
		$("#popupJoinArtist").fadeOut("slow");
		popupStatusJoinArtist = 0;
	}
	$("#form_join_artist input").val("");
}

//centering popup
function centerPopupJoinArtist()
{
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupJoinArtist").height();
	var popupWidth = $("#popupJoinArtist").width();

	//centering
	$("#popupJoinArtist").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupJoinArtistBackground").css({
		"height": windowHeight
	});
	
}

function showJoinArtistSuccess() {
	if(popupStatusJoinArtist == 1) {
		disablePopupJoinArtist();
		centerPopupThankYou();
		$("#thank_you_join").show();
		loadPopupThankYou();
	}
}

function showJoinArtistError() {
	$("#form_join_artist .popup_message_inprogress").hide();
	$("#form_join_artist .popup_error_holder").show();
	$("#form_join_artist .popup_lbl").show();
	$("#form_join_artist .popup_fld").show();
	$("#form_join_artist .popup_join_tos").show();
	$("#form_join_artist .popup_submit_btn").show();
}

function showJoinArtistProgress() {
	$("#form_join_artist .popup_message_inprogress").show();
	$("#form_join_artist .popup_error_holder").empty().hide();
	$("#form_join_artist .popup_lbl").hide();
	$("#form_join_artist .popup_fld").hide();
	$("#form_join_artist .popup_join_tos").hide();
	$("#form_join_artist .popup_submit_btn").hide();
}

function sendJoinArtistRequest() {

	if (!(ja_validator.form())) {
		return false;
	}

	showJoinArtistProgress();
	
	var data = $('#form_request_edit input').fieldSerialize();
	$('#form_join_artist').ajaxSubmit
	({
		type: "POST",
		url:'/external/joinArtist', 
		success: function(msg) {
			var response = JSON.parse(msg);
			if (response.status == "error") {
				if (response.errors != undefined && response.errors.length > 0) {
					var description = "";
					for (var i = 0; i < response.errors.length; i++) {
						var code = response.errors[i].code;
						if (code == "global") code = "";
						else if (code == "name") code = "Full Name: ";
						else if (code == "email") code = "Email: ";
						else if (code == "phone") code = "Phone: ";
						else if (code == "accountType") code = "Account Type: ";
						else if (code == "photo") code = "Attach Photo: ";
						else if (code == "resume") code = "Attach Resume: ";
						else code = "";

						if (i != 0) description += "<br/>";
						description += "<b>" + code + "</b>" + response.errors[i].description;
					}
					$("#form_join_artist .popup_error_holder").append("<div class='popup_error'>"  + description + "</div>");
				}
				else {
					$("#form_join_artist .popup_error_holder").html("<div class='popup_error'>Error to submit your request. Please, try again later!</div>");
				}
				showJoinArtistError();
			}
			else {
				showJoinArtistSuccess();
			}
		},
		error: function (data, status, e) {
			$("#form_join_artist .popup_error_holder").html("<div class='popup_error'>Error to submit your request. Please, try again later!</div>");
			showJoinArtistError();
		}
	});
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#buttonJoinArtist, #buttonLoginAJoin").click(function() {
		disablePopupLogin();
		disablePopupLoginArtist();
		
		centerPopupJoinArtist();
		loadPopupJoinArtist();
	});
	
	$("#popupJoinArtistClose").click(function() {
		disablePopupJoinArtist();
	});
			
	//Click out event!
	$("#popupJoinArtistBackground").click(function() {
		disablePopupJoinArtist();
	});
	
	$("#buttonJoinASubmit").click(sendJoinArtistRequest);
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatusJoinArtist == 1) {
			disablePopupJoinArtist();
		}
	});
	

});


