if(typeof BuTpp == "undefined"){
    var BuTpp = {};
}
BuTpp.timeout = null;
BuTpp.authd = false;
BuTpp.currBTId = null;
BuTpp.currUId = null;
BuTpp.pingIntervall = 60000;
BuTpp.maxAttrListItems = 25;
if(window.location.hostname == 'localhost'){
    BuTpp.remUrl = "http://localhost/Buchtipp/public";
}
else{
    BuTpp.remUrl = "http://" + window.location.hostname + "";
}
BuTpp.ping = function(){
    var url = BuTpp.remUrl + '/a/pimg';
    $.get( url );
    window.setTimeout("BuTpp.ping()",BuTpp.pingIntervall);
}
BuTpp.getForm = function(formName){
    var url = BuTpp.remUrl + '/ajax/index/get-form';
    
    $.ajax({
        type: 'POST',
        url: url,
        data: {
            'formName':formName
        },
        success: BuTpp.addForm,
        dataType: 'JSON'
    });
}
BuTpp.addForm = function(data){
    if(data.success == true){
        if(data.html != ''){
            
    }
    }
}
BuTpp.addRezensionMehrButtom = function(elem){
    
    var mehrLinkmapper =    $('<div class="taRight displNone w100 buttonMapper"><em>&darr; </em><span class="linkSmall more">mehr</span></div>');
    $(elem).parent('div').after(mehrLinkmapper);
    
    $(elem).parent('div').siblings('.buttonMapper').slideDown(60);
    
    
}

