/* mootools sethtml ie6 support for tables patch */
Element.extend({
	setHTML:function(html){
		var tagName = this.getTag();
		
		if(window.ActiveXObject && tagName.test(/thead|tbody|tr|td/)){
		/*- for mootools v1 if(window.ie &#38;& tagName.test(/thead|tbody|tr|td/)){ */		
			var div = new Element('div'), depth;
			switch (tagName) {
	 	        case 'thead':
		        case 'tbody': div.innerHTML = '<table><tbody>'+html+'</tbody></table>';depth = 2;break;
		        case 'tr': div.innerHTML = '<table><tbody><tr>'+ html+'</tr></tbody></table>';depth = 3;break;
		        case 'td': div.innerHTML = '<table><tbody><tr><td>'+html+'</td></tr></tbody></table>';depth = 4;
	      	}
			$A(this.childNodes).each(
				function(node){this.removeChild(node)}, this);
			for(var i=0; i< depth; i++) div = div.firstChild;
			$A(div.childNodes).each(
				function(node){this.adopt(node)}, this);
		}else{
			this.innerHTML = html;
		}
		return this;
	}
});

Element.extend({
	within:function(_x, _y){
		var obj = $(this).getCoordinates();	
		if(_x >= obj.left && _x <= (obj.left + obj.width) &&
		_y >= obj.top && _y <= (obj.top + obj.height)	){		
			return true;
		}else{		
			return false;
		}
	}
});

zero =  0.0001; //hmmm dont know what this is about
one = 0.99999; // 1 and 0 cause flicker in opacity

var hoveron;
var hoveroff;

function pageloaded(){
	    // get rid of any iframes
	if (top!=self) top.location.replace(location.href);	
	attachrollovers();
	document.onmousedown = hideAllFunction.bindWithEvent(document);
	try{
	    Lightbox.init();
	}catch(e){}
}
function pressbutton(element){
	//trace(element+" "+state);
	btn = $(element).getFirst();
	btn.setStyle('background-image', 'url('+btn.src+')'); // set the background to the current image
	btn.setStyle('opacity', zero); // hide the current image
	var fade =  new Fx.Style(btn, 'opacity', {
			duration: 400, transition: Fx.Transitions.sineOut
		});
	
	if(btn.src.indexOf("_off") > 0)	
		btn.src = btn.src.replace(/_off/g,'_on');// replace the current image with the on state
	else
		btn.src = btn.src.replace(/_on/g,'_off');
	
	
	fade.stop();
	fade.start(zero, one); // fade in the new image
}

function showdropdown(button, element, optionsObj){	

	var myEffects = new Fx.Styles(element, {
								  duration: 650, 
								  transition: Fx.Transitions.quadOut, 
								  onStart: function(){
									  pressbutton(button); // swap the button image
									  
									  },
								  onComplete: function(){
								  	prepareHeight(button, element, false);
								  }
								});
	_currentheight = $(element).offsetHeight;
	_maxheight = $(element).scrollHeight;
	
	if(_currentheight == 0 ){
		prepareHeight(button, element, true);
		
		if(optionsObj){
			_left = $(button).getLeft() + optionsObj.left ;
			_top = $(button).getTop() + optionsObj.top ;
		}else{
			//_left = $(button).getLeft() + 8 ;
			//_top = $(button).getTop() + 22 ;
			_left = $(button).getLeft() ;
			_top = $(button).getTop() + $(button).scrollHeight ;
		}
		$(element).setStyle('left', _left+"px");
		$(element).setStyle('top', _top+"px");
		
		myEffects.start({
		'height': [0, _maxheight]
		,'opacity': [0, 1]		
		});

	}

}

/* attach fade in, fade out On state image event to main menu rollovers
required mootools.style.js
*/
function attachrollovers(){
	var atags = $$('#mainNav a')
	
	
	var split_url=document.URL.split('/');	
	var myfolder = split_url[(split_url.length -2 )];
	var mypage = split_url[(split_url.length -1 )];
	
	atags.each(function(element, key){
		var img = element.getFirst();
		element.setStyle('background-image', 'url('+img.src+')'); // set the background image to the current image
		
		
		var imgfader =  new Fx.Style(element.getFirst(), 'opacity', {
			duration: 400, transition: Fx.Transitions.sineOut, onComplete: function(){ // Speed of the fade
				
				
			}
		});
		
		var quickfade =  new Fx.Style(element.getFirst(), 'opacity', {
			duration: 10, transition: Fx.Transitions.sineOut,
			onComplete : function(){img.src = img.src.replace(/_off/g,'_on');}	// replace nav image "_off" with "_on" eg 'menu_home_off.gif' 'menu_home_on.gif'
		});
		quickfade.start(one, zero); /* hide the red images onload */
		
		if(! activesection){
			activesection = new Object();
		}
		if(element.href == document.URL ){
			setActiveLink(element);
		}
		if(activesection){
			//var splits =element.href.split('/');				
			//var elementpage = splits[(splits.length -1 )];
			//trace(element.name +":"+activesection);
			if(element.name == activesection)
			setActiveLink(element);		
		}
		
		element.onmouseover = function(){
			imgfader.stop();	
			
			slideoutmenu = function(_name){
					hoveron = true;
					$(_name).setStyle("height", 0+"px");	
					(function(){
						if(hoveron === true){						 
						 _height = $(_name).scrollHeight;	  
						 					 
						  var slide = new Fx.Styles($(_name), {duration: 200, transition: Fx.Transitions.quadOut, onComplete: function(){} });
				  		  slide.start({'height': [0, _height]});
						 
						}
					}).delay(300);
				}
			
			if(element.name == "movies"){						
				_left = $(element).getLeft() + 1;
				_top = $(element).getTop() + 26 ;			
				$("moviedropdown").setStyle('left', _left+"px");
				$("moviedropdown").setStyle('top', _top+"px");
				slideoutmenu("moviedropdown");
			}
			if(element.name == "giftshop"){				
				_left = $(element).getLeft() - 58;
				_top = $(element).getTop() + 26 ;			
				$("giftshopdropdown").setStyle('left', _left+"px");
				$("giftshopdropdown").setStyle('top', _top+"px");
				slideoutmenu("giftshopdropdown");
			}
			imgfader.start(zero, one);
		}
		element.onmouseout = function(){
					
				slideinmenu = function(_name){
					(function(){
						if(hoveron !== true){
						  imgfader.stop();
						  imgfader.start(one, zero);						  
						  var _size = $(_name).getSize();
						  _height =  _size.size.y;						 
						  var slide = new Fx.Styles($(_name), {duration: 200, transition: Fx.Transitions.quadIn, onComplete: function(){} });
				  		  slide.start({'height': [_height, 0]});
						 
						}
					}).delay(200);
				}
					
				{
					imgfader.stop();
					imgfader.start(one, zero);	
						
				}
		}
		
	});
}
function setActiveLink(atag){
	var img = atag.getFirst();
	img.src = img.src.replace(/_off/g,'_on');
	atag.setStyle('background-image', 'url('+img.src+')');
}

activesection = null;

window.onload = pageloaded;