jQuery(document).ready(function($){
    jQuery('.numeric').live('keyup', function () {
        this.value = this.value.replace(/[^0-9]/, '');
    });
    
  $('#klapp1').click(function() {
    $('#selector_body1').hide('slow');
    $('#klapp1').hide();
    $('#klapp1a').show();
    });
    
  $('#klapp1a').click(function() {
    $('#selector_body1').show('slow');
    $('#klapp1').show();
    $('#klapp1a').hide();
    });

  $('#klapp2').click(function() {
    $('#selector_body2').hide('slow');
    $('#klapp2').hide();
    $('#klapp2a').show();
    });
    
  $('#klapp2a').click(function() {
    $('#selector_body2').show('slow');
    $('#klapp2').show();
    $('#klapp2a').hide();
    });
    
  $('#klapp3').click(function() {
    $('#selector_body3').hide('slow');
    $('#klapp3').hide();
    $('#klapp3a').show();
    });
    
  $('#klapp3a').click(function() {
    $('#selector_body3').show('slow');
    $('#klapp3').show();
    $('#klapp3a').hide();
    });
    
  $('#klapp4').click(function() {
    $('#selector_body4').hide('slow');
    $('#klapp4').hide();
    $('#klapp4a').show();
    });
    
  $('#klapp4a').click(function() {
    $('#selector_body4').show('slow');
    $('#klapp4').show();
    $('#klapp4a').hide();
    });
    
    function getRelatedProducts(sku) {
        var loading = $('<img src="/typo3/sysext/t3skin/icons/login-submit-progress.gif" alt="loading">');
        var dialog = jQuery('#crossselling_layer');
        dialog.find('#crossselling_products').append(loading.clone());

        dialog
            .dialog({
                title: '',
                modal: true,
                width: 800,
                height: "auto"
            });

        dialog.dialog('open');
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&related=" + sku,
            success: function(response) {
                dialog.find('#crossselling_products').html(response);
            }
        });
        
//        jQuery('.ui-dialog-titlebar-close').click(function() {
//            jQuery('#kaufForm').submit();
//        });

        jQuery('#toBasketcrosselling').click(function() {
            jQuery('#kaufForm').submit();
        });

        jQuery('.closebasket').click(function(e) {
            e.preventDefault();
            dialog.dialog('close');
        });
        
        return false;
    }
    
    jQuery('#toBasket').live('click', function(e) {
        e.preventDefault();
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&addBasket=" + jQuery('#skuBasket').val() + "&cnt=" + jQuery('#artikel_anzahl').val()
        });
        
        getRelatedProducts(jQuery('#skuBasket').val());
    });
    
    jQuery('.deleteBasket').live('click', function(e) {
        e.preventDefault();
        
        var delsku = $(this).attr('id').replace('del_', '');
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&deleteBasket=" + delsku
        });
        $(this).parent().parent().parent().fadeOut('fast');
        updateBasketTotal();
    });
    
    jQuery('.updateProdukt').live('click', function(e) {
        var link = jQuery(this).attr('href');
        e.preventDefault();
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&update=" + $(this).attr('id') + "ts=" + e.timeStamp,
            success: function(response) {
                if (response) {
                    jQuery(response).find("content").each(function() {
                        jQuery(this).find("numIndex").each(function() {
                            if(jQuery(this).text()) {
                                jQuery('#selector'+jQuery(this).attr('index')).css('display', 'block');
                                jQuery('#selector_body'+jQuery(this).attr('index')).html(jQuery(this).text());
                            }
                            else {
                                jQuery('#selector'+jQuery(this).attr('index')).css('display', 'none');
                                jQuery('#selector_body'+jQuery(this).attr('index')).empty();
                            }
                            jQuery('#selector_body'+jQuery(this).attr('index')).append('<div class="clear"></div>');
                        });
                        
                        jQuery('#prod_sku').html(jQuery(this).find("skunummer").text());
                        jQuery('#skuBasket').val(jQuery(this).find("skunummer").text());
                        jQuery('.techdata').each(function() { jQuery(this).remove()});
                        
                        jQuery('#product_data').prepend(jQuery(this).find("techdetail").text());
                        /* fix for ringsizes */
                        if(jQuery(this).find("ringsize").text()) {
                            jQuery(jQuery(this).find("ringsize").text()).insertAfter('#prod_art_detail');
                        }
                        jQuery('#detail_image').html(jQuery(this).find("detail_image").text());
                        jQuery('#backfrieder_preis').html('EUR ' + jQuery(this).find("preis").text());
                    });
                }  
            }
        }); 
        
        /* addthis */
        window.addthis.update('share', 'url', 'http://' + location.hostname + '/' + link);
    });
    
    jQuery('.productCount').live('keyup', function() {
        var sku = jQuery(this).attr('name').replace(/tx_sysfirebackfrieder_pi1\[([^\]]*)\]/g, "$1");
        
        //if(jQuery(this).attr('value') < 1) 
        //    jQuery(this).attr('value', 1);
        if(jQuery(this).attr('value') > 99) 
            jQuery(this).attr('value', 99);
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&updateBasket=1&addBasket=" + sku + "&cnt=" + jQuery(this).attr('value')
        });
        var price = ((jQuery(this).parent().parent().find('.basket_price').html()).replace(/[.]/, '')).replace(/[,]/, '.') * (jQuery(this).attr('value')).replace(/[,]/, '.');
        jQuery(this).parent().parent().find('.basket_price_row').html((parseFloat(price).toFixed(2)).replace(/[.]/, ','));
        updateBasketTotal();
    });
    
    jQuery('#geschenkverpackung').live('keyup', function() {
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&checkTotal=1&cnt=" + jQuery(this).attr('value') + "&ts=" + (new Date()).getTime(),
            success: function(response) {
                if (response) {
                    jQuery('#geschenkverpackung').val(response);
                }
            }
        });
    });
    
    jQuery('.payment').change(function () {
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&setPayment=" + jQuery(this).attr('value'),
            success: function(response) {
                updateBasketTotal();
                if(response) {
                    jQuery('#price_nachname').html(response);
                    jQuery('#payment_nachname').fadeIn('fast');
                }
                else {
                    jQuery('#payment_nachname').fadeOut('fast');
                }
            }
        });
    });
    
    jQuery('#country').change(function() {
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&setShipping=" + jQuery(this).attr('value'),
            success: function(response) {
                jQuery('#shipping').html(response);
                updateBasketTotal();
            }
        });
        /* fix for austria */
        if(jQuery(this).attr('value') != '1') {
            jQuery('.payment').each(function() {
                if(jQuery(this).attr('value') == 'nachname') {
                    jQuery(this).attr('disabled', true);
                    jQuery(this).attr('checked', false);
                }
            });
        }
        else {
            jQuery('.payment').each(function() {
                if(jQuery(this).attr('value') == 'nachname')
                    jQuery(this).attr('disabled', false);
            });
        }
    });
    
    jQuery('#artikel_anzahl').keyup(function() {
        var price = jQuery('#price').val() * jQuery(this).val();
        jQuery('#backfrieder_preis').html('EUR ' + parseFloat(price).toFixed(2));
    });
    
    /* fix for live-hover */
    jQuery('.colltotal').click( function(e) {
        e.preventDefault();
    
        var x = document.body.clientWidth / 2;
        var y = document.body.clientHeight / 2;
        
        var dialog = jQuery('#collection_dialog');
                    
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&collection=" + jQuery(this).attr('id') + "&ts=" + (new Date()).getTime(),
            success: function(response) {
                jQuery('#collection_dialog').html(response);
                
                dialog
                    .dialog({
                        title: '',
                        modal: true,
                        width: 'auto',
                        height: 'auto',
                        position: [x-135, y-80]
                    });
                
                dialog.dialog('open') 
            }
        });
    });
    
    jQuery('.collinfo').live('click', function(e) {
        e.preventDefault();
        
        var x = document.body.clientWidth / 2;
        var y = document.body.clientHeight / 2;
        
        var dialog = jQuery('#collection_dialog');
                    
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&collection=" + jQuery(this).attr('id') + "&colldetail=1&ts=" + (new Date()).getTime(),
            success: function(response) {
                jQuery('#collection_dialog').html(response);
                
                dialog
                    .dialog({
                        title: '',
                        modal: true,
                        width: 'auto',
                        height: 'auto',
                        position: [x-230, y-80]
                    });
                
                dialog.dialog('open') 
            }
        });
    });
