 var index = 0;
 var currentIndex = 0;
 var slideNb = 0;
 
 var _NextTeaser = 1;
 var _IsAnimating1 = false;
 var _IsAnimating2 = false;
 var _TeaserIndex1 = 3;
 var _TeaserIndex2 = 3;
 
function slideTeaser1(newIndex){
    _IsAnimating1 = true;
    jQuery.easing.def = "easeInOutQuint";
    if(newIndex<0){        
        $('#teaser .row1').css('margin-left', '-1860px');
        _TeaserIndex1 = 5;
    }
    else if(newIndex >=10){
        $('#teaser .row1').css('margin-left', '-930px');
        _TeaserIndex1 = 4;
    }
    else{
        _TeaserIndex1 = newIndex;
    }
    
    $('#teaser .row1').animate({
        'margin-left': '-'+(_TeaserIndex1*310)+'px'
      }, 500, function() {
          _IsAnimating1 = false;
    });
    
    //console.log(_TeaserIndex1);
    _NextTeaser = 2;
}

function slideTeaser2(newIndex){
   _IsAnimating2 = true;
    jQuery.easing.def = "easeInOutQuint";
    if(newIndex<0){        
        $('#teaser .row2').css('margin-left', '-1860px');
        _TeaserIndex2 = 5;
    }
    else if(newIndex >=10){
        $('#teaser .row2').css('margin-left', '-930px');
        _TeaserIndex2 = 4;
    }
    else{
        _TeaserIndex2 = newIndex;
    }
    
    $('#teaser .row2').animate({
        'margin-left': '-'+(_TeaserIndex2*310)+'px'
      }, 500, function() {
          _IsAnimating2 = false;
    });
    
    _NextTeaser = 1;
}

function slideItem(newIndex){
    jQuery.easing.def = "easeOutQuint";
    $('#slideshow-nav li').removeClass('active');
    $('#slideshow-nav li:eq('+ newIndex +')').addClass('active');
    $('#items-cont').stop();
    $('#items-cont').animate({
        'margin-top': '-'+(newIndex*385)+'px'
      }, 1000, function() {
    });
    currentIndex = newIndex;    
}

$(document).ready(function(){ 
	
	$("a.popup").fancybox({
		'speedIn'		:	800, 
		'speedOut'		:	300,
		"overlayOpacity" :  0.85,
		"overlayColor"  :  "#000"
	});
	
  //console.log(document.getElementById("twitter_update_list"));
  //$.getJSON("http://twitter.com/statuses/user_timeline/KaraBoney.json?count=6") ;
  //alert('here');
  /*$.getJSON("http://twitter.com/statuses/user_timeline/Paradis_Latin.json?callback=?&count=3&format=json", function(data) {
      $.each(data, function() {
        //console.log(this);
        //alert('here');
        $('#twitter-root').append('<div class="clear separation"></div><div class="item"><img class="floated" src="http://a3.twimg.com/profile_images/729982099/para_reasonably_small.jpg"/><div class="floated desc">'+ this.text + '<p class="created">' + this.created_at + '</p><a href="http://twitter.com/#!/Paradis_Latin" class="more">En savoir plus...</a></div></div>');
      });     
  });*/
  
    $.each($('#items-cont .item'), function(){
        $('#slideshow-nav ul').append('<li index="' + index + '"></li>');        
        index ++;
        slideNb ++;
    });
    
    $('#slideshow-nav li:eq(0)').addClass('active');
    
    $('#slideshow-nav li').click(function(){
        $('#slideshow-nav').stopTime();
        if($(this).attr('index') != currentIndex){           
            slideItem($(this).attr('index'));
        }
    });

    $('#slideshow-nav').everyTime(7000, function(i) {
        var newIndex;
        if(currentIndex < slideNb-1){
            newIndex = currentIndex + 1;
        }
        
        else{
            newIndex = 0;
        }   
        slideItem(newIndex);
    });
    
    $('.arrows-nav .prev').click(function(){
        $('.arrows-nav').stopTime();
        if(_NextTeaser == 1 && !_IsAnimating1){
            newIndex = _TeaserIndex1 - 1;
            slideTeaser1(newIndex);
        }
        
        else if(!_IsAnimating2){
            newIndex = _TeaserIndex1 - 1;
            slideTeaser2(newIndex);
        }
    });
    
    $('.arrows-nav .next').click(function(){
        $('.arrows-nav').stopTime();
        if(_NextTeaser == 1  && !_IsAnimating1){
            newIndex = _TeaserIndex1 + 1;
            slideTeaser1(newIndex);
        }
        
        else if(!_IsAnimating2){
            newIndex = _TeaserIndex2 + 1;
            slideTeaser2(newIndex);
        }
    });

    //ipad and iphone fix
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        $("li").click(function(){
            //we just need to attach a click event listener to provoke iPhone/iPod/iPad's hover event
            //strange
        });
    }
    else{
        /*$(".arrows-nav .next").hover(
           function() {
              $(this).stop();
              $(this).animate({"right": "-72px"}, "fast");
           },
           function() {
              $(this).stop();
              $(this).animate({"right": "-62px"}, "fast");
           }
        );

        $(".arrows-nav .prev").hover(
           function() {
              $(this).stop();
              $(this).animate({"left": "-72px"}, "fast");
           },
           function() {
              $(this).stop();
              $(this).animate({"left": "-62px"}, "fast");
           }
        );*/
    }
    
    /*$('.arrows-nav').everyTime(4000, function(i) {
        if(_NextTeaser == 1  && !_IsAnimating1){
            newIndex = _TeaserIndex1 + 1;
            slideTeaser1(newIndex);
        }
        
        else if(!_IsAnimating2){
            newIndex = _TeaserIndex2 - 1;
            slideTeaser2(newIndex);
        }
    });*/
        
    
    $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            mp3: "/sound/cancan.mp3",
            ogg: "/sound/cancan.ogg"
          });
        },
        swfPath: "/js",
        supplied: "mp3, ogg"
      });
      
      $("#player-cont").hover(
      	function(){
      		$("#jquery_jplayer_1").jPlayer("play", 0);
      	},
      	function(){
      		$("#jquery_jplayer_1").jPlayer("pause", 0);

      	}
      );            
 });
 
