var beforeunload;

jQuery(document).ready(function() {
    // load translations
    translator = new translator();
    translator.init();

    // Use jQuery livequery plugin
    jQuery(".tx-rzconfigurator-pi3").livequery(function() {
        //stickyFloat();
        
        // Hide initially    
        jQuery(".rzconfigurator_configurator_options").hide();
        
        // Set height
        var leftHeight = jQuery(".rzconfigurator_configurator_selection_left").height();
        jQuery(".rzconfigurator_configurator_selection_right").css("height", leftHeight);
        
        // Click event optiongroup
        jQuery("a.optiongroup_single_link").click(function() {
            // Hide helps
            jQuery(".option_single_help").fadeOut("slow");
            jQuery("a.help_icon").removeData();

            // Get rel
            var rel = jQuery(this).attr("rel");
            
            // Get clicknum
            var clickNum = jQuery(this).data("clickNum");
            if (!clickNum) clickNum = 1;
            
            // Show/Hide options
            if(clickNum == 1) {
                jQuery(".rzconfigurator_configurator_options_"+rel).fadeIn("slow");
                
                // Add active class
                jQuery(this).addClass("optiongroup_single_link_active");
                
                jQuery.data(this, "clickNum", 2);
            }
            else {
                jQuery(".rzconfigurator_configurator_options_"+rel).fadeOut("slow");
                
                // Remove active class
                jQuery(this).removeClass("optiongroup_single_link_active");
                
                jQuery.data(this, "clickNum", 1);
            }
            
            setMaskHeight();
        });
        
        // Click event option
        jQuery("a.option_single_link").die().live("click", function() {
            beforeunload = 1;
            
            // Hide notice
            jQuery(".not_all_optiongroups").hide();
            jQuery("a.rzconfigurator_inquiry_link").data("clickNumEnquiry", "");
            
            // Activate mask
            var configuratorHeight = jQuery(".rzconfigurator_configurator_wrap").height();
            jQuery(".rzconfigurator_configurator_mask").show().css("height",configuratorHeight);
            
            // Get clicknum
            var clickNumOption = jQuery(this).data("clickNumOption");
            if (!clickNumOption) clickNumOption = 1;           
            
            // Get option text
            var optionText = jQuery(this).text();             

            // Get option rel
            var optionRel = jQuery(this).attr("rel");  

            // Get optiongroup rel
            var optiongroupRel = jQuery(this).parent().parent().parent().children("a").attr("rel");   
            
            // Get "not defined" text
            var notDefined = jQuery("span.not_defined").text();
            
            // First click on option
            if(clickNumOption == 1) {
                jQuery(this).addClass("option_single_link_active");
                
                jQuery.data(this, "clickNumOption", 2);                

                // If selection is empty
                if(jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').children("span").text() == '') {
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').addClass("active").removeClass("inactive");
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').empty().append('<span rel="'+ optionRel +'">' + optionText + '</span>');   
                }
                
                // If not empty add comma seperated list of options
                else {
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').append('<span>, </span><span rel="'+ optionRel +'">' + optionText + '</span>');
                }    
                
                // Make ajax request
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_options',
                    data: 'rel=' + optionRel,
                    type: 'POST',        
                    success: function(result) {                        
                        if(result != '') {
                            var json = jQuery.parseJSON(result);

                            jQuery.each(json, function(k, v) { 
                                jQuery('a.option_single_link[rel="'+ v +'"]').removeClass("option_single_link").removeClass("option_single_link_active").addClass("option_single_link_inactive").removeData(); 

                                // Get optiongroup
                                jQuery.ajax({
                                    url: '?eID=tx_rzconfigurator_options',
                                    data: 'option=' + v,
                                    type: 'POST',        
                                    success: function(optiongroup) {   
                                        if(jQuery('span[rel="'+ v +'"]').index() == 0 || jQuery('span[rel="'+ v +'"]').index() == 1) {
                                            jQuery('span[rel="'+ v +'"]').next().remove();
                                            jQuery('span[rel="'+ v +'"]').remove();
                                        }
                                        
                                        else {
                                            jQuery('span[rel="'+ v +'"]').prev().remove();
                                            jQuery('span[rel="'+ v +'"]').remove();  
                                        }

                                        if(jQuery('td.sel_second[rel="'+ optiongroup +'"]').has("span").length <= 0) {
                                            jQuery('td.sel_second[rel="'+ optiongroup +'"]').empty().append(notDefined);    
                                        }                                  
                                    }   
                                });                              
                            });
                        }   
                    }
                });   
                
                getRecommendedProducts();
            }
            
            // Second click on option
            else {
                jQuery(this).removeClass("option_single_link_active");
                
                jQuery.data(this, "clickNumOption", 1);                

                if(jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').has("span")) {                    
                    // If first element remove comma after
                    if(jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').children('span[rel="'+ optionRel +'"]').index() == 0) {
                        // Remove comma in selection
                        jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').children('span[rel="'+ optionRel +'"]').next().remove();  
                    }
                    
                    // Else remove comma before
                    else {
                        jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').children('span[rel="'+ optionRel +'"]').prev().remove();
                    }
                    
                    // Remove element in selection
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').children('span[rel="'+ optionRel +'"]').remove(); 
                }
                
                // If no more options, append "not defined" text again
                if(jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').has("span").length <= 0) {
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').removeClass("active").addClass("inactive");
                    jQuery('td.sel_second[rel="'+ optiongroupRel +'"]').empty().append(notDefined); 
                }
                
                // Get already excluded options
                var alreadyActive = new Array();
                jQuery("a.option_single_link_active").each(function(index) {
                    alreadyActive[index] = jQuery(this).attr("rel");
                });      
                
                // Make ajax request
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_options',
                    data: 'rel=' + optionRel + '&alreadyActive=' + alreadyActive,
                    type: 'POST',        
                    success: function(result) {                           
                        var json = jQuery.parseJSON(result);                        
                        
                        if(result == '') {
                            jQuery("a.option_single_link_inactive").each(function() {
                                jQuery(this).removeClass("option_single_link_inactive").addClass("option_single_link");
                            });
                        }                        
                        
                        else {
                            jQuery.each(json, function(k, v) {                             
                                jQuery('a.option_single_link_inactive[rel="'+ v +'"]').addClass("option_single_link").removeClass("option_single_link_inactive");
                            });
                        }                    
                    }   
                });   

                // If no more options are active set all back to normal
                if(jQuery("a.option_single_link_active").length <= 0) {
                    jQuery("a.option_single_link_inactive").each(function() {
                        jQuery(this).removeClass("option_single_link_inactive").addClass("option_single_link");
                    });
                }
                
                getRecommendedProducts();                
            }
        });
        
        // Create new bookmark (from selection)
        jQuery("a.rzconfigurator_bookmark_link").die().live("click", function(e) {  
            e.preventDefault();
             
            // Create configuration 
            var hasActiveClass = jQuery(this).parent().parent().prev().children("table").children("tbody").children("tr").children("td:last-child").hasClass("active");           
            
            if(hasActiveClass === false) {
                if(!jQuery(this).hasClass("rzconfigurator_bookmark_link_rec")) {
                    alert(translator.translate('alert_not_one_option'));
                }
                else {
                    var hasActiveClass = true;
                }
            }
                        
            if(hasActiveClass != false) {                   
                // Get rel
                var rel = jQuery(this).attr("rel");
                var rev = jQuery(this).attr("rev");

                // Remove previous active elements
                jQuery(".rzconfigurator_tabs_menue_right_new").children().removeClass("menue_active");

                // Active
                jQuery('.rzconfigurator_tabs_menue_link_configurator[rel="'+ rel +'"]').parent().addClass("menue_active");    

                var config = getSelection(rev,false,false,'');

                // Get configuration ID
                var configID = jQuery.data(document.body, "configID");   
                    
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_config',
                    data: {
                        config: config, 
                        configID: configID,
                        rev: rev
                    },
                    type: 'POST',      
                    success: function(result) {                      

                    }   
                });  

                // Empty content
                jQuery(".rzconfigurator_tabs_content_inner_content").empty();

                // Add loading icon
                jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");  

                // Clear timeout
                clearTimeout(timeout);           

                timeout = setTimeout( function() {     
                    jQuery.ajax({
                        url: '?eID=tx_rzconfigurator_tabs',
                        data: 'getContent=1&page='+ rel,
                        type: 'POST',        
                        success: function(result) {                      
                            // Append result
                            jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();
                            jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");

                            // CSS handling
                            jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel);

                            setMaskHeight();
                        }   
                    });
                },500);   
            }
        });
        
        // Delete bookmark
        jQuery("a.bookmark_delete").live("click", function(e) {
            e.stopImmediatePropagation();
            
            // Get bookmark id
            var bookmarkRel = jQuery(this).attr("rel");
            
            var deleteConfirm = confirm(translator.translate('confirm_delete_bookmark'));
            
            // Remove bookmark from DB
            if(deleteConfirm) {
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_bookmark',
                    data: 'bookmark='+ bookmarkRel,
                    type: 'POST',        
                    success: function(result) {  
                        // Hide and remove bookmark from DOM
                        jQuery('a.bookmark_delete[rel="'+bookmarkRel+'"]').parent().parent().parent().parent().fadeOut("slow", function() {
                            jQuery(this).remove();
                            if(result == 0) {
                                jQuery("span.no_bookmarks").fadeIn("slow");
                            }
                        });
                    }   
                });     
            }
        });
        
        // Comparison add link
        jQuery("a.comparison_link_add").die().live("click", function(e) {
            e.preventDefault();
            
            // Reference this for use in ajax request later
            $this = jQuery(this);
            
            // Check how many items are on the comparison list (not more than 4)
            jQuery.ajax({
                url: '?eID=tx_rzconfigurator_bookmark',
                data: 'comparison_check=1',
                type: 'POST',        
                success: function(resultCheck) {     
                    if(resultCheck == 4) {
                        alert(translator.translate('alert_not_more_than_four'));
                    }
                    else {
                        // Get bookmark id
                        var bookmarkRel = $this.attr("rel");
                        var rev = $this.attr("rev");

                        // Remove previous active elements
                        jQuery(".rzconfigurator_tabs_menue_right_new").children().removeClass("menue_active");

                        // Active
                        jQuery('.rzconfigurator_tabs_menue_link_configurator[rel="'+ rev +'"]').parent().addClass("menue_active");                       

                        // Empty content
                        jQuery(".rzconfigurator_tabs_content_inner_content").empty();

                        // Add loading icon
                        jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading"); 

                        // Add bookmark to comparison list
                        jQuery.ajax({
                            url: '?eID=tx_rzconfigurator_bookmark',
                            data: 'bookmark='+ bookmarkRel+'&comparison_add=1',
                            type: 'POST',        
                            success: function(result) {     
                                // Clear timeout
                                clearTimeout(timeout);           

                                timeout = setTimeout( function() {     
                                    jQuery.ajax({
                                        url: '?eID=tx_rzconfigurator_tabs',
                                        data: 'getContent=1&page='+ rev,
                                        type: 'POST',        
                                        success: function(result2) {                      
                                            // Append result
                                            jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result2).hide();
                                            jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");

                                            // CSS handling
                                            jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                                            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rev);

                                            setMaskHeight();
                                        }   
                                    });
                                },500);   
                            }   
                        });                                     
                    }
                }   
            });              
        });
        
        // Re-edit bookmark
        jQuery("a.bookmark_edit").live("click", function(e){
            e.stopImmediatePropagation();
            
            // Get rel (Configuration ID)
            var rel = jQuery(this).attr("rel");

            // Get rev (Productgroup ID)
            var rev = jQuery(this).attr("rev");
            
            // Overwrite productGroup
            jQuery.data(document.body, 'productGroup', rev);    
            
            // Save configuration ID in DOM to overwrite configuration later on
            jQuery.data(document.body, 'configID', rel);  
            
            var config = getSelection(rel,true,false,'');    
            
            // Remove previous active elements
            jQuery(".rzconfigurator_tabs_menue_right_new").children().removeClass("menue_active");

            // Active
            jQuery('.rzconfigurator_tabs_menue_link_configurator[rel="configurator_page"]').parent().addClass("menue_active");                       

            // Empty content
            jQuery(".rzconfigurator_tabs_content_inner_content").empty();

            // Add loading icon
            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");             
            
            // Get configurator
            getFirstContent(true,config);
        });
        
        // Inquiry button
        jQuery("a.rzconfigurator_inquiry_link").die().live("click", function(e) {  
            e.preventDefault();

            // Create configuration
            var hasClass = jQuery(this).parent().parent().parent().parent().prev().children("table").children("tbody").children("tr").children("td:last-child").hasClass("inactive"); 
            var hasActiveClass = jQuery(this).parent().parent().parent().parent().prev().children("table").children("tbody").children("tr").children("td:last-child").hasClass("active"); 
            var clickNumEnquiry = jQuery(this).data("clickNumEnquiry");            
            
            if(jQuery(this).hasClass("rzconfigurator_inquiry_link_rec")) {
                hasClass = false;
                hasActiveClass = true;
            }            
            
            if(hasActiveClass === false) {
                alert(translator.translate('alert_not_one_option'));
            }
            
            else if(hasClass === true && !clickNumEnquiry) { 
                // Set clickNum
                jQuery(this).data("clickNumEnquiry", "1");                 
                
                // Get position
                var position = jQuery(this).position();
                        
                var options = {
                    easing: 'easeOutExpo'
                };

                jQuery(".not_all_optiongroups").css({
                    "left": position.left+200,
                    "top": position.top+125                    
                }).effect("slide", options, 750);                
                
            }
            
            else {
                // Prevent multiple clicks
                if(jQuery(e.target).data('oneclicked')!='yes') {                 
                    // Get rel
                    var rel = jQuery(this).attr("rel"); 
                    var rev = jQuery(this).attr("rev");

                    // Get config
                    var config = getSelection(rev,false,false,'');               

                    // Get configuration ID
                    var configID = jQuery.data(document.body, "configID");   

                    /* Enquiry when logged-in */
                    if(rel == 100) {                      
                        jQuery.ajax({
                            url: '?eID=tx_rzconfigurator_config',
                            data: {
                                config: config, 
                                configID: configID,
                                rev: rev
                            },
                            type: 'POST',      
                            success: function(resultUid) {                      
                                // Empty content
                                jQuery(".rzconfigurator_tabs_content_inner_content").empty();

                                // Add loading icon
                                jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");

                                    // Clear timeout
                                    clearTimeout(timeout);           

                                    timeout = setTimeout( function() {     
                                        jQuery.ajax({
                                            url: '?eID=tx_rzconfigurator_tabs',
                                            data: 'getContent=1&page='+ rel,
                                            type: 'POST',        
                                            success: function(result) {   
                                                // Append result
                                                jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();

                                                // Hide infobox
                                                jQuery(".tx-rzconfigurator-pi6").hide();
                                                jQuery(".tx-rzconfigurator-pi6").before('<div class="tx-rzconfigurator-pi6-loading"></div>');

                                                // Send enquiry eMail
                                                jQuery.ajax({
                                                    url: '?eID=tx_rzconfigurator_email',
                                                    data: {
                                                        lang: lang,
                                                        resultUid: resultUid                                 
                                                    },
                                                    type: 'POST',        
                                                    success: function(resultMail) { 
                                                        // Get content
                                                        var result = jQuery(".rzconfigurator_tabs_content_inner_content").html();

                                                        // Replace process_numer marker
                                                        var newResult = str_replace("###VORGANGSNUMMER###",resultMail,result);

                                                        // Ajax request infobox
                                                        var url = window.location.href;
                                                        var type = '?type=8002';

                                                        // If there is at least one GET parameter
                                                        url_with_params = url.match(/tx_rzconfigurator_pi1/); 
                                                        url_with_index = url.match(/index.php/); 
                                                        url_with_id = url.match(/id/); 
                                                        if(url_with_params || url_with_index || url_with_id) {
                                                            type = '&type=8002'; 
                                                        }    

                                                        // Make ajax request
                                                        jQuery.ajax({
                                                            url: url + type,
                                                            data: config,
                                                            type: 'POST',        
                                                            success: function(resultInfobox) {     
                                                                jQuery(".tx-rzconfigurator-pi6-loading").hide().remove();
                                                                jQuery(".tx-rzconfigurator-pi6").empty().append().html(resultInfobox).hide().fadeIn("slow");
                                                            }   
                                                        });                                                           

                                                        // Empty container and append new content with process number and fade in
                                                        jQuery(".rzconfigurator_tabs_content_inner_content").empty().append().html(newResult).hide();
                                                        jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");
                                                    }   
                                                });                        

                                                // CSS handling
                                                jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                                                jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel);

                                                setMaskHeight();
                                            }   
                                        });
                                    },500);   
                            }   
                        }); 
                    }

                    /* Enquiry when NOT logged-in */
                    else {
                        // Save temp config when not logged in or registered
                        jQuery.data(document.body, 'tempConfig', config);
                        jQuery.data(document.body, 'tempProduct', rev);

                        clearTimeout(timeout);           

                        timeout = setTimeout( function() {     
                            jQuery.ajax({
                                url: '?eID=tx_rzconfigurator_tabs',
                                data: 'getContent=1&page='+ rel,
                                type: 'POST',        
                                success: function(result) {                      
                                    // Append result
                                    jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();   

                                    // Hide infobox
                                    jQuery(".tx-rzconfigurator-pi6").hide();
                                    jQuery(".tx-rzconfigurator-pi6").before('<div class="tx-rzconfigurator-pi6-loading"></div>');

                                    // Ajax request infobox
                                    var url = window.location.href;
                                    var type = '?type=8002';

                                    // If there is at least one GET parameter
                                    url_with_params = url.match(/tx_rzconfigurator_pi1/); 
                                    url_with_index = url.match(/index.php/); 
                                    url_with_id = url.match(/id/); 
                                    if(url_with_params || url_with_index || url_with_id) {
                                        type = '&type=8002'; 
                                    }    

                                    // Make ajax request
                                    jQuery.ajax({
                                        url: url + type,
                                        data: config,
                                        type: 'POST',        
                                        success: function(resultInfobox) {     
                                            jQuery(".tx-rzconfigurator-pi6-loading").hide().remove();
                                            jQuery(".tx-rzconfigurator-pi6").empty().append().html(resultInfobox).hide().fadeIn("slow");
                                        }   
                                    });                                  

                                    // Fade-in content
                                    jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");

                                    // Live-ready custom event for user registration
                                    jQuery.liveReady("#tx-srfeuserregister-pi1-fe_users_form fieldset", function () {
                                        jQuery(this).children("dl").children("dt").children("label").children("span").each(function() {
                                            jQuery(this).parent().parent().next().children().addClass("rzconfigurator-required-field");                  
                                        }); 
                                    });

                                    // CSS handling
                                    jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                                    jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel);

                                    setMaskHeight();
                                }
                            });
                        },500);                    
                    }
                    jQuery(e.target).data('oneclicked','yes');
                }
            }
        });
        
        // Enquiry Link Bookmark
        jQuery("a.rzconfigurator_inquiry_link_bookmark").die().live("click", function(e) {  
            e.preventDefault();

            // Get rel and rev
            var rel = jQuery(this).attr("rel");
            var rev = jQuery(this).attr("rev");
            
            // Get config
            var config = getSelection(rev,true,false,'');   

            // Empty content
            jQuery(".rzconfigurator_tabs_content_inner_content").empty();

            // Add loading icon
            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");

            timeout = setTimeout( function() {     
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_tabs',
                    data: 'getContent=1&page='+ rel,
                    type: 'POST',        
                    success: function(result) {                           
                        // Append result
                        jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();

                        // Hide infobox
                        jQuery(".tx-rzconfigurator-pi6").hide();
                        jQuery(".tx-rzconfigurator-pi6").before('<div class="tx-rzconfigurator-pi6-loading"></div>');

                        // Send enquiry eMail
                        jQuery.ajax({
                            url: '?eID=tx_rzconfigurator_email',
                            data: {
                                lang: lang,
                                resultUid: rev                                 
                            },
                            type: 'POST',        
                            success: function(resultMail) { 
                                // Get content
                                var result = jQuery(".rzconfigurator_tabs_content_inner_content").html();

                                // Replace process_numer marker
                                var newResult = str_replace("###VORGANGSNUMMER###",resultMail,result);

                                // Ajax request infobox
                                var url = window.location.href;
                                var type = '?type=8002';

                                // If there is at least one GET parameter
                                url_with_params = url.match(/tx_rzconfigurator_pi1/); 
                                url_with_index = url.match(/index.php/); 
                                url_with_id = url.match(/id/); 
                                if(url_with_params || url_with_index || url_with_id) {
                                    type = '&type=8002'; 
                                }    

                                // Make ajax request
                                jQuery.ajax({
                                    url: url + type,
                                    data: config,
                                    type: 'POST',        
                                    success: function(resultInfobox) {     
                                        jQuery(".tx-rzconfigurator-pi6-loading").hide().remove();
                                        jQuery(".tx-rzconfigurator-pi6").empty().append().html(resultInfobox).hide().fadeIn("slow");
                                    }   
                                });                                                           

                                // Empty container and append new content with process number and fade in
                                jQuery(".rzconfigurator_tabs_content_inner_content").empty().append().html(newResult).hide();
                                jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");
                            }   
                        });                       

                        // CSS handling
                        jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                        jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel);

                        setMaskHeight();
                    }   
                });
            },500);   
        });
        
        // Comparison delete link
        jQuery("a.comparison_delete").live("click", function(e) {
            e.stopImmediatePropagation();
            
            // Get bookmark id
            var comparisonRel = jQuery(this).attr("rel");
            
            var deleteConfirm = confirm(translator.translate('confirm_delete_comparison'));
            
            // Remove bookmark from DB
            if(deleteConfirm) {
                var elTr = jQuery(this).parent().parent().parent();
                var elPosition = jQuery(this).parent().parent().index();
                
                // Delete from DB
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_comparison',
                    data: 'comparison='+ comparisonRel,
                    type: 'POST',        
                    success: function(result) {  

                    }   
                });                  
                
                if(jQuery(this).parent().parent().parent().children().length != 2) {               
                    // Fade out and remove entire column
                    jQuery.each(elTr.prevAll(), function(index) {  
                        if(index == 0) {
                            // Fate out "delete"-link
                            jQuery('a.comparison_delete[rel="'+comparisonRel+'"]').parent().parent().delay(50).fadeOut("slow", function() {
                                jQuery(this).remove();
                            });                        
                        }
                        jQuery(this).children().eq(elPosition).delay(50).fadeOut("slow", function() {
                            jQuery(this).remove();
                        });
                    });    
                }
                else {
                    jQuery("table.rzconfigurator_comparison_table").delay(50).fadeOut("slow", function() {
                        jQuery(this).remove();
                    });
                }
            }
        });
        
        // Comparison enquiry link
        jQuery("a.rzconfigurator_inquiry_link_comparison").die().live("click", function(e) {  
            e.preventDefault();
            
            // Get rel and rev and position
            var rel = jQuery(this).attr("rel");
            var rev = jQuery(this).attr("rev");
            var elPosition = jQuery(this).parent().parent().parent().parent().index();

            // Get config
            var config = getSelection(rev,false,true,elPosition);  

            // Empty content
            jQuery(".rzconfigurator_tabs_content_inner_content").empty();

            // Add loading icon
            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");
            
            timeout = setTimeout( function() {     
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_tabs',
                    data: 'getContent=1&page='+ rel,
                    type: 'POST',        
                    success: function(result) {                           
                        // Append result
                        jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();

                        // Hide infobox
                        jQuery(".tx-rzconfigurator-pi6").hide();
                        jQuery(".tx-rzconfigurator-pi6").before('<div class="tx-rzconfigurator-pi6-loading"></div>');

                        // Send enquiry eMail
                        jQuery.ajax({
                            url: '?eID=tx_rzconfigurator_email',
                            data: {
                                lang: lang,
                                resultUid: rev                                 
                            },
                            type: 'POST',        
                            success: function(resultMail) { 
                                // Get content
                                var result = jQuery(".rzconfigurator_tabs_content_inner_content").html();

                                // Replace process_numer marker
                                var newResult = str_replace("###VORGANGSNUMMER###",resultMail,result);

                                // Ajax request infobox
                                var url = window.location.href;
                                var type = '?type=8002';

                                // If there is at least one GET parameter
                                url_with_params = url.match(/tx_rzconfigurator_pi1/); 
                                url_with_index = url.match(/index.php/); 
                                url_with_id = url.match(/id/); 
                                if(url_with_params || url_with_index || url_with_id) {
                                    type = '&type=8002'; 
                                }    

                                // Make ajax request
                                jQuery.ajax({
                                    url: url + type,
                                    data: config,
                                    type: 'POST',        
                                    success: function(resultInfobox) {     
                                        jQuery(".tx-rzconfigurator-pi6-loading").hide().remove();
                                        jQuery(".tx-rzconfigurator-pi6").empty().append().html(resultInfobox).hide().fadeIn("slow");
                                    }   
                                });                                                           

                                // Empty container and append new content with process number and fade in
                                jQuery(".rzconfigurator_tabs_content_inner_content").empty().append().html(newResult).hide();
                                jQuery(".rzconfigurator_tabs_content_inner_content").fadeIn("slow");
                            }   
                        });                       

                        // CSS handling
                        jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                        jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel);

                        setMaskHeight();
                    }   
                });
            },500);
        });
        
        // Edit profile link
        jQuery("a.edit_profile").die().live("click",function(e) {
            e.preventDefault();

            // Remove previous active elements
            jQuery(".rzconfigurator_tabs_menue_right_new").children().removeClass("menue_active");           
            
            // Get rel
            var rel = jQuery(this).attr("rel");

            // Empty content
            jQuery(".rzconfigurator_tabs_content_inner_content").empty();

            // Add loading icon
            jQuery(".rzconfigurator_tabs_content_inner_content").addClass("loading");     
            
            timeout = setTimeout( function() {     
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_tabs',
                    data: 'getContent=1&page='+ rel,
                    type: 'POST',        
                    success: function(result) {                           
                        // Append result
                        jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").append().html(result).hide();                   

                        // CSS handling
                        jQuery(".rzconfigurator_tabs_content_inner_content").attr("class",'').addClass("rzconfigurator_tabs_content_inner_content");
                        jQuery(".rzconfigurator_tabs_content_inner_content").addClass("content_inner_" + rel).fadeIn("slow");

                        setMaskHeight();
                    }   
                });
            },500);       
            
            jQuery.liveReady(".rzconfigurator_edit_form_wrap fieldset", function () {
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_userdata',
                    data: '',
                    type: 'POST',        
                    success: function(result) {             
                        // Get user data
                        var test = jQuery.parseJSON(result);
                        debug(test);

                        // Get form fields
                        debug(jQuery(".rzconfigurator_edit_form_wrap").children("fieldset").children("dl").children("dd").children());
                    }   
                });  
            });
        });
        
        // Edit profile submit
        jQuery("input.rzconfigurator_profile_edit_submit").live("click", function(event) {
            event.preventDefault();
            event.stopImmediatePropagation();           
            
            alert("bra");
        });
        
        // Click event help icon
        jQuery("a.help_icon").click(function(event) {
            // Hide other helps
            jQuery(".option_single_help").fadeOut("slow");
            //jQuery("a.help_icon").removeData();
            
            // Get rel
            var relHelp = jQuery(this).attr("rel");
            
            // Get clicknum
            var clickNumHelp = jQuery(this).data("clickNumHelp");
            if (!clickNumHelp) clickNumHelp = 1; 
            
            // Get position
            var position = jQuery(this).position();
            
            if(clickNumHelp == 1) {                            
                var options = {
                    easing: 'easeOutExpo'
                };
                
                jQuery(".option_single_help_"+relHelp).css({
                    "left": position.left+35,
                    "top": position.top-39                    
                }).effect("slide", options, 750);
                
                jQuery.data(this, "clickNumHelp", 2);
            }
            
            else {
                jQuery(".option_single_help_"+relHelp).fadeOut("slow");
                
                jQuery.data(this, "clickNumHelp", 1);
            }
        });
        
        // Click event close icon help
        jQuery(".close_icon").click(function() {
            jQuery(this).parent().fadeOut("slow");
                        
            // Get rel
            var relCloseHelp = jQuery(this).attr("rel");
                        
            jQuery("a.help_icon[rel=close_icon_"+relCloseHelp+"]").removeData();
        });
    }); 
    
    // Browser back or reload event (not IE)
    if(!jQuery.browser.msie) {
        jQuery(window).bind("beforeunload",function(event) {
            if(beforeunload == 1) {
                var e = e || window.event;

                // For Firefox prior to version 4
                if (e) {
                    e.returnValue = translator.translate('leave_page');
                }

                // For Safari
                return translator.translate('leave_page');
            }
        });  
    }
    
    // Get selection
    function getSelection(rev,editmode,comparisonMode,comparisonIndex) {              
        // Selection
        if(rev == 0) {
            // Get data
            var selectionData = jQuery("table.rzconfigurator_configurator_selection_table").children("tbody").children("tr");
        }
        
        else if(editmode === true) {
            var selectionData = jQuery("table.rzconfigurator_configurator_bookmarks_table_"+rev).children("tbody").children("tr");
        }
        
        // Enquiry from comparison list
        else if(comparisonMode === true) {
            var selectionData = jQuery("table.rzconfigurator_comparison_table").children("tbody").children("tr");
        }
        
        // Recommended products
        else {
            var selectionData = jQuery("table.rzconfigurator_configurator_recommended_table_"+rev).children("tbody").children("tr");
        }

        config = new Object();
        config['productGroup'] = jQuery.data(document.body, "productGroup");

        // Comparison mode getSelection
        if(comparisonMode === true) {
            jQuery.each(selectionData, function(index) {    
                var attr = jQuery(this).children().eq(comparisonIndex).attr('rel');
                if (typeof attr !== 'undefined' && attr !== false) {                 
                    optionGroup = jQuery(this).children().eq(comparisonIndex).attr("rel");
                    var option = jQuery(this).children().eq(comparisonIndex).children("span");

                    config[index] = new Object(); 
                    config[index]["group_uid"] = optionGroup;
                    config[index]["options"] = new Array();               

                    jQuery.each(option, function(indexOption) {   
                        if(jQuery(this).attr("rel") == undefined) {
                            config[index]["options"][indexOption] = '';
                        }
                        else {
                            config[index]["options"][indexOption] = jQuery(this).attr("rel");
                        }
                    });

                    removeByElement(config[index]["options"],"");
                }
            });              
        }
        else {
            jQuery.each(selectionData, function(index) {                                            
                optionGroup = jQuery(this).children("td:nth-child(2)").attr("rel");
                var option = jQuery(this).children("td:nth-child(2)").children("span");

                config[index] = new Object(); 
                config[index]["group_uid"] = optionGroup;
                config[index]["options"] = new Array();               

                jQuery.each(option, function(indexOption) {   
                    if(jQuery(this).attr("rel") == undefined) {
                        config[index]["options"][indexOption] = '';
                    }
                    else {
                        config[index]["options"][indexOption] = jQuery(this).attr("rel");
                    }
                });

                removeByElement(config[index]["options"],"");
            });    
        }
        
        return config;
    }
    
    // Get recommended products
    function getRecommendedProducts() {        
        // Get recommended products
        var url = window.location.href;
        var type = '?type=8001';

        // If there is at least one GET parameter
        url_with_params = url.match(/tx_rzconfigurator_pi1/); 
        url_with_index = url.match(/index.php/); 
        url_with_id = url.match(/id/); 
        if(url_with_params || url_with_index || url_with_id) {
            type = '&type=8001'; 
        }                   

        // Get config
        var config = getSelection('',false,false,''); 

        // Make ajax request
        jQuery.ajax({
            url: url + type,
            data: config,
            type: 'POST',        
            success: function(resultContent) {                        
                // Append result (empty first)
                jQuery(".rzconfigurator_configurator_recommended").hide().empty().append(resultContent);
                
                // If edit-mode, change labels of recommended products
                if(jQuery.data(document.body, "configID")) {
                    jQuery(".rzconfigurator_bookmark_link").empty().append(translator.translate('save'));
                    jQuery(".rzconfigurator_configurator_recommended").fadeIn("slow");
                }
                
                else {
                    jQuery(".rzconfigurator_configurator_recommended").fadeIn("slow");
                }
                
                // Set height
                var leftHeight = jQuery(".rzconfigurator_configurator_recommended_left").height();                
                jQuery(".rzconfigurator_configurator_recommended_right").css("height", leftHeight+30);                   
                
                // Hide mask                        
                jQuery(".rzconfigurator_configurator_mask").hide();               
                
                setMaskHeight();
            }   
        });           
    }
});

