function popup(url, width, height, name)
{
    if (!name)
    {
        name = '_popup';
    }

    window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes,width=' + width);
    return false;
}


/**
*  phpbb_seo_href()
*  Fixes href="#something" links with virtual directories
*  Optionally open external or marked with a css class links in a new window
*  in a XHTML 1.x compliant way.
*/
function phpbb_seo_href() {
    var current_domain = document.domain.toLowerCase();
    if (!current_domain || !document.getElementsByTagName) return;
    if (seo_external_sub && current_domain.indexOf('.') >= 0) {
        current_domain = current_domain.replace(new RegExp(/^[a-z0-9_-]+\.([a-z0-9_-]+\.([a-z]{2,6}|[a-z]{2,3}\.[a-z]{2,3}))$/i), '$1');
    }
    if (seo_ext_classes) {
        var extclass = new RegExp("(^|\s)(" + seo_ext_classes + ")(\s|$)");
    }
    if (seo_hashfix) {
        var basehref = document.getElementsByTagName('base')[0];
        if (basehref) {
            basehref = basehref.href;
            var hashtest = new RegExp("^(" + basehref + "|)#[a-z0-9_-]+$");
            var current_href = document.location.href.replace(/#[a-z0-9_-]+$/i, "");
        } else {
            seo_hashfix = false;
        }
    }
    var hrefels = document.getElementsByTagName("a");
    var hrefelslen = hrefels.length;
    for (var i = 0; i < hrefelslen; i++) {
        var el = hrefels[i];
        var hrefinner = el.innerHTML.toLowerCase();
        if (el.onclick || (el.href == '') || (el.href.indexOf('javascript') >=0 ) || (el.href.indexOf('mailto') >=0 ) || (hrefinner.indexOf('<a') >= 0) ) {
            continue;
        }
        if (seo_hashfix && el.hash && hashtest.test(el.href)) {
            el.href = current_href + el.hash;
        }
        if (seo_external) {
            if ((el.href.indexOf(current_domain) >= 0) && !(seo_ext_classes && extclass.test(el.className))) {
                continue;
            }
            el.onclick = function () {
                window.open(this.href);
                return false;
            };
        }
    }
}
window.onload = function() {
    if (seo_hashfix) {
        phpbb_seo_href();
    }
// here you can add other window.onload events
}
// www.phpBB-SEO.com SEO TOOLKIT END

/**
* Find a member
*/
function find_username(url)
{
    popup(url, 760, 570, '_usersearch');
    return false;
}

/**
* Mark/unmark checklist
* id = ID of parent container, name = name prefix, state = state [true/false]
*/
function marklist(id, name, state)
{
    var parent = document.getElementById(id);
    if (!parent)
    {
        eval('parent = document.' + id);
    }

    if (!parent)
    {
        return;
    }

    var rb = parent.getElementsByTagName('input');

    for (var r = 0; r < rb.length; r++)
    {
        if (rb[r].name.substr(0, name.length) == name)
        {
            rb[r].checked = state;
        }
    }
}

/**
* Play quicktime file by determining it's width/height
* from the displayed rectangle area
*
* Only defined if there is a file block present.
*/
function play_qt_file(obj)
{
    var rectangle = obj.GetRectangle();

    if (rectangle)
    {
        rectangle = rectangle.split(',')
        var x1 = parseInt(rectangle[0]);
        var x2 = parseInt(rectangle[2]);
        var y1 = parseInt(rectangle[1]);
        var y2 = parseInt(rectangle[3]);

        var width = (x1 < 0) ? (x1 * -1) + x2 : x2 - x1;
        var height = (y1 < 0) ? (y1 * -1) + y2 : y2 - y1;
    }
    else
    {
        var width = 200;
        var height = 0;
    }

    obj.width = width;
    obj.height = height + 16;

    obj.SetControllerVisible(true);

    obj.Play();
}


$(document).ready(function() {

    // SPELLENDATABASE BBCODE
    function parseGames(data) {
        $('#autoSuggestionsList').empty();
        $.each(data, function(i,game){
            $('#autoSuggestionsList').append('<li id=\''+game.url+'\'>'+game.title+'</li>');
        });

        $('li').click(function() {
            insert_text("[spdb="+$(this).attr('id')+"]"+$(this).text()+"[/spdb]");
            $("#formDialog").dialog('close');
        });
    }
    
    $('.btnbbcode').each(function() {
        if( $(this).val() == "spellendatabase" ) {
            $(this).removeAttr('onclick');
        }
    });
    
    $('.btnbbcode').click(function() {
        if( $(this).val() == "spellendatabase" ) {

            var dialog = $('<div>').attr('id','formDialog').html("<input type='text' name='spel' id='spel'  />\n\
                                                                    <input type='hidden' name='link' id='link' />\n\
                                                                    <div class='suggestionsBox' id='suggestions'>\n\
                                                                    <div class='suggestionList' id='autoSuggestionsList'></div></div>");
 

            dialog.dialog({
                title: 'Naam van spel:',
                modal: true,
                width:450,
                position: 'center',
                close: function() {
                    $(this).remove();
                }

            });

            $('#spel').keyup(function() {
                if($(this).val().length > 1) {
                    $.ajax({
                        url: 'ajax/spellendatabase_search.php',
                        dataType: 'json',
                        data: {
                            'n' : $(this).val()
                            },
                        success: parseGames
                    });
                }
            });
        }
    });

    // CALENDAR BBCODE
    function parseCalendar(data) {
        $('#autoSuggestionsList').empty();
        $.each(data, function(i,event){
            $('#autoSuggestionsList').append('<li id=\''+event.url+'\'>'+event.title+'</li>');
        });

        $('li').click(function() {
            insert_text("[kal="+$(this).attr('id')+"]"+$(this).text()+"[/kal]");
            $("#formDialog").dialog('close');
        });
    }

    $('.btnbbcode').each(function() {
        if( $(this).val() == "kalender" ) {
            $(this).removeAttr('onclick');
        }
    });

    $('.btnbbcode').click(function() {
        if( $(this).val() == "kalender" ) {

            var dialog = $('<div>').attr('id','formDialog').html("<input type='text' name='evenement' id='evenement'  />\n\
                                                                    <input type='hidden' name='link' id='link' />\n\
                                                                    <div class='suggestionsBox' id='suggestions'>\n\
                                                                    <div class='suggestionList' id='autoSuggestionsList'></div></div>");


            dialog.dialog({
                title: 'Naam van evenement:',
                modal: true,
                width:450,
                position: 'center',
                close: function() {
                    $(this).remove();
                }

            });

            $('#evenement').keyup(function() {
                if($(this).val().length > 1) {
                    $.ajax({
                        url: 'ajax/calendar_search.php',
                        dataType: 'json',
                        data: {
                            'n' : $(this).val()
                            },
                        success: parseCalendar
                    });
                }
            });
        }
    });

    // CALENDAR FILTER
    $('#filter').click(function() {


        var dialog = $('<div>').attr('id','formDialog').load('ajax/calendar_filter.php?mode=view');


        dialog.dialog({
            title: 'Filter:',
            modal: true,
            width:450,
            position: 'center',
            buttons: {
                "Filteren": function() {
                    var query_string = get_query_string();
                    $.ajax(
                    {
                        type: "POST",
                        url: "ajax/calendar_filter.php",
                        data: query_string,
                        success:
                        function(t)
                        {
                            dialog.dialog("close");
                            location.reload();
                        },
                        error:
                        function()
                        {
                            alert("Onze excuses. Er is iets fout gegaan.");
                        }
                    });
                        

                },
                "Filter verwijderen": function() {
                    $.ajax(
                    {
                        type: "POST",
                        url: "ajax/calendar_filter.php",
                        data: "mode=save",
                        success:
                        function(t)
                        {
                            dialog.dialog("close");
                            location.reload();
                        },
                        error:
                        function()
                        {
                            alert("Onze excuses. Er is iets fout gegaan.");
                        }
                    });


                },
                "Annuleren": function() {
                    $(this).dialog("close");
                }
            },
        close: function() {
            $(this).remove();
        }

        });
    });


function get_query_string() {
    var query_string = "mode=save";
    $(":checkbox[name='province']").each(
        function()
        {
            if (this.checked)
            {
                query_string += "&province[]=" + this.value;
            }
        });
    $(":checkbox[name='type']").each(
        function()
        {
            if (this.checked)
            {
                query_string += "&type[]=" + this.value;
            }
        });
    return query_string
}

// MIJN TOP 10
$('.bggSearchTitle').each(function() {
    var titleInput = $(this).attr('id').split("_");
    var number = titleInput[1];
    if( $('#id_'+number).val() != 0) {
        $('#btn_'+number).hide();
        $('#exact_'+number).hide();
        $('#text_exact_'+number).hide();
        $('#img_'+number).show();
    }
});


$('.searchBgg').click(function() {

    var searchBtn = $(this).attr('id').split("_");
    var number = searchBtn[1];
    var title = $('#title_'+number).val();
    var exact = ($('#exact_'+number+':checked').is(':checked')) ? 1 : 0;

    var dialog = $('<div>').attr('id','formDialog').html("<div class='suggestionsBox' id='suggestions'>\n\
                                                                    <div class='suggestionList' id='autoSuggestionsList'></div></div>");
    dialog.dialog({
        title: 'Selecteer het spel:',
        modal: true,
        width:450,
        position: 'center',
        close: function() {
            $(this).remove();
        }

    });
        
    $('#autoSuggestionsList').html("Bezig met laden....");

    $.ajax({
        url: 'ajax/bgg_search.php',
        dataType: 'json',
        data: {
            'n' : title,
            'e' : exact
        },
        success: function(responseText, statusText) {
            parseSearchResults(responseText, statusText, number, dialog);
        }
    });
});

$('.bggSearchTitle').keyup(function() {
    var inputTitle = $(this).attr('id').split("_");
    var number = inputTitle[1];
    $('#btn_'+number).show();
    $('#exact_'+number).show();
    $('#text_exact_'+number).show();
    $('#id_'+number).val(0);
    $('#img_'+number).hide();
});

function parseSearchResults(data, statusText, number, dialog) {
    $('#autoSuggestionsList').empty();
    if(data.length == 0) {
        $('#autoSuggestionsList').html("Geen resultaten gevonden");
    } else {
        $.each(data, function(i,game){
            $('#autoSuggestionsList').append('<li id=\''+game.id+'\'>'+game.name+'</li>');
        });

        $('li').click(function() {
            $('#title_'+number).val($(this).text());
            $('#id_'+number).val($(this).attr('id'));
            $('#btn_'+number).delay(1000).hide();
            $('#exact_'+number).delay(1000).hide();
            $('#text_exact_'+number).delay(1000).hide();
            $('#img_'+number).delay(1000).show();
            $("#formDialog").dialog('close');
        });
    }
}

$("ul#mainMenu li").hover(
  function () {
    $(this).addClass("active");
  },
  function () {
    $(this).removeClass("active");
  }
);

});
