function legendToggle(e) {    // toggle legend div and add symbol +/- for status    
    
            var div = $(e).parent('fieldset').find('div.toggle')
            div.toggle();
            
            if ( $(div).css('display') === 'none') {
                $( e ).find('span').html('+')
            }
            else {
                $( e ).find('span').html('-')
            }  
}

function galerieAddNav(){     // Add step for each galerie on page load   
    
    $("div.galerie").each(function(){ 
        var total = parseInt( $( this ).attr('rel') )
        var line = parseInt( $("#galnb").html() )
        var step = Math.ceil(total/line)
        $( this ).find(".step").html('1 / ' + step )            
        })
}

function closeForm(){  //kill textarea instances and close the form
    CKEDITOR.instances.content_1.destroy();
    CKEDITOR.instances.content_2.destroy();
    CKEDITOR.instances.content_3.destroy();
    CKEDITOR.instances.content_4.destroy();
    $("#blogform").remove()
}

function deletePost(){
            var postid = $("#postid").val();
            var answer = confirm('Really delete post ?');
            if( answer ){
                $.ajax({
               url: "/blog/delete.php",
               type: "POST",
               data: { id : postid },
               success: function(msg){
                    closeForm()
                    location.reload();
               }
            });       
            }
}

function submitForm(){  // Send form and datas
    for ( instance in CKEDITOR.instances )  // get values for each iframe textarea instances
            CKEDITOR.instances[instance].updateElement();

            
            var temp1 = ''
            var temp2 = ''
            var temp3 = ''
            var temp4 = ''
            $(".galerie_1").each(function(e){
                if( e > 0){
                    temp1 += ';'                
                }
                temp1 += $( this ).attr('rel');
                
            })
            $(".galerie_2").each(function(e){
                if( e > 0){
                    temp2 += ';'          
                    }              
                temp2 += $( this ).attr('rel')
                
            })
            $(".galerie_3").each(function(e){
                if( e > 0){
                    temp3 += ';'
                }               
                temp3 += $( this ).attr('rel')
                
            })
            $(".galerie_4").each(function(e){ 
                if( e > 0){
                    temp4 += ';'
                }               
                temp4 += $( this ).attr('rel')
                
            })
            
            var postid = $("#postid").val()
            var postevent = $("#postevent").val()
            var postdate = $("#postdate").val()
            
            var title_1 = $("#title_1").val()
            var content_1 = $("#content_1").val()
            var movie_1 = $("#movie_1").val()
            var picture_1 = temp1
            var link_1 = $("#link_1").val()            
            var attachment_1 = $("#file_upload_1_r").val()
            var attachment_1_code = $("#file_upload_1_u").val()
            
            var title_3 = $("#title_3").val()
            var content_3 = $("#content_3").val()
            var movie_3 = $("#movie_3").val()
            var picture_3 = temp3
            var link_3 = $("#link_3").val()            
            var attachment_3 = $("#file_upload_3_r").val()
            var attachment_3_code = $("#file_upload_3_u").val()
            
            var title_2 = $("#title_2").val()
            var content_2 = $("#content_2").val()
            var movie_2 = $("#movie_2").val()
            var picture_2 = temp2
            var link_2 = $("#link_2").val()            
            var attachment_2 = $("#file_upload_2_r").val()
            var attachment_2_code = $("#file_upload_2_u").val()
            
            var title_4 = $("#title_4").val()
            var content_4 = $("#content_4").val()
            var movie_4 = $("#movie_4").val()
            var picture_4 = temp4
            var link_4 = $("#link_4").val()            
            var attachment_4 = $("#file_upload_4_r").val()
            var attachment_4_code = $("#file_upload_4_u").val()
            
            if( $('#first').is(':checked') ){ 
                var first = '1'
            }
            else {
                var first = '0'
            }
            if( $('#highlight').is(':checked') ){ 
                var highlight = '1'
            }
            else {
                var highlight = '0'
            }
            
            if( $('#displaydate').is(':checked') ){ 
                var displaydate = '1'
            }
            else {
                var displaydate = '0'
            }
            
            if( $('#detail').is(':checked') ){ 
                var detail = '1'
            }
            else {
                var detail = '0'
            }
            
            if( $("#feed").length ) {
                if( $('#feed').is(':checked') ){ 
                    var feed = '1'
                }
                else {
                    var feed = '0'
                }
            }
            else {
                var feed = $("#feed1").val()
            }
            var homedate = $("#homedate").val()
                
            
            $.ajax({
               url: "/blog/post.php",
               type: "POST",
               data: { id : postid, postdate : postdate, postevent : postevent, title_1 : title_1, content_1 : content_1, attachment_1 : attachment_1, attachment_1_code : attachment_1_code, link_1 : link_1, movie_1 : movie_1, picture_1 : picture_1, title_3 : title_3, content_3 : content_3, attachment_3 : attachment_3, attachment_3_code : attachment_3_code, link_3 : link_3, movie_3 : movie_3, picture_3 : picture_3, title_2 : title_2, content_2 : content_2, attachment_2 : attachment_2, attachment_2_code : attachment_2_code, link_2 : link_2, movie_2 : movie_2, picture_2 : picture_2, title_4 : title_4, content_4 : content_4, attachment_4 : attachment_4, attachment_4_code : attachment_4_code, link_4 : link_4, movie_4 : movie_4, picture_4 : picture_4, feed : feed, first : first, highlight : highlight, displaydate : displaydate, detail : detail, homedate : homedate },
               success: function(msg){
                    CKEDITOR.instances.content_1.destroy();
                    CKEDITOR.instances.content_2.destroy();
                    CKEDITOR.instances.content_3.destroy();
                    CKEDITOR.instances.content_4.destroy();
                    $("#blogform").remove()
                    location.reload();
                 
               }
            });    
            return false;    
}

