var root = '/';

$("document").ready(
    function(){      
        loadFirstTab();
        bindTabs();
        bindProduct();
        berekenTotaal();
        bindDatePicker();
        
        //registratie/inloggen
        bindShowRegistratie();
        bindLogin();
        bindKnopStap3();
        
        ajaxBindLocal();
        ajaxBindComponenten();
    }
);

function ajaxBindLocal() {
    $.getScript(root + "js/pageLocal.js", function() {
        bindLocal();
    });
}

function ajaxBindComponenten() {
    $.getScript(root + "js/bindComponenten.js", function() {
        bindComponenten();
    });
}



function loadFirstTab()
{
    $("#main-overzicht div").hide();                //Alles verberegen.
    firstTab = $(".tabs-wrap li a").first();    //Naam van eerste tab ophalen.
    firstTab.parent().addClass('active');           //Class van tabje op active zetten.
    $(firstTab.attr("id")).show();                //content van eerste tabje weergeven.
}

function bindTabs()
{    
    $(".tabs-wrap li a").click(
        function()
        {
            var id = $(this).attr("id");
            
            $(".tabs-wrap li.active").removeClass('active');
            $(this).parent().addClass('active');
            $("#main-overzicht div").hide(); 
            $(id).show();
        }
    );
}

function bindProduct()
{
    $(".check a").click(
        function()
        {
            var vink    = $(this).find('img');
            var id      = $(this).attr('id');
            
            if(vink.attr('alt') == 'geselecteerd')
            {
                  vink.attr('src','/gfx/tmpl/uncheck.gif');
                  vink.attr('alt','niet geselecteerd');
                  removeProduct(id);
            }else{                 
                 vink.attr('src','/gfx/tmpl/check.gif');
                 vink.attr('alt','geselecteerd');
                 berekenTotaal(id);              
            }                   
        }
    )
}

function verwijderProduct(id)
{
    //$('.tbl-overzicht tr.' + id).remove();
    var image = '.check a#' + id + ' img';
    $(image).attr('src','/gfx/tmpl/uncheck.gif');
    $(image).attr('alt','niet geselecteerd');
    removeProduct(id);
}

function berekenTotaal(id)
{
    var post = $('#frm-overzicht').serialize(); 
    
    if(typeof(id)!=="undefined")
    {
        post = post + '&NieuwID=' + id;    
    }
    

    $.post(
        '/ajax/product_overzicht.php',
        post,
        function(data){
            $('.tbl-overzicht inhoud').children().remove();
            $('.tbl-overzicht').html(data);
           // submitButton();
        })
 
}

function removeProduct(id)
{
    var post = $('#frm-overzicht').serialize(); 
    
    if(typeof(id)!== 'undefined')
    {
        post = post + '&VerwijderID=' + id;    
    }
    
    $.post(
        '/ajax/product_overzicht.php',
        post,
        function(data){
            $('.tbl-overzicht inhoud').children().remove();
            $('.tbl-overzicht').html(data);
        });
}

function bindDatePicker()
{
    $(function() {
        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            altField: '#leveringsdatum', 
            altFormat: 'yy-mm-dd'
        });
    });    
    
}
/*
function submitButton()
{    
    bedrag = parseFloat($('.totaal .last p').html().replace('€',''));

    if(bedrag >= 20.00)
    {
        $('#frm-overzicht').attr('action','/bestellen/gegevens.php');
    }else{
        $('#frm-overzicht').attr('action','#');
        $('#minBestelbedrag').css('visibility','visible');
    }
}
*/

function doBestelSubmit()
{
    //bedrag = parseFloat($('.totaal .last p').html().replace('€',''));
    var bedrag = parseFloat($('#hiddenPrice').html());

    if(bedrag >= 20.00)
    {
        $('#frm-overzicht').submit();   
    }else{
        $('#minBestelbedrag').css('visibility','visible');
    }
}

function bindShowRegistratie()
{
    $('#showRegistreer').click(function(){
        $('#inloggen').css('display', 'none');
        $('#registreren').css('display', 'block');
        $('#knopStap3').attr('value','frmRegistreer');
    });
}

function bindLogin()
{
    $('#showLogin').click(function(){
       $('#registreren').css('display', 'none');
       $('#inloggen').css('display', 'block');
       $('#knopStap3').attr('value','frmInloggen');
       $('#knopStap3').attr('name','frmInloggen');
    });
}

function bindKnopStap3()
{
    $('#knopStap3').click(function(){
       var form = '#' + $('#knopStap3').attr('value');
       //alert(form);
       $(form).submit();
    });
}





