// global variables
var baseUrl = document.getElementsByTagName( 'base' )[0].getAttribute( 'href' );

// global elements
var body,
	mainMenu;

// timeouts registry
var timeouts = {};

// handlers
function toggleMainMenu( e ) {
	clearTimeout( timeouts['mainMenu-toggle'] );

	if ( e.type === 'mouseover' ) {
		mainMenu.stop().animate( { 'top' : '0' }, 200, 'linear' );
	} else {
		timeouts['mainMenu-toggle'] = setTimeout( function( e ) {
			mainMenu.stop().animate( { 'top' : '-160' }, 400, 'linear' );
		}, 500 );
	}
}

function stuffSetup()
{
	// adjusting the coda slider plugin
	jQuery( '.coda-slider' ).codaSlider( {
		panelTitleSelector : 'p.title',
		dynamicArrowLeftText : '&#171;',
		dynamicArrowRightText : '&#187;'
	} );

	jQuery( '.coda-slider .content' ).jScrollPane();

}


// main
jQuery( function() {
	body = jQuery( 'body' );

	Shadowbox.init( { skipSetup: true } );

	jQuery( '.more a' ).click( function ( e ) {
		e.preventDefault();

		jQuery.get( this.href, 'html', function( html ) {
			Shadowbox.open({
				content:    html,
				player:     "html",
				title:      jQuery( this ).closest( 'panel-wrapper' ).children( 'p.title:eq(0)' ).html(),
				height:     400,
				width:      500
			});
		} );
	} );

	// adjusting the localScroll

	if ( jQuery.browser.mozilla ) {
		jQuery.localScroll( { offset: { top: -230 }, hash: true } );
	} else {
		jQuery.localScroll( { offset: { top: -230 } } );
	}

	if ( !!window.location.hash ) {
		var target = jQuery( window.location.hash );

		if ( target.length ) {
			body.scrollTo( target, 800, { offset: { top: -230 } } );
		}
	}

	stuffSetup();

	// animating the header
	mainMenu = jQuery( '#header' ).mouseover( toggleMainMenu )
								  .mouseout( toggleMainMenu );

	timeouts['mainMenu-toggle'] = setTimeout( function( e ) {
		mainMenu.trigger( 'mouseout' ); }, 2000 );

	// inner links scroll in the page
	/*jQuery( '#header a' ).click( function( e ) {
		var id = jQuery( this ).parent().attr( 'id' ).substr( 5 );
		var target = jQuery( '#' + id );
		if ( target.length ) {
			e.preventDefault();
			body.scrollTo( target, 800, { offset: { top: -230 } } );
		}
	} );*/

	// Tooltips
	jQuery( '*[title]' ).tooltip( {
		track    : true,
		delay    : 0,
		showURL  : false,
		showBody : ' - ',
		fade     : 250
	} );
	
	


	jQuery( '#agenda a, #twitter a, #myspace a, #album a, #footer a, a[rel="external"]' ).click( function( e ) {
		e.preventDefault();
		window.open( this.href );
	} );

	var messenger = jQuery( '#messenger' );
	if ( messenger.find( 'li' ).length ) {
		messenger.prependTo( '#contact form' ).show();
	}
} );
