var article_tabs;
var product_tabs;
var info_tabs;
var article_tabs_rotate = true;
var product_tabs_rotate = true;

window.enable_info = true;
var info_tabs_rotate = true;

var current_tabs = 'article';
var rotationSpeed = {
    article : 5500,
    info : 2345,
    product : 3000,
    tabs : 250
};

/**
 * Add behaviour on window load
 */

function toggleCategoryBlock(id) {

    if (!$('#category_block_container_' + id).is(':visible')) {
        $('#toggle_category_block_' + id).addClass('toggle_category_block_opened');
    } else {
        $('#toggle_category_block_' + id).removeClass(
            'toggle_category_block_opened');
    }

    if ($.browser.msie) {
        $('#category_block_container_' + id).toggle();
    } else {
        $('#category_block_container_' + id).toggle('slow');
    }
}

function hideExtendedBox(id, delay) {
    delay = delay ? delay : 500;
    if (boxTimersS[id]) {
        clearTimeout(boxTimersS[id]);
        boxTimersS[id] = null;
    }

    if (!boxTimersH[id]) {
        boxTimersH[id] = setTimeout(function() {
            if ($('#subcategories_' + id) && $('#subcategories_' + id).length > 0
                && $('#catopt_' + id) && $('#catopt_' + id).length > 0) {
                $('#subcategories_' + id).slideUp('fast', function() {
                    $('#catopt_' + id).hide();
                    if ($('#catpreview_' + id) && $('#catpreview_' + id).length > 0) {
                        $('#catpreview_' + id).show();
                        if ($('#catimages_' + id).hasClass('cat_preview_hidden')) {
                            $('#catimages_' + id).removeClass('cat_preview_hidden');
                        }
                    }
                });
            }

            boxTimersH[id] = null;
        }, delay);
    }
// }
}

function showExtendedBox(id, delay) {
    if (!window.location.href.match(/icpdas/)) {
        delay = delay ? delay : 600;
        if (!boxTimersS[id]) {
            boxTimersS[id] = setTimeout(function() {
                if ($('#subcategories_' + id) && $('#subcategories_' + id).length > 0
                    && $('#catopt_' + id) && $('#catopt_' + id).length > 0) {
                    if (window.location.href.match(/.*\/ru\/.*/)) {
                        $('#subcategories_' + id).show();
                        $('#subcategories_' + id)
                        .css(
                            'height',
                            ($('#subcategories_' + id + ' .subcategories:first')
                                .height() + 20)
                            + 'px');
                    } else {
                        $('#subcategories_' + id).slideDown('fast');
                    }

                    if ($('#catpreview_' + id) && $('#catpreview_' + id).length > 0) {
                        $('#catpreview_' + id).hide();
                        $('#catimages_' + id).addClass('cat_preview_hidden');
                    }
                    $('#catopt_' + id).show();
                }
                boxTimersS[id] = null;
            }, delay);
        }

        if (boxTimersH[id]) {
            clearTimeout(boxTimersH[id]);
            boxTimersH[id] = null;
        }
    }
}

/**
 * 
 */
function scaleIEImages(image, max_dimension, force) {
    if (jQuery.browser.msie || (typeof force != 'undefined' && force == true)) {
        image = jQuery(image);
        if (image && image.length > 0) {
            jQuery.each(image, function(index, value) {
                var old_width = jQuery(value).width();
                var old_height = jQuery(value).height();

                if (old_height > old_width) {
                    var new_width = parseInt(old_width)
                    * (parseInt(max_dimension) / parseInt(old_height));

                    jQuery(value).css('height', max_dimension + 'px');
                    jQuery(value).css('width', new_width + 'px');
                } else if (old_height < old_width) {
                    var new_height = parseInt(old_height)
                    * (parseInt(max_dimension) / parseInt(old_width));
                    jQuery(value).css('width', max_dimension + 'px');
                    jQuery(value).css('height', new_height + 'px');
                }
            });
        }
    }
}

