/**
 * Functions for claiming business
 *
 */

// wrapper object <=> namespace for related funcs
var ClaimBusiness = {};

ClaimBusiness.X_AJAX_INFO = 'users_claim_business';

ClaimBusiness.initialize = function() {

	ClaimBusiness.box = $('#claim_business');
	
	if( $('#logged_in').val() != 1 ) {
		
		var frmOpen = ClaimBusiness.showNoAuthBox;
		
	} else {

		ClaimBusiness.boxContent = $('.content', ClaimBusiness.box);
		
		var frmOpen = function(hash) {
			var ajax_url = 'http://' + location.hostname + '/' + $('a.claim_business').attr('href'); // 'claim-business.html';
			
			$.post(ajax_url, {'id_company' : $('#id_company').val()}, function(response_data) { // , {ts: (new Date()).getTime()}
				ClaimBusiness.boxContent.html(response_data);
//				hash.w.css('opacity', 1).show();
				hash.w.show();
				
				$(ClaimBusiness.box).jqmAddClose($('#companyCloseButton', ClaimBusiness.box));
				$(ClaimBusiness.box).center();
				$('#closeButton').hide();
				$('#submitBusiness', ClaimBusiness.box).click(
					function() {
						var businessOptions = {
								url			: location.href,
								data		: { X_AJAX_INFO : ClaimBusiness.X_AJAX_INFO,
												ts			: (new Date()).getTime(),
												'action'    : 'action_submit'},
								success		: ClaimBusiness.updateCheck,
								dataType	: 'json' // 'text'
						};
						$('#claimBusinessForm', ClaimBusiness.box).ajaxSubmit(businessOptions);
						return false;
					}
				);
			});
		};
	}
	
	ClaimBusiness.box.jqm({trigger: 'a.claim_business',  overlay: 0, onShow: frmOpen});
	$('a.claim_business').click(function(){
		ClaimBusiness.box.jqmShow();
		return false;
	});
}

ClaimBusiness.updateCheck = function(data, status, form) {
//	alert(ClaimBusiness.renderObj(data));return;

	if(data.status == 'success') {
		alert('Your claim will be checked by our staff.');
		window.location.reload();
	} else {
		// show error message
		alert('Please correct the following errors:\n' + ClaimBusiness.renderValidationErrors(data.errors, '-').replace(/@/ig,''));
	}
	return false;
}

ClaimBusiness.renderValidationErrors = function(err_obj, start_char) {
	var e = '';
	var nl = '';
	for(var i in err_obj) {
		for(var j in err_obj[i]) {
			e += nl + start_char + ' ' + err_obj[i][j];
			nl = "\n";
		}
	}
	return e;
}

ClaimBusiness.showNoAuthBox = function(hash) {
	var getBoxOptions = {
			url			: location.href,
			type		: 'POST',
			data		: { X_AJAX_INFO : 'users_noauth_box',
							ts			: (new Date()).getTime(),
							action		: encodeURIComponent('claim the business'),
							btn			: encodeURIComponent('Is This Your Business?')
						  },
			success		: function(box_content) {
							  $('.content', ClaimBusiness.box).html(box_content);
//							  hash.w.css('opacity', 1).show();
							  hash.w.show();
							  $(ClaimBusiness.box).jqmAddClose($('#companyCloseButton', ClaimBusiness.box));
							  $(ClaimBusiness.box).center();
						  },
			dataType	: 'html'
	};
	$.ajax(getBoxOptions);
}

$(document).ready(function() {
	ClaimBusiness.initialize(); 
});
