/* Effect.js

This document sets the menu effect as well as applying IE6 PNG fix using mootools.

@author: Andrew Lay
@date: 15/07/2009

*/
function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

var isIE6 = getInternetExplorerVersion() == 6;

// menu JS
window.addEvent('domready', function(){
	
	// if javascript is enabled, load the script enabled stylesheet
	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = 'style_script.css';
	cssNode.media = 'screen';
	headID.appendChild(cssNode);
	
	// IE6 PNG fix
	if (isIE6) {
		 $$('div').each(function(el){
			// for some elements, this causes an error in IE so use try/catch
			try {
				// fix for background image
				var imgURL = el.getStyle('background-image');
				var imgURLLength = imgURL.length;
				if ( imgURL != 'none' && imgURL.indexOf(".png") > 0){
					el.setStyles({
						background: '',
						filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='scale', src='" + unescape(imgURL.substring(5,imgURLLength  - 2)) + "')"
					});
				};
			} catch (e) {}			
		})
		$$('img[src$=png]').each(function(el) {
			try {
				var newEl = new Element('input', {
					'styles': {
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + el.getProperty('src') + "')",
					'position': 'relative',
					'background': 'transparent'
					},
					'title': el.getProperty('alt')
				}).setStyles(el.getStyles('padding','margin','border','height','width','display')).setProperties(el.getProperties('id','class')).setProperty("disabled", "true");
				newEl.replaces(el);
			} catch (e) {}
		});
	}
	
	// drop down list
	$('menu').getElements('li').each( function(elem) {	
											   
		elem.getElements('a').each(function(aElem) {
			aElem.addEvents({
				'mouseenter': function(){
					this.tween('opacity', '0.01'); // set opacity to 0.01, or there will be issues when it's fully transparent
				},
				'mouseleave': function(){
					this.tween('opacity', '1');				
				}
			});			
		});	
		elem.getElements('ul').each(function(subElem) {
			var targetH = subElem.getElements('li').length*35+'px';
			elem.addEvents({
				'mouseenter': function(){
					subElem.set('tween', {
						duration: 500,
						transition: Fx.Transitions.Quad.easeOut
					}).tween('height', targetH);					
				},
				'mouseleave': function(){
					subElem.set('tween', {
						duration: 500, 
						transition: Fx.Transitions.Quad.easeOut
					}).tween('height', '0px');
				}
			});
		})							 
	})
});