function bookmarkEdit(config) {    
    // Do stuff after configurator fade in from bookmarks (bookmark edit mode)
    
    // Change labels
    jQuery(".rzconfigurator_configurator_selection").children("h1").empty().append(translator.translate('your_selection')+ ' ' + jQuery.data(document.body, "configID"));
    jQuery(".rzconfigurator_bookmark_link").empty().append(translator.translate('save'));
    
    jQuery.ajax({
        url: '?eID=tx_rzconfigurator_editBookmark',
        data: config,
        type: 'POST',        
        success: function(result) {                                    
            var json = jQuery.parseJSON(result);
            
            jQuery.each(json, function(k, v) {           
                // Make active
                jQuery('a.option_single_link[rel="'+ v +'"]').addClass("option_single_link_active").show(100, function() {
                    // Set clickNum to "2" (as if it was clicked once already)
                    jQuery.data(this, "clickNumOption", 2); 
                });   
                
                // Fade in optiongroups with active options
                jQuery('a.option_single_link[rel="'+ v +'"]').parent().parent().fadeIn("slow");
                
                // Get option text
                var optionText = jQuery('a.option_single_link[rel="'+ v +'"]').text();
                
                optiongroupRel = new Object();                    
                optiongroupRel[v] = jQuery('a.option_single_link[rel="'+ v +'"]').parent().parent().prev().attr("rel");
                              
                // Add active options to selection              
                jQuery.each(optiongroupRel, function(optiongroupRelKey, optiongroupRelVal) {    
                    // Add active
                    jQuery('td.sel_second[rel="'+ optiongroupRelVal +'"]').addClass("active").removeClass("inactive");
                    
                    // If selection is empty
                    if(jQuery('td.sel_second[rel="'+ optiongroupRelVal +'"]').children("span").text() == '') {
                        jQuery('td.sel_second[rel="'+ optiongroupRelVal +'"]').empty().append('<span rel="'+ v +'">' + optionText + '</span>');   
                    }

                    // If not empty add comma seperated list of options
                    else {
                        jQuery('td.sel_second[rel="'+ optiongroupRelVal +'"]').append('<span>, </span><span rel="'+ v +'">' + optionText + '</span>');
                    }                      
                });
                
                // Get exclude options
                jQuery.ajax({
                    url: '?eID=tx_rzconfigurator_options',
                    data: 'rel=' + v,
                    type: 'POST',        
                    success: function(result2) {                         
                        var json2 = jQuery.parseJSON(result2);
                        
                        // Make exclude options inactive
                        jQuery.each(json2, function(k2, v2) {
                            jQuery('a.option_single_link[rel="'+ v2 +'"]').removeClass("option_single_link").removeClass("option_single_link_active").addClass("option_single_link_inactive");                            
                        });
                        
                        jQuery(".rzconfigurator_tabs_content_inner_content").removeClass("loading").fadeIn("slow"); 
                        
                        setMaskHeight();
                    }
                }); 
            });  
        }
    });
}

function debug(mixed) {
    if(window.console&&window.console.debug)window.console.debug(mixed)
}

function stickyFloat() {
    // Scroll function         
    jQuery(".rzconfigurator_configurator_right").containedStickyScroll({
        closeChar: '',
        unstick: false,
        easing: 'easeOutQuart',
        duration: 1500
    });   
}

/* Helper functions BEGIN */
    
function removeByElement(arrayName,arrayElement)
{
    for(var i=0; i<arrayName.length;i++ )
    { 
        if(arrayName[i]==arrayElement)
            arrayName.splice(i,1); 
    } 
}    

function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}
    
/* Helper functions END */
