$(function () 
{
    if(!Array.indexOf)
    {
        Array.prototype.indexOf = function(obj)
        {
            for(var i=0; i<this.length; i++)
            {
                if(this[i]==obj)
                {
                    return i;
                }
            }
        return -1;
        }
    }
    var offkantoren = [];
    var baseUrl = '/';    
    
    // Advocaat aan de offerte queue toevoegen op bijvoorbeeld: http://alleadvocateninnederland.nl/Overijssel/Enschede
    $('.addAdvocaat').click(function()
    {
        var kantoorid = $(this).attr('id').substring(8,$(this).attr('id').length);
        if(offkantoren.indexOf(kantoorid) > -1)
        {
            $(this).css({backgroundImage: 'url('+baseUrl+'images/icons/add.png)'});
            $(this).parent().animate({
                marginLeft: '0px',
                opacity: 1
            }, 'fast');
            offkantoren.splice(offkantoren.indexOf(kantoorid), 1);
            
            if($('#offerteFormulier').length > 0)
            {
                reloadKantoor();
                if(offkantoren.length == 0)
                {
                    $('#offerteFormulier').remove();
                }
            }
        }
        else
        {
            $(this).css({backgroundImage: 'url('+baseUrl+'images/icons/delete.png)'});
            $(this).parent().animate({
                marginLeft: '35px',
                opacity: 0.3
            }, 'fast');
            offkantoren[offkantoren.length] = kantoorid;
            
            if($('#offerteFormulier').length > 0)
            {
                reloadKantoor();
            }
        }
    });
    
    // Advocaten opnieuw laden in de popup
    function reloadKantoor()
    {
        $.get(baseUrl+'Offerte/Aanvragen',{ 'kantoren[]': offkantoren},function(data) 
        {
          $('#offerteContent').html(data);
        });
    }
    
    // Popup openen om de offerte te mailen
    // <div id='offerteReload'>Reload</div> na de 'offerteHead' toevoegen voor een debug-reload knop
    $('#offerteOpvragen').click(function()
    {
        if(offkantoren.length > 0)
        {
            // doe iets
            $('#content').prepend($("<div id='offerteFormulier'><div id='offerteHead'>Offerte aanvragen bij de volgende geselecteerde kantoren:</div><div id='offerteClose'>[X]  Sluiten</div><div id='offerteContent'></div></div>"));
            $('#offerteFormulier').draggable({handle: 'div#offerteHead'});
            
            reloadKantoor();
        }
        else
        {
            $('#content').prepend($("<div id='popup'><div id='popupMessage'>Kies eerst enkele advocatenkantoren door op het plusje naast de naam te klikken voor u het formulier opent.</div><div id='popupClose'>Sluiten</div></div>"));
        }
    });
    
    // Melding popup sluiten
    $('#popupClose').live('click', function()
    {
        $(this).parent().remove();
    });
    
    // Kantoor uit de queue in de popup verwijderen
    $('.removeKantoor').live('click', function()
    {
        if(confirm('Weet je het zeker?'))
        {
            kantoorId = $(this).attr('id').substring(11,$(this).attr('id').length);
            offkantoren.splice(offkantoren.indexOf(kantoorId), 1);
            reloadKantoor();
            $('#kantoor_'+kantoorId).css({backgroundImage: 'url('+baseUrl+'images/icons/add.png)'});
            $('#kantoor_'+kantoorId).parent().animate({
                marginLeft: '0px',
                opacity: 1
            }, 'fast');
            
            if(offkantoren.length == 0)
            {
                $('#offerteFormulier').remove();
            }
        }
    });
    
    
    // Reload knop in de popup die de queue opnieuw laad
    $('#offerteReload').live('click',function()
    {
        $.get(baseUrl+'Offerte/Aanvragen',{ 'kantoren[]': offkantoren},function(data) 
        {
          $('#offerteContent').html(data);
        });
    });
    
    //Offerte popup sluiten. Wanneer alle advocaten in de popup verwijdert zijn wijzigd dit ook de advocaten op de pagina zelf    
    $('#offerteClose').live('click',function()
    {
        $('#offerteFormulier').remove();
    });
    
    
    
       
    function print_r(theObj)
    {
        var str = '';
        if(theObj.constructor == Array ||
        theObj.constructor == Object)
        {
           str +="<ul>\n";
            for(var p in theObj)
            {
                if(theObj[p].constructor == Array||
                theObj[p].constructor == Object)
                {
                    str +="<li>["+p+"] => "+typeof(theObj)+"</li>\n";
                    str += "<ul>\n";
                    print_r(theObj[p]);
                    str +="</ul>\n";
                } 
                else
                {
                    str +="<li>["+p+"] => "+theObj[p]+"</li>\n";
                }
            }
            str +="</ul>";
        }
        return str;
    }

    $('input#searchGemeenten').quicksearch('table#gemeenten tr td');   
    
    $('input.searchKantorenByNaam').quicksearch('table#kantoren tr');
    
    $('table#gemeenten tr td').click(function()
    {
        window.location = $(this).find('a').attr('href');
    });
    
    $('.offerteB').click(function()
    {
        if($('form#offerteForm').length == 0)
        {
            $('#offerteFormDiv').css({height: 'auto', width: '450px'});
        }
        $('#offerteFormDiv').slideDown();
    });
    
    $('.confirmDel').click(function()
    {
        if(!confirm('Weet je het zeker?'))
        {
            return false;
        }
    });
    
    $('#postofferteaanvragen').live('click', function()
    { 
        var data = {};
        data['Name']        = $('#Name').val();
        data['Address']     = $('#Address').val();
        data['Postalcodenum']  = $('#Postalcodenum').val();
        data['Postalcodealp']  = $('#Postalcodealp').val();
        data['City']        = $('#City').val();
        data['Phone']       = $('#Phone').val();
        data['Email']       = $('#Email').val();
        data['Text']        = $('#Text').val();    
        $.post($('#offerteForm').attr('action'), data, function(resp)
        {
            $('#offerteContent').html(resp);
        });
    });
    
    $('#postofferteaanvraag').live('click',function()
    {                  
        $('#offerteResp').fadeOut('fast');
        var data = {};
        data['Name']        = $('#Name').val();
        data['Address']     = $('#Address').val();
        data['Postalcodenum']  = $('#Postalcodenum').val();
        data['Postalcodealp']  = $('#Postalcodealp').val();
        data['City']        = $('#City').val();
        data['Phone']       = $('#Phone').val();
        data['Email']       = $('#Email').val();
        data['Text']        = $('#Text').val();    
         
        $.post($('#offerteForm').attr('action'), data, function(resp)
        {
            if(resp.Response.Valid == false)
            {
                if(resp.Response.Type == 0)
                {
                    alert('1: '+resp.Response.Message);
                }
                else if(resp.Response.Type == 1)
                {
                    output = '<ul>';
                    count = 0;
                    $.each(resp.Response.Message, function(i, value)
                    {
                        output += '<li>'+value.isEmpty+'</li>';
                        count++;
                    });
                    output += '</ul>';
                    if(count > 0)
                    {
                        $('div#offerteResp').html(output);
                        $('div#offerteResp').fadeIn('fast');
                    }
                    
                }
            }
            else
            {
                $('#offerteForm').parent().remove();
                $('#offerteFormDiv').animate(
                {
                    height: '28px',
                    width: '270px'
                });
                $('#offerteResp').html('Uw mail is succesvol verzonden.');
                $('#offerteResp').fadeIn('fast');
                $('.offerteB').unbind();
            }
        }, 'json');
    });
});
