/* GLOBALS - this must come FIRST */

var initScripts = new Array();


/* UTILITIES */

function init() {
	var i;
	
	for ( i = 0; i < initScripts.length; i++ )
	{
		initScripts[ i ]();
	}
}

Event.observe( document, "dom:loaded", init );

// initialize the toc
initScripts.push( function() {
	var lis = $$("#toc #channels LI");
	var url = window.location.href;
	
	if ( url.substr( url.length - 6 ) == '/index' )
		url = url.substr( 0, url.length - 5 );
	
	lis.each( function( li ) {
		var a = $( li.getElementsByTagName( "A" )[0] );
		var h;
		
		if ( a )
		{
			h = a.href;
			
			if ( h.endsWith( '/index' ) )
				h = h.slice( 0, -5 );
			
			if ( h.indexOf( url ) == 0 )
			{
				a.addClassName( "currentItem" );
				throw $break;
			}
		}
	} );
} );

initScripts.push( function() {
	var menus = $$('LI.popupMenu');
	
	function setElementState( menu, state ) {
		var remove, insert;
		
		if ( state )
		{
			remove = "inactivePopup";
			insert = "activePopup";
		}
		else
		{
			remove = "activePopup";
			insert = "inactivePopup";
		}
		
		menu = $(menu);
		
		if ( menu.hasClassName( remove ) )
			menu.removeClassName( remove );
		
		menu.addClassName( insert );
	};
	
	function toggleMenu( menu, ev, state ) {
		if ( arguments.length <= 2 )
			state = ! menu.activeState;
		
		setElementState( menu, state );
		
		menu.activeState = state;
		
		ev.stop();
	};
	
	menus.each( function( menu ) {
		var d = menu.firstChild;
		
		while ( d && ( d.nodeType != 1 ) )
			d = d.nextSibling;
		
		if ( ! d )
			return;
		
		d.activeState = false;
		setElementState( d, false );
		
		menu.observe( 'mouseover', function( ev ) {
			toggleMenu( d, ev, true );
		});
		
		menu.observe( 'mouseout', function( ev ) {
			toggleMenu( d, ev, false );
		});
		
		if ( d.id != 'archivesPopup' )
		{
			var links = $A( d.getElementsByTagName( 'A' ) );
			
			links.each( function( el ) {
				el = $(el);
				
				el.observe( 'click', function( ev ) {
					setElementState( d, false );
				});
			});
		}
	});
	
	var controllers = $$('a.popupControlLink');
	
	controllers.each( function( controller ) {
		controller.observe( 'click', function( ev ) {
			ev.target.blur();
			
			var d = ev.target.parentNode;  // list item
			
			d = d.firstChild;
			while ( d && ( d.nodeType != 1 ) )
				d = d.nextSibling;
			
			if ( ! d )
				return;
			
			toggleMenu( d, ev );
		} );
	});
} );


initScripts.push( function() {
	var hiResLinks = $$( 'IMG.hires' );
	
	hiResLinks.each( function( preloadedImage ) {
		var img = preloadedImage.previousSibling.previousSibling;
		
		if ( "undefined" == typeof img )
			return;
		
		// debugger;
		
		img = $(img.firstChild);
		img.hiResURL = preloadedImage.src;
		
		img.observe( "mouseover", function( e ) {
			var img = e.element();
			
			if ( ImageViewer.showingImage() )
				return;
			
			if ( "undefined" == typeof img.largeImage )
			{
				img.largeImage = {
					src:		img.hiResURL,
					orig:		img,
					height:		0,
					width:		0,
					link:		img.parentNode.href
				};
				
				( new Image() ).src = img.hiResURL;
			}
			
			img.mouseWithin = true;
			
			window.setTimeout( function() {
				if ( img.mouseWithin )
					ImageViewer.showImage( img.largeImage );
			}, 1500);
		});
		
		img.observe( "mouseout", function( e ) {
			var img = e.element();
			
			if ( ( "undefined" == typeof img.mouseWithin ) || ! img.mouseWithin )
				return;
			
			img.mouseWithin = false;
		});
	});
});

