
// BSF object.

var BSF =
{

    // Base onderdel.

    base:
    {

        // Triggers inladen.

        loadTriggers: function()
        {

            // Submit activeren via enter key.

            $('form').keyup(function(e)
            {
                if (e.keyCode == 13)
                {
                    //$('#form_submit').trigger('click');
                }
            });

            if (window.location.href.indexOf('gebruiker/market/item') > 0)
            {
                $('#mm_images_uploader').uploadify(
                {
                    uploader: '/swf/uploadify.swf',
                    cancelImg: '/images/cancel.png',
                    script: '/ajax/news/upload_photo.php',
                    auto: true,
                    multi: true,
                    fileExt: '*.jpg',
                    fileDesc: 'Foto bestanden (jpg)',
                    buttonImg: '/images/upload.png',
                    sizeLimit: 1000000,
                    onComplete: function(event, ID, fileObj, response, data)
                    {
                       
                       
                        var result = $.parseJSON(response);

                        $('#photo_list').append('<div id="upload_photo_box_'+result.image.id+'" class="upload_photo_box"><img id="mm_images_prev_'+result.image.id+'" src="'+result.image.preview+'" onclick="javascript:BSF.base.deleteImage('+result.image.id+');" /> <input type="hidden" name="mm_images[]" id="mm_images_'+result.image.id+'" value="'+result.image.id+'" /></div>');
                    }
                });
            }


            // Uploadify inladen TODOOO FIX!!

            if (window.location.href.indexOf('beheer/nieuws/bericht') > 0)
            {
                $('#news_upload').uploadify(
                {
                    uploader: '/swf/uploadify.swf',
                    cancelImg: '/images/cancel.png',
                    script: '/ajax/news/upload_photo.php',
                    auto: true,
                    multi: true,
                    fileExt: '*.jpg',
                    fileDesc: 'Foto bestanden (jpg)',
                    buttonImg: '/images/upload.png',
                    sizeLimit: 1000000,
                    onComplete: function(event, ID, fileObj, response, data)
                    {
                        //alert(response);
                        
                        var result = $.parseJSON(response);

                        $('#image_list').append('<img src="'+result.image.preview+'" /> <input type="hidden" name="images[]" id="image_'+result.image.id+'" value="'+result.image.id+'" />');
                    }
                });
            }


            // User comments laden.

            if (window.location.href.indexOf('/nieuws') > 0 && window.location.href.indexOf('/beheer') == -1)
            {
                BSF.news.loadReplies();
            }


            // Comments op een market item.

            if (window.location.href.indexOf('/marktplaats/item') > 0 && window.location.href.indexOf('/beheer') == -1)
            {
                BSF.market.loadReplies();
            }


            // Image player laden.

            if ($('a[rel="player"]').length > 0)
            {
                $('a[rel="player"]').lightBox();
            }
        },


        // Site positioneren.

        positionSite: function()
        {
           /* $('#left_shadow, #right_shadow').css(
            {
                height: ($('#container_page').height() + 20)+'px'
            }); */
        },


        // MM delten.

        deleteImage: function(mm_id)
        {
            $('#mm_images_prev_'+mm_id).fadeOut(600, function()
            {
                $('#mm_images_'+mm_id).remove();
                $('#upload_photo_box_'+mm_id).remove();
            });
        },


        // Google maps.

        maps:
        {

            // Maps inladen.

            loadMaps: function(div, lat, lon)
            {
                var location = new google.maps.LatLng(lat.toString().replace(',', '.'), lon.toString().replace(',', '.'));

                var options =
                {
                    zoom: 16,
                    center: location,
                    mapTypeId: google.maps.MapTypeId.SATELLITE,
                    disableDoubleClickZoom: true
                };

                var map = new google.maps.Map(document.getElementById(div), options);
            }
        }
    },


    // Contact pagina.

    contact:
    {

        // Versturen van het formulier.

        send: function()
        {
            if ($('#contact_name').val() == '')
            {
                Alert.show('Je hebt je naam niet ingevuld.');
            }

            else if ($('#contact_emailaddress').val() == '')
            {
                Alert.show('Je hebt je e-mailadres niet ingevuld.');
            }

            else if ($('#contact_text').val() == '')
            {
                Alert.show('Je hebt je reactie niet ingevuld.');
            }

            else
            {

                // Data objectje bakken.

                var data =
                {
                    name: $('#contact_name').val(),
                    emailaddress: $('#contact_emailaddress').val(),
                    text: $('#contact_text').val()
                };


                // Posten naar de servert.

                $.post('/ajax/contact/send_form.php', data, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            $('#all_ok').slideDown();
                            $('#contact_form').slideUp();
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch (e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            }
        }
    },


    // Links.

    links:
    {

        // Formulier toggelen.

        toggleForm: function()
        {
            if ($('#links_form').css('display') == 'block')
            {
                $('#links_form').slideUp();
            }

            else
            {
                $('#links_form').slideDown();
            }
        },


        // Link toevoegen.

        submit: function()
        {
            if ($('#link_title').val() == '')
            {
                Alert.show('Je hebt de titel van de link niet ingevuld.');
            }

            else if ($('#link_uri').val() == '')
            {
                Alert.show('Je hebt de link niet ingevuld.');
            }

            else
            {

                var data =
                {
                    section_id: $('#link_section').val(),
                    title:$('#link_title').val(),
                    uri: $('#link_uri').val()
                };

                $.post('/ajax/links/add_link.php', data, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            window.location.reload();
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch (e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            }
        }
    },


    // Gastenboek.

    gastenboek:
    {

        // Formulier toggelen.

        toggleForm: function()
        {
            if ($('#gastenboek_form').css('display') == 'block')
            {
                $('#gastenboek_form').slideUp();
            }

            else
            {
                $('#gastenboek_form').slideDown();
            }
        },


        // Bericht toevoegen :P

        submit: function()
        {
            if ($('#guestbook_name').val() == '')
            {
                Alert.show('Je hebt je naam niet ingevuld.');
            }

            else if ($('#guestbook_emailaddress').val() == '')
            {
                Alert.show('Je hebt je e-mailadres niet ingevuld.');
            }

            else if ($('#guestbook_text').val() == '')
            {
                Alert.show('Je hebt je reactie niet ingevuld.');
            }

            else
            {

                // Data objectje bakken.

                var data =
                {
                    name: $('#guestbook_name').val(),
                    emailaddress: $('#guestbook_emailaddress').val(),
                    text: $('#guestbook_text').val()
                };


                // posten naar de servert.

                $.post('/ajax/gastenboek/add_message.php', data, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            window.location.reload();
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch (e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            }
        }
    },


    // Gebruiker onderdeel.

    user:
    {

        // Inloggen.

        login:
        {

            // Knop afvangen.

            submit: function()
            {

                if ($('#user_email').val() == '')
                {
                    Alert.show('Je hebt je e-mailadres niet ingevuld.');
                }

                else if ($('#user_password').val() == '')
                {
                    Alert.show('Je hebt je wachtwoord niet ingevuld.');
                }

                else
                {
                    $('#form_login').submit();
                }
            }
        },


        // Registreren.

        register:
        {

            // Knop afvangen.

            submit: function()
            {

                var email_ok = $.parseJSON($.ajax({type: 'POST', url: '/ajax/user/check_emailaddress.ajax.php', data: {email: $('#user_email').val()}, async: false }).responseText);
                var user_ok = $.parseJSON($.ajax({type: 'POST', url: '/ajax/user/check_username.ajax.php', data: {username: $('#user_nickname').val()}, async: false }).responseText);

                if ($('#user_nickname').val() == '')
                {
                    Alert.show('Je hebt je gebruikersnaam niet ingevuld.');
                }

                else if ($('#user_password').val() == '')
                {
                    Alert.show('Je hebt je wachtwoord niet ingevuld.');
                }

                else if ($('#user_password_repeat').val() == '')
                {
                    Alert.show('Je hebt je wachtwoord (herhaling) niet ingevuld.');
                }

                else if ($('#user_name_front').val() == '')
                {
                    Alert.show('Je hebt je voornaam niet ingevuld.');
                }

                else if ($('#user_name_last').val() == '')
                {
                    Alert.show('Je hebt je achternaam niet ingevuld.');
                }

                else if ($('#user_email').val() == '')
                {
                    Alert.show('Je hebt je e-mailadres niet ingevuld.');
                }

                else if ($('#user_password').val() != $('#user_password_repeat').val())
                {
                    Alert.show('De wachtwoorden zijn niet gelijk.');
                }

                else if (email_ok.status != 'OK')
                {
                    Alert.show('Het e-mailadres bestaat al.');
                }

                else if (user_ok.status != 'OK')
                {
                    Alert.show('De gebruikersnaam bestaat al.');
                }

                else
                {
                    $('#form_register').submit();
                }
            }
        },


        // Wachtwoord vergeten.

        passwordforgotten:
        {

            // Stap 1 knop.

            sendEmailaddress: function()
            {
                $.post('/ajax/user/send_password_hash.ajax.php', {email: $('#emailaddress').val()}, function(result)
                {
                    var result = $.parseJSON(result);

                    if (result.status == 'OK')
                    {
                        $('#step1_button').fadeOut(750, function()
                        {
                            $('#emailaddress').attr('disabled', 'disabled');
                            $('#verification_code_box').fadeIn(500);
                        });
                    }

                    else
                    {
                        Alert.show(result.status);
                    }
                });
            },


            // Stap 2 knop.

            checkCode: function()
            {
                $.post('/ajax/user/check_password_hash.ajax.php', {email: $('#emailaddress').val(), hash: $('#verificationcode').val()}, function(result)
                {
                    var result = $.parseJSON(result);

                    if (result.status == 'OK')
                    {
                        $('#step2_button').fadeOut(750, function()
                        {
                            $('#verificationcode').attr('disabled', 'disabled');
                            $('#password_result').fadeIn(500);
                        });
                    }

                    else
                    {
                        Alert.show(result.status);
                    }
                });
            }
        },


        // Wachtwoord aanpassen

        changePassword:
        {

            // Submit.

            submit: function()
            {

                if ($('#current').val() == '')
                {
                    Alert.show('Je hebt je huidige wachtwoord niet ingevuld.');
                }

                else if ($('#new').val() == '')
                {
                    Alert.show('Je hebt je nieuwe wachtwoord niet ingevuld.');
                }

                else
                {
                    $('#password_change').submit();
                }
            }
        },


        // Gegevens aanpassen.

        changeProfile:
        {

            // Submit.

            submit: function()
            {
                if ($('#name_front').val() == '')
                {
                    Alert.show('Je hebt je voornaam niet ingevuld.');
                }

                else if ($('#name_last').val() == '')
                {
                    Alert.show('Je hebt je wachtnaam niet ingevuld.');
                }

                else if ($('#email').val() == '')
                {
                    Alert.show('Je hebt je e-mailadres niet ingevuld.');
                }

                else
                {
                    $('#change_profile').submit();
                }
            }
        },


        // Marktplaats.

        market:
        {

            // Actief aan of niet

            switchActive: function(item_id, new_status)
            {
                $.post('/ajax/user/market_switch_active.php', {item_id: item_id, new_status: new_status}, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            window.location.reload();
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch(e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            },


            // Item opslaan.

            saveItem: function()
            {
                if ($('#item_title').val() == '')
                {
                    Alert.show('Je hebt de titel niet ingevuld.');
                }

                else if ($('#item_text').val() == '')
                {
                    Alert.show('Je hebt de tekst niet ingevuld.');
                }

                else if ($('#item_phonenumber').val() == '')
                {
                    Alert.show('Je hebt je telefoonnummer niet ingevuld.');
                }

                else
                {
                    $('#market_item').submit();
                }
            }
        }
    },


    // Marktplaats pagina -------------------------------------------------------------------------

    market:
    {

        // User comments laden.

        loadReplies: function()
        {

            // Data objectje.

            var data = { item_id: $('#item_id').val() };


            // Ajax post.

            $.post('/ajax/market/get_comments.php', data, function(result)
            {
                $('#comment_box').show().html(result);
            });
        },


        // Reactie van user opslaan.

        saveReply: function(item_id)
        {

            if ($('#comment_text').val() == '')
            {
                Alert.show('Je hebt je reactie niet ingevuld.');
            }

            else
            {

                // Data objectje bakken.

                var data = { item_id: item_id, item_reply: $('#comment_text').val() };


                // Ajax post.

                $.post('/ajax/market/save_reply.php', data, function(result)
                {
                    var result = $.parseJSON(result);

                    if (result.status == 'OK')
                    {
                        $('#comment_form').slideUp();
                        $('#comment_text').val('');
                        BSF.market.loadReplies();

                        setTimeout(function()
                        {
                            BSF.market.toBottom();
                        }, 300);
                    }

                    else
                    {
                        Alert.show(result.status);
                    }
                });
            }
        },


        // Show or hide comment box.

        toggleForm: function()
        {
            if ($('#comment_form').css('display') == 'block')
            {
                $('#comment_form').slideUp(750, function()
                {
                    $('body').scrollTo('#hr', 500);
                });
            }

            else
            {
                $('#comment_form').slideDown(750, function()
                {
                    $('body').scrollTo('#hr', 500);
                });
            }
        },


        // Naar beneden scrollen.

        toBottom: function()
        {
            $('body').scrollTo('#comment_box_end', 750);
        }
    },


    // Nieuws pagina ------------------------------------------------------------------------------

    news:
    {

        // User comments laden.

        loadReplies: function()
        {

            // Data objectje.

            var data = { news_id: $('#news_id').val() };


            // Ajax post.

            $.post('/ajax/news/get_comments.php', data, function(result)
            {
                $('#comment_box').show().html(result);
            });
        },


        // Reactie van user opslaan.

        saveReply: function(news_id)
        {

            if ($('#comment_text').val() == '')
            {
                Alert.show('Je hebt je reactie niet ingevuld.');
            }

            else
            {

                // Data objectje bakken.

                var data = { news_id: news_id, news_reply: $('#comment_text').val() };


                // Ajax post.

                $.post('/ajax/news/save_reply.php', data, function(result)
                {
                    var result = $.parseJSON(result);

                    if (result.status == 'OK')
                    {
                        $('#comment_form').slideUp();
                        $('#comment_text').val('');
                        BSF.news.loadReplies();

                        setTimeout(function()
                        {
                            BSF.news.toBottom();
                        }, 300);
                    }

                    else
                    {
                        Alert.show(result.status);
                    }
                });
            }
        },


        // Show or hide comment box.

        toggleForm: function()
        {
            if ($('#comment_form').css('display') == 'block')
            {
                $('#comment_form').slideUp(750, function()
                {
                    $('body').scrollTo('#hr', 500);
                });
            }

            else
            {
                $('#comment_form').slideDown(750, function()
                {
                    $('body').scrollTo('#hr', 500);
                });
            }
        },


        // Naar beneden scrollen.

        toBottom: function()
        {
            $('body').scrollTo('#comment_box_end', 750);
        }
    },


    // Forum --------------------------------------------------------------------------------------

    forum:
    {

        // Topic toevoegen.

        addTopic: function()
        {

            if ($('#topic_text').val() == '')
            {
                Alert.show('Je hebt geen bericht ingevuld.');
            }

            else if ($('#topic_title').val() == '')
            {
                Alert.show('Je hebt geen titel ingevuld.');
            }

            else
            {

                // Data objectje.

                var data =
                {
                    section_id: $('#section_id').val(),
                    user_id: $('#user_id').val(),
                    title: $('#topic_title').val(),
                    text: $('#topic_text').val()
                }


                // Call.

                $.post('/ajax/forum/add_topic.php', data, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            window.location = '/forum/topic/'+result.topic.id+'/'+result.topic.title+'.html';
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch (e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            }
        },


        // Reactie opslaan.

        saveReply: function(topic_id, user_id)
        {
            if ($('#comment_text').val() == '')
            {
                Alert.show('Je hebt je reactie niet ingevuld.');
            }

            else
            {

                // Data objectje bakken.

                var data =
                {
                    user_id: user_id,
                    topic_id: topic_id,
                    text: $('#comment_text').val()
                }


                // Call.

                $.post('/ajax/forum/save_reply.php', data, function(result)
                {
                    try
                    {
                        var result = $.parseJSON(result);

                        if (result.status == 'OK')
                        {
                            window.location.reload();
                        }

                        else
                        {
                            Alert.show('Er is een fout opgetreden.');
                        }
                    }

                    catch (e)
                    {
                        Alert.show('Er is een fout opgetreden.');
                    }
                });
            }
        },


        // Toggle form.

        toggleForm: function(always)
        {

            if (always)
            {
                $('#comment_form').slideDown(750, function()
                {
                    $('body').scrollTo('#replies', 500);
                });
            }

            else
            {
                if ($('#comment_form').css('display') == 'block')
                {
                    $('#comment_form').slideUp(750, function()
                    {
                        $('body').scrollTo('#replies', 500);
                    });
                }

                else
                {
                    $('#comment_form').slideDown(750, function()
                    {
                        $('body').scrollTo('#replies', 500);
                    });
                }
            }
        }
    },


    // Beheer gedeelte ----------------------------------------------------------------------------

    admin:
    {

        // Nieuws onderdeel  .

        news:
        {

            // Submit knop afvangen.

            submit: function()
            {

                if ($('#news_title').val() == '')
                {
                    Alert.show('Je hebt de titel niet ingevuld.');
                }

                else if ($('#news_text').val() == 'Je hebt de tekst niet ingevuld.')
                {
                    Alert.show('Je hebt de tekst niet ingevuld.');
                }

                else
                {
                    $('#news_form').submit();
                }
            },


            // Image unlinken.

            unlinkImage: function(that)
            {
                $('#image_'+$(that).attr('data')).remove();
                $(that).remove();
            }

        },


        // Kalender.

        calendar:
        {

            // Toevoegen/opslaan van het item.

            submit: function()
            {
                if ($('#item_location').val() == '')
                {
                    Alert.show('Je hebt de locatie niet ingevuld.');
                }

                else if ($('#item_country').val() == '')
                {
                    Alert.show('Je hebt het land niet ingevuld.');
                }

                else if ($('#item_title').val() == '')
                {
                    Alert.show('Je hebt de titel niet ingevuld.');
                }

                else if ($('#item_date').val() == '')
                {
                    Alert.show('Je hebt de datum niet ingevuld.');
                }

                else
                {
                    if ($('#item_date_till').val() == '')
                    {
                        $('#item_date_till').val($('#item_date').val());
                    }

                    $('#calendar_form').submit();
                }
            }
        }
    }
}


// Alert class.

var Alert =
{
    show: function(message)
    {
        alert(message);
    }
}
