﻿// Gate Baron Funds USA page. Opens up modal window with options for investors
BF.fundGate = {

	init: function () {

		// scope alias
		var self = this;

		this.fundCookieName = 'bf-usa-fund';
		this.fundCookieValue = '';
		// dynamic content to place within modal window
		var fundHTML = '<div class="gate"><div class="logoModal"><span class="offscreen">Baron Funds</span></div>';
		fundHTML += '<div class="welcome"><h2>Baron USA Partners Fund</h2><p>Baron offers a BVI Fund via private placement for accredited non-U.S. investors and qualified tax-exempt U.S. institutions.</p>';
		fundHTML += '<p class="legal-intro">Please select from the following:</p></div>';
		fundHTML += '<div class="choices">';
		fundHTML += '<ul class="fund-choices"><li><a href="#" id="non-us-person"><strong class="h4">Non-U.S. Person</strong></a></li>';
		fundHTML += '<li><a href="#" id="us-institution"><strong class="h4">Tax-exempt U.S. Institution</strong></a></li></ul>';
		fundHTML += '<div class="gate-loader" style="display: none;"></div>';
		fundHTML += '<div class="legal-copy" style="display: none;"></div>';
		fundHTML += '</div>'; // end choices
		fundHTML += '<div class="modalCTAs"><a href="#" class="modal-cancel">Cancel</a></div>';
		fundHTML += '</div>';

		var fundGate = new BF.modal({
			triggers: 'a.baron-funds-usa',
			content: 'dynamic',
			contentTarget: $.trim(fundHTML),
			callbacks: {
				afterSetup: function () {
					Cufon.refresh('h2');
					// cancel link, closes modal, reverts to original state
					$('.modal-cancel').live('click', function (e) {
						e.preventDefault();
						fundGate.close();
					});

					$('.fund-choices').delegate('a', 'click', function (e) {
						e.preventDefault();
						var url = '';
						if (this.id === 'non-us-person') {
							self.fundCookieValue = 'accredited';
							self.loadAgreement(window.siteRoot + '/Non-US-Investors/Accredited-Shareholder-Status/');
						} else {
							self.fundCookieValue = 'qualified';
							self.loadAgreement(window.siteRoot + '/Non-US-Investors/Qualified-Purchaser-Status/');
						}
					});
				},
				afterClose: function (e) {
					// reset modal
					$('.legal-copy').hide().css({ 'visibility': 'hidden' }); // IE7 bug
					$('ul.fund-choices').show();
					$('.legal-intro').text('Please select from the following:');
					$('.modal-cancel').removeClass('right');
					$('.modal-continue').remove();

					// redirect to Our Products page, if gate was triggered by redirecting to placeholder page (var for url set in server-side redirect page template)
					if (gatewayRedirectToPage) {
						window.location = gatewayRedirectToPage;
					}
				}

			}
		});
	},

	loadAgreement: function (path) {
		var self = this; // scope alias
		var loader = $('.gate-loader'),
        fundChoices = $('ul.fund-choices');
		var xhr = $.ajax({
			url: path,
			dataType: 'html',
			beforeSend: function () {
				// hide choices
				fundChoices.hide();
				loader.show();
			},
			success: function (response) {
				var copy = $(response).find('.legal'),
                    legal = $('.legal-copy'),
                    intro = $('.legal-intro'),
                    cancelBtn = $('.modal-cancel'),
                    continueBtn = $(response).find('.continue');
				legal.empty().css({ 'visibility': 'visible' });
				legal.append(copy);
				loader.fadeTo(250, 0, function (e) {
					// update copy
					$(this).hide();
					intro.text('Please read the following information carefully before proceeding:');
					legal.fadeTo(150, 1, function (e) {
						$(this).show();
					});
					continueBtn.addClass('modal-continue');
					cancelBtn.addClass('right');
					cancelBtn.before(continueBtn);

					continueBtn.bind('click', function (e) {
						e.preventDefault();
						BF.cookie.set(self.fundCookieName, self.fundCookieValue, 365);
						// make sure cookie is set before redirecting
						setTimeout('document.location = "' + $(this).attr('href') + '"', 100);
					});

				});
			}
		});
	}
};
