tinyMCE.init({
    mode: 'specific_textareas',
    editor_selector: 'mceEditor',
    content_css: '/css/styles.css',
    body_id: 'contentText',
    theme: 'advanced'
});

$(document).ready(function(){
    $('#bookmark').jFav('http://www.fruitschaal.nu/');

    $('#showAll').click(function(){
        $('#banner').hide('slow', function(){
            $('#showAll').parent().hide('fast', function(){
                $('#coverAll').show('slow', function(){
                    sIFR.replace(helvetica, {
                      selector: 'h1',
                      wmode: 'transparent',
                      css: [ '.sIFR-root { color: #cad61a;}'
                    ]
                    });

                    sIFR.replace(helvetica, {
                      selector: 'h2',
                      wmode: 'transparent',
                      css: [ '.sIFR-root { color: #cad61a;}'
                    ]
                    });
                });
            });
        });
        return false;
    });

    if(showAllProducts){
        $('#banner').hide('slow', function(){
            $('#showAll').parent().hide('fast', function(){
                $('#coverAll').show('slow', function(){
                    sIFR.replace(helvetica, {
                      selector: 'h1',
                      wmode: 'transparent',
                      css: [ '.sIFR-root { color: #cad61a;}'
                    ]
                    });

                    sIFR.replace(helvetica, {
                      selector: 'h2',
                      wmode: 'transparent',
                      css: [ '.sIFR-root { color: #cad61a;}'
                    ]
                    });
                });
            });
        });
    }

    if($('.showProduct').length > 1){
        $('.showProduct').colorbox();
        $().bind('cbox_complete', function(){
            sIFR.replace(helvetica, {
              selector: 'h1',
              wmode: 'transparent',
              css: [ '.sIFR-root { color: #cad61a;}'
            ]
            });

            sIFR.replace(helvetica, {
              selector: 'h2',
              wmode: 'transparent',
              css: [ '.sIFR-root { color: #cad61a;}'
            ]
            });
        
            $('.hideProduct').click(function(){
                $.fn.colorbox.close();
            });

            $('.addToCart').click(function(){
                $.fn.colorbox.close();
                $('#OrderProductId').val($(this).attr('id').replace('orderBtn', ''));
                $('#OrderProductId').parent().submit();
            });
        });
    }

    $('.orderBtn').click(function(){
        $('#OrderProductId').val($(this).attr('id').replace('orderBtn', ''));
        $('#OrderProductId').parent().submit();
    });

    $('#Address2Overview').click(function(){
        $('#OrderAddressForm').attr('action', $('#OrderAddressForm').attr('action').replace('date', 'overview'));
        $('#OrderAddressForm').submit();
    });

    $('#Address2Date').click(function(){
        $('#OrderAddressForm').submit();
    });

    $('#Date2Address').click(function(){
        $('#OrderDateForm').attr('action', $('#OrderDateForm').attr('action').replace('options', 'address'));
        $('#OrderDateForm').submit();
    });

    $('#Date2Options').click(function(){
        $('#OrderDateForm').submit();
    });

    $('#Options2Date').click(function(){
        if(getImageData()){
            $('#OrderOptionsForm').attr('action', $('#OrderOptionsForm').attr('action').replace('account', 'date'));
            $('#OrderOptionsForm').submit();
        }
        else
            return false;
    });

    $('#Options2Account').click(function(){
        if(getImageData()){
            $('#OrderOptionsForm').submit();
        }
        else
            return false;
    });

    $('#Account2Options').click(function(){
        $('#OrderAccountForm').attr('action', $('#OrderAccountForm').attr('action').replace('check', 'options'));
        $('#OrderAccountForm').submit();
    });

    $('#Account2Check').click(function(){
        $('#OrderAccountForm').submit();
    });

    $('#Check2Pay').click(function(){
        $('#OrderCheckForm').submit();
    });

    $('#OrderPaypal').click(function(){
        $('#paypalform').submit();
    });

    $('#OrderIDEAL').click(function(){
        $('#ideal_form').submit();
    });

    if($('#datepicker').length > 0){
        $('#datepicker').datepicker({
            beforeShowDay: function(date){
                if(date.getDay() == 0)
                    return Array(false, '');
                else{
                    if((date.getDay() == 1) && (bMondays == false))
                        return Array(false, '');
                    else{
                        var today = date.getDate() + '-' + (parseInt(date.getMonth()) + 1) + '-';

                        if(date.getYear() < 1900)
                            today += (parseInt(date.getYear()) + 1900);
                        else
                            today += date.getYear();

                        if($.inArray(today, aDates) != -1){
                            return Array(false, '');
                        }
                        else{
                            return Array(true, '');
                        }
                    }
                }
            },
            dateFormat: 'yy-mm-dd',
            dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
            defaultDate: dDefaultDate,
            //maxDate: '+5w',
            minDate: dMinDate,
            monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
            onSelect: function(dateText){
                $('#OrderDate').val(dateText);
            }
        });
        if($('#OrderDate').val() == '')
            loadCurrentDate();
    }

    $('div.radio input').click(function(){
        jsToggleCard();
    });

    $('#OrderShowExtra').click(function(){
        toggleExtra();
    });

    if($('#CardCount').length > 0){
        $('#CardCount').html('(' + $('#OrderCardText').val().length + '/250)');
    }

    $('#OrderCardText').keydown(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);

        if((code == 8) || (code == 46)){
            $('#CardCount').html('(' + Math.max(0, $(this).val().length - 1) + '/250)');
            return true;
        }
        else if($(this).val().length < 250){
            $('#CardCount').html('(' + ($(this).val().length + 1) + '/250)');
            return true;
        }
        else{
            return false;
        }
    });

    if($('#ProductUploadButton').length > 0){
        new AjaxUpload('#ProductUploadButton', {
            autoSubmit: true,
            action: '/admin/products/uploadImage/products',
            name: 'image',
            onComplete: function(file, response){
                if(response != 'error'){
                    $('#ProductImage').append('<option value=' + response + '>' + response + '</option>');
                }
            }
        });
    }

    if($('#BannerUploadButton').length > 0){
        new AjaxUpload('#BannerUploadButton', {
            autoSubmit: true,
            action: '/admin/products/uploadImage/banners',
            name: 'image',
            onComplete: function(file, response){
                if(response != 'error'){
                    $('#BannerBanner').append('<option value=' + response + '>' + response + '</option>');
                }
            }
        });
    }

    if($('#PageUploadButton').length > 0){
        new AjaxUpload('#PageUploadButton', {
            autoSubmit: true,
            action: '/admin/products/uploadImage/pages',
            name: 'image',
            onComplete: function(file, response){
                if(response != 'error'){
                    $('#UploadSucces').append(response + ' is geupload');
                }
            }
        });
    }

    if(!admin){
        $('#OrderMayCostExtra').click(function(){
            if($('#OrderMayCostExtra').attr('checked')){
                $('#OptionOption3').attr('checked', 'checked');
                $('#OptionOption3').attr('disabled', 'disabled');
                $('#OptionOption3').append('<input type="hidden" id="HiddenOptionOption3" name="data[Option][Option][]" value="3" />');
            }
            else{
                $('#OptionOption3').removeAttr('checked');
                $('#OptionOption3').removeAttr('disabled');
                $('#HiddenOptionOption3').remove();
            }
        });

        if($('#OptionOption4').length > 0){
            $('#OptionOption4').attr('disabled', 'disabled');
        }

        if($('#OptionOption6').length > 0){
            $('#OptionOption6').attr('disabled', 'disabled');
        }
    }
    
    initialChecks();
    jsToggleCard();
    
    $('#checkall').click(function(){
        if($(this).attr('checked')){
            $('.print').each(function(){
                $(this).attr('checked', 'checked');
            });
        }
        else{
            $('.print').each(function(){
                $(this).removeAttr('checked');
            });
        }
    });
    
    if(!admin){
        $('#OptionOption4').parent().children().each(function(){
            $(this).hide();
        });
        
        $('#OptionOption6').parent().children().each(function(){
            $(this).hide();
        });
    }
});
