function myFileBrowser(field_name, url, type, win){
    var cmsURL = base_url + 'request.php';    // script URL - use an absolute path!
    if (cmsURL.indexOf("?") < 0) {
        //add the type as the only query parameter
        var uri = base64_encode(base_url+'admin/bestanden/browser');
        cmsURL = cmsURL + "?uri=" + uri + "&type=" + type;
    }
    else {
        //add the type as an additional query parameter
        // (PHP session ID is now included if there is one at all)
        cmsURL = cmsURL + "&request=file_browser&type=" + type;
    }

    tinyMCE.activeEditor.windowManager.open({
        file : cmsURL,
        title : lang_file_browser,
        width : 435,  // Your dimensions may differ - toy around with them!
        height : 435,
        resizable : "yes",
        inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous : "no"
    }, {
        window : win,
        input : field_name
    });
    return false;
}
function preload(){
    $('a').each(function(index){
        var uri = $(this).attr('href');
        $.ajax({url:uri});
    });
}
function console(line, is_error){
    var d = new Date();
    var h = d.getHours() + "";
    var m = d.getMinutes() + "";
    var s = d.getSeconds() + "";
    if (m.length == 1) m = "0" + m;
    if (s.length == 1) s = "0" + s;
    var time = h+':'+m+':'+s;
    var error_class = '';
    if (is_error) {
        error_class = ' error';
    }
    $('.console').prepend('<span class="time' + error_class + '">'+time + '</span> ' + line + '<br>');
}
function clear(){
    $('.console').text('');
}
function disable(element){
    element.animate({opacity:0.3},500);
    element.attr('disabled', 'disabled');
    element.attr('href', 'javascript:void(0)');
}

function enable(element){
    element.animate({opacity:1.0},500);
    element.removeAttr('disabled');
}

function is_enabled(element){
    var attr = element.attr('disabled');
    return ! (typeof attr !== 'undefined' && attr !== false);
}
function notification(line){
    $('#content').prepend('<div class="notification">'+line+'</div>')
        .animate({opacity:1.0}, 2000, function(){ $('.notification').fadeOut(); });
}
function IsNumeric(input){
    return (input - 0) == input && input.length > 0;
}
function progress(percentage){
    $(".progressbar").progressbar({
        value: percentage
    });
    if ( ! $(".progressbar").is(":visible") ) {
        $(".progressbar").slideDown();
    }
}
function hide_progress(){
    $(".progressbar").slideUp();
}