function categoryListSetup() {
    scaleIEImages('.catimg_container td img', 33);
    scaleIEImages('.catimg_container img.cat_own_image', 65);
  
    if ($('.category_title_header') && $('.category_title_header').length > 0) {
        $('.category_title_header').unbind();
        $('.category_title_header').hover(function() {
            var _this = $(this);
            if (_this && _this.length > 0) {
                var id = _this.attr('id').replace(/.+_(.+)/gi, '$1');
                jQuery('#toggle_category_block_'+id).addClass('wait_for_toggle_category_block');
                if ($.browser.msie) {
                    if (!boxTimersS[id]) {
                        boxTimersS[id] = setTimeout(function() {
                            $('#category_block_container_' + id).show();
                            boxTimersS[id] = null;
                            jQuery('#toggle_category_block_'+id).addClass('toggle_category_block_opened');
                        }, 930);
                    }
                } else {
                    if (!boxTimersS[id]) {
                        boxTimersS[id] = setTimeout(function() {
                            $('#category_block_container_' + id).show('slow');
                            boxTimersS[id] = null;
                            jQuery('#toggle_category_block_'+id).addClass('toggle_category_block_opened');
                        }, 930);
                    }
                }
            }
        }, function() {
            var _this = $(this);
            if (_this && _this.length > 0) {
                var id = _this.attr('id').replace(/.+_(.+)/gi, '$1');
                jQuery('#toggle_category_block_'+id).removeClass('wait_for_toggle_category_block');
        
                if (boxTimersS[id]) {
                    clearTimeout(boxTimersS[id]);
                    boxTimersS[id] = null;
                }
            }
        });
    }
}

