jQuery(document).ready(function() {

    $(function(){
        $(".tiptip").tipTip({
            maxWidth: "auto",
            edgeOffset: 10
        });
        $("a[rel]").overlay({

            mask: 'darkred',
            effect: 'apple',

            onBeforeLoad: function() {

                // grab wrapper element inside content
                var wrap = this.getOverlay().find(".contentWrap");

                // load the page specified in the trigger
                wrap.load(this.getTrigger().attr("href"));
            }

        });

        $("#changeAll_format").attr('disabled', 'disabled');
        $("#c-start").hide();
        $("#c-start2").hide();
        $("#c-clear").hide();

        $(".cif-product").each(function(){
            $("#c-start").show();
            $("#c-start2").show();
            $("#c-clear").show();

            var fId = $(this).attr('rel');
            var fProduct = $('#ci-' + fId + ' .cif-product').val();
            var fFormat = $('#ci-' + fId + ' .cif-format').val();

            var select = $('#ci-' + fId + ' .cif-format');

            if(select.prop) {
                var options = select.prop('options');
            }
            else {
                var options = select.attr('options');
            }

            $.ajax({
                url: "/cart/ajax/formats",
                type: "GET",
                data: 'id='+fProduct+'&d='+ new Date().getTime(),
                dataType: 'json',
                success: function (result) {

                    $('option', select).remove();

                    var hasVal = false;

                    $.each(result, function(val, text) {
                        options[options.length] = new Option(text, val);
                        if(val == fFormat)
                        {
                            hasVal = true;
                        }
                    });

                    if(hasVal === true)
                    {
                        select.val(fFormat);
                    }
                    else {
                        $('#ci-' + fId + ' .cif-format option:first').val();
                    }

                //select.change();

                },
                error: function (error, response) {
                    alert("Ihre Änderungen konnten leider nicht gespeichert werden."); // Property statt Objekt verwenden
                }
            });
        });
        
        message_index = $("#options_enhancement").val();

                if (message_index == "")
                {
                    $("#enhancementalert").hide();
                }

                if (message_index == "inactive")
                {
                    $("#enhancementalert").show();
                }

                if (message_index == "active")
                {
                    $("#enhancementalert").hide();

                }
                
    });

    $("#ci-submit").hide();

    $('.clearCart').click(function(){
        var answer = confirm('Sind Sie sicher, dass Sie den Warenkorb leeren wollen?');
        return answer // answer is a boolean
    });

    $('#c-cancel').click(function(){
        var answer = confirm('Sind Sie sicher, dass Sie die Bestellung abbrechen wollen?');
        return answer // answer is a boolean
    });

    $(".cif-product").change(
        function(){
            var fId = $(this).attr('rel');
            var fProduct = $('#ci-' + fId + ' .cif-product').val();
            var fFormat = $('#ci-' + fId + ' .cif-format').val();

            var select = $('#ci-' + fId + ' .cif-format');

            if(select.prop) {
                var options = select.prop('options');
            }
            else {
                var options = select.attr('options');
            }

            $.ajax({
                url: "/cart/ajax/formats",
                type: "GET",
                data: 'id='+fProduct+'&d='+ new Date().getTime(),
                dataType: 'json',
                success: function (result) {

                    $('option', select).remove();

                    var hasVal = false;

                    $.each(result, function(val, text) {
                        options[options.length] = new Option(text, val);
                        if(val == fFormat)
                        {
                            hasVal = true;
                        }
                    });

                    if(hasVal === true)
                    {
                        select.val(fFormat);
                    }
                    else {
                        $('#ci-' + fId + ' .cif-format option:first').val();
                    }

                    select.change();
                    
                },
                error: function (error, response) {
                    alert("Ihre Änderungen konnten leider nicht gespeichert werden."); // Property statt Objekt verwenden
                }
            });
        });

    $("#changeAll_product_id").change(
        function(){
            var fId = $(this).attr('rel');
            var fProduct = $('#changeAll_product_id').val();
            var fFormat = $('#changeAll_format').val();

            var select = $('#changeAll_format');

            if(fProduct == "")
            {
                $("#changeAll_format").attr('disabled', 'disabled');
                $('option', select).remove();
                return;
            }

            if(select.prop) {
                var options = select.prop('options');
            }
            else {
                var options = select.attr('options');
            }

            $.ajax({
                url: "/cart/ajax/formats",
                type: "GET",
                data: 'id='+fProduct+'&d='+ new Date().getTime(),
                dataType: 'json',
                success: function (result) {

                    $('option', select).remove();

                    var hasVal = false;

                    $.each(result, function(val, text) {
                        options[options.length] = new Option(text, val);
                        if(val == fFormat)
                        {
                            hasVal = true;
                        }
                    });

                    if(hasVal === true)
                    {
                        select.val(fFormat);
                    }
                    else {
                        $('#changeAll_format option:first').val();
                    }

                    $("#changeAll_format").attr('disabled', '');


                    select.change();

                },
                error: function (error, response) {
                    alert("Ihre Änderungen konnten leider nicht gespeichert werden."); // Property statt Objekt verwenden
                }
            });
        });

    $(".binding").change(
        function(){
            var fId = $(this).attr('rel');
            var fQuantity = $('#ci-' + fId + ' .cif-quantity').val();
            var fProduct = $('#ci-' + fId + ' .cif-product').val();
            var fFormat = $('#ci-' + fId + ' .cif-format').val();
            var fEffect = $('#ci-' + fId + ' .cif-effect').val();

            var data = 'id='+fId+'&quantity='+fQuantity+'&product='+fProduct+'&format='+fFormat+'&effect='+fEffect+'&d='+ new Date().getTime() ;

            $.ajax({
                url: "/cart/ajax/change",
                type: "GET",
                data: data,
                dataType: 'json',
                success: function (result) {
                    $("#ci-realsize-"+fId).html(result.realsize);
                    $("#ci-sum-"+fId).html(result.sum);
                    $("#ci-image-"+fId).attr("src", 'cart/'+fId+'/thumbnail.jpg?' + new Date().getTime() );
                    $("#ci-quality-"+fId+' img').attr("src", result.qualitysrc);
                    $("#ci-quality-"+fId+' img').attr("alt", result.qualitytext);
                    $("#ci-quality-"+fId+' img').attr("title", result.qualitytext);
                    $("#c-total").html(result.total);
                    $("#c-prints").html(result.prints);
                },
                error: function (error, response) {
                    //alert(response);
                    alert("Ihre Änderungen konnten leider nicht gespeichert werden."); // Property statt Objekt verwenden
                }
            });

            
        }
        );

        $("#options_enhancement").change(function()
                {
                    var message_index

                    message_index = $("#options_enhancement").val();

                    if (message_index == "")
                {
                    $("#enhancementalert").hide();
                }

                if (message_index == "inactive")
                {
                    $("#enhancementalert").show();
                }

                if (message_index == "active")
                {
                    $("#enhancementalert").hide();

                }
                });



});