// Op het moment dat het document DOM geladen en gereed is:
$(document).ready(function(){
    /*
     * Toggle:
     * bij het klikken op een .toggle element
     * wisselt de zichtbaarheid van het hiernavolgende element in de DOM
     */
    $('.toggle').addClass('collapsed').click(function(){
        if ($(this).hasClass('collapsed')) {
            $(this).removeClass('collapsed').addClass('expanded');
        } else {
            $(this).removeClass('expanded').addClass('collapsed');
        }
        $(this).next().toggle();
    }).mouseover(function(){
        $(this).css('cursor','pointer');
    });
    $("*[id^='toggle_']").click(function(e){
        e.preventDefault();
        var id = $(this).attr('id').substr(7);
        if ($('#toggleable_'+id).is(':visible')) {
            $('#toggleable_'+id).hide();
            $(this).text('[+] meer…');
        } else {
            $('#toggleable_'+id).show();
            $(this).text('[-] minder…');
        }
    });
    /*
     * Expand/Collapse:
     *
     */
    $('.expand').click(function(){
        $(this).parent().children('ul').toggle();
        var src = $(this).attr('src');
        $(this).attr('src', $(this).attr('toggled_src')).attr('toggled_src', src);
    }).mouseover(function(){
        $(this).css('cursor', 'pointer');
    });
    $('a[href$="toggle_all"]').click(function(e){
        e.preventDefault();
        $.fn.reverse = [].reverse;
        $('.expand').reverse().each(function(index){
            $(this).click();
        });
    });

    /**
     * DatePicker
     */
    if (typeof($('.datepicker').datepicker) !== 'undefined') {
        $('.datepicker').datepicker({'dateFormat': 'dd-mm-yy'});
    }
    if (typeof($('.datetimepicker').datetimepicker) !== 'undefined') {
        $('.datetimepicker').datetimepicker({'dateFormat': 'dd-mm-yy'});
    }
    if (typeof($('.timepicker').timepicker) !== 'undefined') {
        $('.timepicker').timepicker();
    }
    
    $('table.zebra tr:odd').addClass('odd');
    $('table.kleurmaattabel tr:odd').addClass('odd');
    $('table.bestelling tr:odd').addClass('odd');
    $('table.tabel tr').mouseover(function(){
        $(this).addClass('hover');
    }).mouseout(function(){
        $(this).removeClass('hover');
    });
    $('table.bestelling tr').mouseover(function(){
        $(this).addClass('hover');
    }).mouseout(function(){
        $(this).removeClass('hover');
    });
    $('.vergroting').fancybox({
        'overlayShow' : true,
        'overlayOpacity' : '0.3',
        'showCloseButton' : true,
        'hideOnContentClick' : true,
        'hideOnOverlayClick' : true,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'titlePosition' : 'inside'
    });
        
    $('select[name=aantal_dropdown]').change(function(){
        var val = $(this).val();
        if (val == "...") {
            $(this).siblings('input[name=aantal_input]').val(20).show().select().focus();
            $(this).remove();
        } else {
            $(this).parent('form').submit();
        }
    });
    $('input[name=aantal_input]').blur(function(){
        $(this).parent('form').submit();
    });
    $('input[type=text].search').focus(function(){
        $(this).value = '';
    });
    $('input[name=term]').click(function(){
        $(this).val('');
    });
    //$('.notification').animate({opacity:1},2000, function(){$(this).fadeOut()});
    
    /**
     * Help tooltips
     * Worden toegepast op elke element met het attribuut 'help'
     */
    $('*[help]').each(function(){
        var help = $(this).attr('help');
        if (typeof help !== 'undefined' && help !== false) {
            if (help.length > 0) {
                $(this).tipsy({title:'help',gravity:'w'});
            }
        }
    });
    
    $('.button,.bigbutton,.deletebutton').each(function(){
        var text = $(this).text();
        if (text.length === 0) {
            text = $(this).val();
        }
        if (text.length > 12) {
            $(this).addClass('long');
        }
    });
     
    /**
     * Bevestiging voor verwijderen
     */
    var conf_width;
    var conf_long;
    $('.deletebutton').each(function(){
        if ( ! $(this).hasClass('no-confirm')) {
            width = $(this).width();
            var confirm_text = $(this).attr('alt');
            if (confirm_text.length == 0) {
                confirm_text = 'Bevestigen';
            }
            conf_long = $(this).hasClass('long');
            $(this).confirm({
                msg:'',
                timeout:1500,
                buttons: {
                    ok:'<span class="button confirm '+(conf_long === true ? 'long' : '')+'" style="width:'+width+'px">'+confirm_text+'</span>',
                    cancel:'',
                    separator:''
                }
            });
        }
    });
    
    /**
     * Categorieën uitklappen
     */
    /*$('.categories div.collapsed').hide()
        .parents('li').click(function(){
            $(this).find('div.collapsed').removeClass('collapsed').addClass('expanded').show();
        });*/
        
    /**
     * Checkbox selecteer alles
     */
    $('#checkall').click(function(){
        alert('c');
        if (!$(this).attr('checked')) {
            $('input[type=checkbox]').removeAttr('checked');
        } else {
            $('input[type=checkbox]').attr('checked', 'checked');
        }
    });
    
    /**
     * Back-button
     */
    $('a[href*=#back]').click(function(e){
        e.preventDefault();
        history.back();
    });
    
    /**
     * Submit-button
     */
    $('a[href*=#submit]').click(function(e){
        e.preventDefault();
        $(this).parents('form').submit();
    });
    
    /**
     * Cycle
     */
     var id;
    $('.slideshow').each(function(){
        id = $(this).attr('id');
        $(this).before('<div id="slideshow_nav_'+id+'">').cycle({
            fx:     'fade',
            speed:  'fast',
            timeout: 3000,
            pager:  '#slideshow_nav_'+id
        });
    });
    
    /**
     * Update prijs met kleur/maten-selectie
     */
    $('select#kleur').change(function(){
        update_price();
        // Controleer of de geselcteerde maat in de nieuwe kleur voorkomt.
        var selected_maat = $('select#maat').val();
        if ($('select#maat option[value="'+selected_maat+'"]').hasClass('unavailable')) {
            $('select#maat option').not('.unavailable').first().attr('selected', 'selected');
        }
        //update_price();
    });
    $('select#maat').change(function(){
        update_price();
    });
    function update_price()
    {
        var b64_kleur, b64_maat;
        var kleur, maat, prijs;
        var selector = 'input';
        var beyond_supply;
        if ($('select#kleur').length > 0) {
            b64_kleur = $('select#kleur').val();
            selector += '[kleur="'+b64_kleur+'"]';
        }
        if ($('select#maat').length > 0) {
            b64_maat = $('select#maat').val();
            selector += '[maat="'+b64_maat+'"]';
        }
        kleur = base64_decode(b64_kleur);
        maat = base64_decode(b64_maat);
        prijs = $(selector+'[prijs]').val();
        oude_prijs = $(selector+'[oude_prijs]').val();
        vrije_voorraad = $(selector+'[vrije_voorraad]').val();
        beyond_supply = $(selector+'[vrije_voorraad][beyond_supply]') !== undefined;
        $('#oude_prijs').text(oude_prijs);
        $('#prijs').text(prijs);
        if (vrije_voorraad <= 0) {
            $('select#maat option[value="'+b64_maat+'"]').text(maat+' (uitverkocht)');
        } else {
            $('select#maat option[value="'+b64_maat+'"]').text(maat);
        }
        //$('#oude_prijs').effect('pulsate', {times:1});
        //$('#prijs').effect('pulsate', {times:1});
        $('select#maat option').each(function(){
            $(this).addClass('unavailable');
        });
        var aantal_uitverkocht = {};
        var aantal_op_voorraad = {};
        $('input[type=hidden][vrije_voorraad]').each(function(){
            var vrije_voorraad = $(this).val();
            var has_kleur = $(this).attr('kleur') !== undefined;
            var has_maat = $(this).attr('maat') !== undefined;
            var beyond_supply = $(this).attr('beyond_supply') !== undefined;
            var b64_kleur, b64_maat;
            var kleur, maat;
            if ( ! has_maat) {
                return;
            } else {
                b64_maat = $(this).attr('maat');
            }
            maat = base64_decode(b64_maat);
            if (has_kleur) {
                b64_kleur = $(this).attr('kleur');
                kleur = base64_decode(b64_kleur);
                if ( ! aantal_uitverkocht.hasOwnProperty(kleur)) {
                    aantal_uitverkocht[kleur] = 0;
                }
                if ( ! aantal_op_voorraad.hasOwnProperty(kleur)) {
                    aantal_op_voorraad[kleur] = 0;
                }
                var b64_selected_kleur = $('select#kleur').val();
                if (b64_selected_kleur == b64_kleur) {
                    if (vrije_voorraad <= 0 && ! beyond_supply) {
                        $('select#maat option[value="'+b64_maat+'"]').text(maat + ' (uitverkocht)').removeClass('unavailable');
                        aantal_uitverkocht[kleur]++;
                    } else {
                        $('select#maat option[value="'+b64_maat+'"]').text(maat).removeClass('unavailable');
                        aantal_op_voorraad[kleur]++;
                    }
                }
            } else {
                if (vrije_voorraad <= 0 && ! beyond_supply) {
                    $('select#maat option[value="'+b64_maat+'"]').text(maat + ' (uitverkocht)').removeClass('unavailable');
                    //aantal_uitverkocht[kleur]++;
                } else {
                    $('select#maat option[value="'+b64_maat+'"]').text(maat).removeClass('unavailable');
                    //aantal_op_voorraad[kleur]++;
                }
            }
        });
        /*for (var key in aantal_op_voorraad) {

        }*/
    }
    update_price();

    /**
     * Console
     */
    $('#console_title a').click(function(e){
        e.preventDefault();
        $('#console_lines').toggle();
    });
    //$('#console').draggable();
    
    /*CKEDITOR.replace( 'ckeditor',
    {
        //extraPlugins : 'autogrow',
        //autoGrow_maxHeight : 800,
        // Remove the Resize plugin as it does not make sense to use it in conjunction with the AutoGrow plugin.
        //removePlugins : 'resize'
    });*/
});

