var hold = false;
var first = 100;
var count = 0;
var right;
var ileft;

function movelasttofirst() {
         if (hold == false) 
         {
             count = 0;
             return;
         }
         if (count == $('#carousel-wrapper > div').size() -1) {
             // Move any far-righters to the beginning

             var from = '#carousel' + right;
             var to = '#carousel' + ileft;
              //alert('from:'+ from);
            //  alert('to:'+to);
             $(from).insertBefore(to);
             setTimeout(function() { $('.carousel-item').css('left', 0); }, 1);
             count = 0;

             // Decrement the left/right values.
             right = right - 1;
             ileft = ileft - 1;

             if (ileft == 0) {
                 ileft = $('#carousel-wrapper > div').size();
             }
             if (right == 0) {
                 right = $('#carousel-wrapper > div').size();
             }

             hold = false;
         }

         count++;
     }


     function movefirsttoend() {

         if (hold == false) {
             count = 0;
             return;
         }

         if (count == $('#carousel-wrapper > div').size() -1) {
             var from = '#carousel' + ileft;
             var to = '#carousel' + right;

             //  alert('from:'+ from);
             // alert('to:'+to);
             $(from).insertAfter(to);
             setTimeout(function() { $('.carousel-item').css('left', 0); }, 5);
             count = 0;

             // Increment the left/right values.
             right = right + 1;
             ileft = ileft + 1;

             if (ileft > $('#carousel-wrapper > div').size()) 
             {

                 ileft = 1;
             }
             if (right > $('#carousel-wrapper > div').size()) 
             {
                
                 right = 1;
             }
             hold = false;
         }

         count++;
  
     }

     $(document).ready(function() {

     $('#carousel-wrapper').load('/carouselimgfeed', function() 
         {
             // Setup initial Carousel Variables.
             right = $('#carousel-wrapper > div').size();
             ileft = 1;
             $('.carousel-item').mouseover(function() {
                 $(this).children(0).stop().animate({ top: '32px' });
             });
             $('.carousel-item').mouseout(function() {
                 $(this).children(0).stop().animate({ top: '93px' });
             });
             $('.carousel-popup').mouseover(function() {
                 $(this).stop().animate({ top: '32px' });
             });

             $('.carousel-popup').mouseout(function() {
                 $(this).stop().animate({ top: '93px' });
             });

             $('#carousel-prev').click(function() {
                 if (hold == true) {
                     return;
                 }

                 hold = true;
                 _gaq.push(['_trackEvent', 'Move Carousel', 'Left']);

                 var left = $('.carousel-item').show().position().left - 270;

                 // Slide everything to the left.
                 $('.carousel-item').animate({ left: left }, 900, 'swing', function() { movefirsttoend(); });
 
             });
             $('#carousel-prev').mouseenter(function() {
                 if (hold == true) {
                     return;
                 }

                 hold = true;
                 _gaq.push(['_trackEvent', 'Move Carousel', 'Left']);
             


                 var left = $('.carousel-item').show().position().left - 270;

                 // Slide everything to the left.
                 $('.carousel-item').animate({ left: left }, 900, 'swing', function() { movefirsttoend(); });

             });

             $('#carousel-next').click(function() {
                 if (hold == true) {
                     return;
                 }

                 var from = '#carousel' + right;
                 var to = '#carousel' + ileft;
                 //alert('from:'+ from);
                 //  alert('to:'+to);

                 $(from).insertBefore(to);
                 $('.carousel-item').css({ left: -265 });

                 hold = true;
                 _gaq.push(['_trackEvent', 'Move Carousel', 'Right']);

                 // Slide everything to the right ,function() { movelasttofirst(); }
                 setTimeout("var left = $('.carousel-item').show().position().left + 260;$('.carousel-item').animate({ left: left }, 900, 'swing', function() { movelasttofirst(); });", 100);

             });

             $('#carousel-next').mouseenter(function() {
                 if (hold == true) {
                     return;
                 }

                 var from = '#carousel' + right;
                 var to = '#carousel' + ileft;
                 //alert('from:'+ from);
                 //  alert('to:'+to);

                 $(from).insertBefore(to);
                 $('.carousel-item').css({ left: -265 });

                 hold = true;
                 _gaq.push(['_trackEvent', 'Move Carousel', 'Right']);

                 // Slide everything to the right ,function() { movelasttofirst(); }
                 setTimeout("var left = $('.carousel-item').show().position().left + 260;$('.carousel-item').animate({ left: left }, 900, 'swing', function() { movelasttofirst(); });", 100);

             });


         });
     });