var COrganizationsInterface = Class.create();

COrganizationsInterface.prototype = Object.extend( (new CCnfDialog('logout-container')), {
    initialize: function(config)
    {
        this.options = config;
		this.request = null;
		this.timer   = false;
		this.hideDetailsTimeOut  = 2000;
    },
	
	init: function()
	{
		var rows = $$('table[class="data-table"] tbody tr');
        if (rows)
        {
            rows.invoke('observe', 'mouseout', function(evt) { var el = Event.findElement(evt,'tr'); $(el).removeClassName('hover'); } );
            rows.invoke('observe', 'mouseover', function(evt) { var el = Event.findElement(evt,'tr'); $(el).addClassName('hover'); } );
			rows.invoke('observe', 'click', this.showDetailsDescription.bindAsEventListener(this) );
	        rows.invoke('observe', 'mouseover', this.hideTimerReset.bindAsEventListener(this) );
	        rows.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) );
        }
		
		var searchForm = $(this.options['searchForm']);
		if ( searchForm )
		{
			var inputs = searchForm.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;
                }
            });
		}
	},
	
	detailsLayerPosition: function(el)
	{
		var commentBlock = $(this.options['commentBlock']);
        if (commentBlock) 
		{
			commentBlock.setStyle({
				'display': 'block'
			});
			
			var top = Utils.getTopPos(el) + 20;
			var left = Utils.getLeftPos(el);
			
			var blockH = commentBlock.getHeight();
			if ((top - blockH) > 0) {
				var top = (top - blockH - 30);
				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");
				}
			}
		}
	},
	
	successDetailsLoading: function(response)
	{
		var commentBlock = $(this.options['commentBlock']);
        if (commentBlock)
        {
            var blockContent = commentBlock.down('div.block-content');
            if (blockContent)
            {
				blockContent.update(response.responseText);
            }
			
			this.detailsLayerPosition(this.request.options['sourceEl']);
            Event.observe(commentBlock, 'mouseover', this.hideTimerReset.bindAsEventListener(this) );
            Event.observe(commentBlock, 'mouseout', this.hideTimerSet.bindAsEventListener(this) );
        }
        Utils.removeLoadingEffect($('winContainer_loading'));
        this.request = null;
	},
	
	showDetailsDescription: function(evt)
	{
		var el = Event.findElement(evt, 'tr');
		if (el)
		{
			this.detailsLayerPosition(el);
			var orgId = el.down('div[id]').identify();
			Utils.setLoadingEffect($(this.options['commentBlock']), 'winContainer_loading', 'commonLoadingEffect');
            this.request = new Ajax.Request(this.options['detailsUrl']+orgId+'/', {
                'onSuccess':this.successDetailsLoading.bind(this),
				'sourceEl':el
            });
		}
		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);
    }
});

var orgInterfaceObj = new COrganizationsInterface({
	'commentBlock':'comment-block',
	'searchForm':'organization_search'
});
