// JavaScript Document
var addtocartsuccess = '<span class="addsuccess">product toegevoegd aan uw winkelmandje!</span>';

$(function(){
    $('input[type=text]').each(function(){
        var title = $(this).attr('title');
        $(this)
            .focus(function(){ if($(this).val() == title) $(this).val(''); })
            .blur(function(){ if($(this).val() == '') $(this).val(title); });
    });    
    
    $('.sf_file input')
        .change(function(){
                var val = $(this).val();
                $(this).siblings('span').text(val);
            });
        
    $('.sf_file')
        .hover(function(){
                $(this).addClass('hover');
            },
            function(){
                $(this).removeClass('hover');
            });  
});

function addToBasket( id ){
    $.post('/.action/addtobasket.php', { 'id' : id }, function( res ){ if(res == 'success') { reloadBasket(); startAlert( addtocartsuccess ); } });
}

function addToBasketSub( id, pos ){
    $.post('/.action/addtobasket.php', { 'id' : id }, function( res ){ if(res == 'success'){ reloadBasket(); hideVariant( pos ); startAlert( addtocartsuccess ); } });
}

function openVariant( id ){
    $('.popupvar_' + id).animate({'top':'0px'}, 'fast');
}

function hideVariant( id ){
    $('.popupvar_' + id).animate({'top':'265px'}, 'fast');
}

function reloadBasket(){
    $.post('/.includes/common/small.basket.php', { 'action' : 'reload' }, function( res ){ $('#basket_wrapper').html( res ); });
}

function startAlert( alert_msg ){    

    var abox = $('<div>').attr('id', 'alertBox').html( alert_msg );
    var aover = $('<div>').attr('id', 'alertOverlay');
    var closeBWrapper = $('<a>').attr('id', '');
    var closeB = $('<span>').addClass('alertClose').html('sluit venster'); 
    
    closeBWrapper.append( closeB );
    abox.append( closeBWrapper );
    
    $('body').append(aover).append(abox);
    
    adjustAlert( abox );    
    aover.click(function(){ endAlert(); });
    closeB.click(function(){ endAlert(); });
    
    return abox;
}

function endAlert(){
    $('#alertBox, #alertOverlay').hide();
}

function adjustAlert( abox ){    
    abox.css({ 'margin-left': '-' + (abox.width()/2) + 'px', 'margin-top': '-' + (abox.height()/2) + 'px' });
}   
