var is_loading_finished = false;

function preload_images( $images )
{
	jQuery.each
	(
		$images
		,
		function( index, element )
		{
			var img = new Image();
			
			jQuery(img).src = jQuery(element).attr('src');
		}
	)
	;
}

function preload_css_images( $collection )
{
	jQuery.each
	(
		$collection
		,
		function( index, element )
		{
			var backgroundImage = jQuery(element).css('background-image');
			
			if( backgroundImage.length > 7 )
			{
				backgroundImage = backgroundImage.substr(5,backgroundImage.length-7);
			}
			
			var img = new Image();
			
			jQuery(img).src = backgroundImage;
		}
	)
	;
}

jQuery(document).ready
(
	function ()
	{
		jQuery('#rt-header ul.menu a').click
		(
			function ()
			{
				// stop all animations that are in action now ( if user changes the pages too fast )
				
				jQuery( 'body' ).stop( true, true );
				
				jQuery( '#chain-with-hand' ).stop( true, true );
				
				jQuery( '#body-wrapper>div[id^=light-bulb-]' ).stop( true, true );
				
				jQuery( '#main-content>div' ).stop( true, true );
				
				// find out previous page / previous light bulb
				
				var previous_page = jQuery('#rt-header ul.menu li.active:first').find('a>span').text();
				
				var previous_light_bulb = 'light-bulb-red';
				
				var other_light_bulbs	= '#light-bulb-blue,#light-bulb-green';
				
				var previous_page_div	= 'home-page';
				
				var other_page_divs		= 'div.our-works-page,div.contact-us-page';
				
				var previous_r			= 54;
				
				var previous_g			= 5;
				
				var previous_b			= 1;
				
				if( previous_page == 'Our Works' )
				{
					previous_light_bulb = 'light-bulb-blue';
					
					other_light_bulbs	= '#light-bulb-red,#light-bulb-green';
					
					previous_page_div	= 'our-works-page';
					
					other_page_divs		= 'div.home-page,div.contact-us-page';
					
					previous_r			= 2;
					
					previous_g			= 18;
					
					previous_b			= 52;
				}
				else if ( previous_page == 'Contact Us' )
				{
					previous_light_bulb = 'light-bulb-green';
					
					other_light_bulbs	= '#light-bulb-red,#light-bulb-blue';
					
					previous_page_div	= 'contact-us-page';
					
					other_page_divs		= 'div.home-page,div.our-works-page';
					
					previous_r			= 24;
					
					previous_g			= 53;
					
					previous_b			= 0;
				}
				
				// we have stopped animation, however we should double-check that state of the page is correct
				
				jQuery('#chain-with-hand').css('top','0px');
				
				jQuery(other_light_bulbs)
				.
				css
				(
					{
						display: 'block'
						,
						opacity: 0
					}
				)
				;
				
				jQuery('#' + previous_light_bulb)
				.
				css
				(
					{
						display: 'block'
						,
						opacity: 1
					}
				)
				;
				
				jQuery('body').css
				(
					{
						backgroundColor: 'rgb(' + previous_r + ',' + previous_g + ',' + previous_b + ')'
					}
				)
				;
				
				jQuery(other_page_divs)
				.
				css
				(
					{
						display: 'none'
						,
						opacity: 0
					}
				)
				;
				
				jQuery('div.' + previous_page_div)
				.
				css
				(
					{
						display: 'block'
						,
						opacity: 1
					}
				)
				;
				
				// find out new page / new light bulb / new color
				
				var new_page = jQuery(this).find( 'span:first' ).text();
				
				var new_light_bulb	= 'light-bulb-red';
				
				var new_page_div	= 'home-page';
				
				var new_color 		= '#340400';
				
				var new_r			= 54;
				
				var new_g			= 5;
				
				var new_b			= 1;
				
				document.title = 'Bright Web Solutions! - Web-sites and Web-applications  design, development and promotion. Home';
				
				if( new_page == 'Our Works' )
				{
					new_light_bulb 	= 'light-bulb-blue';
					
					new_color 		= '#011133';
					
					new_page_div	= 'our-works-page';
					
					new_r			= 2;
					
					new_g			= 18;
					
					new_b			= 52;
					
					document.title = 'Bright Web Solutions! - Web-sites and Web-applications  design, development and promotion. Our Works';
				}
				else if ( new_page == 'Contact Us' )
				{
					new_light_bulb 	= 'light-bulb-green';
					
					new_color 		= '#183401';
					
					new_page_div	= 'contact-us-page';
					
					new_r			= 24;
					
					new_g			= 53;
					
					new_b			= 0;
					
					document.title = 'Bright Web Solutions! - Web-sites and Web-applications  design, development and promotion. Contact Us';
				}
				
				// find out 'body' color at middle of the animation
				
				var middle_r	= Math.ceil( ( previous_r + new_r ) / 2 );
				
				var middle_b	= Math.ceil( ( previous_b + new_b ) / 2 );
				
				var middle_g	= Math.ceil( ( previous_g + new_g ) / 2 );
				
				// change active menu item
				
				jQuery('#rt-header ul.menu>li').removeClass('active');
				
				jQuery(this).parent().addClass('active');
				
				// chain with hand animation
				
				jQuery('#chain-with-hand').animate
				(
					{
						top: '-200px'
					}
					,
					500
					,
					'linear'
					,
					function()
					{
						jQuery(this).animate
						(
							{
								top: '0px'
							}
							,
							250
							,
							'linear'
						)
						;
					}
				)
				;
				
				// background color and image animation
				
				jQuery('#' + previous_light_bulb)
				.
				css
				(
					{
						display: 'block'
						,
						opacity: 1
					}
				)
				.
				animate
				(
					{
						opacity: 0
					}
					,
					1000
					,
					'linear'
				)
				;
				
				jQuery('#' + new_light_bulb)
				.
				css
				(
					{
						display: 'block'
						,
						opacity: 0
					}
				)
				.
				animate
				(
					{
						opacity: 1
					}
					,
					1000
					,
					'linear'
				)
				;
				
				jQuery('body').animate
				(
					{
						backgroundColor: 'rgb(' + middle_r + ',' + middle_g + ',' + middle_b + ')'
					}
					,
					500
					,
					'linear'
					,
					function()
					{
						jQuery(this).animate
						(
							{
								backgroundColor: new_color
							}
							,
							250
							,
							'linear'
						)
						;
					}
				)
				;
				
				// main content animation
				
				jQuery('div.' + previous_page_div).animate
				(
					{
						opacity: 0
					}
					,
					500
					,
					'linear'
					,
					function()
					{
						jQuery(this).css
						(
							{
								display: 'none'
							}
						)
						;
						
						jQuery('div.' + new_page_div)
						.
						css
						(
							{
								display: 'block'
								,
								opacity: 0
							}
						)
						.
						animate
						(
							{
								opacity: 1
							}
							,
							500
						)
						;
					}
				)
				;
				
				// prevent default behaviour after clicking on link
				
				return false;
			}
		)
		;
	}
)
;

