﻿/*
	—AUTHOR
	Osvaldas Valutis / www.osvaldas.info / osvaldas@osvaldas.info
	Freelance graphics, web / logo / brand identity designer and web developer
*/


/*
	close popup
*/

var validateURL = function()
{
	var hash = window.location.hash;

	if( hash )
		window.location.href = hash.replace( '#', '' );
};
$( window ).load( validateURL );



/*
	tooltip
*/


var toolTip = function()
{
	var target = $( '[rel~=tooltip]' );
	var tip;
	var title;
	var pos;

	target.bind(
	{
		mouseover: function()
		{
			title = $( this ).attr( 'title' );
	
			if( title == '' )
				return false;
	
			$( this ).removeAttr( 'title' );
	
			pos	= $( this ).offset();
			tip	= $( '<div id="tooltip">' + title + '</div>' );
	
			tip.appendTo( 'body' ).fadeIn( 300 );
		},

		mouseout: function()
		{
			tip.fadeOut( 300, function()
			{
				$( this ).remove();
			});
	
			$( this ).attr( 'title', title );
		},

		mousemove: function( mouse )
		{
			tip.css({ left: mouse.pageX, top: mouse.pageY + 20 });
		}
	});
};

$( window ).load( toolTip );



/*
	bubbles
*/

var infoBubbles = function()
{
	var bubble	= $( '.bubble' );

	bubble.hover( function()
	{
		$( this ).stop( true, true ).addClass( 'selected', 300, 'easeInOutExpo' );
	},
	function()
	{
		$( this ).stop( true, true ).removeClass( 'selected', 300, 'easeInOutExpo' );
	});
};

$( window ).load( infoBubbles );



/*
	areas
*/

var areas = function()
{
	var areas		= $( 'ul#areas li' ).not( 'ul#areas li ul li' );
	var area		= null;
	var button		= areas.find( 'h2 a' );
	var touched 	= false;
	var targetted	= new Array();


	$( '#popup' ).draggable({ handle: 'h3' });


	if( areas.hasClass( 'selected' ) )
		touched = true;


	areas.click( function()
	{
		if( touched == false )
		{
			touched = true;
			area	= $( this );
	
			doTransition( area );
		}

		return false;
	});


	button.click( function()
	{
		area = $( this ).closest( 'li' );

		doTransition( area );

		return false;
	});


	$( '.targets a' ).click( function()
	{
		getTargetInfo( this );
	});


	function doTransition( area )
	{
		window.location.hash = '#' + $( 'h2 a', area ).attr( 'href' );

		if( area.hasClass( 'selected' ) )
		{
			area.removeClass( 'selected' );

			areas.not( area )
				 .removeClass( 'deselected' );

			touched = false;
		}
		else if( area.hasClass( 'deselected' ) )
		{
			getTargets( area.attr( 'id' ), area );

			areas.filter( '.selected' ).removeClass( 'selected' );
			areas.not( area ).addClass( 'deselected' );

			area.stop( true, true )
				.removeClass( 'deselected' )
				.addClass( 'selected' )
				.hide()
				.fadeIn( 1000 );
		}
		else
		{
			getTargets( area.attr( 'id' ), area );

			areas.not( area )
				 .stop( true, true )
				 .addClass( 'deselected', 1000, 'easeInOutExpo' );
	
			area.stop( true, true ).addClass( 'selected', 1000, 'easeInOutExpo' );
		}
	}


	function getTargets( key, area )
	{
		var zone = area.find( '.targets' );

		if( zone.find( 'a' ).length > 0 )
			return;

		if( inArray( targetted, key ) )
			return;

		targetted.push( key );

		jQuery.ajax(
		{
			url: 			area.find( 'h2 a' ).attr( 'href' ),
			type:			'get',
			data: 			'key=' + key,
			dataType:		'html',
			timeout: 		10000,

			error: function( request, status, thrown ) 
			{
				//alert( status );
			},

			success: function( data ) 
			{
				zone.append( data );
				zone.find( 'a' ).bind( 'click', function()
				{
					getTargetInfo( this );
					return false;
				});
				toolTip();
			}
		});
	}


	function getTargetInfo( target )
	{
		var link = $( target ).attr( 'href' );

		window.location.hash = '#' + link;

		jQuery.ajax(
		{
			url: 			link,
			type:			'get',
			data: 			'target=' + link,
			dataType:		'html',
			timeout: 		10000,

			error: function( request, status, thrown ) 
			{
				alert( status );
			},

			success: function( data ) 
			{
				$( 'body' ).addClass( 'stop-animation' ).append( data );
				$( '#screen' ).fadeIn( 500 );
				$( 'a#close' ).bind( 'click', function()
				{
					closePopupF();
					return false;
				});
				$( '#popup' ).draggable({ handle: 'h3' });
				tabs();
			}
		});
	}
};

$( window ).load( areas );



/*
	close popup
*/

var pages = function()
{
	$( 'ul#nav li a.taken' ).click( function()
	{
		var link = $( this ).attr( 'href' );

		window.location.hash = '#' + link;
	
		jQuery.ajax(
		{
			url: 			link,
			type:			'get',
			data: 			'url=' + link,
			dataType:		'html',
			timeout: 		10000,
	
			error: function( request, status, thrown ) 
			{
				alert( status );
			},
	
			success: function( data ) 
			{
				$( 'body' ).addClass( 'stop-animation' ).append( data );
				$( '#screen' ).fadeIn( 500 );
				$( 'a#close' ).bind( 'click', function()
				{
					closePopupF();
					return false;
				});
				$( '#popup' ).draggable({ handle: 'h3' });
				tabs();
			}
		});

		return false;
	});
};

$( window ).load( pages );



/*
	close popup
*/

var closePopupF = function()
{
	$( '#screen' ).fadeOut( 500, function()
	{
		window.location.hash = '#' + $( '#close', this ).attr( 'href' );
		$( this ).remove();
		$( 'body' ).removeClass( 'stop-animation' );
	});
};


var closePopup = function()
{
	$( 'a#close' ).click( function()
	{
		closePopupF();
		return false;
	});

	$( document ).bind( 'keyup', function( e )
	{
		if( e.keyCode == 27 )
			closePopupF();
	});
};

$( window ).load( closePopup );



/*
	tabs
*/

var tabs = function()
{
	var nav		= $( '#tab-nav li' );
	var content	= $( '.tab-content' );

	nav.click( function()
	{
		if( $( this ).hasClass( 'selected' ) )
			return false;

		nav.filter( '.selected' ).removeClass( 'selected' );
		$( this ).addClass( 'selected' );

		content.filter( ':visible' ).hide();
		content.eq( $( this ).index() ).fadeIn( 1000 );

		return false;
	});
};

$( window ).load( tabs );



/*
	—FUNCTIONS
*/

function inArray( arr, val )
{
	exists = false;

	for( i = 0; i < arr.length; i++ )
		if( val == arr[i] )
			exists = true;

	return exists;
}
