$("a[rel=external]").live("click", function() {
    window.open(this.href);
    return false;
});

$(document).ready(function() {
    
    if ($('.news_img').length > 0) {
        $('.news_img').fancybox();
    }
    
    if ($('input:file').length > 0 ) {
        if (jQuery.browser.msie == true) {
            $('input:file').next().remove();
            $('#file_total').parent().remove();
        }
        $('input:file').bind('change',function(){
            var ext = $(this).val().split('.').pop().toLowerCase();
            if($.inArray(ext, ['gif','png','jpg','jpeg','bmp']) == -1) {
//            if (0){
                $(this).val('');
                alert('Het bestand dat je heeft geselecteerd is geen afbeelding');
            } else {
                if (jQuery.browser.msie != true) {
                    $(this).next().children('strong:first').html((this.files[0].size / 1000));
                }

                if ( $('input:file').last().val() != '' ) {
                    $(this).clone().val('').attr('id','file'+$('input:file').length).appendTo($(this).parent());
                    if (jQuery.browser.msie != true) {
                        var bestandgrootte = $(this).next().clone();
                        bestandgrootte.children('strong:first').html('0');
                        $(this).parent().append(bestandgrootte);
                    }
                }
                
                if (jQuery.browser.msie != true) {
                    var total = 0
                    $('.single_size').each(function(){
                        total       = total + (parseFloat($(this).html()) / 1000);
                    })
                    $('#file_total').attr('value',total).html(total.toFixed(4));

                    if (total > 5 && $('#no_upload').length == 0 ) {
                        $('.total').attr('style','color:red');
                        var no_upload = $('<input>').attr('type','hidden').attr('value','TRUE').attr('name','no_upload').attr('id','no_upload');
                        $('form').append(no_upload);
                    } else if (total < 5 && $('#no_upload').length > 0 )  {
                        $('#no_upload').remove(); 
                        $('.total').removeAttr('style');
                    }                
                }
            }

            

        });
        
        $('#file_extra').live('click',function(){
            too_much        = false;
            i_counter       = 0;
            $('input:file').each(function(){
                if ($(this).val() == '' ) {
                    i_counter = i_counter + 1;
                }
            });
            if (i_counter <= 4) {
                $('.input_file:last').clone().attr('id','file'+$('input:file').length).val('').appendTo($('.input_file:last').parent());
                if (jQuery.browser.msie != true) {
                    var bestandgrootte = $('#file').next().clone();
                    bestandgrootte.children('strong:first').html('0');
                    $('#file').parent().append(bestandgrootte);
                } 
            } else {
                alert('Gebruik eerst de bovenstaande velden');
            }
            
            return false;
        });
    }
    
    $('form').submit(function(){
        if ( $('#file_total').length > 0 && $('#no_upload').length > 0 ) {
            alert('Je mag maximaal 5 mb uploaden!');
            return false;
            
        }
    });
    
});

