var theInt = null;
var $crosslink, $navthumb, $current_slide_id;
var curclicked = 0; 
var reading = 0; 
var disable_slide = 0;	


theInterval = function(cur, disable_slide, disable_updown){
clearInterval(theInt);
      


      // Set the mouse cursor to the hand pointer
      $("#main-photo-slider #main-content-slider").mouseover(function(){
         $(this).css("cursor","pointer");
      });

      // Whatever the img, get the alt attr as the destination for a click
      $("#main-photo-slider div div div").click(function(){
         var $destination_link = jQuery(this).find("img").attr('alt');
         window.location.href = $destination_link;
      });

	
	  if( typeof cur != 'undefined' ){
	    curclicked = cur;
      }
		
      // Find the current slide id and set the arrow pointer above the corresponding navbar button
      $crosslink.removeClass("active-thumb");
      $current_slide_id = $("#main-photo-slider div div.panel").eq(curclicked).attr('id');
      $navthumb.parent("#navbar_" + $current_slide_id).addClass("active-thumb");



      // Animation queue buildup, this is a workaround 
      $(this).stop();

   
   





      if(!disable_slide){	
      
        
      
	     //top slider fade out
		 jQuery($(".stripNav ul li a").eq(curclicked)).parent().parent().parent().next().find("div.panelContainer").stop(true, true).fadeOut('slow'); 
			
		 // Trigger the button action (SLIDE)
		 $(".stripNav ul li a").eq(curclicked).trigger('click');
			
			
		 //top slider fade in
		 jQuery($(".stripNav ul li a").eq(curclicked)).parent().parent().parent().next().find("div.panelContainer").stop(true, true).fadeIn('slow');


      
      
      	
          theInt = setInterval(function(){
            

          // Find the current slide id and set the arrow pointer above the corresponding navbar button
	      $crosslink.removeClass("active-thumb");
          $current_slide_id = $("#main-photo-slider div div.panel").eq(curclicked).attr('id');
          $navthumb.parent("#navbar_" + $current_slide_id).addClass("active-thumb");







          // Only do the slide and fade animations here,  not on every click,  specify in a flag argument passed to the function
          if(!disable_updown){     
		          
		    // bottom slider      
			jQuery($(".stripNav ul li a").eq(curclicked)).parent().parent().parent().parent().parent().next().find("div.panelContainer").stop(true, true).slideUp('fast');

           }


           
           // SLIDE both top and bottom sliders (see coda-slider lib) 
           $(".stripNav ul li a").eq(curclicked).trigger('click');



          if(!disable_updown){
          
             // bottom slider
             jQuery($(".stripNav ul li a").eq(curclicked)).parent().parent().parent().parent().parent().next().find("div.panelContainer").stop(true, true).slideDown('slow');

           }

          
           if(last_curclicked == curclicked){
               disable_updown = 0;
           }
		   curclicked++;
		   if( 6 == curclicked )
			  curclicked = 0;
		    }, 10000);
            
           last_curclicked = curclicked;  
            
	  };

};
	
 $(function(){
 
 
    // Turn animations off,  globally 
	var stopFx = function() {
	  $.fx.off = true;
	};

    // Turn animations on,  globally 
	var startFx = function() {
	  $.fx.off = false;
	};





   // Stop animations if hovering over a form
   $(".bottom_home_right").mouseover(function () {
         //alert("in");
         // Stop sliders
         //stop_sliders();
         stopFx();
         
        
         theInterval(curclicked,1,1);

   
   });
   

   // Instantiate the codaSliders
   $("#main-content-slider").codaSlider();
   $("#main-photo-slider").codaSlider();
       
       



       
   // Prepare to slide to a random slide
   var randomnumber=Math.floor(Math.random()*6);
   curclicked = randomnumber;

       


   // After page load
   $(document).ready(function () {

        // Change to a random slide, delayed: 
        var t = setTimeout ( "$('.stripNav ul li a').eq(curclicked).trigger('click')", 6000 );

    });	


	$navthumb = $(".nav-thumb");
	$crosslink = $(".cross-link");
	
            // Button behavior, changing slides, selecting the destination	
	$navthumb.mouseover(function() {
		var $this = $(this);

		jQuery(this).parent().parent().parent().parent().parent().next().find("div.panelContainer").stop(true, true).slideUp('fast');
		
		
		// Call the slide for the button action, setting the interval for the auto-slide
	    theInterval(($this.parent().attr('index').slice(1) - 1), 0, 0);
		     
		jQuery(this).parent().parent().parent().parent().parent().next().find("div.panelContainer").stop(true, true).slideDown('slow');
           

        $(this).addClass("active-nav");
        $this.mouseout(function(){
          $(this).removeClass("active-nav");
        });
        $this.click(function(){
          var $destination = $this.attr('alt');
          window.location.href = $destination;
        });
                    
		return false;

                    
	});

    $navthumb.mouseout(function () {
     
       // Restart the timer and resume sliding onmouseout  
       theInterval(curclicked, 1, 1);
       
    });
            
		
	
	
});


