﻿$j = jQuery.noConflict();
if (typeof console == "undefined") {//
    var console = {
        log: function(outputString) {
            alert(outputString);
        },
        info: function(outputString) {
            alert('Info: '+outputString);
        },
        warn: function(outputString) {
            alert('Warning: '+outputString);
        },
        error: function(outputString) {
            alert('Error: '+outputString);
        }
    }
}

$j(function() {
    $j('a.current').click(function() { return false; });
    $j('a[rel*="external"], a[rel*="download"]').attr('target', '_blank');

    //search functions
    $j('.nav_site .search').click(function() {
        $j('.site_search_form').toggle(0, function() {
            $j('.site_search_form .txt').select().blur(function() { });
        });
        $j(this).toggleClass('active');
        return false;
    });
    $j('input[id*=uxSearchTermsTextBox]').keydown(function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            window.location = $j('a[id*=uxSearchLinkButton]').attr('href');
        }
    });

    //photo bugs
    $j('.bug_desc').css({ opacity: .85 });
    $j('.bug_photo').hover(function() {
        //mouseover
        $j(this).children('.bug_desc').fadeIn('fast');
    }, function() {
        //mouseout
        $j(this).children('.bug_desc').fadeOut('fast');
    });

    //show filter
    $j('#show_filter').click(function() {
        $j('#category_filter').toggle();
        $j(this).toggleClass('open');
        return false;
    });

    //'compare' interactivity
    $j('#grid_wrapper .compare').css({ opacity: .75 }).hover; //set overlay opacity
    $j('.product_grid').each(function() {
        var $container = $j(this);
        $j('.product_wrapper .compare .cbx', this).click(function() {
            var $button = $j('.compare_selected', $container);
            var numChecked = $j(':checked', $container).length;
            if (numChecked > 1 && numChecked <= 5) {
                $button.show();
            } else if (numChecked > 5) {
                alert('Only 5 products can be compared within a category. Please select only 5 items.'); // MUST BE TRANSLATED
                return false;
            } else {
                $button.hide();
            }
        });
    });

    //vertically center product thumbs
    $j(window).load(function() {
        $j('.product_grid .product_photo img').each(function() {
            var thisHeight = $j(this).height();
            var parentHeight = $j(this).parent('a').height();
            var offset = Math.floor((parentHeight - thisHeight) / 2) - 10;
            $j(this).css({ marginTop: offset + 'px' });
        });
    });

    //hover-over for product items
    $j('<div id="subcat_hoverover"></div>"').css({ opacity: .9 }).appendTo('body');


    $j().mousemove(function(e) {
        $j('#subcat_hoverover').css({ top: (e.pageY + 15) + 'px', left: (e.pageX + 15) + 'px' });
    });

    //hover state for border change
    $j('.product_grid .product_wrapper').hover(function() {
        $j(this).addClass('hover');
        if (!$j(this).hasClass('featured')) {
            var pContent = $j(this).children('.product_info').children('p').clone();
            $j('#subcat_hoverover').addClass('active').html(pContent);
        }
    }, function() {
        $j(this).removeClass('hover');
        $j('#subcat_hoverover').removeClass('active');
    });

    //create hlists for innovation page, etc

    $j('dl.hlist').each(function() {
        var $hlist = {};
        $hlist.el = $j(this);
        $hlist.tabContainer = $j('<div class="hlist_tabs">').insertBefore($hlist.el);
        $hlist.featureImg = $j(this).children('dd.current').children('img.feature').eq(0).clone().prependTo($hlist.tabContainer);

        $j(this).children('dt').each(function() {
            var $thisTab = $j(this);
            var $thisItem = $thisTab.next('dd');
            $j('<a href="#"></a>').addClass($thisTab.attr('class')).html($thisTab.html()).appendTo($hlist.tabContainer).click(function() {
                if (!$j(this).hasClass('current')) {
                    //change tab presentation
                    $hlist.el.children('.current').removeClass('current');
                    $hlist.tabContainer.children('.current').removeClass('current');
                    //change tab presentation
                    $j(this).addClass('current');
                    $thisItem.addClass('current');
                    //swap image
                    $hlist.featureImg.attr('src', $j('img.feature', $thisItem).eq(0).attr('src'));
                }
                return false;
            });
        });
    });

    //equalize column height
    $j(window).load(function() { //wait for window load to accommodate for possible images
        $j('.list_options .column:even').each(function() {
            var thisHeight = $j(this).height(), nextHeight = $j(this).next('.column').height();
            if (thisHeight < nextHeight) {
                $j(this).height(nextHeight);
            } else {
                $j(this).next('.column').height(thisHeight);
            }
        });

        $rowHeight = function(tableClass) {
            var rowHeight = {};
            $j(tableClass + ' td table').each(function() {
                var tmpIndex = 0;
                $j('th, td', this).each(function() {
                    var thisHeight = $j(this).height();
                    if (typeof rowHeight[tmpIndex] == 'undefined' || rowHeight[tmpIndex] < thisHeight) {
                        rowHeight[tmpIndex] = thisHeight;
                    }
                    tmpIndex++;
                });
            });
            //wait?
            var tblNum = 0;
            $j(tableClass + ' td table').each(function() {
                var tmpIndex = 0;
                $j('th, td', this).each(function() {
                    $j(this).height(rowHeight[tmpIndex]);
                    //console.info('table #' + tblNum + ', row #' + tmpIndex + ', height: ' + rowHeight[tmpIndex]);
                    tmpIndex++;
                });
                tblNum++;
            });
        } ('.product_compare');
    });
});

// This is 
//assumes that $j has been set to jQuery.noConflict();
$j(function() {
    $j('.content_info .copy').eq(0).click(function() {
        //var location = new String(document.location);
        //$j(".content_info .copy_field").val(location).attr('size', location.length).show().focus().select().blur(function() { $j(this).hide(); });
        $j(".content_info .copy_field").attr('size', $j(this).val.length).show().focus().select().blur(function() { $j(this).hide(); });
        return false;
    });
});
