Agenda.Info = Class.create();
Agenda.Info.prototype = {
    initialize: function(oClass, cAttr) {
        this.oClass = oClass;
        this.cAttr = cAttr;

	    this.detectInfo(this.oClass, this.cAttr);
    },
    
    detectInfo: function() {
        //this.dId = $A(document.getElementsByTagName('div')).map(Element.extend);
        this.dId = $$('.tableRow');
        //console.log(this.dId);
        
        this.sInfo = this.showInfo.bindAsEventListener(this);
        
        /*for(var i=0; i < this.dId.length; i++) {
            if(this.dId[i].getAttribute(this.cAttr)) {
                Event.observe(this.dId[i], 'click', this.sInfo);
            }
        }*/
        this.dId.each(function(a) {
        	if(a.getAttribute(this.cAttr)) {
        		Event.observe(a, 'click', this.sInfo);
        	}
        }.bind(this));
    },
    
    showInfo: function(evnt) {
        this.evnt = evnt;
        
        if(Event.element(this.evnt).nodeName == 'A') {
        	//NIETSSSSS
        } else if(Event.element(this.evnt).nodeName == 'SPAN') {
            this.elem = Event.element(this.evnt).parentNode.parentNode.getAttribute(this.cAttr);
            this.cElem = Event.element(this.evnt).parentNode.parentNode;  
        } else if(Event.element(this.evnt).className.match(/tableData/)) {
        	//alert(Event.element(this.evnt).className);
            this.elem = Event.element(this.evnt).parentNode.getAttribute(this.cAttr);
            this.cElem = Event.element(this.evnt).parentNode;
        } else {
        	this.elem = Event.element(this.evnt).getAttribute(this.cAttr);
        	this.cElem = Event.element(this.evnt);
        }
        
        if(Event.element(this.evnt).nodeName != 'A') {
        	if(!$(this.elem)) {
	        	this.qElemA = this.elem.split('.');
	        	this.qElem = this.qElemA[1];
	        	
	        	this.options = {
	        		asynchronous: false,
	        		method: 'get',
	        		parameters: '?sid=' + this.qElem,
	        		onSuccess: function(t) {
	        			this.parseInfo(t, this.elem, this.cElem);
	        		}.bind(this)
	        	}
	        	
	        	new Ajax.Request('/backbase/php/includes/ajax/agenda.ajax.php', this.options);
        	} else {
        		new Effect.BlindUp(this.elem, { duration: 0.5, afterFinish: function() { Element.remove(this.elem); }.bind(this) });
            	this.cElem.className = 'tableRow tContainer';
        	}
        }
    },
    
    parseInfo: function(t, elem, elem2) {
    	this.elem = elem;
    	this.elemArr = elem.split('.');
    	this.response = t.responseText.evalJSON(true);
    	this.cElem = elem2;
        
        this.sElem = document.createElement('div');
        this.sElem.id = this.elem;
        this.sElem.className = 'tShowInfo';
        this.sElem.style.display = 'none';
        
        this.sElemImg = document.createElement('img');
        this.sElemImg.src = this.response.event_img;
        this.sElemImg.setAttribute('alt', 'Flyer');
        this.sElemImg.style.cssText = 'height:72px';
        
        this.sElemUL = document.createElement('ul');
        
        this.sElemLIT = document.createElement('li');		this.sElemLIT.innerHTML = '<span class="sL">Time:</span><span>' + this.response.event_time + '</span>';

        this.sElemLIF = document.createElement('li');		this.sElemLIF.innerHTML = '<span class="sL">Fee:</span><span>' + this.response.event_fee + '</span>';

        this.sElemLIM = document.createElement('li');		this.sElemLIM.innerHTML = '<span class="sL">Music:</span><span>' + this.response.event_music + '</span>';

        this.sElemLID = document.createElement('li');		this.sElemLID.innerHTML = '<span class="sL">Line-up:</span><span>' + truncate(this.response.event_djs) + '</span>';

        this.sElemLIW = document.createElement('li');
        this.sElemLIW.innerHTML = "<span class='sL'>Website:</span><a target='_blank' href=" + this.response.event_website + ">" + this.response.event_website.replace('http://www.', '').replace('/', ''); + "</a>";
        
        this.sElemUL.appendChild(this.sElemLIT);	this.sElemUL.appendChild(this.sElemLIF);	this.sElemUL.appendChild(this.sElemLIM);
        this.sElemUL.appendChild(this.sElemLID);	this.sElemUL.appendChild(this.sElemLIW);
        
        this.sElemClear = document.createElement('div');
        this.sElemClear.className = 'clear';
        
    	this.dateArr = this.response.event_date.split('-');
	   	this.date = this.dateArr[0] + '-' + this.dateArr[1] + '-' + this.dateArr[2];
	   	
        this.sElemA = document.createElement('a');
        this.sElemA.style.cssText = 'padding-left:10px;';
        this.sElemA.href = "/agenda/" + this.date + '/' + this.response.event_name.toLowerCase() + '/';
        this.sElemA.innerHTML = "Go to event &raquo;";
        
        this.sElem.appendChild(this.sElemImg);	this.sElem.appendChild(this.sElemUL);	this.sElem.appendChild(this.sElemClear);
        this.sElem.appendChild(this.sElemA);
        
        new Insertion.After(this.cElem, this.sElem);
        
        new Effect.BlindDown(this.elem, { duration: 0.5 });
        this.cElem.className += ' tActive';

    }
}

function truncate(text) {
	len = 70;
	trunc = text;
	
	if(trunc.length > len) {
		trunc = trunc.substring(0, len);
	    trunc = trunc.replace(/\w+$/, '...');
	}
	
	return trunc;
}