/*
Copyright (c) 2009 Merten Falk (http://www.aequinoctium.org/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var menu = new function() {
	var min_height	= 439;
	var multiple	= 1;
	
	var quirksMode = (
		Prototype.Browser.IE &&
		(
			! document.compatMode ||
			document.compatMode.indexOf("BackCompat") > -1
		)
	);
	
	var add_item = function( elem, the_hash, is_sub ) {
		if ( ! the_hash.get( 'loc' ) || ! the_hash.get( 'name' ) ) return;

		var div = new Element(
			'div',
			{
				'class'	: 'item',
				'id'	: format_id( the_hash.get( 'loc' ) )
			}
		);

		elem.insert( div );

		if ( is_sub && is_sub.get( 'is_sub' ) ) {
			div.addClassName( 'sub' );

			if ( is_sub.get( 'is_first' ) ) {
				div.addClassName( 'first' );
			}
			if ( is_sub.get( 'is_last' ) ) {
				div.addClassName( 'last' );
			}
		}

		var span = new Element(
			'span',
			{
				'class'	: 'fake_a'
			}
		).update( the_hash.get( 'name' ) );

		span.observe(
			'click',
			function( evt ) {
				handle_click( the_hash.get( 'loc' ), null );
			}
		);

		div.update( span );

		if ( the_hash.get( 'subs' ) ) {
			var sl_wrapper = new Element(
				'div',
				{
					'class' : 'sl_wrapper'
				}
			);

			var slider = new Element(
				'div',
				{
					'class' : 'slider'
				}
			);

			slider.setStyle(
				{
					'display' : 'none'
				}
			);

			var sl_inner = new Element(
				'div',
				{
					'class' : 'sl_inner'
				}
			);

			elem.insert(
				sl_wrapper.update(
					slider.update(
						sl_inner
					)
				)
			);

			the_hash.get( 'subs' ).each(
				function( item, idx ) {
					var sub_hash = new Hash(
						{
							'is_sub'	: true
						}
					);

					if ( idx == 0 ) {
						sub_hash.set( 'is_first', true );
					}
					if ( idx == ( the_hash.get( 'subs' ).length - 1 ) ) {
						sub_hash.set( 'is_last', true );
					}
					add_item( sl_inner, item, sub_hash );
				}
			);
		}
	};


	var format_id = function( loc ) {
		if ( loc == '/' ) {
			return 'main';
		}

		return loc.replace( /^\//, '' ).replace( /\/$/, '' ).replace( /\//g, '_' );
	};

	return {
		init		: function() {
			menu_config_array.each(
				function( item ) {
					add_item( $( 'div_menu' ), item, null );
				}
			);
		},
		process	: function() {
			var args = $A( menu.process.arguments );

			var loc = args.shift();

			if ( ! $( 'div_menu' ).select( 'div.item' ).length ) {
				setTimeout( "menu.process( '" + loc + "' );", 500 );
				return;
			}

			if ( ! loc ) { return; }

			var the_id = format_id( loc );

			$( 'div_menu' ).select( 'div.item' ).each(
				function( div ) {
					div.show();

					var link_id = div.readAttribute('id');

					if ( the_id == link_id ) {
						if ( ! div.firstDescendant().hasClassName( 'aktiv' ) ) {
							div.firstDescendant().addClassName( 'aktiv' );
						}
					}
					else {
						if ( div.firstDescendant().hasClassName( 'aktiv' ) ) {
							div.firstDescendant().removeClassName( 'aktiv' );
						}
					}
				}
			);


			$( 'div_menu' ).select( 'div.slider' ).each(
				function( div ) {
					var link_id = div.parentNode.previousSibling.readAttribute('id');

					if ( the_id.startsWith( link_id ) ) {
						if ( ! div.visible() ) {
							new Effect.SlideDown( div );
						}
					}
					else {
						if ( div.visible() ) {
							new Effect.SlideUp( div );
						}
					}
				}
			);

		},
		set_height : function () {
			var menu_height = $( 'div_menu' ).getHeight() + parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingTop' ) ) + parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingBottom' ) ) + ( quirksMode ? ( $( 'div_menu_wrapper' ).getStyle( 'borderTopWidth' ) + $( 'div_menu_wrapper' ).getStyle( 'borderBottomWidth' ) ) : 0 );
			
			var content_height = $( 'div_content_wrapper' ).getHeight();
			
			var tmp_min_height = min_height + parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingTop' ) ) + parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingBottom' ) ) + ( quirksMode ? ( $( 'div_menu_wrapper' ).getStyle( 'borderTopWidth' ) + $( 'div_menu_wrapper' ).getStyle( 'borderBottomWidth' ) ) : 0 );
			
			var new_height = Math.max( Math.max( menu_height, content_height ), tmp_min_height );
			
			new_height = new_height - ( new_height % multiple ) + multiple;
			
			new_height -= parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingTop' ) ) + parseInt( $( 'div_menu_wrapper' ).getStyle( 'paddingBottom' ) ) + ( quirksMode ? ( $( 'div_menu_wrapper' ).getStyle( 'borderTopWidth' ) + $( 'div_menu_wrapper' ).getStyle( 'borderBottomWidth' ) ) : 0 );
			
			$( 'div_menu_wrapper' ).setStyle(
				{
					'height'	: new_height + 'px',
					'minHeight'	: '0px'
				}
			);
			
			if ( ! Object.isUndefined( curvyCorners ) ) {
				curvyCorners.handleWinResize();
			}
		}
	}
}

var image = new function() {
	var preloaded_images = new Array();
	
	var check_content = function( item ) {
		if ( ! item.get( 'loc' ) ) return;

		new Ajax.Request(
			'/content' + item.get( 'loc' ) + 'index.htm?' + Math.round( Math.random() * 99999 ) + '=' + Math.round( Math.random() * 99999 ),
			{
				method:	'get',
				onComplete	: function( req ) {
					var div = new Element( 'div' ).update( req.responseText.stripScripts() );

					div.select( 'img' ).each(
						function( img ) {
							var bild = new Image();
							bild.src = img.readAttribute('src');
							
							preloaded_images[preloaded_images.length] = bild;
						}
					)
				}
			}
		);

		if ( item.get( 'subs' ) ) {
			item.get( 'subs' ).each(
				function( sub_item ) {
					check_content( sub_item );
				}
			);
		}
	};

	return {
		preload	: function() {
			menu_config_array.each(
				function( item ) {
					check_content( item );
				}
			);
		}
	}
}

var content = new function() {
	return {
		process	: function() {
			var args = $A( content.process.arguments );

			var loc = args.shift();

			if ( ! loc ) { return; }

			var inner_div = new Element( 'div' );

			inner_div.setStyle(
				{
					'padding'	: '0px',
					'margin'	: '0px'
				}
			);

			if ( $( 'div_content' ).visible() ) {
				new Effect.Fade(
					'div_content',
					{
						afterFinish	: function() {
							
							new Ajax.Request(
								'/content' + loc + 'index.htm?' + Math.round( Math.random() * 99999 ) + '=' + Math.round( Math.random() * 99999 ),
								{
									method:	'get',
									onComplete	: function( req ) {
										var res = req.responseText;
										
										var the_content = inner_div.update( res.stripScripts() );
										
										$( 'div_content' ).update( the_content );
										
										new Effect.Appear(
											'div_content',
											{
												from		: 0.01,
												afterSetup	: function() {
													menu.set_height();
												},
												afterFinish	: function() {
													res.evalScripts();
												}
											}
										);
									}
								}
							)
						},
						to	: 0.01
					}
				);
			}
			else {
				new Ajax.Request(
					'/content' + loc + 'index.htm?' + Math.round( Math.random() * 99999 ) + '=' + Math.round( Math.random() * 99999 ),
					{
						method:	'get',
						onComplete	: function( req ) {
							var res = req.responseText;
							
							var the_content = inner_div.update( res.stripScripts() );
							
							$( 'div_content' ).update( the_content );
							
							new Effect.Appear(
								'div_content',
								{
									from		: 0.00,
									afterSetup	: function() {
										menu.set_height();
									},
									afterFinish	: function() {
										res.evalScripts();
									}
								}
							);
						}
					}
				)
			}
		}
	}
}

// ----------------------------------------------------------------------------

var wait = 0;

window.dhtmlHistory.create(
	{
		toJSON: function(o) {
			return Object.toJSON(o);
		},
		fromJSON: function(s) {
			return s.evalJSON();
		}
	}
);

function handle_location( newLocation, historyData ) {
	newLocation = newLocation ? newLocation : '/';

	if ( ! wait && newLocation ) {
		wait = 1;

		content.process( newLocation );
		menu.process( newLocation );

		reset_wait();
	}
}

function handle_click( newLocation, historyData ) {
	if ( ! wait ) {
		dhtmlHistory.add( newLocation, historyData );
	}
	handle_location( newLocation, historyData );
}

function reset_wait() {
	var queue = $A(Effect.Queue);

	if ( queue.length > 0 ) {
		setTimeout( "reset_wait();", 1000 );
	}
	else {
		wait = 0;
	}
}

window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(handle_location);

	menu.init();

	initialLocation = dhtmlHistory.getCurrentLocation() || '/';

	if ( dhtmlHistory.isFirstLoad() ) {
		if ( ! wait ) {
			dhtmlHistory.add( initialLocation, null );
		}
		handle_location( initialLocation, null );
	}

	image.preload();
}

