$(document).ready(function () {

    // Andris
	//$('#viewport').carousel('#previous', '#next');
	
	//The auto-scrolling function
	//function slide(){
	//  $('#next').click();
	//}
	//Launch the scroll every 2 seconds
	//var intervalId = window.setInterval(slide, 5000);
	//Andris

    doMain = function(){

        var u=$('#u').val();
        var l=$(".activeflag").attr("id");

        $(".star-y").unbind("click").bind("click", function(){
            var i=$(this).parent().attr('class');
            var bid = i.substring(3);
            $(this).addClass('star-e').removeClass('star-y');            
            $.post('modules/jqf.php', { op:'shelf-set-star', bid:bid, star:'e',l:l, u:u }, function(data) {
                doMain();
            });
        });
        $(".star-e").unbind("click").bind("click", function() {
            var i=$(this).parent().attr('class');
            var bid = i.substring(3);
            $(this).addClass('star-y').removeClass('star-e');            
            $.post('modules/jqf.php', { op:'shelf-set-star', bid:bid, star:'y',l:l, u:u }, function(data) {
                doMain();
            });
        });
        /*
        $(".star-y").click(function() {
            var i=$(this).parent().attr('class');
            var bid = i.substring(3);
            $.post('modules/jqf.php', { op:'shelf-set-star', bid:bid, star:'e' }, function(data) {
                $(this).addClass('star-e').removeClass('star-y');
            });
        });

        $(".star-e").click(function() {
            var i=$(this).parent().attr('class');
            var bid = i.substring(3);
            $.post('modules/jqf.php', { op:'shelf-set-star', bid:bid, star:'y' }, function(data) {
                $(this).removeClass('star-e').addClass('star-y');
                doMain();
            });
        });
        */
        $(".row-closed").unbind("click").bind("click", function(){
                var i= $(this).parent().parent().attr('class');
                var bid = i.substring(3);
                $('tr.'+i+' > td > div.row-closed').addClass('row-opened').removeClass('row-closed');
                var on_off = $('tr.'+i+' > td').css('background-color');
                if(on_off=='rgb(153, 204, 255)' || on_off=='#99ccff'){
                    //$('tr.'+i+' > td').css({'background-color':'#FFFFFF'});                
                }
                else{
                    alert(offline_alert);
                }
                $('.label-element-new').css({'display':'inline'});
                //$('tr.'+i+' > td.row-closed').removeClass('row-closed');
                $('#'+bid+'-r').slideDown('fast');
                doMain();
        });
        $(".row-opened").unbind("click").bind("click", function() {
                var i= $(this).parent().parent().attr('class');
                var bid = i.substring(3);
                $('tr.'+i+' > td > div.row-opened').addClass('row-closed').removeClass('row-opened');
                var on_off = $('tr.'+i+' > td').css('background-color');
                if(on_off=='rgb(153, 204, 255)' || on_off=='#99ccff'){
                }
                else{
                    $('tr.'+i+' > td').css({'background-color':'#EEEEEE'});
                }
                $('.label-element-new').css({'display':'none'});
                $('#'+bid+'-r').slideUp('fast');
                $('#search-result-'+bid).html('');
                $('#search-result-'+bid).css({'height':'0px'});
                doMain();
        });
        $(".w-label-cell").unbind("click").bind("click", function(){
            var h=$(this).html();
            $.post('modules/jqf.php', { op:'shelf-filter-label', h:h, u:u,l:l }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".w-label-cell-del").unbind("click").bind("click", function() {
            var label=$(this).prev(".w-label-cell").html();
            var i= $(this).parent().parent().parent().attr('class');
            var bid = i.substring(3);
            $.post('modules/jqf.php', { op:'shelf-set-label-del', bid:bid, label:label, u:u, l:l }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".shelf-filter-star-on").unbind("click").bind("click", function() {
            $.post('modules/jqf.php', { op:'shelf-filter-stars', s:1, u:u, l:l }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".shelf-filter-star-off").unbind("click").bind("click", function() {
            $.post('modules/jqf.php', { op:'shelf-filter-stars', s:0, u:u, l:l }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".shelf-filter-all").unbind("click").bind("click", function() {
            $.post('modules/jqf.php', { op:'shelf-filter-all', u:u, l:l }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".label-element").unbind("click").bind("click", function(){
            var h=$(this).html();
            h = h.replace("<span>","");
            h = h.replace("</span>","");
            $.post('modules/jqf.php', { op:'shelf-filter-label', l:l, u:u,h:h }, function(data) {
                $('#ppw').html(data);
                doMain();
            });
        });
        $(".label-element-new").unbind("click").bind("click", function(){
            var nl = "";
            $.post("modules/jqf.php", {op:'shelf-add-new-label', l:l}, function(data) {
                Tip(data, CLICKSTICKY, true, FOLLOWMOUSE, false, LEFT, false, DELAY, 20);
                $("#nl").focus();        
            });
        });
        $(".note").unbind("click").bind("click", function() {
            var i=$(this).parent().attr('class');
            var bid=i.substring(9);
            $.post("modules/jqf.php", {op:'note-set',bid:bid, u:u, l:l}, function(data) {
                Tip(data, CLICKSTICKY, true, FOLLOWMOUSE, false, LEFT, false, DELAY, 20);
                $("#nl").focus();        
            });
        });
        $(".onoff-off").unbind("click").bind("click", function() {
            var i=$(this).attr('id');
            var bid=i.substring(3);
            $.post("modules/jqf.php", {op:'set-on', bid:bid, u:u }, function(data){
                $('#ppw').html(data);
            }); 
        });
        $(".onoff-on").unbind("click").bind("click", function() {
            var i=$(this).attr('id');
            var bid=i.substring(2);
            $.post("modules/jqf.php", {op:'set-off', bid:bid, u:u }, function(data){
                $('#ppw').html(data);
            }); 
        });
        $(".del-from-shelf").unbind("click").bind("click", function() {
            var i=$(this).attr('id');
            var bid=i.substring(3);
            $.post("modules/jqf.php", {op:'del-from-shelf', bid:bid, u:u,l:l }, function(data){
                $('#ppw').html(data);
            }); 
        });
        $(".label-element-new-inside").unbind("click").bind("click", function(){
            var i=$(this).attr('id');
            var bid=i.substring(4);
            var nl = "";
            $.post("modules/jqf.php", {op:'shelf-add-new-label-inside', bid:bid, l:l}, function(data) {
                Tip(data, CLICKSTICKY, true, FOLLOWMOUSE, false, LEFT, false, DELAY, 20);
                $("#nl").focus();        
            });
        });
        $(".read-online").unbind("click").bind("click", function() {
            var i= $(this).attr('id');
            var bid=i.substring(4);
            //document.location.target='_blank';
            var newWindow = window.open('index.php?eb='+bid, '_blank');
            newWindow.focus();
            return false;            
            //window.open('index.php?eb='+bid);
        });
        $(".nonactive-shelf").unbind("click").bind("click", function(){
            var thisid = $(this).attr('id');
            if(thisid=='osszes'){
                $.post("modules/jqf.php", {op:'shelf-get-all', u:u, l:l }, function(data){
                    $('#ppw').html(data);
                    doMain();
                }); 
            }
            else{
                $.post("modules/jqf.php", {op:'shelf-get-own', u:u, l:l }, function(data){
                    $('#ppw').html(data);
                    doMain();
                }); 
            }
        });
        
        $('#searchButton').unbind("click").bind("click", function(){
            var kulcsszo=$('#kulcsszo').val(); 
            $.post("modules/jqf.php", { op:'kereses', kulcsszo:kulcsszo, u:u,l:l }, function(data) {
                $('#ppw').html(data);
                doMain();    
            });
            
        });
        
        $(window).resize(reformat);
        function reformat()  {
            $('#polc-wrapper').width($(window).width()-154);
        }
        reformat();

        
        function page_enter(event) {
               if (event.which == 13) {
                    $('#searchButton').click();
               }
        }
        $("#kulcsszo").keypress(page_enter);        
        
    };    

    
});