function prepareForm(e){
    $("body").append('<div id="blogform"></div>')
    $("#blogform").load("/blog/pages/form.php", { id: e.attr("rel"), event : e.attr("title") } , function(){} )
    
    var width = Math.floor(( parseInt($(window).width())-parseInt( $("#blogform").width() ) )/2);
    
    $("#blogform").css("left", width + "px" )
    var top = ( parseInt( $("html").scrollTop() )  );
    $("#blogform").css("top", top + "px" )
}

function galerieUp(e){
    
    var line = parseInt( $("#galnb").html() )     
    var div = e.parents("div.galerie")
    
    var total = parseInt( div.attr('rel') )
    var step = Math.ceil(total/line)
    var position = parseInt( div.attr('title') )
    
    div.find('a.fancybox').css("display", "none")
    
    for (var i=position*line+line;i<position*line+(line*2);i++) {
        div.find('a.fancybox:eq(' + i + ')').fadeIn(1800)
    }

    div.attr('title', position+1)
    if( position+1 != 0 ){
        div.find("div.down").removeClass('hide')    
    }
    if( total <= (position+1)*line+line) {
        div.find("div.up").addClass('hide')
    }
    
    div.find(".step").html((position+2) + ' / ' + step)
}

function galerieDown(e){
    
    var line = parseInt( $("#galnb").html() )     
    var div = e.parents("div.galerie")

    var total = parseInt( div.attr('rel') )
    var step = Math.ceil(total/line)
    var position = parseInt( div.attr('title') )

    div.find('a.fancybox').css("display", "none")

    for (var i=position*line-line;i<position*line;i++) {
        div.find('a.fancybox:eq(' + i + ')').fadeIn(1800)
    }

    div.attr('title', position-1)
    if( position-1 == 0 ){
        div.find("div.down").addClass('hide')    
    }
    if( total > (position-1)*line+line) {
        div.find("div.up").removeClass('hide')
    }

    div.find(".step").html((position) + ' / ' + step)
    
}


$(document).ready(function(){
    

    
    $("div.up").click(function(){
        galerieUp( $( this ) )
    })
    
    $("div.down").click(function(){
        galerieDown( $( this ) )
    })
    
    if( $("div.galerie").length ) {
         galerieAddNav()
    }
    
    $("a.addPost").click(function(){
        prepareForm( $( this ) )
        return false;
    })

    $("#blogclose").live('click', function(){
        closeForm()
    })

    $("#postdelete").live('click',function(){
        deletePost()
    })

    $("#postsubmit").live('click',function(){
        submitForm()
    })

    
    $("span.clear").live('click', function(){
        var rel = $( this ).attr('rel')
        $( "#" + rel + "_r").val('')
        $( "#" + rel + "_u").val('')
        $( "#" + rel + "_t").html('')        
    })
    
    $("span.clearimage").live('click', function(){
        $( this).parent('span').remove() 
    })
    
    $('legend').live('click', function(){
        legendToggle( $( this ) )
    })
    

    $("a.fancybox").fancybox();

    
});
