$(function() {

$('#trows, #authshlf, #bkshlf, #covers').click(function(e){
    var $clicked = $(e.target);
    if ($($clicked).parent().parent().parent().get(0).id == 'authshlf') {var authshelf='y';}
    if ($clicked.parent().hasClass('viewTip')) {
             $($clicked).parent().cluetip({
                 width: '400px',
                 activation: 'click',
                 titleAttribute: 'title1',
                 ajaxCache: true,
                 arrows: true,
                 hoverIntent: false,
                 sticky: true,
                 mouseOutClose: true,
                 closePosition: 'title',
                 closeText: 'X',
                 waitImage: false
             });
             $($clicked.parent()).trigger('click');
             return false;
    }
    if ($clicked.hasClass('statTip')) {
            $($clicked).cluetip({
                width: '175px',
                activation: 'click',
                arrows: true,
                ajaxCache: false,
                hoverIntent: false,
                sticky: true,
                mouseOutClose: true,
                closePosition: 'title',
                closeText: 'X',
                waitImage: false,
                onShow : function (ct, c) {
                              $(".tbr").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 3);
                              $("#cluetip-close").click();
                               });
                              $(".read").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 2);
                              $("#cluetip-close").click();
                               });
                              $(".currread").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 1);
                              $("#cluetip-close").click();
                               });
                              $(".loan").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 4);
                              $("#cluetip-close").click();
                               });
                              $(".order").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 5);
                              $("#cluetip-close").click();
                               });
                              $(".none").click(function() {
                              var bookid1 = $(this).get(0).id;
                              var bookid = bookid1.substring(2);
                              updateStatus(bookid, 0);
                              $("#cluetip-close").click();
                               });
                }
            });
             $($clicked).trigger('click');
             return false;
    }
    if ($clicked.hasClass('shelfTip1')) {
                    $($clicked).cluetip({width: '175px',
                     activation: 'click',
                     arrows: true,
                     hoverIntent: false,
                     sticky: true,
                     mouseOutClose: true,
                     waitImage: false,
                     ajaxCache: false,
                     closePosition: 'title',
                     closeText: 'X'
                  });
             $($clicked).trigger('click');
             return false;
    }
    if ($clicked.parent().hasClass('fav') || $clicked.parent().hasClass('nofav')) {
        var parentTag = $($clicked).parent().parent().get(0).id;
        var id = parentTag.substring(3);
        updateFav(id);
        return false;

    }

       if ($clicked.parent().hasClass('del')) {
                   parentTag = $($clicked).parent().parent().get(0).id;
               var bookid = parentTag.substring(2);
                   id = parentTag.substring(2);
               var warn = 'Are you sure?';
               var warntit = 'Delete All My Info From This Book';
               var dtyp = 'b';
               if (parentTag.substring(0,3) == 'sha') {
                   id = parentTag.substring(3);
                   warntit = 'Delete This Author From My Shelf';
                   warn = 'Are you sure? All books will be removed.';
                   dtyp = 'a';
                   }
               $.alerts.confirm(warn, warntit, function(r) {
               if (r) { }
               else {return;}
               $.get ('/includes/shelfdel.php',
                     {id:id, dtyp:dtyp},
                     function(data) {
                         temp = data.split('/');
                         var userid = temp[0];
                         if (userid > 0) {
                             var shba = temp[1] ;
                             var bookid = temp[2] ;
                             var ins = temp[3] ;
                             var basestar = temp[4] ;
                             var bsba = temp[5] ;
                             var shnum = shba + bookid;
                             var shid = shnum + " li:first-child";
                             if (shba == '#sh') {
                                 var emptya = '<a href="#" title="add book to this shelf">&nbsp;&nbsp;&nbsp;&nbsp;</a>';
                                 $(shid).attr('class', 'status');
                                 }
                             else{
                                 emptya = '<a href="#" title="add books by this author to this shelf">&nbsp;&nbsp;&nbsp;&nbsp;</a>';
                                 $(shid).attr('class', 'nofav');
                             }
                             shid = shnum + " li:nth-child(2)";
                             $(shid).attr('class', 'adds1').html(emptya);
                             shid = shnum + " li:nth-child(3)";
                             $(shid).attr('class', 'adds2').html(emptya);
                             shid = shnum + " li:nth-child(4)";
                             $(shid).attr('class', 'adds3').html(emptya);
                             shid = shnum + " li:nth-child(5)";
                             $(shid).attr('class', 'delhide');
                             $('#edbk').attr('class', 'hide');
                             onClickHide();
                             if (basestar > "0") {
                                var rtid = bsba + bookid;
                                var newrating = "rating " + basestar;
                                $(rtid).attr('class', newrating);
                            }
                         if (authshelf=='y') {location.reload();}
                         }
               }
                 );
           });
           return false;
       }
    parentTag = $($clicked).parents('ul').get(0).id;
    id = parentTag.substring(2);
    var action = $clicked.parents('li').attr('className');
    var atyp = 'b';
    if (parentTag.substring(0,3) == 'sha' || parentTag.substring(0,3) == 'rta') {
        id = parentTag.substring(3);
        atyp = 'a';
        }
    switch (action) {
        case 'one': updateRating(id, 1, atyp); return false; break;
        case 'two': updateRating(id, 2, atyp); return false; break;
        case 'three': updateRating(id, 3, atyp); return false; break;
        case 'four': updateRating(id, 4, atyp); return false; break;
        case 'five': updateRating(id, 5, atyp); return false; break;
        case 'adds1': addShelf(id, 1, atyp); if (authshelf=='y') {location.reload();} return false; break;
        case 'adds2': addShelf(id, 2, atyp); if (authshelf=='y') {location.reload();} return false; break;
        case 'adds3': addShelf(id, 3, atyp); if (authshelf=='y') {location.reload();} return false; break;
    }
})
});
	function onSelectChange(){
		var selected = $("#genre option:selected");
        var selected2 = $("#edition option:selected");
        var selected3 = $("#binding option:selected");
        var selected4 = $("#filters option:selected");
        var genresel = 'tbody tr';
        var filterssel = 'tbody tr';
        var edsel = 'tbody tr';
        var bindssel = 'tbody tr';
        var sel2 = '1';
        var sel3 = '1';
        var sel4 = '1';
        switch (selected.val()) {
            case 'r': genresel = 'tbody tr.r'; break;
            case 's': genresel = 'tbody tr.s'; break;
            case 'p': genresel = 'tbody tr.p'; break;
            case 'w': genresel = 'tbody tr.w'; break;
            case 'g': genresel = 'tbody tr';
            }
        switch (selected2.val()) {
            case 'fe': edsel = 'tbody tr.fe'; break;
            case 're': edsel = 'tbody tr.re'; break;
            case 'a':  edsel = 'tbody tr'; sel2 = '0'; break;
        }
        switch (selected3.val()) {
            case 'hc': bindssel = 'tbody tr.hc'; break;
            case 'pb': bindssel = 'tbody tr.pb'; break;
            case 'al': bindssel = 'tbody tr'; sel3 = '0'; break;
        }
        switch (selected4.val()) {
            case 'ps': filterssel = 'tbody tr.ps'; break;
            case 'fav': filterssel = 'tbody tr.fav'; break;
            case 'n': filterssel = 'tbody tr'; sel4 = '0'; break;
        }
            var filsel = sel2 + sel3 + sel4;
            $('tbody tr').hide();
            switch (filsel) {
            case '111': var $filterstring = $(genresel).filter(edsel).filter(bindssel).filter(filterssel); break;
            case '110': $filterstring = $(genresel).filter(edsel).filter(bindssel); break;
            case '101': $filterstring = $(genresel).filter(edsel).filter(filterssel); break;
            case '100': $filterstring = $(genresel).filter(edsel); break;
            case '011': $filterstring = $(genresel).filter(bindssel).filter(filterssel); break;
            case '010': $filterstring = $(genresel).filter(bindssel); break;
            case '001': $filterstring = $(genresel).filter(filterssel); break;
            case '000': $filterstring = $(genresel); break;
        }
            $filterstring.show();
            $('#cntrec').text ($filterstring.length);

	}
	function onSelectChangeAuth(){
		var selected = $("#list option:selected");
        var selected2 = $("#status option:selected");
        var selected3 = $("#rating option:selected");
        var selected4 = $("#favorite option:selected");
        var listsel = 'tbody tr';
        var statussel = 'tbody tr';
        var ratingsel = 'tbody tr';
        var othersel = 'tbody tr';
        var sel2 = '1';
        var sel3 = '1';
        var sel4 = '1';
        switch (selected.val()) {
            case '1': listsel = 'tbody tr:has(li.chks1)'; break;
            case '2': listsel = 'tbody tr:has(li.chks2)'; break;
            case '3': listsel = 'tbody tr:has(li.chks3)'; break;
            case '-': listsel = 'tbody tr';
            }
        switch (selected2.val()) {
            case 'n': statussel = 'tbody tr:has(li.status)'; break;
            case 'w': statussel = 'tbody tr:has(li.statuswant)'; break;
            case 'c': statussel = 'tbody tr:has(li.statusreading)'; break;
            case 'r': statussel = 'tbody tr:has(li.statusread)'; break;
            case 'l': statussel = 'tbody tr:has(li.statusloan)'; break;
            case 'o': statussel = 'tbody tr:has(li.statusorder)'; break;
            case '-':  statussel = 'tbody tr'; sel2 = '0'; break;
        }
        switch (selected3.val()) {
            case '0': ratingsel = 'tbody tr:has(ul.onestaro, ul.twostaro, ul.threestaro, ul.fourstaro, ul.fivestaro, ul.nostar)'; break;
            case '1': ratingsel = 'tbody tr:has(ul.onestar)'; break;
            case '2': ratingsel = 'tbody tr:has(ul.twostar)'; break;
            case '3': ratingsel = 'tbody tr:has(ul.threestar)'; break;
            case '4': ratingsel = 'tbody tr:has(ul.fourstar)'; break;
            case '5': ratingsel = 'tbody tr:has(ul.fivestar)'; break;
            case '-': ratingsel = 'tbody tr'; sel3 = '0'; break;
        }
        switch (selected4.val()) {
            case 'fav': othersel = 'tbody tr:has(li.fav)'; break;
            case '-': othersel = 'tbody tr'; sel4 = '0'; break;
        }
            var filsel = sel2 + sel3 + sel4;
            $('tbody tr').hide();
            switch (filsel) {
            case '111': var $filterstring = $(listsel).filter(statussel).filter(ratingsel).filter(othersel); break;
            case '110': $filterstring = $(listsel).filter(statussel).filter(ratingsel); break;
            case '101': $filterstring = $(listsel).filter(statussel).filter(othersel); break;
            case '100': $filterstring = $(listsel).filter(statussel); break;
            case '011': $filterstring = $(listsel).filter(ratingsel).filter(othersel); break;
            case '010': $filterstring = $(listsel).filter(ratingsel); break;
            case '001': $filterstring = $(listsel).filter(othersel); break;
            case '000': $filterstring = $(listsel); break;
        }
            $filterstring.show();
            $('#cntrec').text ($filterstring.length);

	}
    	function onSelectChangeSort(){
		var selected = $("#covsrt option:selected");
        action = selected.val().substring(0,1);
        authorid = selected.val().substring(1);
        switch (action) {
            case 'n': location.href = '../author/author.php?ltyp=1&sort=n&authorid=' + authorid; break;
            case 'a': location.href = '../author/author.php?ltyp=1&sort=a&authorid=' + authorid; break;
            case 't': location.href = '../author/author.php?ltyp=1&sort=t&authorid=' + authorid; break;
            case 'd': location.href = '../author/author.php?ltyp=1&sort=d&authorid=' + authorid; break;
            case 'p': location.href = '../author/author.php?ltyp=1&sort=p&authorid=' + authorid; break;
            case 'r': location.href = '../author/author.php?ltyp=1&sort=r&authorid=' + authorid;
            }
        }
        function onClickShow(){
           $('#bookedit').attr('class', 'none');
           $('#bc').attr('class', 'breadcrumb hide');
           $('#bcmem').attr('class', 'breadcrumb none');
           return false;
        }
        function onClickHide(){
           $('#bookedit').attr('class', 'hide');
           $('#bcmem').attr('class', 'breadcrumb hide');
           $('#bc').attr('class', 'breadcrumb none');
           return false;
        }
    	function updateRating(id, rating, atyp){
               $.get ('/includes/rating.php',
                     {id:id, rating:rating, atyp:atyp},
                     function(data) {
                         temp = data.split('/');
                         var userid = temp[0];
                         if (userid > 0) {
                         var id = temp[1] ;
                         var basestar = temp[2] ;
                         var ins = temp[3] ;
                         var rtba = temp[4] ;
                         var shba = temp[5] ;
                         var rtid = rtba + id;
                         var newrating = "rating " + basestar;
                         $(rtid).attr('class', newrating);
                         if (rtba == "#rt"){
                             var shid = shba + id + " li:first-child";
                             $(shid).attr('class', 'statusread');
                             }
                         if (ins == 1) {
                             var delid = shba + id + " li:nth-child(5)";
                                $(delid).attr('class', 'del');
                                $('#edbk').attr('class', 'none');
                            }
                         }
               }
                 );
	}
        	function updateStatus(bookid, status){
               $.get ('/includes/shelfstatusupd.php',
                     {bookid:bookid, status:status},
                     function(data) {
                         temp = data.split('/');
                         var userid = temp[0];
                         if (userid > 0) {
                         var bookid = temp[1] ;
                         var status = temp[2] ;
                         var ins = temp[3] ;
                         var shid = "#sh" + bookid + " li:first-child";
                         $(shid).attr('class', status);
                         if (ins == 1) {
                             var delid = "#sh" + bookid + " li:nth-child(5)";
                                $(delid).attr('class', 'del');
                                $('#edbk').attr('class', 'none');
                            }
                         }
               }
                 );
	        }
          function addShelf(id, shelf, atyp){
               $.get ('/includes/shelfadd.php',
                     {id:id, shelf:shelf, atyp:atyp},
                     function(data) {
                         temp = data.split('@');
                         var userid = temp[0];
                         if (userid > 0) {
                         var shba = temp[1] ;
                         var id = temp[2] ;
                         var shelf1 = temp[3] ;
                         var shelf2 = temp[4] ;
                         var shelf3 = temp[5] ;
                         var ins = temp[6] ;
                         var nth = temp[7] ;
                         var inner = temp[8];
                         var shnum = shba + id;
                         if (shba == '#sh') {
                            var emptya = '<a href="#" title="add book to this shelf">&nbsp;&nbsp;&nbsp;&nbsp;</a>';
                            var shid = shnum + " li:nth-child(2)";
                            $(shid).attr('class', shelf1).html(emptya);
                            shid = shnum + " li:nth-child(3)";
                            $(shid).attr('class', shelf2).html(emptya);
                            shid = shnum + " li:nth-child(4)";
                            $(shid).attr('class', shelf3).html(emptya);
                            }
                         else {
                            if (shelf1 > "0"){
                                shid = shnum + " li:nth-child(2)";
                                $(shid).attr('class', shelf1);
                                }
                            if (shelf2 > "0"){
                                shid = shnum + " li:nth-child(3)";
                                $(shid).attr('class', shelf2);
                                }                                                         
                            if (shelf3 > "0"){
                                shid = shnum + " li:nth-child(4)";
                                $(shid).attr('class', shelf3);
                            }
                            }
                         shid = shnum + " li:nth-child(" + nth + ")";
                         $(shid).html(inner);
                         if (ins == 1) {
                             var delid = shba + id + " li:nth-child(5)";
                                $(delid).attr('class', 'del');
                                $('#edbk').attr('class', 'none');
                            }
                         }
               }
                 );
	        }
    function updateFav(id){
        $.get ('/includes/favauth.php',
              {id:id},
              function(data) {
                temp = data.split('@');
                var userid = temp[0];
                if (userid > 0) {
                   var shba = temp[1] ;
                   var id = temp[2] ;
                   var favstat = temp[3] ;
                   var shnum = shba + id;
                   var shid = shnum + " li:first-child";
                   $(shid).attr('class', favstat);
                }
               }
                 );
	        }
    function changeDate(){
        var d = new Date();
        var day = d.getDate();
        var month = d.getMonth();
       document.getElementById('month').selectedIndex = month + 1;
       document.getElementById('day').selectedIndex = day;
       document.getElementById('year').selectedIndex = 1;
        return false;
	        }
