/* Author: 

*/
    function morelivre(pagenum) {
        $('#aj').show();
        $.ajax({
          url: "/livredor/ajax/"+pagenum+"/",
          dataType: "json",
          success: function(data){
            var obj = data;//eval('(' + data + ')');
            jQuery.each(obj, function(i, val) {
                log(val.fields);
                val.fields.commentaire = val.fields.commentaire.replace(/\n/g, '<br />');
                var content = "<li class=\"clearfix\"><h3>"+val.fields.auteur+" - "+val.fields.date+"</h3>     "+val.fields.commentaire+"                            </li><hr>";
                $("ul#news").append(content);

            });
            $('#plus').unbind(); 
            $('#plus').click(function() {
                morelivre(pagenum+1); 
                return false;});

            $('#aj').hide();
          }
        });
    }

    function addlatest(model) {
        $.ajax({
          url: "/"+model+"/ajax/latest/",
          dataType: "json",
          success: function(data){
            var obj = data;//eval('(' + data + ')');
            jQuery.each(obj, function(i, val) {
                log(val.fields);
                val.fields.commentaire = val.fields.commentaire.replace(/\n/g, '<br />');
                var content = "<li class=\"clearfix\"><h3>"+val.fields.auteur+" - "+val.fields.date+"</h3>     "+val.fields.commentaire+"                            </li><hr>";
                $("ul#news").prepend(content);

            });

            $('#aj').hide();
          }
        });
    }

    function change_album(to) {
        var curr_id = $('#slideshow').attr("src").split("/")[3];
        $.ajax({
          url: "/photos/get_id/"+(curr_id)+"/"+(to)+"/",
          dataType: "json",
          success: function(data){
            var next_src = "/photos/view/"+data.id+"/1/";
            $('#slideshow').attr("src", next_src);
            $('#album_nom').html(data.nom);
            $('#album_nom').attr("href", "/photos/"+data.id+"/1/");
            loop_fix_size();
          }
        });
    }

    function switch_photo() {
        var curr_id = $('#slideshow').attr("src").split("/")[3];
        var curr_photo = $('#slideshow').attr("src").split("/")[4];
        var next_photo = curr_photo+1;
        var next_src = "/photos/view/"+curr_id+"/"+next_photo+"/";
        $('#slideshow').fadeOut('slow', function() {
            $('#slideshow').attr("src", next_src);
            loop_fix_size_change();
        });
    }

    function fix_size() {
        var ratio = $('#slideshow')[0].naturalHeight / $('#slideshow')[0].naturalWidth;
        if ($('#slideshow')[0].naturalHeight > $('#slideshow')[0].naturalWidth) {
            //portrait
            $('#slideshow').width(163);
            $('#slideshow').height(163*ratio);
        }
        else {
            //paysage
            $('#slideshow').width(330);
            $('#slideshow').height(330*ratio);
        }
    }

    function loop_fix_size() {
        if ($('#slideshow')[0].naturalHeight) {
            fix_size();
        }
        else {
            setTimeout(loop_fix_size,10);
        }
    }
    function loop_fix_size_change() {
        if ($('#slideshow')[0].naturalHeight) {
            fix_size();
            $('#slideshow').fadeIn('normal');
            setTimeout(switch_photo,2500);
        }
        else {
            setTimeout(loop_fix_size_change,10);
        }
    }

    
    if ($('#slideshow').length > 0) {
        loop_fix_size_change();
    }



    function submitForm(form, model) {
        $('#aj').show();
        $.ajax({
          url: "/"+model+"/soumettre/",
          type: "POST",
          data: $(form).serialize(),
          complete: function(data){
            if(data.responseText!="") {
                $('#aj').hide();
                alert(data.responseText);
            }
            else {
                addlatest(model);
            }
          }
        });
        return false;
    }

    function submitConfirmation(form) {
        $('.aj').show();
        $.ajax({
          url: "/calendrier/confirme/",
          type: "POST",
          data: $(form).serialize(),
          complete: function(data){
            if(data.responseText!="") {
                $('.aj').hide();
                alert(data.responseText);
            }
            else {
                $('.aj').hide();
                $(".pop").html('<h3>Merci <a class="close" href="/">Fermer</a></h3>')
                $(".close").live('click', function() {
                    $(".pop").slideFadeToggle(function() { 
                        $("#contact").removeClass("selected");
                        $(".pop").remove();
                    });
                    return false;
                });
                /*$(".pop").slideFadeToggle(function() { 
                    $("#contact").removeClass("selected");
                    $(".pop").remove();
                });*/
            }
          }
        });
        return false;
        
    }


    (function($){  
  
        //cache nav  
        var nav = $("#navigation");  
  
        //add indicators and hovers to submenu parents  
        nav.find("li").each(function() {  
            if ($(this).find("ul").length > 0) {  
  
                //$("<span>").text("^").appendTo($(this).children(":first"));  
  
                //show subnav on hover  
                $(this).mouseenter(function() {  
                    $(this).find("ul").stop(true, true).show();  
                });  
  
                //hide submenus on exit  
                $(this).mouseleave(function() {  
                    $(this).find("ul").stop(true, true).hide();  
                });  
            }  
        });  
    })(jQuery);  

    //ajaxify news
    (function($) {
        if ($('#plus').size() > 0) {
            $('#plus').click(function() {morelivre(2); return false;});
        }
    })(jQuery);
    
    $(function() {
        $.ajax({
          url: "/calendrier/get_form/",
          success: function(data){
            $("a.confirme").live('click', function(event) {
                $(".pop").remove();
                var id = $(this).attr('rel');
                var name = $(this).parent().children('span').html()
                if (!name) name = $(this).parent().children('h3').html()
                $(this).addClass("selected").parent().append(data);

                $("#id_evenement").val(id);
                $("#id_evenement").after(name);
                $("#id_evenement").hide();
                $(".pop").slideFadeToggle(function() { 
                    //
                });
                return false;
            });

            $(".close").live('click', function() {
                $(".pop").slideFadeToggle(function() { 
                    $("#contact").removeClass("selected");
                    $(".pop").remove();
                });
                return false;
            });
          }
        });
    });


    $.fn.slideFadeToggle = function(easing, callback) {
        return this.animate({ opacity: 'toggle', height: 'toggle' }, "fast", easing, callback);
    };   





