jQuery(document).ready
(
	function ()
	{
		jQuery('#rt-logo').click
		(
			function ()
			{
				// simulation of "Home" menu item click ( this is first menu item in the list )
				
				jQuery('#rt-header ul.menu>li:first>a').click();
				
				return false;
			}
		)
		;
		
		jQuery('a.our-works-link').click
		(
			function ()
			{
				// simulation of "Our Works" menu item click ( this is a 2-nd menu item in the list )
				
				jQuery('#rt-header ul.menu>li:eq(1)>a').click();
				
				return false;
			}
		)
		;
		
		jQuery('a.our-skills-link').click
		(
			function ()
			{
				// scroll the view to the "Our Skills" section
				
				jQuery('html,body').animate( { scrollTop: jQuery('#fact-sheet').offset().top }, 1000 );
				
				return false;
			}
		)
		;
		
		// support of "hover" effect in IE 6
		
		jQuery('#rt-header ul.menu li').hover
		(
			function()
			{
				jQuery(this).addClass( 'highlightedMainMenuItem' );
			}
			,
			function()
			{
				jQuery(this).removeClass( 'highlightedMainMenuItem' );
			}
		)
		;
	}
)
;

jQuery(document).ready
(
	function ()
	{
		jQuery('#our-works-menu .next-mi').click
		(
			function()
			{
				// first of all we should stop all existing animations
				
				jQuery('#our-works>.example').stop( true, true );
				
				// now we can set up new animation
				
				var current_example = jQuery('#our-works>div.current-example');
				
				var next_example = current_example.next();
				
				if( next_example.length == 0 )
				{
					next_example = jQuery('#our-works>.example:first');
				}
				
				next_example
				.
				css
				( 
					{
						display: 'block'
						,
						left: '470px'
					}
				)
				.
				addClass('current-example')
				;
				
				current_example.css
				( 
					{
						display: 'block'
						,
						left: '0px'
					}
				)
				.
				removeClass('current-example')
				;
				
				current_example.animate
				(
					{
						left: '-470px'
					}
					,
					1000
					,
					'linear'
					,
					function()
					{
						jQuery(this).css( 'display', 'none' );
					}
				)
				;
				
				next_example.animate
				(
					{
						left: '0px'
					}
					,
					1000
					,
					'linear'
				)
				;
				
				return false;
			}
		)
		;
		
		jQuery('#our-works-menu .previous-mi').click
		(
			function()
			{
				// first of all we should stop all existing animations
				
				jQuery('#our-works>.example').stop( true, true );
				
				// now we can set up new animation
				
				var current_example = jQuery('#our-works>div.current-example');
				
				var previous_example = current_example.prev();
				
				if( previous_example.length == 0 )
				{
					previous_example = jQuery('#our-works>.example:last');
				}
				
				previous_example
				.
				css
				( 
					{
						display: 'block'
						,
						left: '-470px'
					}
				)
				.
				addClass('current-example')
				;
				
				current_example
				.
				removeClass('current-example')
				.
				animate
				(
					{
						left: '470px'
					}
					,
					1000
					,
					'linear'
					,
					function()
					{
						jQuery(this).css( 'display', 'none' );
					}
				)
				;
				
				previous_example.animate
				(
					{
						left: '0px'
					}
					,
					1000
					,
					'linear'
				)
				;
				
				return false;
			}
		)
		;
	}
)
;

