var ISPM_Request = mint.Base.extend({
    ispm: null,
    
    el: 'ispm-request',
    selectEl: 'ispm-request-select',
    selectProvidersEl: 'ispm-request-select-providers',
    selectEmailEl: 'ispm-request-select-email',
    selectPhoneEl: 'ispm-request-select-phone',
    selectRemarksEl: 'ispm-request-select-remarks',
    confirmEl: 'ispm-request-confirm',
    confirmCodeEl: 'ispm-request-confirm-code',
    confirmEmailEl: 'ispm-request-confirm-email',
    doneEl: 'ispm-request-done',
    
    loaderEl: 'ispm-request-loader',
    
    selectSendErrorEl: 'ispm-request-select-send-error',
    selectEmailErrorEl: 'ispm-request-select-email-error',
    selectPhoneErrorEl: 'ispm-request-select-phone-error',
    selectProvidersErrorEl: 'ispm-request-select-providers-error',
    
    confirmCodeErrorEl: 'ispm-request-confirm-code-error',
    
    selectLoaderEl: 'ispm-request-select-loader',
    confirmLoaderEl: 'ispm-request-confirm-loader',
    
    lat: 0,
    lng: 0,
    
    email: "",
    phone: "",
    
    open: function(reset) {
	    $(this.el).show();
	    if(reset) this.reset();
    },
    
    close: function(reset) {
	    $(this.el).hide();
	    if(reset) this.reset();
    },
    
    reset: function() {
	with(this) {
	    closeSelect();
	    closeConfirm();
	    closeDone();
	    
	    $(selectProvidersEl).clear();
	    $(confirmEmailEl).clear();
	}
    },
    
    submitSelect: function() {
	var that = this, selectedProviders = [], list = $(this.selectProvidersEl).getElementsByTagName('input');
	
	for(var i = 0 ; i < list.length; ++i) {
	    if(list[i].checked) selectedProviders.push(parseInt(list[i].getAttribute('db_id')));
	}
	
	this.hideSelectErrors();
	
	this.email = $(this.selectEmailEl).value;
	this.phone = $(this.selectPhoneEl).value;
	
	$(this.confirmEmailEl).innerHTML = this.email;
	
	$(this.selectLoaderEl).show();
	
	new mint.Request({
	    url: this.ispm.base_url + 'ispm_remote/request',
	    method: "POST",
	    
	    params: {
		lat: this.lat,
		lng: this.lng,
		email: this.email,
		phone: this.phone,
		remarks: $(this.selectRemarksEl).value,
		providers: selectedProviders.join()
	    },
	    
	    events: {
		complete: function() {
		    $(that.selectLoaderEl).hide();    
		},
		
		done: function() {
		    switch(this.response) {
			case 'ok' : {
			    that.closeSelect();
			    //that.openConfirm();
			    that.openDone();
			    break;
			}
			case 'error_send' : {
			    $(that.selectSendErrorEl).show();
			    break;
			}
			case 'error_email' : {
			    $(that.selectEmailErrorEl).show();
			    break;
			}
			case 'error_phone' : {
			    $(that.selectPhoneErrorEl).show();
			    break;
			}
			case 'error_providers' : {
			    $(that.selectProvidersErrorEl).show();
			    break;
			}
		    }
		}
	    }
	});
    },
    
    hideSelectErrors: function() {
	$(this.selectSendErrorEl).hide();
	$(this.selectEmailErrorEl).hide();
	$(this.selectPhoneErrorEl).hide();
	$(this.selectProvidersErrorEl).hide();
    },
    
    openSelect: function() {
	$(this.selectProvidersEl).clear();
	
	for(var i in this.ispm.providers) {
	    $(this.selectProvidersEl).appendChild(createElement('li', {
		'innerHTML': "<input onclick='$ispm.request.toggleProvider(this)' type='checkbox' db_id='"+this.ispm.providers[i].id+"' id='ispm-request-provider-"+this.ispm.providers[i].id+"' checked /><label for='ispm-request-provider-"+this.ispm.providers[i].id+"'>"+this.ispm.providers[i].name+" ("+this.ispm.providers[i].city+")</label>"
	    }));
	}
	
	$(this.selectEl).show();
    },
    
    closeSelect: function() {
	$(this.selectEl).hide();
	$(this.selectLoaderEl).hide();
	this.hideSelectErrors();
    },
    
    submitConfirm: function() {
	var that = this, code = $(this.confirmCodeEl).value;
	
	this.hideConfirmErrors();
	
	$(this.confirmLoaderEl).show();
	
	new mint.Request({
	    url: this.ispm.base_url + 'ispm_remote/request_confirm',
	    method: "POST",
	    
	    params: {
		code: code,
		email: this.email
	    },
	    
	    events: {
		complete: function() {
		    $(that.confirmLoaderEl).hide();    
		},
		
		done: function() {
		    switch(this.response) {
			case 'ok' : {
			    that.closeConfirm();
			    that.openDone();
			    break;
			}
			case 'error_code' : {
			    $(that.confirmCodeErrorEl).show();
			    break;
			}
		    }
		}
	    }
	});
    },
    
    hideConfirmErrors: function() {
	$(this.confirmCodeErrorEl).hide();
    },
    
    openConfirm: function() {
	$(this.confirmEl).show();
    },
    
    closeConfirm: function() {
	$(this.confirmEl).hide();
	$(this.confirmLoaderEl).hide();
	this.hideConfirmErrors();
    },
    
    openDone: function() {
	$(this.doneEl).show();
    },
    
    closeDone: function() {
	 $(this.doneEl).hide();
    },
    
    showLoader: function() {
	$(this.loaderEl).show();
    },
    
    hideRequestLoader: function() {
	$(this.loaderEl).hide();
    },
    
    toggleProvider: function(el) {
	var label =  mint.$(mint.$(el).getNext('label')), li = mint.$(mint.$(el).getParent('li'));
	
	if(el.checked) {
	    label.setStyle('backgroundPosition', 'top right');
	    li.setStyle('backgroundPosition', 'top left');
	}
	else {
	    label.setStyle('backgroundPosition', 'bottom right');
	    li.setStyle('backgroundPosition', 'bottom left');
	}
    }
});