/* GA Google Analytics Pro - Opt-Out Box */

(function($) {
	
	'use strict';
	
	if (document.cookie.indexOf('gapro-close=1') > -1) {
		$('.ga-google-analytics-pro-optout').addClass('ga-google-analytics-pro-optout-closed');
		gapro_optout_close();
	} else {
		$('.ga-google-analytics-pro-optout').addClass('ga-google-analytics-pro-optout-open');
		gapro_optout_open();
	}
	
	if (document.cookie.indexOf('gapro-optout=1') > -1) {
		$('.ga-google-analytics-pro-optout-message').html(gapro.success_optout);
	}
	
	if (document.cookie.indexOf('gapro-optin=1') > -1) {
		$('.ga-google-analytics-pro-optout-message').html(gapro.success_optin);
	}
	
	document.addEventListener('click', function(e) {
		if (!e.target.matches('.ga-google-analytics-pro-optout-close')) return;
		e.preventDefault();
		gapro_cookie_close();
		gapro_optout_close();
	}, false);
	
	document.addEventListener('click', function(e) {
		if (!e.target.matches('.ga-google-analytics-pro-optout-info')) return;
		e.preventDefault();
		if ($('.ga-google-analytics-pro-optout').hasClass('ga-google-analytics-pro-optout-closed')) {
			gapro_cookie_remove('close');
			gapro_optout_open();
		} else {
			alert(gapro.info_text);
		}
	}, false);
	
	document.addEventListener('click', function(e) {
		if (!e.target.matches('.ga-google-analytics-pro-optout-link')) return;
		e.preventDefault();
		var box = $('.ga-google-analytics-pro-optout');
		box.find('.ga-google-analytics-pro-optout-message').html(gapro.success_optout);
		if (gapro.after_optin == 1) {
			gapro_cookie_close();
			gapro_optout_close();
		} else if (gapro.after_optin == 2) {
			gapro_cookie_close();
			box.hide();
		}
		gapro_cookie_remove('optin');
		gapro_cookie_optout();
	}, false);
	
	document.addEventListener('click', function(e) {
		if (!e.target.matches('.ga-google-analytics-pro-optin-link')) return;
		e.preventDefault();
		var box = $('.ga-google-analytics-pro-optout');
		box.find('.ga-google-analytics-pro-optout-message').html(gapro.success_optin);
		if (gapro.after_optin == 1) {
			gapro_cookie_close();
			gapro_optout_close();
		} else if (gapro.after_optin == 2) {
			gapro_cookie_close();
			box.hide();
		}
		gapro_cookie_remove('optout');
		gapro_cookie_optin();
	}, false);
	
	function gapro_optout_open() {
		var box = $('.ga-google-analytics-pro-optout');
		box.addClass('ga-google-analytics-pro-optout-open');
		box.removeClass('ga-google-analytics-pro-optout-closed');
		if (gapro.css_location == 0 || gapro.css_location == 1) {
			box.animate({'width': '100%', 'opacity': 1}, 300, function() {
				box.find('.ga-google-analytics-pro-optout-close').show();
				box.find('.ga-google-analytics-pro-optout-message').fadeIn(100);
			}).css('display', 'block');
		} else if (gapro.css_location == 2) {
			box.css({'display': 'block'});
			box.find('.ga-google-analytics-pro-optout-close').show();
			box.find('.ga-google-analytics-pro-optout-message').fadeIn(100);
		}
	}
	
	function gapro_optout_close() {
		var box = $('.ga-google-analytics-pro-optout');
		box.find('.ga-google-analytics-pro-optout-close').hide();
		box.find('.ga-google-analytics-pro-optout-message').hide();
		if (gapro.css_location == 0 || gapro.css_location == 1) {
			box.css({'display': 'block', 'right': 0, 'left': 'auto'}).animate({'width': '40px', 'opacity': 0.8}, 300);
		} else if (gapro.css_location == 2) {
			box.css({'display': 'block'});
		}
		box.addClass('ga-google-analytics-pro-optout-closed');
		box.removeClass('ga-google-analytics-pro-optout-open');
		if (gapro.disable_hide == 0) {
			if (document.cookie.indexOf('gapro-optout=1') > -1 || document.cookie.indexOf('gapro-optin=1') > -1) {
				if (gapro.css_location == 0 || gapro.css_location == 1) {
					box.fadeOut(300);
				} else if (gapro.css_location == 2) {
					box.hide();
				}
			}
		}
	}
	
})(jQuery);