$(document).ready(function(){
    var count = 0;
    var max = 0;
    $('#page-content h2').each(function(){
       max++;
    });
    max--;
    $('#page-content h2').each(function(){
        if(count==0)
        {
            $(this).before('<div id="page-menu"><ul></ul></div>');
        }
        $('#page-menu ul').append('<li title="' + count + '">' + $(this).html() +'</li>');
                var group = this;
        var i = this;
        while($(i).html()!=null)
        {
            group = $(group).add(i);
            /*$(group).each(function(){
                alert($(this).html());
            });*/

            i = $(i).next('p, ul, h3');
        }
        $(group).wrapAll('<div class="page-part"></div>');
        count++;
    });
    /*$('#page-menu ul li').hover(function(){
        $(this).css
    }, function(){

    });*/
    $('.page-part h2').hide();
    $('#page-menu').css('marginTop', '-10px');
    $('.page-part').hide();
    $('.page-part:first').show();
    $('#page-menu ul li:first').addClass('active');
     $('#page-menu ul li').click(function(){
         $('.active').removeClass('active');
         $(this).addClass('active');
         $('.page-part').hide();
          $('.page-part:eq(' + $(this).attr('title') +')').show();
     });
     $('#backlist').click(function(){
         $('#list-recettes').animate({marginLeft: "0px"}, "slow");
         return false;
     });
     $('#box-recette').hide();
     $('ul#list-recettes li a').click(function(){
         $('#box-recette').hide('fast');
         var href = $(this).attr('href');
         $.getJSON($(this).attr('href') + '/json', function(data){
             $('#nom-recette').html("<center><strong>" + data.nom + "</strong></center>");
             $('#ingredients').html(data.ingredients);
             $('#description').html(data.description);
             if(data.offert!='') $('#offert').html('Offert par <img src="' + data.image + '" alt="' + data.offert + '" />');
             else $('#offert').html('');
             $('#box-recette').show('fast');
             pageTracker._trackPageview(href);
         })
         return false;
     });
     $('#soloo').hide();
     $('#duoo').hide();
     $('#familleo').hide();
     $('h3').click(function(){
         $('#soloo').hide();
     $('#duoo').hide();
     $('#familleo').hide();
         $(this).next().show();
     });
     $('#easter').click(function(){
         swfobject.embedSWF("http://www.bio-c-bon.eu/images/template/moutonsurprise.swf", "top-img", "960", "142", "9.0.0", "http://www.bio-c-bon.eu/js/expressInstall.swf",{}, {wmode:"transparent"}, {});
     });
     var fiches = new Array;
     $('#fiches .fiche').each(function(){
         nom = $(this).find('h4>a').html();
         prems = nom.charAt(0);
         if((fiches.length!=0 && fiches[fiches.length-1]!=prems) || fiches.length==0 )
         {
            fiches.push(prems);
         }
         $(this).addClass("lettre-" + prems);
         $(this).hide();
     });
     if(fiches.length>0)
     {
         fiches.sort();
         var toto = '<div id="index-conseils">';
         var tmp = "AZ";
         var present = false;
         for(var l = tmp.charCodeAt(0); l<tmp.charCodeAt(1)+1; l++)
         {
             for(var i=0; i<fiches.length; i++)
             {
                 if(fiches[i] == String.fromCharCode(l))
                 {
                    if(l!=tmp.charCodeAt(0)) toto += " - ";
                    toto += '<span class="lettre">' + fiches[i] + '</span>';
                    present = true;
                    break;
                 }
             }
             if(!present)
             {
                 if(l!=tmp.charCodeAt(0)) toto += " - ";
                 toto += '<span class="lettre-in">' + String.fromCharCode(l) + '</span>';
             }
             present = false;
         }
         /*for(var i=0; i<fiches.length; i++)
         {
             if(i!=0) toto += " - ";
             toto += '<span class="lettre">' + fiches[i] + '</span>';
         }*/
         toto += '</div>';
         $('#fiches').before(toto);
         $('.lettre').click(function(){
             $('.fiche').hide();
            lettre = $(this).html();
            $('.lettre-' + lettre).show();
         });
     }

     $('#docs .fiche').hide();
     $('#docs .decouvrir').show();
     $('#docs h3').click(function(){
         $('#docs .fiche').hide();
         $('#docs .' + $(this).attr("id")).show();
     });
     
     $('.fiche a').click(function(){
    	 pageTracker._trackPageview($(this).attr('href'));
     });
     $("#registerform").validate({
    	 rules: {
    	 	numero: {
    	 		minlength: 13,
    	 		maxlength: 13
     		}
     	 }
     });
     $("#registerformb").validate();
});

function next_test(obj, lot)
{
    if($(obj).next('p').html()!=null)
    {
        //alert(($(obj).next('p').html()));
        lot = $(lot).add(next_test($(obj).next('p')));
    }
}

function print_r(obj) {
  win_print_r = window.open('about:blank', 'win_print_r');
  win_print_r.document.write('<html><body>');
  r_print_r(obj, win_print_r);
  win_print_r.document.write('</body></html>');
 }

 function r_print_r(theObj, win_print_r) {
  if(theObj.constructor == Array ||
   theObj.constructor == Object){
   if (win_print_r == null)
    win_print_r = window.open('about:blank', 'win_print_r');
   }
   for(var p in theObj){
    if(theObj[p].constructor == Array||
     theObj[p].constructor == Object){
     win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
     win_print_r.document.write("<ul>")
     r_print_r(theObj[p], win_print_r);
     win_print_r.document.write("</ul>")
    } else {
     win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
    }
   }
  win_print_r.document.write("</ul>")
 }

