jQuery(window)
    .load(
    function () {

        id = jQuery('#stofType').find(':selected').val();
        selectFabric(id);

        jQuery('a.choosePillow')
            .bind(
            'click',
            function (e) {

                e.preventDefault();
                URL
                    .setURL(
                    'index.php?option=com_order_proces&task=chooseFabric&Fabric='
                        + jQuery(this)
                        .parent()
                        .parent()
                        .parent()
                        .find(
                        '.currentFabricID')
                        .html(),
                    1);
                URL.followURL();

            });

        jQuery('a.choosePillow_large, .mouseover_fabric')
            .bind(
            'click',
            function (e) {

                e.preventDefault();
                URL
                    .setURL(
                    'index.php?option=com_order_proces&task=chooseFabric&Fabric='
                        + jQuery(this)
                        .parent()
                        .parent()
                        .find(
                        '.currentFabricID')
                        .html(),
                    1);
                URL.followURL();

            });

        jQuery(
            '.fabricView_step1_a a.choosePillow_large, .fabricView_step1_a .mouseover_fabric')
            .unbind()
            .bind(
            'click',
            function (e) {

                e.preventDefault();
                URL
                    .setURL(
                    'index.php?option=com_order_proces&view=step1_b&Fabric='
                        + jQuery(this)
                        .parent()
                        .parent()
                        .find(
                        '.currentFabricID')
                        .html(),
                    1);
                URL.followURL();

            });

        jQuery('a.chooseFabric')
            .bind(
            'click',
            function (e) {

                e.preventDefault();
                URL
                    .setURL(
                    'index.php?option=com_order_proces&task=chooseFabric&Fabric='
                        + jQuery(this)
                        .parent()
                        .parent()
                        .parent()
                        .find(
                        '.currentFabricID')
                        .html(),
                    1);
                URL.followURL();

            });

        jQuery('a.chooseFabric')
            .bind(
            'click',
            function (e) {

                e.preventDefault();
                URL
                    .setURL(
                    'index.php?option=com_order_proces&task=chooseFabric&Fabric='
                        + jQuery(this)
                        .parent()
                        .parent()
                        .parent()
                        .find(
                        '.currentFabricID')
                        .html(),
                    1);
                URL.followURL();

            });

        jQuery('.mouseover_fabric').bind(
            'mouseenter',
            function () {
                jQuery(this).parent().parent().find(
                    '.fabricImage img').attr('src',
                    jQuery(this).attr('rel'));
                jQuery(this).parent().parent().find(
                    '.currentFabricID').html(
                    jQuery(this).attr('id'));

                return;
            });

        jQuery('#pillowLoading img').fadeOut('fast');
        jQuery('#pillowLoader').css('opacity', 1);

        jQuery('.fabrics a').bind('click', function (e) {

            e.preventDefault();

            fogScreen();
            selectFabric(jQuery(this).attr('rel'));
            jQuery(this).css('border', '1px solid #6e5727');

            return false;

        });

        if (typeof shiftzoom != 'undefined') {

            shiftzoom.add(document.getElementById('shiftZoom'), {
                showcoords:false,
                zoom:1
            });
            shiftzoom.defaultCurpath = 'shiftzoom/images/cursors/';

        }

        jQuery('#shiftZoom').bind('load', function () {

            clearScreen();

        });

        if( jQuery('#thickness').length > 0 ) {
            jQuery('#thickness').bind('blur', function() {
                if( jQuery(this).val() < 3 ) {
                    alert('Kussens moeten minimaal 3 centimeter dik zijn');
                }
            })
        }


        jQuery('#stofType').bind('change', function () {
            id = jQuery(this).find(':selected').val();
            selectFabric(id);
            jQuery('#idFabric').val(id);
            jQuery('#calculated').slideUp('fast');
        });

        jQuery('.legenda-icon').tipsy({
            gravity:'s'
        });

        jQuery('.numberInput').bind('keydown', function (e) {

            if (e.shiftKey) {
                e.preventDefault();
            }


            if (isNaN(String.fromCharCode(e.which)) && e.which != 8 && (e.which < 96 || e.which > 106)) {
                e.preventDefault();
            }

        });

        stepBarBinder();

        jQuery('#dimensionError').find('input').bind('click',
            function () {

                jQuery(this).parent().fadeOut('fast');

            });

        jQuery('#calcForm input, #calcForm select').bind('change',
            function () {

                jQuery('#calcFormPriceDisplay').hide();
                retrieveCalculation();

            });

        jQuery('#calcForm input, #calcForm select').bind('keyup',
            function () {

                jQuery('#calcFormPriceDisplay').hide();
                setTimeout(function () {
                    retrieveCalculation()
                }, 100);

            });

        // Make sure we have the right fabric on submit
        jQuery('.button_container input').bind('click'),
            function () {
                id = jQuery('#stofType').find(':selected')
                    .val();
                jQuery('#idFabric').val(id);
            }

        // Init for fabrics
        retrieveCalculation();

    });

URL = {
    URL:false,
    setURL:function (newURL, overwrite) {

        if (this.URL == false || overwrite == 1) {

            this.URL = newURL;

        }

    },
    followURL:function () {

        if (typeof this.URL != 'undefined') {
            location.href = this.URL;
            // console.log(this.URL)

        }

    }
};

var selectFabric = function (id) {
    if (typeof FabricTypes != 'undefined') {
        jQuery('#selected').html('&nbsp;').css('background-image',
            'url(./materials/' + FabricTypes[id] + '_small.jpg)');
        jQuery('select#stofType option').attr('selected', false);
        jQuery('#_option' + id).attr('selected', true);
        shiftzoom.source(document.getElementById('shiftZoom'), './materials/'
            + FabricTypes[id] + '.jpg', false);
        jQuery('#idFabric').val(id);
    }

}

var clearScreen = function () {
    jQuery('#pillowLoading').fadeOut('fast');
    jQuery('#pillowLoader').animate({
        'opacity':1
    }, 'fast');
}

var fogScreen = function () {

    // jQuery('#pillowLoading').fadeIn('fast');
    // jQuery('#pillowLoader').animate({'opacity': 0.4}, 'fast');

}

var stepBarBinder = function () {

    jQuery('#progressBar a img').css('opacity', 0.4);
    jQuery('#progressBar a.inactive img').css('opacity', 0.4);

    jQuery('#progressBar').find('a.active img').hover(function () {

        jQuery(this).css('opacity', 1);

    }, function () {

        jQuery(this).css('opacity', 0.4);

    });

}

var retrieveCalculation = function () {

    var doRequest = 1;// Assume we can do a request
    // Unless proven otherwise
    jQuery('#calcForm form').find('input, select').each(function () {

        if (jQuery(this).val() == "" && jQuery(this).attr('type') != 'hidden') {
            doRequest = 0;
        }

    });

    if (doRequest == 1) {
        var params = jQuery('#calcForm form').serialize();

        jQuery('#calcFormPriceDisplay').load(
            'index.php?option=com_order_proces&view=ajax&format=raw&'
                + params, function () {

                if( jQuery('#thickness').length > 0 ) {
                    if( jQuery('#thickness').val() < 3 ) {
                        jQuery('#calcFormPriceDisplay').hide();
                    } else {
                        jQuery('#calcFormPriceDisplay').show();
                    }
                } else {
		      jQuery('#calcFormPriceDisplay').show();
		  }

            });

    }

}
