// DIE G-CONTAINER SPRACHE >> ON THE FLY PARAMS
function GContLingua(){
	this.followers = null;
		
	var inPlaceTransform = function(ev){	
		var e = new Event( ev );
		if (!$chk(e.target)){
			return;
		}
		
		var target = e.target;
		var parent = e.target.parentNode;
		
		if(target.tagName.toLowerCase() == "input" || target.tagName.toLowerCase() == "textarea" ){
			val = trimSpaces(stripTags(target.value));
		}
		
		else{ 
			val = trimSpaces(stripTags(target.innerHTML));
		}
		
		if(target.getAttribute("mode") == "edit"){
			var classname = (val == target.defaultText) ? target.origClass : "print";
			bFly = new Element("div", { 'mode' : 'view'}).setHTML( val );
						
			gCont.inPlaceEdit(bFly);
			
			bGetFocus = false;
		}
		
		else{
			// CREATE NEW INPUT
			if(target.editTag.toLowerCase() == "input"){
				bFly = new Element("input", { 'type' : 'text', 'value' : val });
			}
			
			// CREATE TEXTAREA
			else if(target.editTag.toLowerCase() == "textarea"){
				bFly = new Element("textarea", { 'styles' : { 'width' : "100%" } }).setHTML( val );
			}
			
			bFly.setAttribute("mode", "edit");
			bFly.transform = inPlaceTransform;
			bFly.addEvent("blur", bFly.transform);
			
			bGetFocus = true;
		}
				
		bFly.setAttribute("id", target.id);
		bFly.defaultText = trimSpaces(target.defaultText);
		bFly.origClass = target.getAttribute("class");
		bFly.editTag = target.editTag;
			
		parent.replaceChild(bFly, target);	
		
		if(bGetFocus){
			bFly.focus();
		}
	};
	
	//// SETUP
	this.preLoad = function(){	
		this.followers = [];
		this.dl_els = document.getElementsByTagName('dl');
	
		// FOR ANY ELEMENT THAT HAS THE RESPECTIVE ATTRIBUTE - ADD THIS EVENT LISTENER
		for (var i = 0; i < this.dl_els.length; i++){
			var el = $(this.dl_els[i]);
					
			// HEIGHT MATCHING
			var val = el.getAttribute("heightMatch");
			if(val){  
				this.heightMatch(el, val);
			}
			
			// MIN HEIGHT
			var val = el.getAttribute("minHeight");
			if(val){  
				this.minHeight(el, val);
			}
			
			// MAKE-FOLLOW
			if(el.getAttribute("makeFollow")){  
				this.makeFollow(el);
			}
			
			// V-ALIGNED
			if(el.getAttribute("vAlign")){  
				this.vAlign(el);
			}
			
			// IN-PLACE EDITING
			var tag = el.getAttribute("inPlaceEdit");
			if( tag ){  
				this.inPlaceEdit(el, tag);
			}
		}
	};
	
	this.heightMatch = function(el, other){
		var other_side = $(other);
		if(!other_side){
			return;
		}
		
		el.style.minHeight = other_side.offsetHeight + "px";
	};
	
	this.minHeight = function(el, val){
		el.style.minHeight = val;
	};
	
	this.vAlign = function(el){
		var par = el.parentNode;
		var height = par.getSize().size.y;
		el.style.position = "relative";
		el.style.top = (height/2 - 1)  + "px";
	};
	
	this.makeFence = function(id, color){
		var bridge = document.getElementById(id);
		bridge.style.border = '1px solid' + color;
		
		var block = bridge.firstChild;
		var max_size = block.offsetHeight;
			
		while(block !== null && block.id !== ''){
			if(max_size < block.offsetHeight) max_size = block.offsetHeight;
			block = block.nextSibling;
		}
		
		block = bridge.firstChild;
		while(block !== null && block.id !== ''){
			block.style.height = max_size + "px";
			
			if(block != bridge.firstChild){
				block.style.borderLeft = '1px dotted' + color;
			}
			
			block = block.nextSibling;
		}
	};

	this.makeFollow = function(el){
		el.yOffset = el.offsetTop;
		left = el.offsetLeft;
		width = el.offsetWidth;
		
		// SET THEY STYLE AND POSITION ATTRS
		el.style.position = "absolute";
		el.style.top = (el.offsetTop) + "px";
		el.style.left = (left + 6) + "px";
		el.style.width = (width) + "px";
		
		// ADD TO THE LIST OF YOUR DISCIPLES
		if(!this.followers || this.followers.length === 0){
			this.followers[0] = el;
		}
		else{
			this.followers[this.followers.length] = el;
		}
		
		setInterval(function(){gCont.moveFollowers();}, 1);
	};
	
	this.moveFollowers = function(){
		for(i = 0; i < this.followers.length; i++){
			lamb = this.followers[i];
			var coords = lamb.getCoordinates();
			var y = coords.top;
			var sy = window.getScrollTop();
			lamb.setStyle("top", ((sy - y + lamb.yOffset) / 3) + y);
		}
	};
	
	this.inPlaceEdit = function(el, tag){
		el.transform = inPlaceTransform;	
		el.editTag = tag;		
		el.defaultText = trimSpaces(el.innerHTML);
		el.origClass = el.className;
			
		el.addEvent("click", el.transform);
	};
}

// DYNAMIC DL-EL
function DLL(title, object){
	this.title = title;

	this.displayNode = function(bg){
		if(!bg){
			bg = "bg_b0bec7";
		}
	
		head = document.createElement("div");
		head.className = bg + " padded_light overflowed";
		
		title_el = document.createElement("a");
		title_el.className = "sided_medium column threefourths subtle";
		//title_el.style.cursor = "all-scroll";
		title_el.innerHTML = this.title;
				
		icon_div = document.createElement("div");
		icon_div.className = "fRight";
			
		// QUESTION MARK BUTTON
		q = document.createElement("img");
		q.alt = 'Q';
		q.src = 'images/Icons/icon.question-mark.gif';
		q.setAttribute('class', 'pointer');
		q.onmouseover = function(e){this.obj.help(true);};
		q.onmouseout = function(e){this.obj.help(false);};
		
		// CLOSE BUTTON
		close_b = document.createElement("img");
		close_b.alt = 'x';
		close_b.src = 'images/Icons/icon.close_b.gif';
		close_b.setAttribute('class', 'pointer');
		close_b.setAttribute('title', 'Remove');
		close_b.onclick = function(e){this.obj.remove();};
				
		// TOGGLE BUTTON
		toggle = document.createElement("img");
		toggle.alt = 'Tog';
		toggle.src = 'images/Icons/icon.minus.gif';
		toggle.setAttribute('class', 'pointer');
		toggle.setAttribute('title', 'Toggle');
		title_el.onclick = toggle.onclick = function(e){this.obj.toggle(this.obj);};
		
		toggle.obj = close_b.obj = title_el.obj = q.obj = object;	// OBJECTS
		
		icon_div.appendChild(q);
		icon_div.appendChild(toggle);
		icon_div.appendChild(close_b);
		 
		head.appendChild(title_el);
		head.appendChild(icon_div);
	
		return head;
	};
}

var gCont = new GContLingua();

window.addEvent("domready", function(){ 
	gCont.preLoad();
});