function homepageTabsSetup() {
    if(!window.jQuery.ui)
        return;
  
    if(!window.jQuery.ui.tabs)
        return;
  
    // Configure article tabs
    if ($('#articletabs_container') && ($('#articletabs_container').length > 0)) {
        article_tabs = $('#articletabs_container').tabs( {
            fx : {
                opacity : 'show',
                duration : rotationSpeed.tabs
            },
            event : 'mouseover'
        });

        article_tabs.bind('tabsshow', function() {
            if (article_tabs.tabs('option', 'selected') == (article_tabs
                .tabs('length') - 1)) {
                if (info_tabs_rotate) {
                    product_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', rotationSpeed.info);
                    current_tabs = 'info';
                } else if (product_tabs_rotate) {
                    product_tabs.tabs('rotate', rotationSpeed.product);
                    article_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', 0);
                    current_tabs = 'product';
                } else if (article_tabs_rotate) {
                    product_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', rotationSpeed.article);
                    info_tabs.tabs('rotate', 0);
                    current_tabs = 'article';
                } else {
                    product_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', 0);
                    current_tabs = '';
                }
            }
        });

        // Trzebaby pozbyć się mousemove, żeby nie zajmować js
        // niepotrzebnie
        article_tabs.mousemove(function() {
            article_tabs.tabs('rotate', 0);
            product_tabs.tabs('rotate', 0);
            info_tabs.tabs('rotate', 0);
        });

        article_tabs
        .mouseleave(function() {
            setTimeout(
                'if(article_tabs_rotate == true){'
                + 'if(article_tabs && article_tabs.length>0){article_tabs.tabs("rotate", rotationSpeed.product)};'
                + 'if(product_tabs && product_tabs.length>0){product_tabs.tabs("rotate", 0);}'
                + 'if(info_tabs && info_tabs.length>0){info_tabs.tabs("rotate", 0);}'
                + '}', rotationSpeed.product);
        });

        // Article tabs configuration end
        if (typeof enable_info != 'undefined' && $('#infodiv_tabs').length > 0) {
            // Configure info tabs
            info_tabs = $('#infodiv_tabs').tabs( {
                fx : {
                    opacity : 'show',
                    duration : rotationSpeed.tabs
                },
                event : 'mouseover'
            });

            info_tabs.bind('tabsshow',
                function() {
                    if (info_tabs.tabs('option', 'selected') == (info_tabs
                        .tabs('length') - 1)) {
                        if (product_tabs_rotate == true) {
                            article_tabs.tabs('rotate', 0);
                            info_tabs.tabs('rotate', 0);
                            product_tabs.tabs('rotate', rotationSpeed.product);
                            current_tabs = 'product';
                        } else if (article_tabs_rotate == true) {
                            article_tabs.tabs('rotate', rotationSpeed.article);
                            info_tabs.tabs('rotate', 0);
                            product_tabs.tabs('rotate', 0);
                            current_tabs = 'article';
                        } else if (info_tabs_rotate == true) {
                            article_tabs.tabs('rotate', 0);
                            info_tabs.tabs('rotate', rotationSpeed.info);
                            product_tabs.tabs('rotate', 0);
                            current_tabs = 'info';
                        } else {
                            article_tabs.tabs('rotate', 0);
                            info_tabs.tabs('rotate', 0);
                            product_tabs.tabs('rotate', 0);
                            current_tabs = '';
                        }
                    }
                });

            // Trzebaby pozbyć się mousemove, żeby nie zajmować js
            // niepotrzebnie
            info_tabs.mousemove(function() {
                article_tabs.tabs('rotate', 0);
                product_tabs.tabs('rotate', 0);
                info_tabs.tabs('rotate', 0);
            });

            info_tabs
            .mouseleave(function() {
                setTimeout(
                    'if(info_tabs_rotate==true){if(article_tabs && article_tabs.length>0){article_tabs.tabs("rotate", 0)};'
                    + 'if(product_tabs && product_tabs.length>0){product_tabs.tabs("rotate", 0);}'
                    + 'if(info_tabs && info_tabs.length>0){info_tabs.tabs("rotate", rotationSpeed.info);}}',
                    rotationSpeed.product);
            });
        }
    }

    // Info tabs configuration end
    if ($('#product_tabs')) {
        // Product tabs configuration
        product_tabs = $('#product_tabs').tabs( {
            fx : {
                opacity : 'show',
                duration : rotationSpeed.tabs
            },
            event : 'mouseover'
        });
        product_tabs.bind('tabsshow', function() {
            if (product_tabs.tabs('option', 'selected') == (product_tabs
                .tabs('length') - 1)) {
                if (article_tabs_rotate) {
                    product_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', rotationSpeed.article);
                    current_tabs = 'article';
                } else if (info_tabs_rotate) {
                    product_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', rotationSpeed.info);
                    article_tabs.tabs('rotate', 0);
                    current_tabs = 'info';
                } else if (product_tabs_rotate) {
                    product_tabs.tabs('rotate', rotationSpeed.product);
                    info_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', 0);
                    current_tabs = 'product';
                } else {
                    product_tabs.tabs('rotate', 0);
                    article_tabs.tabs('rotate', 0);
                    info_tabs.tabs('rotate', 0);
                    current_tabs = '';
                }
            }
        });

        // Trzebaby pozbyć się mousemove, Şeby nie zajmować js
        // niepotrzebnie
        product_tabs.mousemove(function() {
            product_tabs.tabs('rotate', 0);
            article_tabs.tabs('rotate', 0);
            info_tabs.tabs('rotate', 0);
        });
        product_tabs
        .mouseleave(function() {
            setTimeout(
                'if(current_tabs=="product"){if(article_tabs && article_tabs.length>0){article_tabs.tabs("rotate", 0);}'
                + 'if(product_tabs && product_tabs.length>0){product_tabs.tabs("rotate", rotationSpeed.product);}'
                + 'if(info_tabs && info_tabs.length>0){info_tabs.tabs("rotate", 0);}}',
                rotationSpeed.product);
        });
    // Product tabs configuration end
    }
      
    if ( $('#articletabs li a') && $('#articletabs li a').length > 0) {
        $('#articletabs li a')
        .mouseenter(
            function(event) {
                if (current_tabs == 'article') {
                    article_tabs_rotate = false;
                    article_tabs.tabs('rotate', 0);
                    setTimeout(
                        'if(info_tabs && info_tabs.length>0){info_tabs.tabs("rotate", rotationSpeed.info);}'
                        + 'current_tabs="info"', rotationSpeed.product);
                }
            });
    }

    if ($('#infotabs li a') && $('#infotabs li a').length > 0) {
        $('#infotabs li a')
        .mouseenter(
            function(event) {
                if (current_tabs == 'info') {
                    info_tabs_rotate = false;
                    info_tabs.tabs('rotate', 0);
                    setTimeout(
                        'if(product_tabs && product_tabs.length>0){product_tabs.tabs("rotate", rotationSpeed.product);}'
                        + 'current_tabs="product"', rotationSpeed.product);
                }
            });
    }

    if ( $('#producttabs li a') && $('#producttabs li a').length > 0) {
        $('#producttabs li a')
        .mouseenter(
            function(event) {
                if (current_tabs == 'product') {
                    product_tabs_rotate = false;
                    product_tabs.tabs('rotate', 0);
                    setTimeout(
                        'if(article_tabs && article_tabs.length>0){article_tabs.tabs("rotate", rotationSpeed.article);}'
                        + 'current_tabs="article"', rotationSpeed.product);
                }
            });
    }

    if (article_tabs && article_tabs.length > 0) {
        article_tabs.tabs('rotate', rotationSpeed.article);
    }
}

