jQuery(document).ready(function() {


  jQuery('#news_slideshow').cycle({
      fx:      'scrollVert',
      timeout:  3000,
      prev:    '#prev',
      next:    '#next',
      pager:   '#news_nav',
      pagerAnchorBuilder: pagerFactory,
      before: turn

  });

  function pagerFactory(idx, slide) {
      var s = idx > 2 ? ' style="display:none"' : '';
      return '<li id="li_'+(idx+1)+'" '+s+'><a id="link_'+(idx+1)+'" href="#">'+(idx+1)+'</a></li>';
  };

  function turn() {
    var a = jQuery(".activeSlide");
    var nextli = a.parent().next();
    if (  typeof(nextli.attr("id")) != "undefined"   ) {
      if(nextli.css("display") == "none") {
        nextli.show();
        jQuery("#news_nav li").each(function() {
          if(jQuery(this).css("display")!="none") {
            jQuery(this).hide();
            return false;
          }
        });
      }
    }
    else {
        jQuery("#news_nav li").each(function(i) {
          if(i<3)
            jQuery(this).show();
          else
            jQuery(this).hide();
        });
    }
  };

  jQuery('#video_thumbnail_play').each(function(){
    var height = jQuery(this).parent().children("img").height();
    jQuery(this).css("height", height);
  });

  // newsletter
  jQuery('#newslettermail input').click(function(){
    jQuery(this).val("");
  });

  // post comment
  jQuery('#post_body').click(function(){
    if (jQuery('#post_body').val() == "Votre commentaire") {
      jQuery('#post_body').val("");
    }
  });

  // clean "Votre commentaire"
  jQuery('#post-form').submit(function(){
    if (jQuery('#post_body').val() == "Votre commentaire") {
      jQuery('#post_body').val("");
    }
  });

  // render error
  jQuery('#post-form img.error').each(function(){
    jQuery(this).mouseover(function(){
      jQuery('#post-form .error_box_post').empty();
      jQuery('#post-form .error_box_post').show();
      var error = jQuery(this).parent().children("span.message").text();
      jQuery('#post-form .error_box_post').text(error);

    });
  });

  // moderation
  jQuery('#comments .moderate a').click(function(){
    var id       = jQuery(this).parent().attr("id");
    var split_id = id.split("-");
    var rev      = jQuery(this).attr("rev");
    jQuery.post(site_path+'/post/moderate',
      { id: split_id[1], rev: rev },
      function(data){
        jQuery("#"+id).empty();
        jQuery(".moderate-msg", jQuery("#"+id).parent()).text(data);
      }
    );
    return false;
  });

  // ratings
  jQuery('.rating-rate').each(function(){

    // first load
    jQuery('input', jQuery(this).parent()).each(function(){
      var value = jQuery(this).val() - 1;
      jQuery('.star', jQuery(this).parent()).each(function(cur_pos){
        if (cur_pos <= value){
          jQuery(this).addClass("selected");
        }
      });
    });

    jQuery('.star', jQuery(this)).each(function(pos){

      // click
      jQuery(this).click(function(){

        // change stars
        jQuery('.star', jQuery(this).parent()).removeClass("selected");
        jQuery('.star', jQuery(this).parent()).each(function(cur_pos){
          if (cur_pos <= pos){
            jQuery(this).addClass("selected");
          }
        });

        // change values
        jQuery('.rating-value', jQuery(this).parent().parent()).text(pos+1);
        jQuery('input', jQuery(this).parent().parent()).val(pos+1);

        // change average
        var sum = 0;
        jQuery('.post_rating input').each(function(){
          sum += parseInt(jQuery(this).val());
        });
        sum = Math.round((sum / 3) * 10) / 10;
        jQuery('#rating-result').text(sum);

      });
    });
  });
});


function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(email)) {
    return false;
  }
  return true;
}

function getAreaId(area)
{
  var reg=new RegExp("[#]+", "g");
  var id = jQuery(area).attr("id");
  var tid=id.split(reg);
  return tid[1];
}

function getAreaName(area)
{
  var reg=new RegExp("[#]+", "g");
  var id = jQuery(area).attr("id");
  var tid=id.split(reg);
  return tid[0];
}