var Tips = new Class({
 
	getOptions: function(){
		return {
			onShow: function(tip){
				tip.setStyle('visibility', 'visible');
			},
			onHide: function(tip){
				tip.setStyle('visibility', 'hidden');
			},
			maxTitleChars: 30,
			showDelay: 100,
			hideDelay: 100,
			className: 'tool',
			offsets: {'x': 16, 'y': 16},
			fixed: false
		};
	},
 
	initialize: function(elements, options){
		// this checks for IE 5 and 6, then adds an iframe to the document to act as a shim 
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) { 
			if(!$('DivShim')){ 
				var divShimProperties = {
					'id': "DivShim", 
					'src': "javascript: document.write('');",
					'scrolling': "no",
					'frameborder': 0
				}; 
				var divShimStyles = {
					'position': "absolute",
					'top': "0px",
					'left': "0px",
					'display': "none", 
					'font-size': "6px", 
					'padding': "0px"
				}; 
				var divShim = new Element('iframe').setProperties(divShimProperties).setStyles(divShimStyles);
				divShim.injectInside(document.body); 
			} 
		} 
 
		this.setOptions(this.getOptions(), options);
		this.toolTip = new Element('div').addClass(this.options.className+'-tip').setStyles({
			'position': 'absolute',
			'top': '0',
			'left': '0',
			'visibility': 'hidden'
		}).injectInside(document.body);
		this.wrapper = new Element('div').injectInside(this.toolTip);
		$each(elements, function(el){
			this.build($(el));
		}, this);
		if (this.options.initialize) this.options.initialize.call(this);
	},
 
	build: function(el){
		el.myTitle = el.href ? el.href.replace('http://', '') : (el.rel || false);
		if (el.title){
			var dual = el.title.split('::');
			if (dual.length > 1) {
				el.myTitle = dual[0].trim();
				el.myText = dual[1].trim();
			} else {
				el.myText = el.title;
			}
			el.removeAttribute('title');
		} else {
			el.myText = false;
		}
		if (el.myTitle && el.myTitle.length > this.options.maxTitleChars) el.myTitle = el.myTitle.substr(0, this.options.maxTitleChars - 1) + "&hellip;";
		el.addEvent('mouseover', function(event){
			this.start(el);
			this.locate(event);
		}.bindWithEvent(this));
		if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
		el.addEvent('mouseout', this.end.bindWithEvent(this));
	},
 
	start: function(el){
		this.wrapper.setHTML('');
		if (el.myTitle){
			new Element('span').injectInside(
				new Element('div').addClass(this.options.className+'-title').injectInside(this.wrapper)
			).setHTML(el.myTitle);
		}
		if (el.myText){
			new Element('span').injectInside(
				new Element('div').addClass(this.options.className+'-text').injectInside(this.wrapper)
			).setHTML(el.myText);
		}
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this);
	},
 
	end: function(event){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this);
		event.stop();
	},
 
	locate: function(event){
		var win = {'x': window.getWidth(), 'y': window.getHeight()};
		var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
		var tip = {'x': this.toolTip.offsetWidth, 'y': this.toolTip.offsetHeight};
		var prop = {'x': 'left', 'y': 'top'};
		for (var z in prop){
			var pos = event.page[z] + this.options.offsets[z];
			if ((pos + tip[z] - scroll[z]) > win[z]) pos = event.page[z] - this.options.offsets[z] - tip[z];
			this.toolTip.setStyle(prop[z], pos + 'px');
		};
 
		this.toolTip.setStyle('z-index', 1000);
 
		// added the zindex here 
		// make sure the iframe moves with our tooltip
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) { 
			$('DivShim').setStyle('z-index', 999);
			for (var z in prop) {
				var pos = event.page[z] + this.options.offsets[z];
				if ((pos + tip[z] - scroll[z]) > win[z]) {
					pos = event.page[z] - this.options.offsets[z] - tip[z];
				}
				$('DivShim').setStyle(prop[z], pos + 'px');
			}
		} 
		event.stop();
	},
 
	show: function(){
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) { 
			var IfrRef = $('DivShim'); 
			var myValues = this.toolTip.getCoordinates();
 
			IfrRef.style.width = myValues['width'] + "px";
			IfrRef.style.height = myValues['height'] + "px";
			IfrRef.style.display = "block";
		} 
		this.fireEvent('onShow', [this.toolTip]);
	},
 
	hide: function(){
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) { 
			var IfrRef = $('DivShim'); 
			IfrRef.style.display = "none"; 
		} 
		this.fireEvent('onHide', [this.toolTip]);
	}
 
});
 
Tips.implement(new Events);
Tips.implement(new Options);

/**
 *  Functions used on Home page
 */


Menu = {
	
	menu_timers: new Object(),
	/**
	 * Initializes entire menu system
	 */
	initialize: function() {
		l1li = $E('#main_menu ul').getChildren();
		for (var i = 0;i<l1li.length;i++) {// looping through 1st level menu LI's
			if (i+1 == l1li.length) l1li[i].addClass('last');
			if (!l1li[i].getProperty('id')) l1li[i].setProperty('id','mainmenuitem_'+(i+1));
			if ($E('ul',l1li[i])) {
				Menu.menu_timers[l1li[i].getProperty('id')] = null;
				var sm = $E('ul',l1li[i]); 
				sm.setStyle('display','none');
				sm.setStyle('opacity',0);
				sm.getChildren()[sm.getChildren().length-1].addClass('last');
				l1li[i].addEvents({
					'mouseenter':Menu.enter,
					'focus':Menu.keyEnter
				});
				l1li[i].addEvents({
					'mouseleave':Menu.leave
				});
			};
		};
	},
	
	/**
	 * onMouseEnter event handler (for first level links)
	 */
	enter: function() {
		Menu.menu_timers[this.getProperty('id')] = $clear(Menu.menu_timers[this.getProperty('id')]);
		Menu.showFx($E('ul',this));
	},
	
	/**
	 * onKeyEnter event handler (for first level links)
	 */
	keyEnter: function() {
		thissm = $E('ul',this);
		Menu.hideOther(thissm);
		Menu.showFx(thissm);
	},
	
	/**
	 * onMouseLeave event handler (for first level links)
	 */
	leave: function(e) {
		Menu.menu_timers[this.getProperty('id')] = Menu.hideFx.delay(200,this,$E('ul',this));
	},
	
	/**
	 * Hides all submenus except for the one passed as param
	 * @param {Object} sm - the UL (submenu) to keep shown - all the other will be hidden
	 */
	hideOther: function(sm2keep) {
		if (!$defined(sm2keep)) return false;
		var lis = $E('#main_menu ul').getChildren();
		lis.each(function(li) {
			csm = $E('ul',li);
			if ($defined(csm) && sm2keep != csm) Menu.hideFx(csm)
		})
	},
	
	/**
	 * Fades passed element
	 * @param {Object} element - the object to fade
	 */
	hideFx: function( element ) {
		if (!element) return false;
		var fx = new Fx.Styles(element, {
			duration:100,
			transition: Fx.Transitions.Quart.easeInOut, 
			wait:false,
			onComplete: function(item) {
				item.setStyle('display','none');
			}
		});
		fx.start({
			'opacity': 0
		});
	},
	/**
	 * Appears passed element
	 * @param {Object} element - the object to appear
	 */
	showFx: function( element ) {
		var fx = new Fx.Styles(element, {
			duration:200,
			transition: Fx.Transitions.Quart.easeInOut, 
			wait:false
		});
		element.setStyle('display','block');
		fx.start({
			'opacity': 1
		});
	}
}
