/**
 * SWFMacMouseWheel v2.0: Mac Mouse Wheel functionality in flash - http://blog.pixelbreaker.com/
 *
 * SWFMacMouseWheel is (c) 2007 Gabriel Bucknall and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Dependencies:
 * SWFObject v2.0 rc2 <http://code.google.com/p/swfobject/>
 * Copyright (c) 2007 Geoff Stearns, Michael Williams, and Bobby van der Sluis
 * This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 *
 * Modified by Matan Uberstein.
 * Gives wheel event back to browser if flash does not have focus.
 */
var swfmousewheel = function(){
    if (!swfobject) {
        return null;
    }
    var swfFocus = null;
    var k = [];
    var r = function(event){
		var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** Prevent default actions caused by mouse wheel if flash does not have focus.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
		if (swfFocus != null) {
			if (swfFocus) {
				if (event.preventDefault) 
					event.preventDefault();
				event.returnValue = false;
			}
		}
		return delta;
    };
    var l = function(event){
        var o = r(event);
        for (var i = 0; i < k.length; i++) {
            var c = swfobject.getObjectById(k[i]);
            if (typeof(c.externalMouseEvent) == 'function') {
                if (swfFocus == null) {
                    c.onmouseover = function(){
                        swfFocus = true;
                    }
                    c.onmouseout = function(){
                        swfFocus = false;
                    }
                }
                if ((swfFocus) || (swfFocus == null)) 
					try {c.externalMouseEvent(o);}catch(e){}
            }
        }
    };
	if (window.addEventListener) 
		window.addEventListener('DOMMouseScroll', l, false);
	window.onmousewheel = document.onmousewheel = l;
    return {
        registerObject: function(m){
			swfFocus = null;
			k[k.length] = m;
        }
    };
}();

