//coomon library to control backet and other common javascript elements

//Basket

function initialise_basket(){
   console.log('initialise basket');
   load_basket_html();
   $('#basket_panel').hide();
   
   console.log('initialise login');
   load_login_html();
   $('#login_panel').hide();
   
}

function load_basket_html(){
    //load basket html from remote script
    $('#basket_panel_content').load('/acquire/rpc_basket_html.php');
}



//login form
function initialise_login(){
   console.log('initialise login');
   load_login_html();
   $('#login_form_panel').hide();
}


//methods

function add_remove_vintage_basket(key, action){
    //add or remove vintage from basket
    //action: add or remove
    
    $.post("/vintage/rpc_add_remove_from_basket.php", {
            key: key,
            action: action
        }, function(){
        //re-load html
        load_basket_html();
    });
}

//hide basket panel
$('#btn_hide_basket').live('click', function(){
    load_basket_html()
    $('#basket_panel').slideToggle("medium");
});


//add to basket
$('.btn_add_to_basket').live('click', function(){
    console.log('add to basket ='+$(this).attr('id'));
    add_remove_vintage_basket($(this).attr('id'),'add');
    //$(this).hide();
    //show and then hide basket when adding vintage
    $('#basket_panel').slideDown().delay(1500).slideUp();
});


//remove from basket
$('.btn_remove_vintage_basket').live('click', function(){
    var vintage_id = ($(this).attr('id').replace("basket=", ""))*1
    add_remove_vintage_basket(vintage_id,'remove');
});


//empty basket
$('#btn_clear_basket').live('click', function(){
    if(confirm('Are you sure you want to empty the basket?')){
        add_remove_vintage_basket(false,'empty');
        load_basket_html();
        $('#basket_panel').slideUp();
    }
});

//*************login form************************


function load_login_html(callback){
    //load basket html from remote script
    console.log('load login');
    $('#login_panel_content').load('/user/rpc_login_html.php', function(){
        if(typeof callback == 'function' ){
            callback();
        }
    });
}

//toggle login panel
$('#btn_hide_login').live('click', function(){
    
    $('#login_panel').slideToggle("medium");
    if($('#login_panel').css('display')=='block'){
        //panel is being opened - load html and set focus
        load_login_html(function(){
            set_focus();
        });
    }  

});


function set_focus(){
    console.log('set focus');
    $("#username").focus();
}

//toggle login panel
$('#btn_login').live('click', function(){
    console.log('btn_login');
    login();
});


function login(){
    //login user
    console.log('function_login');
    //get values
    var username = $("#username").val();
    var password = $("#password").val();
    var remember = $("#remember").is(":checked");
    console.log("username="+username+" password="+password+" remember="+remember);
    
    $.post("/user/rpc_login.php", {
        //login
        action: 'login',
        username: username,
        password: password,
        remember: remember
    }, function(data){
        if(data.success){
            
            console.log('login successful');
            $("#login_msg").show();
            $("#login_msg_txt").text("Success!");
            $("#login_msg").css('background-color','green');
            //reload page
            location.reload();

        } else {
            console.log('login failed error='+data.error)
            $("#login_msg").show();
            $("#login_msg_txt").text("Incorrect username or password");
        }

    }, "json"); 
}