jQuery(document).ready
(
	function()
	{
		jQuery('#our-works div.fd-dialog')
		.
		dialog
		(
			{
				autoOpen: false
				,
				width: 600
				,
				modal: true
				,
				resizable: false
				,
				buttons: { 'Cancel' : function(){ jQuery(this).dialog('close'); } }
			}
		)
		;

		jQuery('#our-works a.fd-link').click
		(
			function()
			{
				var link_id = jQuery(this).attr( 'id' );
				
				var dialog_id = link_id.substring( 0, link_id.length-8 ) + '-fd-dialog';
				
				jQuery('#' + dialog_id).dialog( 'open' );
				
				return false;
			}
		)
		;
	}
)
;

jQuery(document).ready
(
	function()
	{
		// setting up "loading..." dialog
		
		jQuery('#loading-dialog')
		.
		dialog
		(
			{
				dialogClass: 'loading-dialog'
				,
				autoOpen: true
				,
				width: 150
				,
				height: 100
				,
				minHeight: 85
				,
				modal: true
				,
				resizable: false
				,
				draggable: false
				,
				beforeclose: function()
				{
					if( !is_loading_finished )
					{
						return false;
					}
				}
			}
		)
		;
	}
)
;

jQuery(window).load
(
	function()
	{
		is_loading_finished = true;
		
		// trick to load all main images
		
		jQuery('#light-bulb-green').hide().css('left','-220px');
		
		jQuery('#light-bulb-blue').hide().css('left','-220px');
		
		// now "loading..." dialog can be closed
		
		jQuery('#loading-dialog').dialog('close');
		
		// now we can show main content
		
		jQuery('#body-wrapper').hide().removeClass('transparent-element').css('left','0px').fadeIn('slow');
	}
)
;

