$( function() {
	product.init();
})
var product = {
    init: function () {

        var product_overlay = $(".product-overlay");
        var product_list = $(".product-list");

        $(".change-view li").bind("touchstart", function () {
            $(this).trigger('click');
        });

        $(".change-view li").click(function () {
            $(".change-view li.selected").removeClass('selected');
            $(this).addClass("selected");

            if ($(this).hasClass('grid')) {
                $('.product-list').removeClass('list-view');
                $('.product-list').addClass('grid-view');
                $(".product-overlay").css({ left: 115 }); // Reset overlay position
            }
            if ($(this).hasClass('list')) {
                $('.product-list').removeClass('grid-view');
                $('.product-list').addClass('list-view');
            }
        });

        $(".product-list>li").hover(product.onItemMouseHoverOver, product.onItemMouseHoverOut);
        $(".product-list>li").mouseover(product.onItemMouseOver);

        $(".product-list>li").mousemove(function (event) {
            if (!product_list.hasClass('list-view')) return;
            product_overlay.css({ left: event.pageX - product_list.offset().left + 100 });
        });

        $(".product-list>li").click(function (e) {
            if (!application.isIDevice()) {
                window.location = $(this).find('a').attr('href');
            }
            return false;
        });

        $(".product-list>li").click(function (e) {
            product.fadeAllOut();
            product.fadeItemIn($(this));
            e.stopImmediatePropagation();
            $(this).addClass("selected");
        });

        $(".product-list>li").bind('touchstart', function () {
            if ($(this).hasClass("selected")) {
                window.location = $(this).find('a').attr('href');
            }
        });

        $('body').bind('touchstart', function () {
            product.lastListItem = null;
            product.ignoreMouseOver = false;

            product.fadeAllIn();
            $(".product-list>li.selected").removeClass("selected");
        });
    },
    onItemMouseHoverOut: function () {
        if (!product.lastListItem.hasClass("selected")) return;

        product.fadeAllIn();
        product.lastListItem.removeClass("selected");
        product.lastListItem.find(".product-overlay").bind("webkitTransitionEnd", function () {
            $(this).hide();
        });
    },
    onItemMouseHoverOver: function (event) {
        var target = $(this);

        product.lastListItem = target;
        product.fadeAllOut();
        product.fadeItemIn(target);

        target.find(".product-overlay").css('display', 'block');
        target.addClass("selected");
    },
    onItemMouseOver: function (event) {
        var targetIsOverlay = $(event.relatedTarget).attr("class").indexOf("product-overlay") != -1;
        if (targetIsOverlay) product.onItemMouseHoverOut();
    },
    fadeAllOut: function () {
        $(".product-list>li").removeClass('fade-in');
        $(".product-list>li").addClass('fade-out');
        product.handleNoWebkit();

    },
    fadeAllIn: function () {
        $(".product-list>li").removeClass('fade-out');
        $(".product-list>li").addClass('fade-in');
        product.handleNoWebkit()
    },
    fadeItemIn: function (item) {
        item.removeClass('fade-out');
        item.addClass('fade-in');
        product.handleNoWebkit();
    },
    handleNoWebkit: function () {
        if ($.browser.webkit || $.browser.msie) return;

        $(".product-list>li.fade-out").css({ opacity: .4 });
        $(".product-list>li.fade-in").css({ opacity: 1 });
        $(".product-overlay ul>li").css({ opacity: 1 });
    }
};
