/*
 * Corllete Lab Theme - e107.org
 * 
 * Copyright (C) 2006-2009 Corllete ltd (clabteam.com)
 * Support and updates at http://www.free-source.net/
 * License - http://www.free-source.net/licenses/design.txt
 * 
 * Theme e107.org - JS init
 */

var CLToggle     = function(container, ev) {
	ev.stop();
	if (!$(container).visible())  new Effect.SlideDown(container, { duration: 0.5, queue: { scope: container, limit: 1 } }); 
	else                          new Effect.SlideUp(container, { duration: 0.5, queue: { scope: container, limit: 1 } });
}

var CLToggleNav  = function(trigger, container)
{
	var c = $$('#'+container+' .fadeto1');
	
	c.invoke('setOpacity', 0);
	
	$(trigger).observe('mouseover', function(ev) {
			c.each(function(el) { CLSmartMorph(el, el.identify(), 'opacity: 1', 0.5); });
			ev.stop();
		})
		.observe('mouseout', function(ev){
			c.each(function(el) { CLSmartMorph(el, el.identify(), 'opacity: 0', 0.5); });
			ev.stop();
		});
};
var CLSmartMorph = function(el, qid, style, duration) {
	Effect.Queues.get(qid).invoke('cancel');
	new Effect.Morph(el, {
	  style: style,
	  queue: { scope: qid, limit: 1 },
	  duration: duration ? duration : 0.3
	});
};

// based on qTip - http://qrayg.com/learn/code/qtip
var tooltip = {
  name : "e-tip",
  offsetX : 15,
  offsetY : 15,
  opacity : 0.85,
  selectors: '.e-tooltip', // comma separated selectors
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml", sTitle, elements, tipContainer;
	
	elements = $$(this.selectors);
	if(!elements.length) return;
	
	if(!tipContainerID){ var tipContainerID  = this.name}
	tipContainer = document.getElementById(tipContainerID);
	

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = $(this.name);
	if (this.tip) { 
		document.onmousemove = function (evt) {tooltip.move (evt)};  
		this.tip.setOpacity(this.opacity);
	};
	elements.each(function(el) {
		sTitle = el.getAttribute("title");	
		if(sTitle)
		{
			el.setAttribute("tiptitle", sTitle);
			el.removeAttribute("title");
			el.removeAttribute("alt");
			el.observe('mouseover', function(ev) { ev.stop(); tooltip.show(el.getAttribute('tiptitle')); });
			el.observe('mouseout', function(ev) { ev.stop(); tooltip.hide(); } );
		}
	});
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if(!this.tip) return;
	
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}


// Psilo misc
var initializetabcontent = function (id) { 
	$(id).select('li').each(function(li){
		var a = li.select('a')[0];
		if(!li.hasClassName('selected')) {
			$(a.rel).hide();
		}
		
		a.observe('click', function(ev) {
			ev.stop();
			var c = ev.findElement('a');
			toggletabcontent(id, c);
		});
	}); 
};
var toggletabcontent = function (container, clicked) { 
	var c = $(container).select('li'), a;
	c.each(function(li) {
		li.removeClassName('selected');
		a = li.select('a')[0];
		$(a.rel).hide();
	});
	clicked.up('li').addClassName('selected');
	$(clicked.rel).show();
}


document.observe('dom:loaded', function(event) {
	// BoF Socials fade
	var social = $$('.fade img');
	
	social.invoke('setOpacity', 1)
		.invoke('observe', 'mouseover', function(ev){
			var el = ev.findElement('img'), qid = el.identify(); ev.stop();
			CLSmartMorph(el, qid, 'opacity: 0.6', 0.5);
		})
		.invoke('observe', 'mouseout', function(ev){
			var el = ev.findElement('img'), qid = el.identify(); ev.stop();
			CLSmartMorph(el, qid, 'opacity: 1', 0.5);
		});
	// EoF Socials fade

	// BoF Accordions
/*
	if ($('faq-container'))
	{
		var Accordion_FAQ = new accordion('faq-container', {
			activate: 1,
			classNames : {
				toggle : 'accordion-toggle',
				toggleActive : 'accordion-toggle-active',
				content : 'accordion-content'
			}
		});
	}
	if ($('downloads-container'))
	{
		var Accordion_DOWNLOADS = new accordion('downloads-container', {
			activate: 1,
			classNames : {
				toggle : 'accordion-toggle',
				toggleActive : 'accordion-toggle-active',
				content : 'accordion-content'
			}
		});
	}*/

	// EoF Accordions
	
	// BoF Toggled menus
	if ($('fs-login'))  $('fs-login').select('.l-toggle' ).invoke('observe', 'click', CLToggle.curry('fs-login-container'));
	// EoF Toggled menus

	// BoF FBox Navi Fade
	if ($('fbox-navigation-fade')      && $('fbox-navigation'))  		CLToggleNav('fbox-navigation-fade',      'fbox-navigation');
	// EoF FBox Navi Fade
	
	tooltip.init();
	
});
