newBGs=[];

window.onload = function() {
$('body').fadeIn(1000);
};

function preload(arrayOfImages) {
var cache = [];
    $(arrayOfImages).each(function(){
        var image = $('<img />').attr('src', this[0]);
    });
}


$(document).ready(function() {

//see if there are BGs to preload
if(newBGs){
	preload(newBGs);
}


// By suppling no content attribute, the library uses each elements title attribute by default
   $('a[href][title]').qtip({
      content: {
         text: false // Use each elements title attribute
      },
      position: {
            corner: {
               tooltip: 'leftBottom',
               target: 'rightTop'
            }
         },
         style: {
         
         		width:221,
               height:78,
               paddingTop:3,
               paddingRight:12,
               paddingBottom:8,
               paddingLeft:34,
               background: 'url(../images/tooltip.png)',
                  border: {
                     width: 0,
                     radius: 0
                  },
                  padding: 10,
         tip: false
         }
   });
   

$('.fancybox').fancybox({
		width:940,
		height:560,
		helpers : {
        overlay : {
            opacity : 0.4
        }
    }
});

var theWindow        = $(window),
			    $bg              = $("#bg"),
			    aspectRatio      = $bg.width() / $bg.height();
			    			    		
			function resizeBg() { 
				
				if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
				    $bg
				    	.removeClass()
				    	.addClass('bgheight');
				} else if(theWindow.width() < 1200){
						$bg
				    	.removeClass()
				    	.addClass('bgmin');
				    	
				}else {
				    $bg
				    	.removeClass()
				    	.addClass('bgwidth');
				}
				
										
			}
			                   			
			theWindow.resize(function() {
				resizeBg();
			}).trigger("resize");
		



	// Sets the slides width on page load
	var i = $(window).width() + 1000;
	if (i > 319){ $('#items > div').css({ width: i }); }
	var currentIndex=0;
	
	// Scrollable and navigator plugin settings.
	
	$("#slider").scrollable({ easing: 'easeOutQuad', 
	
		onBeforeSeek : function (event, i){
		
		
						$('ul.navigation')
                .find('a')
                    .removeClass('active')
                .end()
            .end();
            
            var tmp=this;
            
            var stopMovement=true;
            
           $('ul.navigation li a').each(function(index){
            	if(index == i){
            		$(this).addClass('active');
            		window.location.hash = $(this).attr('href');
            		//if not within a sub sub toggle close all nested lists
            		if(!$(this).parent().parent().is(':visible')){
            			//jump to parent?
            				thisHash = $(this).parent().parent().parent().find(":first").attr('href');
            				
            				$('ul.navigation li a').each(function(index){
            					if(thisHash == $(this).attr('href')){
            						$(this).addClass('active');
            						ScrollTo(index);
            					}	
            				});
            				
            			//window.api.seekTo($(this).parent().parent().parent().index());
            			//ScrollTo($(this).parent().parent().parent().index());
            			stopMovement=false;
            		}
            		if($(this).parent().parent().parent().get(0).tagName == 'DIV'){
            			$('.subsub').not(this).slideUp("fast");
            		}
            		//open sibiling
            		$(this).siblings("ul").slideDown("fast");
            		
            	}	
            	
            });
          
          if(stopMovement && newBGs.length > 0){
          
          	if(i<0)
          		i=0;
          	//if current BG is same,  don't do anything
          	if($('body img').first().attr('src') != newBGs[i][0]){
          
          	//this is good show new BG
          	//take id off current image
          	//$('body img').first().attr('id','');
          	classX=$('body img').first().attr('class');
        	$('body img').first().after('<img id="newBG" src="'+newBGs[i][0]+'" alt="" class="'+classX+'"/>');  	
        	
        	//set it to the left or the right?
        	negative=0;
        	if(i < currentIndex || currentIndex==0 && i>1 )
        		negative=1;
        		
        		if(negative)
        			$('#newBG').css('left','-='+$('body img').first().width());
        		else 
        			$('#newBG').css('left',$('body img').first().width());	
        	
        	
        	if(negative){
        		$('#bg').animate({
        			 left: $('#bg').width()
        		},900);
        	}else{
        		$('#bg').animate({
        			 left: '-'+$('#bg').width()
        		},900);
        	}
        	
        	$('body').removeClass().addClass(newBGs[i][1]);
        	
        	$('#newBG').animate({
   				 left: '0'
  			}, 900, function() {
    	
    				$('body img').first().remove();
    				$('body img').first().attr('id','bg');
    				$bg	= $("#bg");
    				
 			 });
 			 }
		
          }
          
          return stopMovement;
			
		},
		onSeek: function(event, i)  {  
            	currentIndex=i;
             _gaq.push(['_trackPageview', document.location.pathname + document.location.hash]);
             //alert(document.location.pathname + document.location.hash);
            
    } , speed: 900, circular: true });
	
	// Window resize code
	window.api = $("#slider").data("scrollable");
	
	$('#next').bind('click', function(event) {event.preventDefault();});
	$('#prev').bind('click', function(event) {event.preventDefault();});
	//$('ul.navigation li a').bind('click', function(event) {event.preventDefault();});
	
	
		
		//if there is a hash,  loop through until we find it,  then seek to it
		if(window.location.hash != ""){
			$('ul.navigation li a').each(function(index){
            	if(window.location.hash == $(this).attr('href')){
            		$(this).addClass('active');
            		$(this).parent().parent().show();
            		ScrollTo(index);
            	}	
            });

		}else{
			//make sure to be on the first element
			//ScrollTo(0);
		}
		
			maxheight=360;
		$('.panel div').each(function(index) {
    		if($(this).height() > maxheight){
    			maxheight=$(this).height();
    		}
});
		$('#slider').css('height',maxheight);
		
		$('#navigation-wrapper').children().css('z-index', '50010');

		
		$('body').hide();
		
		
		
		
		});
		
function ScrollTo(index){
		window.api.seekTo(index,500);	
	}
	
function expandChild(who){
	$('#'+who).slideDown('fast');
}
		
	

