var allButtons, interval, button_index = 0, hasFocus=true; 

jQuery(document).ready(function(){
	 	 
	$('.accordionButton').click(function() {
	 	clickSwitch( this );		  
	 });  
 	 
	$('.accordionContent').hide(); 
	//$('.testimonial').css({opacity:0}); 
	$('.office').css({opacity:0}); 
	
	//testimonialSwitch();
	officeSwitch();
	onProductionMove(); 
	
	interval = setInterval( "slideSwitch()", 5000 );
	setInterval( "imageSwitch()", 5000 );
	setInterval( "officeSwitch()", 5000 );
	//setInterval( "testimonialSwitch()", 9000 );
 	
	allButtons = $('.accordionButton');
	   	
	slideSwitch( allButtons[0]);        
 });    
    
jQuery(window).resize(function(e){ 
	onProductionMove ();
});

window.onfocus = function() { hasFocus = true; }; 
window.onblur = function() { hasFocus = false; };
	 
 
// check this later http://bavotasan.com/downloads/full-sizebackground-image-jquery-plugin/

function switchProperties( panel )
{ 	 
   if (!hasFocus) return;

   $('.accordionContent').slideUp('normal');

   if($(panel).next().is(':hidden') == true) { 
	   $(panel).next().slideDown('normal');
	} 	
} 

function clickSwitch( panel )  
{	 
   clearInterval(interval);
   switchProperties( panel );
}

function slideSwitch() 
{	 
   if (!hasFocus) return;
   
   switchProperties( allButtons[button_index] );    
   button_index = (button_index == (allButtons.length-1)) ? 0 : button_index+1;	
} 


function imageSwitch() 
{   
   if (!hasFocus) return;
   
   var $active = $('#slideshow div.active'); 

   if ( $active.length == 0 ) $active = $('#slideshow div:last');

   var $next = $active.next().length ? $active.next() : $('#slideshow div:first');

   $active.addClass('last-active');

   $next.css({opacity: 0.0})
	   .show()
   .addClass('active')
   .animate({opacity: 1.0}, 1000, function() {
	   $active.removeClass('active last-active').hide(); 
   });
}

function testimonialSwitch()
{

   if (!hasFocus) return;	
   
   var $active = $('#quote_wrapper div.active'); 

   if ( $active.length == 0 ) $active = $('#quote_wrapper div:last');
	
   var $next = $active.next().length ? $active.next() : $('#quote_wrapper div:first');
	
   $active.addClass('last-active');
   $active.animate({opacity: 0}, 1000);

   $next.css({opacity: 0.0})
   .addClass('active')
   .animate({opacity: 1.0}, 1000, function() {
	   $active.removeClass('active last-active'); 
   });
}

function officeSwitch()
{
   if (!hasFocus) return;
   
   var $active = $('#office_wrapper div.active'); 

   if ( $active.length == 0 ) $active = $('#office_wrapper div:last');
	
   var $next = $active.next().length ? $active.next() : $('#office_wrapper div:first');
	
   $active.addClass('last-active');
   $active.animate({opacity: 0}, 1000);

   $next.css({opacity: 0.0})
   .addClass('active')
   .animate({opacity: 1.0}, 1000, function() {
	   $active.removeClass('active last-active'); 
   });
}



function onProductionMove ()
{
	var w = $(window).width();
	var h = $(window).height();
	
	$("#video_production_wrapper").css('left', (w<1030) ? 630 : w-400 ); 
	$("#video_production_wrapper").css('top', (h<510) ? 220 : h-290 );  
	 
}
 


