$(function()
{
  /* BROWSER DETECTION ------------------------------------------------------ */
  
  var userAgent = navigator.userAgent.toLowerCase();
  $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  
  if($.browser.mozilla) { $('body').addClass('firefox'); }
  else if($.browser.chrome) { $('body').addClass('chrome'); }
  else if($.browser.safari) { $('body').addClass('safari'); }
  else if($.browser.opera) { $('body').addClass('opera'); }
  else if($.browser.msie && $.browser.version == 6.0) { $('body').addClass('ie6'); }
  else if($.browser.msie && $.browser.version == 7.0) { $('body').addClass('ie7'); }
  else if($.browser.msie && $.browser.version == 8.0) { $('body').addClass('ie8'); }
  
  var URL_BASE = $('base').attr('href');
  
  /*concours*/
    /*jquery ui dragable + resazable */
  $(".resizable").resizable({ maxWidth: 386,  maxHeight: 501 });
  $(".draggable").draggable({ containment: 'parent', opacity: 0.35 });
  
  $rotate = 0;
  
  $( ".draggable" ).draggable({
    drag: function(event, ui)
    {
      $top = ui.position.top;
      $left = ui.position.left;
      $("#bebebdort").css('left', $left+"px");
      $("#bebebdort").css('top', $top+"px");
      $("#coorX").attr('value', $left);
      $("#coorY").attr('value', $top);
    }
  });
  
  $('.draggable, #bebebdort').css('width', $('#widthImg').val() + 'px').css('height', $('#heightImg').val() + 'px').css('top', $('#coorY').val() + 'px').css('left', $('#coorX').val() + 'px')
  
  $(".resizable").resizable({
    resize: function(event, ui)
    {
      $width = ui.size.width;
      $height = ui.size.height;
      $("#bebebdort").css('width', $width+"px");
      $("#bebebdort").css('height', $height+"px");
      $("#widthImg").attr('value', $width);
      $("#heightImg").attr('value', $height);
    }
  });
  
    /*end ui*/
  
  var $rotate = parseInt($('#angle').val());
  var $link = "";
  
  $('#bebebdort').attr("src", URL_BASE + 'concours/image?image=' + $('#image').val() + '&angle=' + $rotate);
  
  $("#rotleft, #rotright").click(function ()
  {
    $rotate += $(this).attr('id') == 'rotleft' ? 10 : -10;
    $('#bebebdort').attr("src", URL_BASE + 'concours/image?image=' + $('#image').val() + '&angle=' + $rotate);
    $("#angle").attr('value', $rotate);
    return false;
  });
  
  $('#col2').mouseover(function()
  {
    $('#bebeimg').attr('src', URL_BASE + 'images/kangourou50.png');
  }).mouseout(function()
  {
    $('#bebeimg').attr('src', URL_BASE + 'images/kangourou.png');
  });
  
  
  $('#concours_upload').click(function()
  {
    $('#concours_loading').show();
  });
  
  /*fin concours*/
  
  $("a.titlehome").each(
    function () {
      $rel = $(this).attr('rel');
      $(this).colorbox({width:"500px", inline:true, height:"640px", href:"#"+$rel});
    }
  );

  $("a.btn4bg").each(
    function () {
      $rel = $(this).attr('rel');
      //inline:true, href:"#"+$rel
      $(this).colorbox({width:"780px", height:"430px", iframe:true});
    }
  );

  $(".relhome").each(
    function () {
      $rel = $(this).attr('rel');
      $($rel).colorbox({transition:"none", speed:500, slideshow:false, width:"500px"});
    }
  );
  
  $(".relnewspast").each(
    function () {
      $rel = $(this).attr('rel');
      $(this).colorbox({width:"780px", inline:true, height:"430px", href:"#"+$rel});
    }
  );
  
  $(".relnewsfutur").each(
    function () {
      $rel = $(this).attr('rel');
      $(this).colorbox({width:"780px", inline:true, height:"430px", href:"#"+$rel});
    }
  );
  
  $(".relnewsall").each(
    function () {
      $rel = $(this).attr('rel');
      $(this).colorbox({width:"780px", inline:true, height:"430px", href:"#"+$rel});
    }
  );

  $(".relhome").colorbox({transition:"none", speed:500, slideshow:false, width:"500px"});
  $('.sousmenu').css('display', 'none');
  $(".menuli.active").children("ul").css('display', 'block');

  $(".menuli").hover(
    function () {
      /*if($(".menuli.active").attr('id') != $(this).attr('id'))
      {
        $(".menuli.active").children("ul").css('display', 'none');
        alert("ss");
      }*/
      /*
      if($(this).hasClass(".pages"))
      {
        
      }*/
      

      //alert($(this).children("ul").hasClass('pages'))
      //si contient ul
      if($(this).children("ul").size())
      {
        if($(".menuli.active").attr('id') != $(this).attr('id'))
        {
          $(".menuli.active").children("ul").css('display', 'none');
        }
        
        $(this).children("ul").css('display', 'block');
        
        if(!$(this).children("ul").hasClass('pages'))
        {
          $("ul.pages").css('display', 'none')
        }
      }
      else
      {
        $(".menuli ul").css('display', 'none');
      }
    },
    function () {
      if(!$(this).hasClass("active"))
        $(this).children("ul").css('display', 'none');
        
      $(".menuli.active").children("ul").css('display', 'block');
    }
  );
  
  //$.easy.navigation();
  $(".clnfs").each(
    function () {
      $idback = $(this).children(".reelback").attr('id');
      $idnext = $(this).children(".reelnext").attr('id');
      
      $($(this).children(".test")).reel({
        frame:        1,
        frames:       12,
        footage:      4,
        sensitivity:  70,
        saves:        true,
        next:         $("#"+$idback),
        previous:     $("#"+$idnext),
        speed:        200, 
        suffix:       "",
        //hotspot lien void pour cancleller comportement drag gauche droite
        hotspot:      $('#langue')
      });
    }
  );

  
  var home = $('#home');
  var slides = home.children('.slide');
  var size = slides.length;
  
  $('#home .slide > .prev, #home .slide > .next').click(function()
  {
    var slide = $(this).parent();
    var modifier = $(this).hasClass('prev') ? -1 : 1;
    
    slide.fadeOut('fast', function()
    {
      if(slide.index() + modifier == -1)
      {
        $(slides[size-1]).fadeIn('fast');
      }
      else if(slide.index() + modifier == size)
      {
        $(slides[0]).fadeIn('fast');
      }
      $(slides[slide.index() + modifier]).fadeIn('fast');
    });
    
    return false;
  });
  
  /*
  $size = $('.slide').length;
  $i = 0;

  $.each($('.slide'), function($key, $value)
  {
    $display = $(this).css('display');
    if($display != "none")
    {
      $i=$key;
      
      $(this).children(".persogauche").animate({
        opacity: 1
      }, 1500 );
      
      $(this).children(".persodroite").animate({
        opacity: 1
      }, 1500 );
    }
    else
    {
      $(this).children(".persogauche").animate({
        opacity: 0
      }, 1500 );
      
      $(this).children(".persodroite").animate({
        opacity: 0
      }, 1500 );
    }
  });
  
  $prev =  $('.prev');
  $next =  $('.next');
  
  $prev.click(function() {
    if($i > 0)
    {
      $.each($('.slide'), function($key, $value)
      {
        if($key == $i-1)
        {
          $display = $(this).css('display', 'block');
        }
        if($key == $i)
        {
          $(this).children(".persogauche").fadeIn(1500);
          $display = $(this).css('display', 'none');
        }
      });
      
      if($i == 1)
      {
        $prev.animate({opacity: 0}, 1500 );
      }
      $i--;
    }
  });

  $next.click(function() {
    if($i < $size-1)
    {
      $.each($('.slide'), function($key, $value) {
        $(this).children(".persodroite").animate({
          opacity: 0
        }, 1500 );
        
        if($key == $i+1)
        {          
          $display = $(this).css('display', 'block');
        }
        if($key == $i)
        {          
          $display = $(this).css('display', 'none');
        }
      });
      
      if($i == $size-1)
      {
        $next.animate({opacity: 0}, 1500 );
      }
      $i++;
    }
  });*/
});
