// jQuery UI-Enhancements active on every page

jQuery(document).ready(function() {
	
	// Update all LOGIN links
	jQuery('a[href$=\\\/user\\\/login\\\/loginAjax]').live('click', function(event) {
		event.preventDefault();
		sponsorPost_jqeui_showhead(jQuery(this).attr('href'));
	});
	
	// Update all REGISTRATION links
	jQuery('a[href$=\\\/user\\\/registration\\\/registrationAjax]').live('click', function(event) {
		event.preventDefault();
		sponsorPost_jqeui_showhead(jQuery(this).attr('href'));
	});
	
	// Update all RECOVERY links
	jQuery('a[href$=\\\/user\\\/recovery\\\/recoveryAjax]').live('click', function(event) {
		event.preventDefault();
		sponsorPost_jqeui_showhead(jQuery(this).attr('href'));
	});
	
	// Update all ADMIN links
	jQuery('a[href$=\\\/admin\\\/dashboard\\\/indexAjax]').live('click', function(event) {
		event.preventDefault();
		sponsorPost_jqeui_showhead(jQuery(this).attr('href'));
	});
	
	jQuery('.headpopup_close').live('click', function() {
		sponsorPost_jqeui_hidehead();
	});
	
	jQuery('#form-login-ajax').live('submit', function(event) {
		event.preventDefault();
		var url    = jQuery(this).attr('action');
		var params = jQuery(this).serialize();
		jQuery.ajax({
			url: url,
			data: params,
			method: 'post',
			dataType: 'json',
			success: function(data) {
				if(data.result == 'success') {
					// clean error field
					jQuery('#form-login-ajax-errors ul').html('');
					if(data.login == 'valid') {
						location.reload();
					} else {
						jQuery('#form-login-ajax-errors').show();
						jQuery.each(data.errors, function(fieldname, errors) {
							jQuery.each(errors, function(index, error) {
								jQuery('#form-login-ajax-errors ul').append('<li class="error-'+fieldname+'">'+error+'</li>');
							});
						});
					}
				}
			}
		});
	});
	
	jQuery('#form-register-ajax').live('submit', function(event) {
		event.preventDefault();
		var url    = jQuery(this).attr('action');
		var params = jQuery(this).serialize();
		jQuery.ajax({
			url: url,
			data: params,
			method: 'post',
			dataType: 'json',
			success: function(data) {
				if(data.result == 'success') {
					// clean error field
					jQuery('#form-register-ajax-errors ul').html('');
					if(data.register == 'valid') {
						if(data.reload) {
							location.reload();
						}
						if(data.html) {
							jQuery('#headpopup_content').html(data.html);
						}
						if(data.show) {
							sponsorPost_jqeui_showhead(data.show);
						}
					} else {
						jQuery('#form-register-ajax-errors').show();
						jQuery.each(data.errors, function(fieldname, errors) {
							jQuery.each(errors, function(index, error) {
								jQuery('#form-register-ajax-errors ul').append('<li class="error-'+fieldname+'">'+error+'</li>');
							});
						});
					}
				}
			}
		});
	});
	
});

function sponsorPost_jqeui_showhead(url) {
	if(!jQuery('#headpopup_wrapper').hasClass('shown')) {
		// clean head elements
		jQuery('#headpopup_content').html('');
		// load and display partial template from "url"
		jQuery.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			success: function(data) {
				if(data.result == 'success') {
					// place html code in content div and remember the request
					jQuery('#headpopup_content').html(data.html).data('ajaxcall', url);
					// add the class to show
					jQuery('#headpopup_wrapper').addClass('shown').show('blind', { easing: 'easeInOutCirc' }, 500);
				}
			}
		});		
	} else {
		// only load if the new request is different from the one already loaded
		if(url !== jQuery('#headpopup_content').data('ajaxcall')) {
			sponsorPost_jqeui_hidehead(url);			
		} else {
			sponsorPost_jqeui_hidehead();
		}
	}
}

function sponsorPost_jqeui_hidehead(url) {
	jQuery('#headpopup_wrapper').removeClass('shown').hide('blind', { easing: 'easeInOutCirc' }, 500, function() {
		jQuery('#headpopup_content').html('').data('ajaxcall', '');
		if('undefined' != typeof(url)) {
			sponsorPost_jqeui_showhead(url);
		}
	});
}

