mytime=0;

window.addEvent('domready',function(){
	
	//FUNCTIONS
	function hover_out(event)
	{
		var browse_pos = $('browse-panel').getPosition();
		var browse_size = $('browse-panel').getSize();		
		
		if( event.client.x < browse_pos.x || event.client.x > ( browse_pos.x + browse_size.x ) || 
		event.client.y < browse_pos.y || event.client.y > ( browse_pos.y + browse_size.y ) )
		{
			if(mytime==0){
				mytime = hide_panel_mouse.delay(2000);
			}
		}else{
			if(mytime!=0){				
				mytime = $clear(mytime);
				mytime=0;
			}			
		}
	}
	
	function hide_panel_mouse(){
		hidePanel();
		if(Browser.Engine.trident4){
			IE6ShowSelect();
		}
		document.removeEvent('mousemove', hover_out);		
	}	
	
	function showPanel(){
		$('browse-panel').setStyles
		({
			'right':0,
			'left':'auto'									
		})	
	}
	
	function hidePanel(){
		$('browse-panel').setStyles
		({
			'right':'auto',
			'left':'-9999px'									
		})
	}
	
	function IE6HideSelect(){
		$$('#container #right-panel .orange-contact-middle .form-row select').setStyles({
			'display':'none'				   
		});
	}
	
	function IE6ShowSelect(){
		$$('#container #right-panel .orange-contact-middle .form-row select').setStyles({
			'display':''				   
		});
	}
	
	
	//TRIGGERS		
	$('quick-jump').addEvent('click',function(event){
		showPanel()	
		if(Browser.Engine.trident4){
			IE6HideSelect();
		}		
		document.addEvent('mousemove', hover_out);		
	})	
	
	$('browse-close').addEvent('click',function(event){
		hidePanel();
		if(Browser.Engine.trident4){
			IE6ShowSelect();
		}
		//remove hover out event
		document.removeEvent('mousemove', hover_out);

	})



	//SETUP BLOCKQUOTES -- (Will fail gracefully if no-blockquotes exist)
	$$('blockquote').each(function(item)
	{
		item.getChildren().getLast().addClass('last-element');
	});

	function absolutize(elem)
	{
		var pos = $(elem).getCoordinates('container');

		//console.log(pos);
		//set to asolute with xy
		$(elem).setStyles(
		{
			'position':'absolute',
			'top':pos.top
		});

		//return orignal xy location as object
		return {x:pos.left, y:pos.top,height:pos.height}
	}

	//Scrolling div
	var offset = $("right-panel").getPosition();
	var origCoords = absolutize("right-panel");
	var CTANavSlideFX = new Fx.Tween("right-panel", { link:'cancel' });

	window.addEvent("scroll", function(event)
	{
		var vals = window.getScroll();

		var windowSize = window.getScrollSize();
		

		vals.y = (vals.y - offset.y)+ origCoords.y;
		var viewportSize = window.getSize();
		var currentCoords = $("right-panel").getCoordinates();

		//console.log("viewport size: "+viewportSize.y+"\nForm height: "+origCoords.height);

		if(viewportSize.y > origCoords.height)
		{
			if(vals.y>227)
			{
				//if scroll is passed the furthest point then fix slider distance
				/*350 = roughly size of footer plus a bit*/
				if(vals.y+10 > ((windowSize.y-450)-currentCoords.height))
					CTANavSlideFX.start('top', (windowSize.y-450)-currentCoords.height);
				else
					CTANavSlideFX.start('top', vals.y+10);
			}

			else
				CTANavSlideFX.start('top', origCoords.y);
		}
		else if(currentCoords.top>origCoords.y)
			CTANavSlideFX.start('top', origCoords.y);
		


	});
})

