// DOM LOAD - ANYTHING THAT IS DEPENDENT ON DOM AND MUST WAIT FOR THE PAGE TO LOAD GOES HERE
$(document).ready(function() {

     // HOME IMAGE SLIDER
     $('.image1').css({'left': 0});
     $('.caption1').css({'display': 'block'});
     currentImage = '1';
     homeSlideshow();
     

     // AUTOPLAY HOME IMAGE SLIDER
     var ssPlay = setInterval(function() {playSlideshow();},slideDelay);


     // PAUSE HOME IMAGE SLIDER ON HOVER - RESUME ON MOUSEOUT
     $('#home .rightBrain').hover(function() {
          clearInterval(ssPlay);
          ssPlay = false;
     });
     
     $('#home .rightBrain').mouseleave(function() {
          ssPlay = setInterval(function() {playSlideshow();},slideDelay);
     });
     
     // PAUSE HOME IMAGE SLIDER WHEN OUT OF VIEWER - RESUME WHEN IN VIEWER
     $(window).scroll(function(){
          homePosition = $(window).scrollTop();
          if (!ssPlay && homePosition < 500) {
               ssPlay = setInterval(function() {playSlideshow();},slideDelay);
          } else if (ssPlay && homePosition > 500) {
               clearInterval(ssPlay);
               ssPlay = false;
          }
     });
     
     
     // IMAGE GALLERIES
     Shadowbox.init({
          animate: false,
          continuous: true
     });


     window.onload = function() {
          if (location.search.slice(1) == "thanks") {
              // open a welcome message as soon as the window loads
              Shadowbox.open({
                  content:    '<div id="thanks-msg">Thank you for activating the hyperlink to send an electronic mail across the information superhighway. Your message, encoded into a series of zeroes and ones, is traversing the world wide web, where it shall arrive safely in our hands, at which point we shall respond back accordingly.</div>',
                  player:     "html",
                  title:      "Thanks!"
              });
          }
     
     };

     // COOL DROPDOWN MENU
     $('#queryType').selectmenu({
		width: 220
     });
     
	// IF YOU WANT TO PRELOAD ANY IMAGES, ADD THEM AS AN ARRAY BELOW
	// preload('INSERT ARRAY OF IMAGES TO PRELOAD');
});


//DECLARE VARS
var currentImage;
var homePosition;
var slideDelay = 5000;
var slideCount = 0;
var slideLoops = 2;

function playSlideshow() {
     if (slideCount <= slideLoops) {
          var whichImage = parseInt(currentImage);
          var nextImage;
          if (whichImage < 4) {
               nextImage = whichImage + 1;
          } else {
               nextImage = 1;
               slideCount++;
          }
          $('.image' + nextImage).animate({left: 0});
          $('.image' + currentImage).animate({left: 950});
          $('.index' + nextImage).addClass("selected");
          $('.index' + currentImage).removeClass("selected");
          $('.caption' + nextImage).fadeIn('fast');
          $('.caption' + currentImage).fadeOut('slow');
          currentImage = nextImage;
     }
}

function homeSlideshow() {
     for (j=1; j<=4; j++) {
          (function(i){
               $('.index' + i).click(function() {
                    if ('image'+currentImage != 'image' + i) {
                         $('.image' + i).animate({left: 0});
                         $('.image' + currentImage).animate({left: 950});
                         $('.index' + i).addClass("selected");
                         $('.index' + currentImage).removeClass("selected");
                         $('.caption' + i).fadeIn('fast');
                         $('.caption' + currentImage).fadeOut('slow');
                         currentImage = i;
/*                          console.log(currentImage) */
                         return false;
                    }
               });
          })(j);
     }


}