if ($(document)) {
    var cat;
    var boxTimersS = new Array();
    var boxTimersH = new Array();

    $(document)
        .ready(
            function() {
        
                if( $('#more_search_options')  )
                {
                    $('#more_search_options').click(function(event){
                        $('#more_search_options_div').toggle();
                        $('.search_center').toggleClass('sc_more_search_options');
                        $(this).toggleClass('more_search_options_visible');
                        event.stopPropagation();
                        return false;
                    });
                }

                categoryListSetup();
                homepageTabsSetup();

                var country_select = $('#registration_country_select');
                if (country_select && country_select.length > 0) {
                    country_select
                    .change(function() {
                        if (window.location.href.match(/^.*\/en\/.*$/)
                            && country_select.val() == '170') {
                            if (!confirm("We have distinct services available for customers from Poland and from other countries.\n"
                                + "Do you really want to choose Poland as Your country?")) {
                                country_select.val("");
                            }
                        }
                    });
                }

                if (typeof window.ddajaxtabs != 'undefined' && !window.cat) 
                {
                    window.tab_noload = true;
                    if(jQuery('#countrydivcontainer').length)
                    {
                        if(jQuery('#countrytabs').length) {
                            window.cat = new ddajaxtabs("countrytabs", "countrydivcontainer");
                        }
                        else if(jQuery('#cattabs').length) {
                            window.cat = new ddajaxtabs('cattabs', 'countrydivcontainer');
                        }
                    }
                    else if(jQuery('#product_page_container').length)
                    {
                        window.cat = new ddajaxtabs('product_page_tabs', 'product_page_container');
                    }
                    else if(jQuery('#article-content').length)
                    {
                        window.cat = new ddajaxtabs('cattabs', 'article-content');
                    }
                    else if(jQuery('#category_container').length)
                    {
                        window.cat = new ddajaxtabs("cattabs", "category_container");
                    }
      
                    if(window.cat)
                    {
                        window.cat.setpersist(false);
                        window.cat.setselectedClassTarget("link"); // "link" or "linkparent"
                        window.cat.init();
                    }
                }
        
                jQuery('#keywords').focus(function(e){
                    var _this = jQuery(this);
                    if(_this.val() == jQuery('#default_search_keyword').val()) {
                        _this.val('');
                    }
                });
        
                jQuery('#keywords').blur(function(e){
                    var _this = jQuery(this);
                    if(_this.val() == '') {
                        _this.val(jQuery('#default_search_keyword').val());
                    }
                });
        
                jQuery('#quick_find').submit(function(e){
                    var q_value = jQuery('#keywords').val();
                    var q_url = '/';
                    
                    if(window.language == 'en') {
                        q_url = '/en/';
                    }

                    if(q_value.length && q_value != jQuery('#default_search_keyword').val()) {
                        if(!q_value.match(/\W+/)) {
                            q_url += q_value + '-s.html';
                        }
                        else {
                            q_url = URL.returnAdd('q_', q_value, jQuery('.header_search_button_left a').attr('href'));
                        }
                        window.location.href = q_url;
                    }
          
                    e.stopPropagation();
                    return false;
                })
            });
}