//    jQuery('.collinfo').live('hover', function(e) {
//        if(e.stopPropagation)
//            e.stopPropagation();
//        else
//            e.cancelBubble = true;
//        
//        if(e.type == 'mouseenter') {
//            jQuery('#collection_dialog')
//                .css("top",(e.pageY + 20) + "px")
//                .css("left",(e.pageX + 0) + "px")
//                .css("position",'absolute');
//            
//            if(jQuery('#collection_dialog').css('display') == 'none') {
//                
//                jQuery('#collection_dialog').empty();
//                jQuery.ajax({
//                    type: 'GET',
//                    url: 'index.php',
//                    data: "eID=backfrieder_ajax&collection=" + jQuery(this).attr('id') + "&colldetail=1&ts=" + (new Date()).getTime(),
//                    success: function(response) {
//                        jQuery('#collection_dialog').html(response);
//                    }
//                });
//                jQuery('#collection_dialog')
//                    .fadeIn("slow");
//            }        
//        }
//        else if(e.type == 'mouseleave') {
//            jQuery('#collection_dialog').fadeOut('fast');
//            jQuery('#collection_dialog').css('display', 'none');
//            jQuery('#collection_dialog').empty();
//        }
//    });
    
    jQuery('.collflip').bind("click", function(e) {
        e.preventDefault();
        var elem = jQuery(this);
                        
        if(elem.data('flipped')) {
            elem.revertFlip();

            elem.data('flipped',false)
        }
        else {
            elem.flip({
                direction:'lr',
                speed: 350,
                onBefore: function(){
                    elem.html(elem.next().html());
                },
                onEnd: function() {
                    elem.removeAttr('style');
                }
            });
            elem.data('flipped',true);
            
            var dialog = jQuery('#collection_dialog');
            jQuery('#collection_dialog').addClass('boxing');
            
            jQuery.ajax({
                type: 'GET',
                url: 'index.php',
                data: "eID=backfrieder_ajax&collection=" + jQuery(this).attr('id') + "&colldetail=1&ts=" + (new Date()).getTime(),
                success: function(response) {
                    jQuery('#collection_dialog').html(response);
                }

            });
            
            var x = document.body.clientWidth / 2;
            var y = document.body.clientHeight / 2;
            
            setTimeout(function() {
                dialog
                    .dialog({
                        title: '',
                        modal: true,
                        width: 630,
                        height: "auto",
                        position: [x-235, y-80]
                    });
                
                dialog.dialog('open') 
            }, 2000);
        } 
    });
    
    jQuery('.taglink').bind("click", function(e) {
        jQuery('.taglink').each(function() {
            jQuery(this).removeClass('tagactive');
        });
        
        jQuery(this).addClass('tagactive');
        
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&gettag=" + encodeURIComponent(jQuery(this).text()),
            success: function(response) {
                updateBasketTotal();
                if(response) {
                    jQuery('#weg3rechts').empty();
                    jQuery('#weg3rechts').html(response);
                }
            }
        });
    });
        
    function updateBasketTotal() {
        jQuery.ajax({
            type: 'GET',
            url: 'index.php',
            data: "eID=backfrieder_ajax&updateBasket=1" + "&ts=" + (new Date()).getTime(),
            success: function(response) {
                if (response) {
                    jQuery('#basketTotal').html(response);
                }
            }
        });
    };
});

