﻿/*
 * jScroller 0.4 - Autoscroller PlugIn for jQuery
 *
 * Copyright (c) 2007 Markus Bordihn (http://markusbordihn.de)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2010/11/02 20:24:39 $
 * $Rev: 0.4 $
 */

$jScroller = {
    config: {
        obj : [],
        refresh: 30,
        boxWidth: 234, // Includes padding + border.
        regExp: {
            px: /([0-9,.\-]+)px/
        }
    },

    cache: {
        timer: 0,
        init: 0,
        content: {}
    },

    add: function(parent, child, speed, mouse) {
        if ($(parent).length && $(child).length && speed >= 1) {
            $(parent).css({overflow: 'hidden'});
            $(child).css({position: 'absolute', left: 0, top: 0});
            $(child).find('a').each(function() {
                $jScroller.bindHover($(this));
            });
            if (mouse) {
                $(child).hover(function() {
                    $jScroller.pause($(child), true);
                }, function() {
                    $jScroller.pause($(child), false);
                });
            }
            $jScroller.config.obj.push({
                parent: $(parent),
                child: $(child),
                speed: speed,
                pause: false
            });
        }
    },

    bindHover: function(ele) {
        ele.hover(function(event) {
            var x = $jScroller.get.mouseX(event);
            var y = $jScroller.get.mouseY(event);
            var productId = ele.find('img').attr('class');
            var box = $("<div>").css({
                position: "absolute",
                // Figure out which side of the mouse the box should be on.
                left: event.pageX - ((x < ($("body").width() / 2)) ? -10 : $jScroller.config.boxWidth + 10),
                bottom: $(window).height() - y // Position bottom edge near mouse cursor.
            }).addClass("product-box").hide();
            $("body").append(box);

            var showBox = function(content) {
                box.html(content);
                box.fadeIn(300);
            };

            // If there is no cache, request content via ajax call.
            if ($jScroller.cache.content[productId] == null) {
                $.get('scroller_details.asp?idproduct=' + productId, '', function(response) {
                    $jScroller.cache.content[productId] = response; // Cache the content.
                    showBox(response);
                });
            } else {
                showBox($jScroller.cache.content[productId]); // Pull content from cache.
            }
        }, function() {
            $(".product-box").remove();
        });
    },

    pause: function(obj, status) {
        if (obj && typeof status !== 'undefined') {
            for (var i in $jScroller.config.obj) {
                if ($jScroller.config.obj[i].child.attr('id') === obj.attr('id')) {
                    $jScroller.config.obj[i].pause = status;
                }
            }
        }
    },

    start: function() {
        if ($jScroller.cache.timer === 0 && $jScroller.config.refresh > 0) {
            $jScroller.cache.timer = window.setInterval($jScroller.scroll, $jScroller.config.refresh);
        }
        if (!$jScroller.cache.init) {
            $(window).blur($jScroller.stop);
            $(window).focus($jScroller.start);
            $(window).resize($jScroller.start);
            $(window).scroll($jScroller.start);
            $(document).mousemove($jScroller.start);
            if ($.browser.msie) {window.focus();}
            $jScroller.cache.init = 1;
        }
    },

    stop: function() {
        if ($jScroller.cache.timer) {
            window.clearInterval($jScroller.cache.timer);
            $jScroller.cache.timer = 0;
        }
    },

    get: {
        px: function(value) {
            var result = '';
            if (value) {
                if (value.match($jScroller.config.regExp.px)) {
                    if (typeof value.match($jScroller.config.regExp.px)[1] !== 'undefined') {
                        result = value.match($jScroller.config.regExp.px)[1];
                    }
                }
            }
            return result;
        },

        mouseX: function(event) {
            return event.pageX - (($.browser.msie && $.browser.version == "6.0") ? document.body.scrollLeft : 0)
        },

        mouseY: function(event) {
            return event.pageY - (($.browser.msie && $.browser.version == "6.0") ? document.body.scrollTop : 0);
        }
    },

    scroll: function() {
        for (var i in $jScroller.config.obj) {
            if ($jScroller.config.obj.hasOwnProperty(i)) {
                var obj         = $jScroller.config.obj[i];
                var left        = Number(($jScroller.get.px(obj.child.css('left')) || 0));
                var minWidth    = obj.parent.width();
                var width       = obj.child.width();
                var last        = obj.child.find('a:last');

                if (left >= 0) {
                    left = -last.width()
                    obj.child.css('left', left + 'px');
                    last.remove();
                    obj.child.prepend(last);
                    $jScroller.bindHover(last);
                }

                if (!obj.pause) {
                    if (left >= minWidth) {
                        left = -1 * width;
                    }
                    obj.child.css('left', left + obj.speed + 'px');
                }
            }
        }
    }
};