BuTpp.showMessage = function(message,timeShow){
    window.clearTimeout(BuTpp.timeout);
    if(!timeShow){
        var intTimeShow = 4500;
    }
    else{
        var intTimeShow = timeShow;
    }
    
    $('p.message').slideUp(2000,function(){
        $(this).remove();
    })
    var m = $('<p class="message">' + message + '</p>');
    m.css('opacity','0');
    $('table#btmenu,table#umenu').before(m);
    m.slideDown( 8000, function(){
        $(this).animate(
        {
            opacity: 1
        }
        , 1200,function(){
            
            BuTpp.timeout = window.setTimeout('BuTpp.hideMessage()', intTimeShow);
        }
        );
    });
}
BuTpp.hideMessage = function(){
    $("p.message").animate(
    {
        opacity: 0
    }, 3000, function() {
        $(this).slideUp( 600, function(){
            $(this).remove();
        });
    });
}
BuTpp.hide = function(elem){
    $(elem).animate({
        opacity: 0.05
    },0,function(){
        $(this).animate({
            height: 0.0
        },200,function(){
            $(this).remove();
        });
    });
}    
BuTpp.show = function(elem){
    $(elem).css('opacity',0);
    $(elem).slideDown(210,function(){
        
        $(this).animate({
            opacity: 1
        },200,BuTpp.doAferShow(elem));
    });
}
BuTpp.doAferShow = function(elem){
    if($(elem).hasClass('btAttrList') && $(elem).hasClass('attaches') ){
        $(".rezension p").each(function(index,element){
            if($(element).height()==71){
                BuTpp.addRezensionMehrButtom(element);
            }
            else{
                $(element).siblings('.trans').remove(); 
            }
        });
    }
}
BuTpp.initAjaxActions = function(){
    // Neue Nachrichten Button oben blinken
    $('#newBuchtippButton').live("click",function(){
        document.location.href = BuTpp.remUrl + '/buchtipp/neu-ean-isbn';
    });
    if($('#pMessages').size()){
       
        var timoutNewMessage = window.setInterval(function(){
            
            if(parseInt($('#pMessages em').text()) > 0){
                $('#pMessages em').animate({
                    "background-color":"#EDD"
                },500,function(){
                    $('#pMessages em').animate({
                        "background-color":"#E00"
                    },500);
                });
            }
        },10000);   
    }
           
      
        
    
    //PrivateMessage
    $('#pmessageButton').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-new-p-messaage/'+ (parseInt(BuTpp.uid));
            $.ajax({
                type: 'GET',
                url: url,
                
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#umenu').siblings('.a'));
                        BuTpp.hide($('.btAttrList .a'));
                        
                        BuTpp.addContentAfter('umenu','fPersonalMessage',data.html);
                        
                        $('#fPersonalMessage #senden').on('click',function(){
                            $.ajax({
                                url: url,
                                data: $('#fPersonalMessage').serialize(),
                                type: 'post',
                                dataType: 'json',
                                beforeSend: function( xhr ) {
                
                                },
                                success: function( data ) {
                
                    
                                    if(!data.valid){
                                        
                                        $('#fPersonalMessage').children('dl').html(
                                            $(data.html).children('dl').html()
                                            );
                                    }
                                    else if(data.stored){
                                        
                                        BuTpp.menuAddmessage('Ihre Nachricht wurde gesendet.');
                                        BuTpp.hide($('#fPersonalMessage'));
                                        
                                    }
                                },
                                error: function (){
                
                                }
                            });
                            
                        });
                    }
                    else{
                        BuTpp.showMessage(data.errorMessage,18000);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
        
        
        
        
        
        
    //End
    
    
    
    var searchHasFocus = false;
    $('#search').on('mouseover',function(){
        $(this).children().css('opacity',1);
    });
    $('#search').on('mouseout',function(){
        if(!searchHasFocus){
            $(this).children().css('opacity',0.3);
        }
    });
    
    $('#search button').click(function(){
        var q = $('#search input').val();
        $(this).css('opacity',1);
        $(this).siblings().css('opacity',1);
        if(q){
            window.location.href = BuTpp.remUrl + "/buchtipp/suche/"+ escape(q);
        }
    });
    
    $('#search input').keypress(function(event) {
        if ( event.which == 13 ) {
            $('#search button').trigger('click');
        }
    
    })
    .focus(function(event) {
        searchHasFocus = true
    })
    .blur(function(event) {
        searchHasFocus = false;
        $(this).css('opacity',0.3);
        $(this).siblings().css('opacity',0.3);
    });
    
    $('ul.btAttrList.attaches .buttonMapper span.more').live('click',function(){
        $(this).html('weniger').removeClass('more').addClass('less').siblings('em').html('&uarr; ');
        
        $(this).parent('.buttonMapper').siblings('.mapper').children('p').css('max-height','1000px').siblings('.trans').css('display','none');
    });
    
    $('ul.btAttrList.attaches .buttonMapper span.less').live('click',function(){
        $(this).html('mehr').removeClass('less').addClass('more').siblings('em').html('&darr; ');
        $(this).parent('.buttonMapper').siblings('.mapper').children('p').css('max-height','71px').siblings('.trans').css('display','block');
    });
    
    $('#umenu a').live('click',function(e){
        var url = BuTpp.remUrl + '/a-u/' + $(this).attr('rel') + '/'+ BuTpp.uid +'/';
                                      

            
        $.ajax({
            type: 'POST',
            url: url,
            data: {
                start:0,
                limit:BuTpp.maxAttrListItems,
                id:BuTpp.uid
            },
            success: function(data){
                if(data.success){
                    var elToShow = $(data.html);
                    BuTpp.hide($('.btAttrList'));
                    BuTpp.show(elToShow);
                    $('#umenu').after(elToShow);
                } 
            }
        });
    });
    
    // Mag und habe Gelesem Burron
    $('#buchtippViewList #readButton[value=1],#buchtippViewList #likeButton[value=1],#followButton[value=1],#followButtonProf[value=1]').hover( 
        function(){
            $(this).removeClass('yes').addClass('no');
                                    
        }, 
        function(){
            $(this).removeClass('no').addClass('yes');  ;   
        } );
                                
    $('#buchtippViewList #readButton,#buchtippViewList #likeButton,#followButton,#followButtonProf').live('click',
        function(e){
            $(this).css('opacity',0.2);
            var elem = $(this);
            
            var url;
            var pData = {};
            pData.id = BuTpp.currBTId;
            
            switch(elem.attr('id')){
                case 'readButton':
                    url = BuTpp.remUrl + '/a/set-has-read/';
                    pData.hasRead = $(this).val();
                    break;
                case 'likeButton':
                    url = BuTpp.remUrl + '/a/set-like/';
                    pData.like = $(this).val();
                    break;
                case 'followButtonProf':
                    url = BuTpp.remUrl + '/a/follow-from-btipp';
                    pData.follow = $(this).val();
                    pData.BuTppId = BuTpp.currBTId;
                    break;
                case 'followButton':
                    url = BuTpp.remUrl + '/a/follow';
                    pData.follow = $(this).val();
                    pData.BuTppUid = BuTpp.uid;
                    break;
            }

            
            $.ajax({
                type: 'POST',
                url: url,
                data: pData,
                success: function(data){
                    
                    if(data.success){
                        elem.val(data.val);
                        elem.attr('value',data.val);
                    
                        if(data.val==-1){
                            elem.removeClass('yes')
                            .removeClass('no')
                            .unbind('mouseenter').unbind('mouseleave');
                        
                            switch(elem.attr('id')){
                                case 'readButton':
                                
                                    $('#cLeserMenu').text(parseInt($('#cLeserMenu').text())-1);
                                    BuTpp.showMessage('Sie haben "' + BuTpp.currBTTitel + '" von ihren gelesenen Büchern entfernt.');
                                    $('#mLGelesenVon').trigger('click');
                                
                                    break;
                                case 'likeButton':
                                    $('#cLikesMenu').text(parseInt($('#cLikesMenu').text())-1);
                                    BuTpp.showMessage('Sie haben "Mag ich" von "' + BuTpp.currBTTitel + '" entfernt.');
                                    $('#mLBuchtippVon').trigger('click');
                                    break;
                                case 'followButton':
                                    BuTpp.showMessage('Sie folgen "@' + BuTpp.uName + '" nichr mehr.');
                                    break;
                                
                                
                            }
                            
                            
                        }
                        else{
                            elem.hover( 
                                function(){
                                    $(this).removeClass('yes').addClass('no');
                                    
                                }, 
                                function(){
                                    $(this).removeClass('no').addClass('yes');  ;   
                                } );
                            elem.addClass('yes');
                            switch(elem.attr('id')){
                                case 'readButton':
                                    $('#cLeserMenu').text(parseInt($('#cLeserMenu').text())+1);
                                    BuTpp.showMessage('Sie haben "' + BuTpp.currBTTitel + '" ihren gelesenen Büchern hinzugefügt.');
                                    $('#mLGelesenVon').trigger('click');
                                    break;
                                case 'likeButton':
                                    $('#cLikesMenu').text(parseInt($('#cLikesMenu').text())+1);
                                    BuTpp.showMessage('Sie haben "' + BuTpp.currBTTitel + '" mit "Mag ich" markiert.');
                                    $('#mLBuchtippVon').trigger('click');
                                    break;
                                case 'followButton':
                                    BuTpp.showMessage('Sie folgen jetzt "@' + BuTpp.uName + '".');
                                    break;
                            }
                             
                        }
                        
                        elem.css('opacity',1);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
    
    
    
    $('.authD').live('click',
        function(e){
            
            if (!BuTpp.authd ){
                
                window.location.href = BuTpp.remUrl + "/buchtipp/anmelden";
            }
            
        }
        );
    
    $('.a').not('#startMenuCat a.a').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
        }
        );
    $('.a input[type=submit],.a button').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            return false;
        }
        
        );
            
    $('div.closer a,div.closer em').live('click',
        function(e){
            e.preventDefault();
            BuTpp.hide($(this).parent().parent());
            return false;
        }
        );
            
    $('#addTags').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-tags/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
            $.ajax({
                type: 'GET',
                url: url,
                
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#btmenu').siblings('.a'));
                        BuTpp.hide($('.btAttrList .a'));
                        BuTpp.addContentAfter('btmenu','fAddTags',data.html);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
    
        
    $('#newBewertung').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-bewertung/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
            $.ajax({
                type: 'GET',
                url: url,
                data:{
                    'btId':BuTpp.currBTId
                },
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#btmenu').siblings('form'));
                        BuTpp.hide($('.btAttrList .a'));
                        BuTpp.addContentAfter('btmenu','fAddBewertung',data.html,false);
                        BuTpp.initStars();
                        BuTpp.initBewButton();
                        
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
        
        
    $('#fAddTags.a #senden').live('click',function(){
        var url = BuTpp.remUrl + '/a/add-tags/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
        $.ajax({
            url: url,
            data: $('#fAddTags.a').serialize(),
            type: 'post',
            dataType: 'json',
            beforeSend: function( xhr ) {
                
            },
            success: function( data ) {
                if(!data.valid){
                    
                    $('#fAddTags').children('dl').html(
                        $(data.html).children('dl').html()
                        );
                }
                else{
                    if(data.tags && data.tags != ','  ){
                        $('span#tags.fSerCur').text($('span#tags.fSerCur').text()+ ',' + data.tags);
                        BuTpp.showMessage('Tags wurden hinzugefügt.');
                        BuTpp.hide($('form#fAddTags'));
                    }
                }
            },
            error: function (){
                
            }
        });
        return false;
    });  
    
    
    
    $('#newRezension').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-rezension/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
            $.ajax({
                type: 'GET',
                url: url,
                data:{
                    'btId':1
                },
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#btmenu').siblings('form.a'));
                        BuTpp.hide($('.btAttrList .a'));
                        BuTpp.addContentAfter('btmenu','fAddTags',data.html);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
        
        
    $('#fRezension.a #senden').live('click',function(){
        var url = BuTpp.remUrl + '/a/add-rezension/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
        $.ajax({
            url: url,
            data: $('#fRezension.a').serialize(),
            type: 'post',
            dataType: 'json',
            beforeSend: function( xhr ) {
                
            },
            success: function( data ) {
                
                    
                if(!data.valid){
                    $('#fRezension').children('dl').html(
                        $(data.html).children('dl').html()
                        );
                }
                else if(data.stored){
                    $('a[rel="rezensionen-zitate"]').trigger('click');
                    BuTpp.menuAddmessage('Ihre Rezension wurde gespeichert.')
                    $('#cAttachesMenu').text(1 + parseInt($('#cAttachesMenu').text()));
                }
            },
            error: function (){
                
            }
        });
        return false;
    });
    
    $('#newPMessage').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-mew-p-message/'+ $(this).attr("rel");
            $.ajax({
                type: 'GET',
                url: url,
                data:{
                    'btId':1
                },
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#btmenu').siblings('form.a'));
                        BuTpp.hide($('.btAttrList .a'));
                        BuTpp.addContentAfter('btmenu','fNewPMessage',data.html);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
        
        
    $('#fNewPMessage.a #senden').live('click',function(){
        var url = BuTpp.remUrl + '/a/add-mew-p-message/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
        $.ajax({
            url: url,
            data: $('#fRezension.a').serialize(),
            type: 'post',
            dataType: 'json',
            beforeSend: function( xhr ) {
                
            },
            success: function( data ) {
                
                    
                if(!data.valid){
                    $('#fRezension').children('dl').html(
                        $(data.html).children('dl').html()
                        );
                }
                else if(data.stored){
                    $('a[rel="rezensionen-zitate"]').trigger('click');
                    BuTpp.menuAddmessage('Ihre Rezension wurde gespeichert.')
                    $('#cAttachesMenu').text(1 + parseInt($('#cAttachesMenu').text()));
                }
            },
            error: function (){
                
            }
        });
        return false;
    });
    
    $('#newZitat').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var url = BuTpp.remUrl + '/a/add-zitat/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
            $.ajax({
                type: 'GET',
                url: url,
                data:{
                    'btId':1
                },
                success: function(data){
                    
                    if(data.success){
                        BuTpp.hide($('#btmenu').siblings('form.a'));
                        BuTpp.hide($('.btAttrList .a'));
                        BuTpp.addContentAfter('btmenu','fAddTags',data.html);
                    }
                },
            
                dataType: 'JSON'
            });
            return false;
        });
    $('#fZitat.a #senden').live('click',function(){
        var url = BuTpp.remUrl + '/a/add-zitat/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
        $.ajax({
            url: url,
            data: $('#fZitat.a').serialize(),
            type: 'post',
            dataType: 'json',
            beforeSend: function( xhr ) {
                
            },
            success: function( data ) {
                
                    
                if(!data.valid){
                    //alert('aaaa' + data.html+  $(data.html).children('dl').html());
                    $('#fZitat').children('dl').html(
                        $(data.html).children('dl').html()
                        );
                }
                else if(data.stored){
                    $('a[rel="rezensionen-zitate"]').trigger('click');
                    BuTpp.menuAddmessage('Ihr Zitat wurde gespeichert.')
                    $('#cAttachesMenu').text(1 + parseInt($('#cAttachesMenu').text()));
                }
            },
            error: function (){
                
            }
        });
        return false;
    });
        
    // Buchtipp Menu
    $('#btmenu a.aM').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            BuTpp.hide($('#btmenu').siblings('.a'));
            var action = $(this).attr('rel');
            $('ul.btAttrList').animate({
                opacity:.1
            },200,function(){
                var url = BuTpp.remUrl + '/a/' + action  + '/' + (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
                $.ajax({
                    type: 'GET',
                    url: url,
                    data:{
                        'btId':(parseInt(BuTpp.currBTId)?BuTpp.currBTId:'')
                    },
                    success: function(data){
                    
                        if(data.success){
                            var id = $(data.html).attr('id');
                            BuTpp.hide($('ul.btAttrList'));
                            BuTpp.addContentAfter('btmenu',id,data.html,false);
                        }
                    },
            
                    dataType: 'JSON'
                });
            });
            return false;
            
            
        });
    $('#shBewertungen').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            
            $('#btmenu a[rel="bewertungen"]').trigger('click');
            return false;
        }
    
    
        );
    $('.edit').live('click',
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            
            
            var strId = '' + $(this).attr('id');    
            var arrIdParts = strId.split('_');
            if(arrIdParts.length==2 && $(this).siblings('form').length < 1){

                var url = BuTpp.remUrl + '/a/update-attach'
                $.ajax({
                    type: 'post',
                    url: url,
                    data:{
                        't':arrIdParts[0],
                        'id':arrIdParts[1],
                        'act':'get'
                    },
                    success: function(data){
                    
                        if(data.success){
                            var form = $(data.html).css('display','none');
                            $('#'+data.id).siblings('.attCont').slideUp(200);
                            $('#'+data.id).siblings('.attCont').after(form);
                            $(form).css('background','#DAEDFA').css('padding-bottom','10px').
                            css('position','relative').css('margin','5px 0');
                            $(form).prepend('<div class="absol fsmall closer update"><a>Schließen</a> <em></em></div>')
                            $(form).children('.closer').click(function(){
                                $(form).siblings('.attCont').slideDown(200,function(){
                                    $(form).remove();
                                }
                                );
                            });
                            $(form).slideDown(200);
                        
                        }
                    },
            
                    dataType: 'JSON'
                });
            }
            return false;
        }
        );
    
    
    $('.btAttrList form #senden').live('click',
    
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var formToSend = $(this).parents('dd').parents('dl').parents('form');
            var strId = '' + $(formToSend).siblings('span.edit').attr('id');    
            var arrIdParts = strId.split('_');
            var typeToChange = null;
            if(arrIdParts[0]=='z'){
                typeToChange = 'z';
            }
            else if(arrIdParts[0]=='r'){
                typeToChange = 'r';
            }
            
            
            if(arrIdParts.length==2){

                var url = BuTpp.remUrl + '/a/update-attach'
                $.ajax({
                    type: 'post',
                    url: url,
                    data:{
                        't':arrIdParts[0],
                        'id':arrIdParts[1],
                        'act':'store',
                        'formdata':$(formToSend).serialize()
                    },

                    success: function(data){
                        if(data.success){
                            if(data.valid == false){
                                var form = $(data.html);
                                //alert('aaa');
                                //                            .css('display','none');
                                //                            $(form).css('background','#DAEDFA').css('padding-bottom','10px').
                                //                            css('position','relative').css('margin','5px 0');
                                //                            $(form).prepend('<div class="absol fsmall closer update"><a>Schließen</a> <em></em></div>')
                                //                            $(form).children('.closer').click(function(){
                                //                                $(form).siblings('.attCont').slideDown(200,function(){
                                //                                    $(form).remove();
                                //                                }
                                //                                );
                                //                            });
                                //                            $(form).slideDown(200);
                                $(formToSend).html($(form).html());
                             
                            }
                            else{
                                if(typeToChange=='z'){
                                    BuTpp.showMessage('Ihre Änderungen am Zitat wurden gespeichert.');
                                }
                                else if(typeToChange=='r'){
                                    BuTpp.showMessage('Ihre Änderungen an der Rezension wurden gespeichert');
                                }
                                if($('#btmenu').length){
                                    $('#btmenu #mLRezensionenZitate').trigger('click');
                                }
                                else if($('#umenu').length){
                                    if(typeToChange=='z'){
                                        $('#umenu a[rel=zitate]').trigger('click');
                                    }
                                    else if(typeToChange=='r'){
                                        $('#umenu a[rel=rezensionen]').trigger('click');
                                    }
                                }
                            }
                        }
                        else{
                    }
                        
                    },
            
                    dataType: 'JSON'
                });
            }
            return false;
            
        }
    
        );
    $('#startMenu a').live('click',

    

    
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var clickedElement = this;
            var url = BuTpp.remUrl + "/a/get-arr-buchtipps-startseite";
            var postData = {
                type:$(clickedElement).attr('rel')
            }
            $.ajax({
                type: 'post',
                url: url,
                data:postData,
                success: function(data){
                    $('#buchtipplisteAll').animate({
                        opacity:0.2
                    },200,function(){
                        $('#buchtipplisteAll').html(data.html);
                        BuTpp.initSliders();
                        $('#buchtipplisteAll').animate({
                            opacity:1
                        },200);
                    });
                    
                },
                dataType: 'JSON'
            });
                    
        });
        
    $('#startMenuUser a').live('click',
    
        function(e){
            // If IE
            if (!e)
                event.returnValue = false;
            // Firefox, Safari, Opera
            else
                e.preventDefault();
            var clickedElement = this;
            var url = BuTpp.remUrl + "/a-u/get-arr-user-startseite/1";
            var postData = {
                id:1,
                type:$(clickedElement).attr('rel')
            }
            $.ajax({
                type: 'post',
                url: url,
                data:postData,
                success: function(data){
                    $('#UserlisteAll').animate({
                        opacity:0.2
                    },200,function(){
                        $('#UserlisteAll').html(data.html);
                        BuTpp.initSliders();
                        $('#UserlisteAll').animate({
                            opacity:1
                        },200);
                    });
                    
                },
                dataType: 'JSON'
            });
                    
        });    
        
    $("#buchtippliste .coverImg img").live("mouseover",function(){
        var currImg = this;
        var content = $(this).parents("span").siblings(".preText").html();
        $("#buchtippliste .coverImg img").css("box-shadow","#fff 0 0 8px 2px");
        $("#descr").animate({
            opacity:.3
        }, 10, function() {
                
            $(currImg).css("box-shadow","#333 0 0 8px 2px");
            $(this).html(content).animate({
                opacity:1
            },300);
        });
    });
    $("#userliste .userImg img").live("mouseover",function(){
        var currImg = this;
        var content = $(this).parents("span").siblings(".preText").html();
        $("#userliste .userImg img").css("box-shadow","#fff 0 0 8px 2px");
        $("#descrUser").animate({
            opacity:.3
        }, 10, function() {
                
            $(currImg).css("box-shadow","#333 0 0 8px 2px");
            $(this).html(content).animate({
                opacity:1
            },300);
        });
    });
    
    $("#changeProfilImage").live("click",function(e){
        // If IE
        if (!e)
            event.returnValue = false;
        // Firefox, Safari, Opera
        else
            e.preventDefault();
        $("#uImgUplMapper").remove();
        BuTpp.hide($(".btAttrList"));
        var iframe = $('<iframe id="uImgFrame" name="uImgFrame" width="1" height="1" scrolling="no" marginheight="0" marginwidth="0" frameborder="0"></iframe>');
        var url = BuTpp.remUrl + "/a-u/profilbild/" + BuTpp.uid;
        var postData = {
            action:'get',
            id: BuTpp.uid

        }
        $.ajax({
            type: 'post',
            cache:false,
            url: url,
            data:postData,
            success: function(data){
                    
                var html = data.html;
                    
                var id = $(html).children('form').attr('id');
                BuTpp.addContentAfter('umenu',id,html);
                $('#'+id).attr('target','uImgFrame');
                $('#'+id).attr('action',url);
                $('#'+id).attr('class','');
                    
                $('#'+id).append(iframe);
                BuTpp.addCloser($("#uImgUplMapper"));    
                
            },
            dataType: 'JSON'
        });
    });
    BuTpp.initSliders();
    
    $("#fUserImage #profilhochladen").live("click",function(e){
        var iframe = $('<iframe id="uImgFrame" name="uImgFrame" width="1" height="1" scrolling="no" marginheight="0" marginwidth="0" frameborder="0"></iframe>');
        var interval = window.setInterval(function(){
            if($('#uImgFrame').contents().find('#uImgUplMapper').length >0){
                        
                var uImgUplMapper = $('#uImgFrame').contents().find('#uImgUplMapper')
                $("#uImgUplMapper").html($(uImgUplMapper).html());
                var id = $("#uImgUplMapper").children('form').attr('id');
                if(id == 'fUserImage'){
                    var url = BuTpp.remUrl + "/a-u/profilbild/" + BuTpp.uid;
                    $('#'+id).attr('target','uImgFrame');
                    $('#'+id).attr('action',url);
                    $('#'+id).attr('class','');
                    $('#'+id).append(iframe);
                }
                
                else if($('#cropbox').length >0){
                    //alert($("#cropbox").attr('src') + $("#cropbox").attr('title') + 'd');
                    $("#cropbox").attr("src",$("#cropbox").attr("title"));
                    $('#cropbox').load(function(){
                        var minDim = Math.min($('#cropbox').height(),$('#cropbox').width());
                        
                        var hw = minDim - 20;
                        $('#cropbox').Jcrop({
                            onSelect:    BuTpp.setCoords,
                            bgColor:     'black',
                            bgOpacity:   .4,
                            setSelect:   [ Math.round(($('#cropbox').width()- hw)/2), Math.round(($('#cropbox').height()- hw)/2), hw, hw ],
                            aspectRatio: 1 / 1
                        });
                        $('#jcrop-tracker').trigger('mouseup');
                        
                    });
                    $('#startCropImg:not([disabled])').die();
                    $('#startCropImg:not([disabled])').live('click',function(e){
                        
                        // If IE
                        if (!e)
                            event.returnValue = false;
                        // Firefox, Safari, Opera
                        else
                            e.preventDefault();
                        var postData = {
                            id: BuTpp.uid,
                            act:'crop',
                            x:$('#x').val(),
                            y:$('#y').val(),
                            x2:$('#x2').val(),
                            y2:$('#y2').val(),
                            w:$('#w').val(),
                            h:$('#h').val()
                        }
                        var url = BuTpp.remUrl + "/a-u/profilbild/" + BuTpp.uid;
                        $.ajax({
                            type: 'post',
                            cache: false,
                            url: url,
                            data:postData,
                            success: function(data){
                                BuTpp.hide($(".bgrBlueLight"));
                                BuTpp.showMessage(data.message);
                                if(data.success){
                                    $('#profBild img').attr('src',BuTpp.remUrl + '/img/user/generated/'+ data.newImage)
                                }
                                window.setTimeout(function(){
                                    100,
                                    $('#umenu a[rel=aktionen]').trigger('click');
                                    $("body").scrollTop(-($("body").scrollTop));
                                
                                    
                                });
                            },
                            dataType: 'JSON'
                        });
                    });
                }
                window.clearInterval(interval);
                BuTpp.addCloser($("#uImgUplMapper"));
            }
                    
        }, 200);
    });
   
    
    BuTpp.initAddResultItems();
    
}

BuTpp.setCoords = function(c){
    $('button[disabled]').removeAttr('disabled');
    $('#x').val(c.x);
    $('#y').val(c.y);
    $('#x2').val(c.x2);
    $('#y2').val(c.y2);
    $('#w').val(c.w);
    $('#h').val(c.h);      
}

BuTpp.addContentAfter = function(id,idNewElem,content,addClose){
    
    
    var newElem = $('#' + idNewElem);
    if(newElem){
        $(newElem).remove();
    }
    var cont = $(content);
    if(typeof addClose == 'undefined' ||  addClose){
        BuTpp.addCloser(cont);
    }
    $(cont).css('display','none');
    $(cont).css('opasity',.1);
    $('#'+id).after($(cont)); 
    BuTpp.show($(cont));
}    
BuTpp.addCloser = function(elem){
    $(elem).prepend('<div class="absol fsmall closer"><a>Schließen</a> <em></em></div>')
}    
BuTpp.menuAddmessage = function(message){
    if(message){
        var elemMessage = $('<p class="message a">' + message + '</p>');
        window.setTimeout(function(elem){
            $(elemMessage).css('display','none');
            $('table.menu').after($(elemMessage));
            $(elemMessage).fadeIn(200);
            window.setTimeout(function(elem){
                $(elemMessage).animate({
                    opacity:0,
                    heigh:0
                },1000,function(){
                    $(this).remove();
                });
            }, 8000, elemMessage);
        }, 600, elemMessage);
        
    }
}    
    
    
$(document).ready(function(){
    BuTpp.initAjaxActions();

    $("button,a.button,:submit").on('mousedown',
        function(){
        
            $(this).addClass('click');
        });
    $("button,a.button,:submit").on('mouseup',
        function(){
            $(this).removeClass('click');
        
        });
    if($("p.message").length){
        BuTpp.timeout = setTimeout(BuTpp.hideMessage, 6000);
    }
    
    
    $("a.a").live('click',function(){
			
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        // moves to a new page. 
        // pageload is called at once. 
        $.history.load(hash);
        return false;
    });

    BuTpp.initStars();
    
    var starsId = '#stern' + $('#bewertung').val();
    $(starsId).trigger('click');
    $('p.description').after('<div class="pfeil"></div>');
    $('textarea,input,select').on('focus',function(){
        $(this).siblings('p,div').each(function(){
            $(this).fadeIn(1000);
        //alert($(this).attr('class'));
        });
    });
    $('textarea,input,select').on('focusout',function(){
        $(this).siblings('p,div').each(function(){
            $(this).fadeOut(400);
        //alert($(this).attr('class'));
        });
    });
});

BuTpp.initStars = function(){
    $('table#stars img').on('click',function(){
        $(this).attr('src',BuTpp.remUrl + '/img/page/star_full_30.png');
        $(this).parent('td').prevAll('td').children('img').attr('src',BuTpp.remUrl + '/img/page/star_full_30.png');
        $(this).parent('td').nextAll('td').children('img').attr('src',BuTpp.remUrl + '/img/page/star_empty_30.png');
        $('#bewertung').val($(this).parent('td').prevAll('td').length + 1);
    });
}
BuTpp.initBewButton = function(){
    $('#bewerten').on('click',function(e){// If IE
        if (!e)
            event.returnValue = false;
        // Firefox, Safari, Opera
        else
            e.preventDefault();
        var url = BuTpp.remUrl + '/a/add-bewertung/'+ (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
        $.ajax({
            type: 'post',
            url: url,
            data:{
                'btId':BuTpp.currBTId,
                'bewertung':$('form #bewertung').val(),
                'text':$('form #text').val()
            },
            success: function(data){
                    
                if(data.success && !data.valid){
                    BuTpp.hide($('#btmenu').siblings('form'));
                    BuTpp.hide($('.btAttrList .a'));
                    BuTpp.addContentAfter('btmenu','fAddBewertung',data.html,false);
                    window.setTimeout(function(){
                        BuTpp.initStars();
                        var bewertung = $('input#bewertung').val();
                        
                        $('#stern' + bewertung).trigger('click');
                        BuTpp.initStars();
                        BuTpp.initBewButton();
                    }, 310);
                        
                        
                }
                if(data.valid){
                    BuTpp.hide($('#btmenu').siblings('form'));
                    BuTpp.hide($('.btAttrList .a'));
                    $('a[rel=bewertungen]').trigger('click');
                    $('cBewertungenMenu').text($('cBewertungenMenu').text() + 1);
                    $('a#newBewertung').remove();
                    BuTpp.showMessage('Ihre Bewertung wurde gespeichert.');
                }
            },
            
            dataType: 'JSON'
        });
        return false;
    });
}

BuTpp.initSliders = function(){
    $("#userliste .userImg img:first").trigger("mouseover");
    $("#buchtippliste .coverImg img:first").trigger("mouseover");
    
    
    window.setTimeout(function(){
        var buchtipplisteClone = $("#buchtippliste").clone();

        $(buchtipplisteClone).addClass("buchtipplisteClone").css("visibility","hidden").css("position","absolute").css("left","-3000px").css("top","-1100px").css("width","auto");
        $("body").append($(buchtipplisteClone));
        var cImg = $(".buchtipplisteClone  .coverImg img").length;
        var buchtipplisteRealWidth = null;
        var buchtipplisteMapperRealWidth = $("#buchtipplisteMapper").width()+21;
        var loaded = 0;
        $(".buchtipplisteClone  .coverImg img").each(
            function(){
                $(this).load(function(){
                    loaded++;
                    if(loaded == cImg){
                    
                    
                        buchtipplisteRealWidth = $(".buchtipplisteClone").width();

                        $("#buchtippliste").css("width",buchtipplisteRealWidth + "px");
                    
                        $(".buchtipplisteClone").remove();
                        if(buchtipplisteMapperRealWidth < buchtipplisteRealWidth){
                       
                            var interval;
                            $("#buchtipplistenNext div").mouseup(
                                function(){
                                        
                                    window.clearInterval(interval);
                                }
                                );
                            $("#buchtipplistenNext div").mousedown(
                                function(){
                                    interval = window.setInterval(
                                        
                                        function(){
                                            var position = $("#buchtippliste").position();
                                            var currMargin = parseInt($("#buchtippliste").css("margin-left"));
                                            if(((currMargin * -1) +  buchtipplisteMapperRealWidth) < buchtipplisteRealWidth){
                                                
                                                $("#buchtippliste").css("margin-left",(currMargin - 10) + "px");
                                            }
                                            

                                        },30);
                                        
                                }
                                );
                                

                            var intervalNext;
                            $("#buchtipplisteBack div").mouseup(
                                function(){
                                        
                                    window.clearInterval(intervalNext);
                                }
                                );
                            $("#buchtipplisteBack div").mousedown(
                                function(){
                                    
                                    intervalNext = window.setInterval(
                                        
                                        function(){
                                            var currMargin = parseInt($("#buchtippliste").css("margin-left"));
                                            if(currMargin < 0){
                                                
                                                $("#buchtippliste").css("margin-left",(currMargin + 10) + "px");
                                            }
                                            

                                        },30);
                                        
                                }
                                );
                        }
                    
                    
                    
                            
                    }
                });
            });
    },1850);
        
    
    var userlisteClone = $("#userliste").clone();
    $(userlisteClone).addClass("userlisteClone").css("visibility","hidden").css("position","absolute").css("top","-1100px").css("width","auto");
    $("body").append($(userlisteClone));
    var cImg = $(".userlisteClone  img").length;
    var userlisteRealWidth = null;
    var userlisteMapperRealWidth = $("#userlisteMapper").width()+21;
    var userlisteloaded = 0;
    $(".userlisteClone  img").each(
        function(){
            $(this).load(function(){
                userlisteloaded++;
                
                if(userlisteloaded == cImg){
                    userlisteRealWidth = $(".userlisteClone").width();
                    $("#userliste").css("width",userlisteRealWidth + "px");
                    
                    $(".userlisteClone").remove();
                    if(userlisteMapperRealWidth < userlisteRealWidth){
                        var interval;
                        $("#userlisteNext div").mouseup(
                            function(){
                                        
                                window.clearInterval(interval);
                            }
                            );
                        $("#userlisteNext div").mousedown(
                            function(){
                                interval = window.setInterval(
                                        
                                    function(){
                                        var position = $("#userliste").position();
                                        var currMargin = parseInt($("#userliste").css("margin-left"));
                                        if(((currMargin * -1) +  userlisteMapperRealWidth) < userlisteRealWidth){
                                                
                                            $("#userliste").css("margin-left",(currMargin - 10) + "px");
                                        }
                                            

                                    },30);
                                        
                            }
                            );
                                

                        var intervalNext;
                        $("#userlisteBack div").mouseup(
                            function(){
                                        
                                window.clearInterval(intervalNext);
                            }
                            );
                        $("#userlisteBack div").mousedown(
                            function(){
                                    
                                intervalNext = window.setInterval(
                                        
                                    function(){
                                        var currMargin = parseInt($("#userliste").css("margin-left"));
                                        if(currMargin < 0){
                                                
                                            $("#userliste").css("margin-left",(currMargin + 10) + "px");
                                        }
                                            

                                    },30);
                                        
                            }
                            );
                    }
                            
                }
            });
        }
        );
}
BuTpp.isLoadMoreResultItems = false;
var currLiMaxLength = BuTpp.maxAttrListItems;
BuTpp.initAddResultItems = function(){
    var url = "";
    $(window).scroll(function(){
        
        
        if($("#left .btAttrList").length && $("#left .btAttrList li").length > 5){
            var btAttrList = $("#left .btAttrList");
            var btAttrListLi =    $("#left .btAttrList li");

            var cElems = $("#left .btAttrList li").length;
            var currElemOnLoadTop = $(btAttrListLi[$("#left .btAttrList li").length-5]).position().top;
            var currWindowPosBottom = $(window).height() + $(window).scrollTop()
            
            if(currLiMaxLength == cElems){
        
                if(currWindowPosBottom > currElemOnLoadTop && !BuTpp.isLoadMoreResultItems){
                    BuTpp.isLoadMoreResultItems = true;
                    jQuery.each(BuTpp.listResult,function (index, action) {
                        
                        if($(btAttrList).hasClass(index)){
                            
                            if($("#umenu").length){
                                url = BuTpp.remUrl + '/a-u/' + action + '/'+ BuTpp.uid +'/';
                            }
                            else if($("#btmenu").length){
                                url =  BuTpp.remUrl + '/a/' + action  + '/' + (parseInt(BuTpp.currBTId)?BuTpp.currBTId:'');
                            }
                        }
                        
                    });
                    
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: {
                            start: cElems + 1,
                            limit: currLiMaxLength,
                            id: BuTpp.uid,
                            ajax:1
                        },
                        success: function(data){
                            currLiMaxLength = currLiMaxLength + BuTpp.maxAttrListItems;
                            if(data.success){
                                var elToShow = $(data.html);
                                
                                $(elToShow).children("li").each(function(i,v){
                                    $("#left .btAttrList").append($(v));
                                });
                                BuTpp.isLoadMoreResultItems = false;
                                BuTpp.initAddResultItems();
                            } 
                        }
                    });
                }
            }
        }
    });
    
}

BuTpp.listResult = {

    'attaches':'rezensionen-zitate',
    'bewertungen':'bewertungen',
    'gelesenVon':'gelesen-von',
    'magichVon':'buchtipp-von',

    'buchTipps':'buchtipps',
    'actions':'aktionen',
    'rezensionen':'rezensionen',
    'zitate':'zitate',
    'following':'folgt',
    'followingFrom':'gefolgt-von'
}
