(function($){
    
    $.fn.reduce = function(){
        this.each(function(){
            $(this).addClass(""+$(this).find(".zonetitle h2").html()+" "+$(this).find('.dragzone').height()+"");
        });
    }
    

    
})(jQuery);

$(document).ready(function(){
    
        $('.reduce').click(function(){
        
            var cclass = $(this).parent().parent().attr("class").split(" ");
            
            var elem =  cclass[1];
            var cheight = cclass[2];

            if($(this).attr("rel")=="down"){
                $(this).attr("rel","up");
                $(this).removeClass("down").addClass("up");
                $("."+elem+"").find(".dropfile").animate({
                    opacity : 0  
                },500,function(){
                    $("."+elem+"").find(".dragzone").animate({
                        height: "-="+cheight+""
                    },500);                    
                });

            }else{
                $(this).attr("rel","down");
                $(this).removeClass("up").addClass("down");
                $("."+elem+"").find(".dragzone").animate({
                    height: "+="+cheight+""
                },500,function(){
                    $("."+elem+"").find(".dropfile").animate({
                        opacity : 1  
                    },500);
                });
            }
        });
        
    $(".zonetitle > h2").click(function(){
        $(this).hide();
        $(this).parent().find(".rename").show();
        $(this).parent().find(".rename").focus();
    });
    
    $(".rename").blur(function(){
        $(this).hide();
        $(this).parent().parent().find(".hidetitle").show();
    });
    
    /*$('.pcontent').children().hide();
    $('.pcontent').children().fadeIn(500);*/
    
});

function confirmer (message){
    return confirm(message);
}

window.onload = function(){
    var textareas = document.getElementsByTagName("textarea");
    for(var i = 0, t = textareas.length; i < t; i++){
        textareas[i].onkeydown = function(e){
            var tab = (e || window.event).keyCode == 9;
            if(tab){
                var tabString = String.fromCharCode(9);
                
                if(window.ActiveXObject){
                    var textR = document.selection.createRange();
                    var selection = textR.text;
                    textR.text = tabString + selection;
                    textR.moveStart("character",-selection.length);
            textR.moveEnd("character", 0);
                    textR.select();
                }
                else {
                    var beforeSelection = this.value.substring(0, this.selectionStart);
                    var selection = this.value.substring(this.selectionStart, this.selectionEnd);
                    var afterSelection = this.value.substring(this.selectionEnd);
                    this.value = beforeSelection + tabString + selection + afterSelection;
                    this.setSelectionRange(beforeSelection.length + tabString.length, beforeSelection.length + tabString.length + selection.length);
                }                
                this.focus();
                return false;
            }
        };        
    }
};

function answer(idcom,idnews){
    
    $("#comm"+idcom).find(".answercom").hide();
    
    var form = "<form action='pages/adm/addcom.php' method='post' class='contact' style='float:left;display: block;position:relative;width: 100%;margin-top: 10px;'>\
                        <br />\
                        <input type='hidden' value='"+idnews+"' name='idnews' />\
                        <input type='hidden' value='"+idcom+"' name='parent' />\
                        <label for='name' style='width:218px;' >Votre nom</label>\
                        <input type='text' id='name' name='name' />\
                        <label for='email' style='width:218px;'>Votre email</label>\
                        <input type='text' id='email' name='email' />\
                        <label for='url' style='width:218px;'>Votre site web (optionnel)</label>\
                        <input type='text' id='url' name='url' value='http://' />\
                        <label for='comcontent' style='width:218px;'>Votre message</label><br />\
                        <textarea cols='50' rows='10' class='textcom' id='comcontent' name='comcontent'></textarea>\
                        <input type='submit' style='float:right' value='Commenter' />\
                    </form><a class='cancelanswer' style='cursor:pointer;'>Annuler</a>";
    
    $("#comm"+idcom).append(form);
    
    $("#comm"+idcom).find('.textcom').markItUp(mySettings);
    
    $('.cancelanswer').click(function(){
        $(this).parent().find("form").slideUp(1000);
        $(this).hide();
        $(this).parent().find(".answercom").show();
    });
}
