var CCharitySelectorInterface = Class.create();

CCharitySelectorInterface.prototype = Object.extend( (new CCnfDialog('logout-container')), {
    initialize: function(config)
    {
        this.options = config;
		this.request = null;
		this.timer   = false;
		this.hideDetailsTimeOut  = 2000;
		this.submitParentForm = true;
    },
	
	init: function()
	{
		this.initCarityForm();
		this.initSelectForm();
		this.initMoreInfo();
	},
	
	initMoreInfo: function()
	{
		var links = $$(this.options['moreSelector']);
        links.each(function(el) { el.onclick = function() { return false }; } )
        links.invoke('observe', 'click', this.showDetailsDescription.bindAsEventListener(this) );
		links.invoke('observe', 'mouseover', this.hideTimerReset.bindAsEventListener(this) );
        links.invoke('observe', 'mouseout', this.hideTimerSet.bindAsEventListener(this) );
		
		var commentBlock = $(this.options['commentBlock']);
		if (commentBlock)
		{
			var links = commentBlock.getElementsBySelector('a[rel="close"]');
			links.each(function(el) { el.onclick = function() { return false }; } )
            links.invoke('observe', 'click', this.hideDetailsDescription.bindAsEventListener(this) );
		}
	},
	
	showDetailsDescription: function(evt)
	{
		var el = Event.findElement(evt, 'a');
		if (el)
		{
			var commentBlock = $(this.options['commentBlock']);
			if (commentBlock)
			{
				var blockContent = commentBlock.down('div.block-content');
				if (blockContent)
				{
					var container = el.up().next('div[id]');
					if (container)
					{
						blockContent.update(container.innerHTML);
					}
					else
					{
						blockContent.update('');
					}
                    var top = Utils.getTopPos(el);
                    var left = Utils.getLeftPos(el);
                    commentBlock.setStyle({
                        'display':'block'
                    });
					
					var blockH = commentBlock.getHeight();
					if ( (top-blockH) > 0 )
					{
						var top = (top-blockH);
						commentBlock.setStyle({
                            'visibility':'visible',
                            'left':left+'px',
                            'top':top+'px'
                        });
						var arr = commentBlock.down('div.block-top-arrow');
                        if (arr)
                        {
                            arr.removeClassName("block-top-arrow");
                            arr.addClassName("block-bottom-arrow");
                        }
					}
					else
					{
						commentBlock.setStyle({
                            'visibility':'visible',
                            'left':left+'px',
                            'top':(top-10)+'px'
                        });
						var arr = commentBlock.down('div.block-bottom-arrow');
						if (arr)
						{
							arr.removeClassName("block-bottom-arrow");
							arr.addClassName("block-top-arrow");
						}
					}
					Event.observe(commentBlock, 'mouseover', this.hideTimerReset.bindAsEventListener(this) );
					Event.observe(commentBlock, 'mouseout', this.hideTimerSet.bindAsEventListener(this) );
				}
			}
		}
		return false;
	},
	
	hideDetailsDescription: function()
    {
        var commentBlock = $(this.options['commentBlock']);
        if (commentBlock)
        {
            commentBlock.setStyle({
                'display':'none'
            });
			if (this.timer) clearTimeout(this.timer);
        }
        return false;
    },
	
	/*
    * Sets timeout
    */
    hideTimerSet: function()
    {
        if (this.timer) clearTimeout(this.timer);
        this.timer = setTimeout(this.hideDetailsDescription.bind(this), this.hideDetailsTimeOut);
    },

    /*
    * Resets timeout
    */
    hideTimerReset: function()
    {
        if (this.timer) clearTimeout(this.timer);
    },
	
	initCarityForm: function()
	{
		var f = $(this.options['searchForm']);
		if (f)
		{
			f.onsubmit = function() { return false; }
            Event.observe(f, "submit", this.submitSearchForm.bindAsEventListener(this) );
			
			var inputs = f.getInputs('text');
            inputs.invoke('observe', 'focus', function(evt){
                if (this.value == this.alt)
                {
                    this.value = '';
                }
            });
            inputs.invoke('observe', 'blur', function(evt){
                if (this.value.empty())
                {
                    this.value = this.alt;
                }
            });
		}
	},
	
	initSelectForm: function()
	{
		var links = $$(this.options['pageNavigatorSelector']);
		links.each(function(el) { el.onclick = function() { return false }; } )
        links.invoke('observe', 'click', this.sendRequest.bindAsEventListener(this) );
		
		var f = $(this.options['selectForm']);
		if (f)
		{
			f.onsubmit = function() { return false; }
            Event.observe(f, "submit", this.submitSelectForm.bindAsEventListener(this) );
		}
	},
	
	sendRequest:function(evt)
	{
		if (this.request) return;
		
		var el = Event.element(evt);
		if (el)
		{
			Utils.setLoadingEffect($(this.options['windowContainer']), 'winContainer_loading', 'commonLoadingEffect');
			this.request = new Ajax.Request(el.href, {
                'onSuccess':this.successFormLoading.bind(this)
            });
		}
		return false;
	},
	
	submitSelectForm: function(evt)
	{
		if (this.request) return;
        
        var f = Event.findElement(evt, 'form');
        
        Utils.setLoadingEffect($(this.options['windowContainer']), 'winContainer_loading', 'commonLoadingEffect');
        this.request = f.request({
            'onComplete':this.successSelFormLoading.bind(this)
        });
        return false;
	},
	
	successSelFormLoading: function(response)
	{
		popupWindow.successLoading(response);
        Utils.removeLoadingEffect($('winContainer_loading'));
        this.request = null;
	},
	
	submitSearchForm: function(evt)
	{
		if (this.request) return;
		
		var f = Event.findElement(evt, 'form');
        
		Utils.setLoadingEffect($(this.options['windowContainer']), 'winContainer_loading', 'commonLoadingEffect');
		this.request = f.request({
			'onComplete':this.successFormLoading.bind(this)
		});
		return false;
	},
	
	successFormLoading: function(response)
	{
		popupWindow.successLoading(response);
		Utils.removeLoadingEffect($('winContainer_loading'));
		this.request = null;
	},
	
	afterSelecting: function(name, id)
	{
		if (interfaceObj)
		{
			var el = $(interfaceObj.options['charityField']);
			if (el)
			{
				el.setValue(name);
				var el = $(interfaceObj.options['charityFieldId']);
	            if (el)
	            {
	                el.setValue(id);
					if (this.submitParentForm)
					{
						var selInPopup = $(interfaceObj.options['charityFieldSelectedInPopup']);
						if (selInPopup)
						{
							selInPopup.setValue(1);
						}
						interfaceObj.submitCharityForm( el.up("form") );
					}
					else
					{
						this.getSupportedCharity();
					}
	            }
			}
		}
		this.initClose();
	},
	
	initClose: function()
	{
		var links = $$('input[name="close"]');
        links.each(function(el) { el.onclick = function() { return false }; } )
        links.invoke('observe', 'click', popupWindow.deactivate.bindAsEventListener(popupWindow) );
	},
	
	getSupportedCharity: function()
	{
		Utils.setLoadingEffect($(this.options['fieldContainerId']), 'fieldContainer_loading', 'commonLoadingEffect');
        this.request = new Ajax.Request(this.options['fieldUpdaterURL'], {
            'onSuccess':this.successFieldLoading.bind(this),
			'postBody':this.options['fieldCharityName']+'='+$(this.options['fieldCharityName']).getValue()+'&'+this.options['fieldCharityId']+'='+$(this.options['fieldCharityId']).getValue()
        });
	},
	
	successFieldLoading: function(response)
	{
		var fieldContainerId = $(this.options['fieldContainerId']);
		if (fieldContainerId)
		{
			fieldContainerId.update(response.responseText);
		}
		Utils.removeLoadingEffect($('fieldContainer_loading'));
        this.request = null;
	},
	
	initClearAction: function()
	{
		var fieldContainerId = $(this.options['fieldContainerId']);
        if (fieldContainerId)
        {
            var links = fieldContainerId.getElementsBySelector('a[rel="confirm"]');
			links.each(function(el) { el.onclick = function() { return false }; } )
            links.invoke('observe', 'click', this.clearCharityConfirmation.bindAsEventListener(this) );
        }
	},
	
	clearCharityConfirmation: function(evt)
    {
		var el = Event.findElement(evt,'a');
		var XY = interfaceObj._calcCenterViewportPosition();
		interfaceObj.okButtonTitle = interfaceObj.okClearCharityButtonTitle; 
		interfaceObj.initCnfButtons();
		interfaceObj.displayDialog ( el, XY[0]-150, XY[1], {
            'ok':this.clearSupportedCharity.bind(this)
        } );
        
		return false;
    },
	
	clearSupportedCharity: function()
	{
		$(this.options['fieldCharityName']).setValue('');
        $(this.options['fieldCharityId']).setValue('');
		this.getSupportedCharity();
		interfaceObj.hideCnf();
	}
});

var charityInterfaceObj = new CCharitySelectorInterface({
	'windowContainer':'win-form',
	'searchForm':'searchCharity',
	'selectForm':'selectCharity',
	'moreSelector':'a[rel="more"]',
	'commentBlock':'comment-block',
	'pageNavigatorSelector':'div.pageNavigator a',
	'fieldContainerId':'selCharityOptions'
});

