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

function scrollBackground( element , speed , width , offset )
{
	$( element ).css( "background-position" , offset+"px 0" );
	$( element ).animate( { backgroundPosition: (offset+width)+"px 0" } , speed , "linear" , function(){ scrollBackground( element , speed , width , offset ); } );
}

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

$(document).ready(function()
{
	/* Nav dropdowns */
	
	if ( !( $.browser.msie && ( parseInt($.browser.version, 10) < 8 ) ) ) /* Excludes IE7- */
	{
		$('.sub-menu').css('display','block').slideUp( 0 );
	
		$('#menu-main-menu > .menu-item').hover(
			function() { var subMenu = $(this).find('.sub-menu'); $(subMenu).stop().slideDown( 150 ); },
			function() { var subMenu = $(this).find('.sub-menu'); $(subMenu).stop().css('display','block').slideUp( 150 , function() { $(this).css( "height" , "auto" ); } ); }
		);
	}

	/* Floating the sidebar */

	$('#content_sidebar').portamento( { wrapper:$('#content') , gap:20 });

	/* Cloud animation */

	if ( screen.width > 800 )
	{
		scrollBackground( ".cloud.closer"  , 1600*50 , 1600 , Math.floor( Math.random() * 1600 ) );
		scrollBackground( ".cloud.close"   , 1800*60 , 1800 , Math.floor( Math.random() * 1800 ) );
		scrollBackground( ".cloud.middle"  , 1400*70 , 1400 , Math.floor( Math.random() * 1400 ) );
		scrollBackground( ".cloud.far"     , 1600*80 , 1600 , Math.floor( Math.random() * 1600 ) );
		scrollBackground( ".cloud.farther" , 2000*90 , 2000 , Math.floor( Math.random() * 2000 ) );
		
		if ( !Math.floor( Math.random() * 3 ) )
		{
			$('.cloud.balloon').show();
			scrollBackground( ".cloud.balloon" , 300000 , 3200 , Math.floor( Math.random() * 3200 ) );
		}
	}

	/* Homepage slideshow */

	var slideshow = $('#slideshow_frame').bxSlider
	({
		speed: 750,
		infiniteLoop: true,
		controls: false,
		pager: true,
		mode: 'fade',
		auto: true,
		autoHover: true,
		pause: 6000
	});

	/* FAQ slider */

	var faq = $('#faq_frame').bxSlider
	({
		speed: 750,
		infiniteLoop: true,
		controls: false,
		auto: false,
		randomStart: true			
	});

	$('#faq_nav .arrow').css( 'opacity' , 0.25 );
	$('#faq').hover( function(){ $('#faq_nav .arrow').stop().fadeTo(350,1); } , function(){ $('#faq_nav .arrow').stop().fadeTo(350,0.25); } );

	$('#faq_nav .prev .arrow').click(function(){ faq.goToPreviousSlide(); return false; });
	$('#faq_nav .next .arrow').click(function(){ faq.goToNextSlide(); return false; });

	/* Homepage services slider */

	var carousel = $('#carousel_frame').bxSlider
	({
		speed: 600,
		infiniteLoop: true,
		controls: false,
		displaySlideQty: 5,
		moveSlideQty: 3
	});

	$('#carousel_nav .arrow').css( 'opacity' , 0.25 );
	$('#carousel').hover( function(){ $('#carousel_nav .arrow').stop().fadeTo(350,1); } , function(){ $('#carousel_nav .arrow').stop().fadeTo(350,0.25); } );

	$('#carousel_nav .prev .arrow').click(function(){ carousel.goToPreviousSlide(); return false; });
	$('#carousel_nav .next .arrow').click(function(){ carousel.goToNextSlide(); return false; });

	/* Colorbox (map) */
	
	$('.popup_iframe').colorbox( { iframe:true , width:'940px' , height:'600px' , opacity:0.75 } );

});

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

