$(document).ready(bsa3_init);

function bsa3_init(){    
    $('#bc_loader').click(load_community);
    //$('#login_but').live('click', show_login_box);
    $('#uir_message').click(uir_inline_edit);
    $('#manage_account li').mouseover(function(){jQuery('#manage_account_box').show()});
    $('#manage_account li').mouseout(function(){jQuery('#manage_account_box').hide()});
    jQuery('.search_dd_wrap').mouseover(function(){jQuery('.search_dd').show();});
    jQuery('.search_dd_wrap').mouseout(function(){jQuery('.search_dd').hide();});
    jQuery('.search_dd a').click(function(){
        if(jQuery(this).prev().length){
            jQuery('#local-search').hide();
            jQuery('#cse-search-box').show();            
        }else{
            jQuery('#cse-search-box').hide();
            jQuery('#local-search').show();
        }
        jQuery('.search_dd').fadeOut('fast');
    })
    
    quick_read_init();

    if(navigator.appName=='Microsoft Internet Explorer' && navigator.appVersion.search('MSIE 6.0') != -1){
        scale_images_ie6();
    }
    try{
        jQuery('.left_twitter, .top_right_search').corner("5px");        
    }catch(err){
        ;
    }
}

var bs3_globals = new Object;
function load_community(){
    if(bs3_globals['bc_opened']==undefined || bs3_globals['bc_opened']==false){
        bs3_globals['bc_opened'] = true;
        if(bs3_globals['bc_content']!=undefined){
            $('#community_container').html(bs3_globals['bc_content']);
            $('#community_container').slideDown("slow");        
            return;
        }
    }else{                               
        $('#community_container').slideUp("slow"); 
        bs3_globals['bc_opened'] = false;
        return;
    }
    bs3_globals['bc_text'] = $('#bc_loader').html(); 
    $('#community_container').html('<a style="margin:4px">loading...</a>');
    $('#community_container').slideDown("fast");
    $.ajax({
       type: "POST",
       url: ajx_url,
       data: 'ajx_action=browse_community',
       success: function(msg){   
            $('#community_container').html(msg);
            $('#community_container').slideDown("slow");
            bs3_globals['bc_content']=msg;
       }
     });
             
}

function select_community_section(section){

    if($('#tab_'+section).hasClass('selected')){
        return;
    }
    $('#community_container #opis li').removeClass('selected');
    $('#community_container .tab_content').slideUp("fast");
    $('#'+section).slideDown("fast");
    $('#tab_'+section).addClass('selected');    
}

function show_login_box(){
    if(bs3_globals['lb_opened']==undefined || bs3_globals['lb_opened']==false || !bs3_globals['lb_opened']){
        bs3_globals['lb_opened'] = true;
        if(bs3_globals['lg_content'] != undefined){
            $('#login_box').html(bs3_globals['lg_content']);
            $('#login_box').slideDown("slow");        
            return;            
        }
    }else{                               
        $('#login_box').slideUp('slow'); 
        bs3_globals['lb_opened'] = false;
        return;
    }  
    $('#login_box').html('<a>loading...</a>');
    $('#login_box').slideDown("fast");    
    $.ajax({
       type: "POST",
       url: ajx_url,
       data: 'ajx_action=load_template&template=login_box&redirect_to='+location.href,
       success: function(msg){   
            $('#login_box').html(msg);
            $('#login_box').slideDown('slow');
            $('#login_box input[name="log"]').focus();
            bs3_globals['lg_content']=msg;
       }
     });
    return false;         
}

function uir_inline_edit(){
    var uir = $('#user_is_reading');    
    
    if(bs3_globals['uir_content']!=undefined){
        uir.html(bs3_globals['uir_content']);
        $('#user_is_reading_form input[value=\'cancel\']').click(uir_inline_edit_close);
        $('#user_is_reading_form').submit(uir_save);
        uir.slideDown();        
        return;
    }
    
    uir.html('<a>loading...</a>');
    uir.slideDown("fast");    
    
    $.ajax({
       type: "POST",
       url: ajx_url,
       data: 'ajx_action=load_template&template=uir_inline',
       success: function(msg){   
            uir.html(msg);
            uir.slideDown();    
            $('#user_is_reading_form').submit(uir_save);
            $('#user_is_reading_form input[value=\'cancel\']').click(uir_inline_edit_close);
            bs3_globals['uir_content']=msg;
       }
     });    
}
function uir_inline_edit_close(){
    $('#user_is_reading').slideUp();  
}

