
/**
* Make any tag into a clickable link. Just give it an href attribute.
*
* @param xxx $xxx	xxx
* @param xxx $xxx	xxx
* @return	xxx
* @access public
*/
function setLinks() {
	/* make any non <a> tag with an href into a link */
	/* <div href='http://www.google.com'>Click Me</div> */
	var links = $(document.body).getElements('*[href]');	// get the elements with an href
	links.each(
			function(el,i) {
				if(el.get('tag') != 'a') {	// if it is not a <a> tag
					el.addEvents(
						{
							'click' : function() {
									  	document.location = this.getProperty('href');
									  }
						}
					);
					if(!$(el.getProperty('nav'))) {												// if it does not have a subnav in the dom
						el.addEvents(
							{
								'mouseover': function(){
												el.set('class','selected');						// add the selected class
												el.setStyles({'cursor':'pointer'});				// give it a pointer
											 },
								'mouseout' : function() {
												el.removeClass('selected');
											 }
							}
						);
					}
				}
			});
}


function setSubnavs(pos,padding) {
	/* associate sublinks with their parents */
	/* example <a href='#' nav='myDivID' />Navigation</a><div id='myDivID'>Sublinks</div> */

	var linkTimer;		// subnav hide timer
	var currentNav;		// last subnav shown
	var links = $(document.body).getElements('*[nav]');	// get the navigation links with defined subnavs

	links.each(			// go through all the found links
		function(el,i) {
			var subnav = $(el.getProperty('nav'));	// get the id of the subnav from the attribute 'nav'
			if(subnav) {
				subnav.store('nav', el);
				el.addEvents(
					{
						'mouseover': function(){
										$clear(linkTimer);					// clear the linkTimer
										if(currentNav) {
											currentNav.hide();		// hide the previous subnav
											currentNav.retrieve('nav').removeClass('selected');
										}
										el.set('class','selected');	// add the selected class
										el.setStyles({'cursor':'pointer'});
										currentNav = subnav;					// set it globally
										subnav.show();						// show the current subnav
									 },
						'mouseout' : function() {
										// linkTimer = currentNav.hide.delay(50,currentNav);	// hide the subnav on a timer
										if(currentNav) linkTimer = (
															function(){
																currentNav.hide();
																currentNav.retrieve('nav').removeClass('selected');
															}
														).delay(50,currentNav);
									 }
					}
				);

				if(subnav) {
					subnav.addEvents(
						{
							'mouseover' : function() {
											$clear(linkTimer);					// clear the timer
										  },
							'mouseout' :  function() {
											// if(currentNav) linkTimer = currentNav.hide.delay(50,currentNav);	// hide the subnav on a timer
											if(currentNav) linkTimer = (
																function(){
																	currentNav.hide();
																	currentNav.retrieve('nav').removeClass('selected');
																}
															).delay(50,currentNav);
										  }
						}
					);
					subnav.hide();
				}
			}
		}
	);
	setSubnavPositions(links);

	// sometimes the subnav's are not positioned correctly because all the images are not loaded - this will go through them again
	// and reset their position after the images have loaded - so subnav's maybe positioned incorrectly until all the images are loaded.
	window.addEvent('load', function(){
		setSubnavPositions(links);
	});
}

function setSubnavPositions(links) {
	if(!$chk(links)) return;
	links.each(			// go through all the found links
		function(el,i) {
			var subnav = $(el.getProperty('nav'));	// get the id of the subnav from the attribute 'nav'
			var pos = el.getProperty('nav_pos');	// get position
			var pad = el.getProperty('nav_pad').toInt();	// get padding
			if(subnav) {
				var coords = el.getCoordinates(subnav.getParent());			// get location of the element
				var size = subnav.getSize();
				switch(pos) {
					case 'top':
						subnav.setStyles(
							{
								'z-index' : '10',
								'position' : 'absolute',
								'top' : coords.top-(size.y+pad),
								'left' : coords.left
							}
						).hide();
						break;
					case 'left':
						subnav.setStyles(
							{
								'z-index' : '10',
								'position' : 'absolute',
								'top' : coords.top,
								'left' : coords.left-(size.x+pad)
							}
						).hide();
						break;
					case 'right':
						subnav.setStyles(
							{
								'z-index' : '10',
								'position' : 'absolute',
								'top' : coords.top,
								'left' : coords.left+(size.x-pad)
							}
						).hide();
						break;
					default :
						subnav.setStyles(										// remove and position the element
							{
								'z-index' : '10',
								'position' : 'absolute',
								'top' : coords.bottom+pad,
								'left' : coords.left
							}
						).hide();
						break;
			   }
			}
	});
}

function setToolTips() {
	/* Add a tooltip to all elements with a tip attribute */
	/* Example <a href='#' tip='Title::Text' /> */


	var as = $(document.body).getElements('*[tip]');	// get all the items with a tip attribute

	as.each(
		function(el,i) {
			var c = el.getProperty('tip').split('::');
			el.store('tip:title', c[0]);
			el.store('tip:text', c[1]);
		}
	);

	var tip = new Tips(									// add the tip to all of the items
		as,
		{
			fixed: false,
			hideDelay: 50,
			showDelay: 50
		}
	);

	tip.addEvents({										// fade them in and out
			'show': function(t) {
						t.fade('in');
					},
			'hide': function(t) {
						t.fade('out');
					}
	});
}

