/*
====================================================
Name: RTM product tour script
Version: 1.0
Author: Apples To Oranges - Ryan Nichols
Date: 8/31/2006
Requires: moo.fx.js, prototype.lite.js, ato-popup.js
====================================================
*/

serena = {};
serena.ui = {};

serena.ui.rtm = {

	//pseudo constructor
	_initiate : function() {
		var self = serena.ui.rtm;
		
		self._selectedIndex = 0;
		self._links = self._toArray($('tour-nav').getElementsByTagName('li'));
		self._items = self._getChildType($('tour-screenshots'), 'LI');
		self._foot = self._toArray($('tour-footnav').getElementsByTagName('li'));
		self._text = self._toArray($('tour-desc').getElementsByTagName('div'));
		self._nextLink = $('tour-next').getElementsByTagName('a')[0];
		self._nextFootLink = $('tourfoot-next').getElementsByTagName('a')[0];
		self._links.pop();
		//connect to tour links both the header and footer sets								
		for(var j = 0; j <= self._links.length; j++) {
			if(self._links[j] != null) {
				var item = self._links[j].getElementsByTagName('a')[0];
				var foot = self._foot[j];	
				item.index = foot.index = j;
				item.onclick = foot.onclick = function() {					
					self.selectItem(this.index);
					return false;
				}
			}			
		}

		//connect to the 'next' link.
		self._nextLink.onclick = self._nextFootLink.onclick = function() {
			self._hideAll();			
			if((self._selectedIndex + 1) < self._links.length) {
				self.selectItem(self._selectedIndex + 1);
			} else {
				self.selectItem(0);
			}	
			return false;		
		}

		//create manager for tool tips
		self.tooltips = new ato.ui.PopUpManager();
		var t = [];
		self._toArray(document.getElementsByClassName('tooltips', $('tour-screenshots'))).each(function(g) {
			var h = self._toArray(self._getChildType(g, 'LI')).each(function(j) { 
				t.push(j);
			});			
		});
		t.each(function(li, index) {			
			self.tooltips.create({
				element: document.getElementsByClassName('tooltip', li)[0],
				hoverTriggers: [li.getElementsByTagName('a')[0]],
				isExclusive: true
			});
			li.getElementsByTagName('a')[0].onclick = function() { return false; }
		});
		
	},

	//selects an item based in an index number
	selectItem : function(index) {
		var self = serena.ui.rtm;
		self._hideAll();
		Element.removeClassName(self._items[index], 'hide');
		Element.addClassName(self._links[index], 'current');
		Element.addClassName(self._foot[index], 'current');
		Element.removeClassName(self._text[index], 'hide');
		self.tooltips.hideAll();
		self._selectedIndex = index;
		
		
		
		for(i=0;i<=6;i++){
			
			
			if(self._selectedIndex == i)
			  {
				  	
				
				  document.getElementById("display_content"+i).setAttribute("class", "greymenus");
				  
				  
			  }
			  else{
				  	
					document.getElementById("display_content"+i).setAttribute("class", "blackmenu");
					
			  }
		}
		
		
	},

	//hides all other items and removes the current class if it is set
	_hideAll : function(item) {
		var self = serena.ui.rtm;		
		for(var i = 0; i <self._items.length; i++) {
			Element.addClassName(self._items[i], 'hide');
			Element.addClassName(self._text[i], 'hide');
					}
		for(var k = 0; k <self._links.length; k++) {
			Element.removeClassName(self._links[k], 'current');
			Element.removeClassName(self._foot[k], 'current');			
		}
	},

	//convenience method to get a particular node that is a direct descendent of the parent
	_getChildType: function(parent, name) {
		var a = [];
		for(var i = 0; i < parent.childNodes.length; i++) {
			var child = parent.childNodes[i];
			if(child.nodeName == name) { a.push(child); }
		}
		return a
	},

	_toArray : function(collection) {
		var a = new Array();
		for(var i = 0; i < collection.length; i++) {
			a.push(collection[i]);
		}
		return a;
	}
}

function trace(m) {
	console.log(m)
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(serena.ui.rtm._initiate);