function uir_save(){
    var ser = $('#user_is_reading_form').serialize();
    $('#user_is_reading_form input').attr('disabled','disabled');
    $.ajax({
       type: "POST",
       url: ajx_url,
       data: 'ajx_action=save_uir&' + ser,
       success: function(msg){   
            uir_inline_edit_close();                        
            $('#uir_by').html('by ' + '<i>'+$('#user_is_reading_form input[name="by"]').val()+'</i>');            
            $('#uir_book').html($('#user_is_reading_form input[name="book"]').val());
            if($('#user_is_reading_form input[name="ean"]').val()){
                if(0 === $('#user_is_reading_form input[name="ean"]').val().search('http://')){
                    $('#uir_url').attr('href', $('#user_is_reading_form input[name="ean"]').val());
                }else{
                    $('#uir_url').attr('href', 'http://book.co.za/bookfinder/ean/'+$('#user_is_reading_form input[name="ean"]').val());
                }                
            }else{
                $('#uir_url').attr('href','#');
            }   
            $('#img_ldr').hide();         
            bs3_globals['uir_content'] = undefined;
            $('#user_is_reading_form input').removeAttr('disabled');
            return false;
       }
     });        
    return false;
}

function load_side_articles(s){
    jQuery('#recent-articles-side-in').css("background-color","#eee").css('opacity','0.2'); 
    jQuery('#progress').fadeIn("fast");    
    jQuery('#recent-articles-side-in').load(ajx_url+'?get_recent_articles',{offset:s},function(){jQuery('#progress').fadeOut("fast");jQuery('#recent-articles-side-in').css("background-color","#fff").css('opacity','1'); ;});
}
function load_forum_posts(s){
    jQuery('#forum-posts-widget').css("background-color","#eee").css('opacity','0.2'); 
    jQuery('#progress-f').fadeIn("fast");    
    jQuery('#forum-posts-widget').load(ajx_url+'?get_recent_forum_posts',{offset:s},function(){jQuery('#progress-f').fadeOut();jQuery('#forum-posts-widget').css("background-color","#fff").css('opacity','1'); });
}

function quick_read_init(){
    var dheight = jQuery('body').css('height')!='auto'?jQuery('body').css('height'):document.body.clientHeight;
    var dwidth = jQuery('body').css('width')!='auto'?parseInt(jQuery('body').css('width')):document.body.clientWidth;
    
    jQuery('body').prepend('<div id="_wrapper_mask" style="position: absolute;' + 
        'left:0;top:0;background-color:#000;opacity:.50;filter:alpha(opacity=50);-moz-opacity:0.5;'+
        'height:'+dheight+';width:100%;z-index:1000;display:none;"></div>');    
    width = 641;
    height = 400;
    left = (dwidth-width)/2;
    qrtop = 100;
    jQuery('body').prepend('<div id="quick_read_spot" style="position: absolute;' + 
        'left:'+left+'px;top:'+qrtop+'px;'+
        'height:'+height+'px;width:'+width+'px;z-index:1001;display:none;"><div id="quick_read_content"></div></div>');        
    jQuery('.quick_read').click(quick_read);
}
function quick_read_close(){        
    jQuery('#_wrapper_mask, #quick_read_spot').fadeOut('fast'); 
    jQuery('object').css('visibility','visible');
}
function quick_read(){    
    jQuery('object').css('visibility','hidden');    
    jQuery('#_wrapper_mask').fadeIn('fast');
    jQuery('#quick_read_content').html(jQuery(this).parent().prev().html());
    jQuery('#quick_read_spot').fadeIn('fast');
    
}

/* hscroller */
$(document).ready(function(){
    $('a[href="#prev-articles"]').click(hs_load_article_p);
    $('a[href="#next-articles"]').click(hs_load_article_n);
});

function hs_load_article_p(){
    $('#hor-scroller ul li:last').hide();
    $('#hor-scroller ul li:last').prependTo($('#hor-scroller ul')); 
    $('#hor-scroller ul li:first').fadeIn('slow');
}
function hs_load_article_n(){
    $('#hor-scroller ul li:eq(3)').hide();
    $('#hor-scroller ul li:first').appendTo($('#hor-scroller ul')); 
    $('#hor-scroller ul li:eq(2)').fadeIn('slow');
}


function scale_images_ie6(){
    jQuery('img').each(function(){
        max_width = jQuery(this).css('max-width');        
        if(max_width != undefined){
            real_width = jQuery(this).attr('width');
            max_width = max_width.replace(/px$/,'');
        }
        max_height = jQuery(this).css('max-height');
        if(max_height != undefined){            
            real_height = jQuery(this).attr('height');
            max_height = max_height.replace(/px$/,'');

        }
        if(!max_height) max_height = max_width;
        if(max_width && max_height){
            if(real_width > max_width && real_height <= max_height){
                ratio = max_width/real_width;
                scaled_width = max_width;
                scaled_height = real_height*ratio;                                        
            }
            else if(real_width <= max_width && real_height > max_height){
                ratio = max_height/real_height;
                scaled_height = max_height;
                scaled_width = real_width*ratio;                                        
            }
            else if(real_width > max_width && real_height > max_height){
                if(real_width > real_height){
                    ratio = max_width/real_width;
                    scaled_width = max_width;
                    scaled_height = real_height*ratio;                                        
                }else{
                    ratio = max_height/real_height;
                    scaled_height = max_height;
                    scaled_width = real_width*ratio;                                        
                }
            }
            try{            
                if(scaled_width && scaled_height){
                    jQuery(this).attr('width',scaled_width).attr('height',scaled_height);
                }
            }catch(err){
                ;
            }
        }
    })
}