$(document).ready(function(){
    $("#left").corner("15px");
    $(".box").corner("10px");
    $(".inner:first").corner("7px");
    $(".inner:last").corner("tl tr 7px");
    $("#tabs").tabs();
    $("#tabs").corner("bl br");
    
    $("#content a").attr("target","_blank");
    $("#content p.pdf a, #content  a.removeTarget").removeAttr("target");
    
    $("#comment_submit_button").click(function() {
        document.comment_submit.submit();
    
    });
    
    $("#activate_email").focus(function() {                               
        $(this).val('');  
    });

    $("#activate").click(function() {
        var e = $("#activate_email").val();
        $(this).attr("href","register.php?email="+e);
    });
    
    //wysiwyg editor plus word count
    $("#extraction, #source").wysiwyg();
    
    $("#extractionIFrame").contents().keyup(function() {

        var chars = $(this).find("body").text().length;
        var maxChars = 5000;
        window.parent.document.getElementById("extract_chars").innerHTML = maxChars - chars;
        
        if (chars >= maxChars)
        {
            $(this).find("body").text($(this).find("body").text().substring(0,(maxChars-1)));
        }
        
    });
    
    $("#article_submit_button").click(function () {
        $.prompt('Are you sure?',{
                buttons: {yes:true, no:false},
                submit:function(v) { 
                    if (v == true) {
                        document.article_submit.submit();
                    } 
                }
            });
    });
                    
    $(".category").each(function () {
        //max height = 85px;
        //cur line height = 17px;
        // ul padding = 4.5px;
        //alert($(this).height());
        if(((($(this).height())-9)/17)>5) {
            
            var linesTooMany = Math.ceil(($(this).height()-9-85)/17);
            //alert(linesTooMany);
            $(this).children("ul").each(function () {
                                   
                for (i=0;i<linesTooMany;i++) {
                                         
                 //$(this).children("li:last-child").hide();
                 $(this).children("li:last-child").remove();
                 
                }        
                
                $(this).after("<a href='javascript:void(0)' title='' alt='' class='showMore'>...more</a>");
                $(".showMore").css({"color":"#e74495","font-weight":"bold"});
                
                $(".showMore").click(function() {
    
                    var cat = $(this).parents("div.category").attr("id");                   
                    $(this).parents("div.category").load("library/articles.php",{category:cat,clearCache:new Date().getTime()},function () {
                    
                    $(".showLess").css({"color":"#e74495","font-weight":"bold"});
                    
                    });
                    
    
                });   
                
            });
        }
    
    });                    

});
            
function showLess(cat) {
    
    if($("#"+cat+"> .showLess").size() > 0) {
        $("#"+cat+"> .showLess").remove();
    }
    
    $("#"+cat+".category").each(function () {
        //max height = 92px;
        //cur line height = 15px;
        // ul padding = 4px;
        //alert($(this).height());
        if((($(this).height()-9)/17)>5) {
            
            var linesTooMany = Math.ceil(($(this).height()-9-85)/17);
            
            $(this).children("ul").each(function () {
                                   
                for (i=0;i<linesTooMany;i++) {
                                         
                 //$(this).children("li:last-child").hide();
                 $(this).children("li:last-child").remove();
                 
                }        
                
                $(this).after("<a href='javascript:void(0)' title='' alt='' class='showMore'>...more</b></a>");
                $(".showMore").css({"color":"#e74495","font-weight":"bold"});
                
                $(".showMore").click(function() {
    
                    var cat = $(this).parents("div.category").attr("id");                   
                    $(this).parents("div.category").load("library/articles.php",{category:cat,clearCache:new Date().getTime()},function () {
                    
                    $(".showLess").css({"color":"#e74495","font-weight":"bold"});
                    
                    });
                    
    
                });   
                
            });
        }
    
    });
    
    
}

    function upload() {
        new AjaxUpload('pdf_upload', {
        action: 'library/upload.php',
        name:'file',
        data: {
            title:document.getElementById('pdf_upload_title').value,
            category:$('#category option:selected').text()
        },
        autoSubmit: true,
        responseType: false,
        onSubmit: function(file,ext) {

            if (! (ext && /^(pdf)$/.test(ext))){
            // extension is not allowed
            $.prompt('Error : Invalid file type.');
            // cancel upload
            return false;
            }
            
            
            $('#pdf_upload').addClass('hidden');
            $('#upload_progress').addClass('unhidden');

            },
        onComplete: function(file,response) {
            $('#upload_progress').removeClass('unhidden');
            $('#upload_progress').addClass('hidden');
            $.prompt(response);
            }
        });
    }
    
    function categorySelected() {
        
        var title = document.getElementById('pdf_upload_title').value;
        
        if (title != '') {
            $('#pdf_upload_div').addClass('unhidden');
            $('#pdf_upload').addClass('unhidden').addClass('pdf_upload');
            upload();
        } else {
            $('#category option[value="Please select"]').attr('selected','selected');
            $.prompt('Please enter a title before selecting a category.');
        }
    }
