reload_user_info_dynamicaly = 'CZ_reload_user_voyo';
var voyo_is_first_call = true;
var voyo_is_logged_in = false;
var voyo_user = null;

function CZ_reload_user_voyo(userinfo) {
	voyo_user = userinfo;
	if(voyo_is_first_call) {
		voyo_is_logged_in = userinfo ? true : false;
		voyo_is_first_call = false;
		
		if(voyo_is_logged_in) {
			setInterval(function() {
				var credits = jQuery(".ewallet-credits").text();
				if(!credits){ return; }
				credits = credits.match(/(\d*\.\d*).*/)[1];
				if(!credits) { return; }
				
				var display_name = voyo_user.name;
				if(!display_name || display_name == "") {
					display_name = voyo_user.username;
				}
				jQuery(".js_voyoFullName").text(display_name);
				jQuery(".js_voyo_charge_credits_credit_indicator").text(
					credits
				);
			}, 500);
		}
			
		var display_name = userinfo.name;
		if(!display_name || display_name == "") {
				display_name = userinfo.username;
		}
		jQuery(".ewallet-profile").text(display_name);
		setTimeout(function() { jQuery(".ewallet-profile").text(display_name); }, 500);
		return;
	}
	
	if(userinfo && !voyo_is_logged_in) {
		window.location.reload();
	}
	
	if(!userinfo && voyo_is_logged_in) {
		window.location.reload();
	}
};

function CZ_close_display_settings() {
	open_banner();
	Cookie.remove('user_settings_pending', { duration: -2, path: '/' });
	mbox_close();
	return false;
};

function CZ_voyo_goto_user_settings() {
	CZ_close_display_settings();
	window.location = "/user/";
};

function CZ_voyo_show_charge_credits() {
	CZ_close_display_settings();
	jQuery(".charge-credit").click();
};

var voyo_display_settings_extras = "";
jQuery(function() {
	jQuery.ajax("/static/cz/microsites/voyo/html/display_settings_extra.html", {
		success: function(data, _, _) {
			voyo_display_settings_extras = data;
		}
	});
});

var CZ_inject_voyo_settings = function() {
	setTimeout(function() {
		jQuery(".registration_box .hline").first().before(
			"<div class='clear hline'></div>" +
			voyo_display_settings_extras
		);

		jQuery(".js_voyo_user_credits_indicator").empty().append(
			jQuery(".ewallet-credits").clone()
		);
	}, 100);
};

var init_dialog_watcher = null;
var init_dialog_watcher_repeat = 30;
var block_init_dialog_watcher = false;

// Display voyo link in standard user settings form
function CZ_voyo_display_settings() {
	var res = display_settings();
	var dialog_watcher = null;
	block_init_dialog_watcher = true;
	dialog_watcher = setInterval(function() {
		var reg_form = jQuery(".registration_box");
		if(reg_form.length != 0 && jQuery(reg_form).parents("html").length != 0) {
			CZ_inject_voyo_settings();
			clearInterval(dialog_watcher);
			dialog_watcher = null;
		}
	}, 40);
	return res;
}

// Try to locate settings form on page load
init_dialog_watcher = setInterval(function() {
	if(block_init_dialog_watcher) {
		clearInterval(init_dialog_watcher);
		init_dialog_watcher = null;
		return;
	}

	var reg_form = jQuery(".registration_box");
	if(reg_form.length != 0 && jQuery(reg_form).parents("html").length != 0) {
		CZ_inject_voyo_settings();
		clearInterval(init_dialog_watcher);
		init_dialog_watcher = null;
	}
	init_dialog_watcher_repeat--;
	if(init_dialog_watcher_repeat <= 0) {
		clearInterval(init_dialog_watcher);
		init_dialog_watcher = null;
	}
}, 50);

jQuery(function() {
	if(Cookie.get("PBZReject")) {
		Cookie.remove("PBZReject", {duration: -1, path: "/"});
		jQuery.ajax("/static/cz/microsites/voyo/html/transaction_failed.html", {
			success: function(data, _, _) {
				lbox(data);
			}
		});
	}

	if(Cookie.get("PBZAccept")) {
		Cookie.remove("PBZAccept", {duration: -1, path: "/"});
		jQuery.ajax("/static/cz/microsites/voyo/html/transaction_successful.html", {
			success: function(data, _, _) {
				lbox(data);
			}
		});
	}
});

function voyo_open_payment_info_window(url) {
	window.open(url, 'win', 'width=540, height=450, resizable=no, scrollbars=no, toolbar=no, status=no, menubar=no, location=no');
	return false;
}

js_ewallet_funds = function(){
		jQuery(".lbox .close").click();
		jQuery(".charge-credit").click();
		return false;
}