var ImageViewer = {
	viewer: null,
	flShowingImage: false,
	imgNode: null,
	
	init: function() {
		this.viewer = $("image_view");
		
		$('image_view_close').observe( "click", function( e ) {
			ImageViewer.hideImage();
			Event.stop( e );
		});
	},
	
	showingImage: function() {
		return this.flShowingImage;
	},
	
	getViewer: function() {
		return this.viewer;
	},
	
	showImage: function( imgData ) {
		if ( this.flShowingImage )
			return;
		
		var img = new Image();
		var link;
		
		img.src = imgData.src;
		
		link = document.createElement("A");
		link.href = imgData.link;
		link.target = "_blank";
		link.appendChild( img );
		
		this.viewer.appendChild( link );
		this.imgNode = link;
		
		$(link).observe( "click", function( e ) {
			ImageViewer.hideImage();
		});
		
		var w = img.width + 6;
		var body = $('body');
		var displayWidth = body.getWidth();
		var closer = $('image_view_close');
		
		if ( document.viewport.getWidth() < displayWidth )
			displayWidth = document.viewport.getWidth();
		
		// set closer position
		this.viewer.show();
		
		var closerSize = closer.getDimensions();
		
		this.viewer.hide();
		
		closer.style.left = w - ( closerSize.width / 2 ) + "px";
		closer.style.top = 0 - ( closerSize.height / 2 ) + "px";
		
		// center viewer in screen
		this.viewer.style.left = ( ( displayWidth / 2 ) - ( w / 2 ) ) + "px";
		
		this.flShowingImage = true;
		
		new Effect.Grow( this.viewer, {
			direction: "top-right"
		});
	},
	
	hideImage: function() {
		new Effect.Shrink( this.viewer, {direction: "top-right", afterFinish: (function() {
			this.viewer.hide();
			
			$A(this.viewer.childNodes).each( (function(el) {
				if ( el.id != "image_view_close" )
					this.viewer.removeChild( el );
			}).bind(this));
			
			this.flShowingImage = false;

			}).bind( this )} );
	}
};

initScripts.push( ImageViewer.init.bind( ImageViewer ) );


initScripts.push( function() {
	var links = $$('.editCategoriesLink');
	
	function cancelForm( ev ) {
		new Ajax.Request( this.action, {
			method: 'post',
			parameters: {
				__Cancel__:		"Cancel",
				__sessionId:	this.elements.__sessionId
			},
			onComplete: (function() {
				new Effect.BlindUp( this, {
					afterFinish: ( function() { this.parentNode.removeChild( this ); } ).bind( this )
				});
				
				$('messageCategoriesEditorFormLayout').id='';
			}).bind( this )
		});
		
		if ( ev )
			ev.stop();
	}
	
	links.each( function( link ) {
		link.observe( "click", function( e ) {
			var url = link.href;
			var catEditor = $('messageCategoriesEditorFormLayout');
			
			if ( catEditor )
				cancelForm.call( catEditor.parentNode );
			
			new Ajax.Updater( $(link.parentNode.parentNode.parentNode.parentNode), url, {
				method: 'get',
				insertion: 'after',
				onComplete: function() {
					catEditor = $('messageCategoriesEditorFormLayout');
					
					if ( catEditor )
					{
						new Effect.BlindDown( catEditor );
						
						catEditor.parentNode.observe( "submit", ( function( e ) {
							var params = $(this).serialize( true );
							delete( params.__Cancel__ );
							
							new Ajax.Request( this.action, {
								method: 'post',
								parameters: params
							});
							
							new Effect.BlindUp( this, {
								afterFinish: ( function() { this.parentNode.removeChild( this ); } ).bind( e.element() )
							});
							
							e.stop();
						} ).bindAsEventListener( catEditor.parentNode ));
						
						$(catEditor.parentNode.elements.__Cancel__).observe( "click", cancelForm.bindAsEventListener( catEditor.parentNode ) );
					}
				}
			});
			
			e.stop();
		});
	});
});