var currentScrollKitId = "";
var currentScrollableFlashItem = "";
var scrollWheelKitManifest = new Array();
var scrollWheelKitNames = new Array();

if (!window.addEventListener){
   var primaryOnmousewheel = window.onmousewheel = document.onmousewheel;
}


function logIt(_message){
   //console.log(_message);
   //alert(_message);
}

function ScrollWheelEventKit(_id){
   this.init(_id);
}

ScrollWheelEventKit.prototype = new Object();

ScrollWheelEventKit.prototype.targetObject = "";

ScrollWheelEventKit.prototype.messageToFlash = "";


ScrollWheelEventKit.prototype.init = function(_id){
   this.targetObject = _id;
   scrollWheelKitManifest.push(this);
   
   //logIt("init called "+_id);
}


ScrollWheelEventKit.prototype.handle = function(_delta){
  
  //console.log(_delta+":"+currentScrollableFlashItem);
  this.messageToFlash = _delta+":"+this.currentScrollableFlashItem+":"+this.targetObject;
  //logIt(this.messageToFlash);
  sendToActionScript(this.messageToFlash,this.targetObject);
}


ScrollWheelEventKit.prototype.wheel = function(event){
      
    //alert("wheel called");
        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;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                this.messageToFlash = delta+":"+currentScrollableFlashItem+":"+getScrollKit(currentScrollKitId).targetObject;
                sendToActionScript(this.messageToFlash,getScrollKit(currentScrollKitId).targetObject);
        
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}


function startEvents(_id){
   if (window.addEventListener){
       
           /** DOMMouseScroll is for mozilla. */
           window.addEventListener('DOMMouseScroll', getScrollKit(_id).wheel, false);
           window.addEventListener('mousewheel', getScrollKit(_id).wheel, false);
   }
   /** IE/Opera. */
   else{
            
           window.onmousewheel = document.onmousewheel = getScrollKit(_id).wheel;
   }
}

function stopEvents(_id){
//alert("stop events called");
   if (window.addEventListener){
           /** DOMMouseScroll is for mozilla. */
           window.removeEventListener('DOMMouseScroll', getScrollKit(_id).wheel, false);
           window.removeEventListener('mousewheel', getScrollKit(_id).wheel, false);
   }
   /** IE/Opera. */
   else{
           window.onmousewheel = primaryOnmousewheel;
           document.onmousewheel = primaryOnmousewheel;
   }
}


function thisMovie(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1) {
       return window[movieName];
   } else {
       return document[movieName];
   }
}
function sendToActionScript(value, movieId) {
	
   thisMovie(movieId).sendToActionScript(value);
}

function sendToJavaScript(value) {
   //alert(value);
   //logIt(value);
   currentScrollKitId = value.split(":")[2];

   currentScrollableFlashItem = value.split(":")[1];

   if(value.split(":")[0] == "scroll_true"){
        startEvents(currentScrollKitId);
          
   }
   else if(value.split(":")[0] == "scroll_false"){
        stopEvents(currentScrollKitId);
   }

}

function startScrollWheelKit(value){
   if(validateScrollWheelKitName(value)){
      var swek = new ScrollWheelEventKit(value);
      scrollWheelKitNames.push(value);
   }
}

function validateScrollWheelKitName(value){
   var validName = true;
   for(var i = 0;i<scrollWheelKitNames.length;i++){
     if(scrollWheelKitNames[i] == value){
        validName = false;
        i = scrollWheelKitNames.length;
     }
   }
   return validName;
}

function getScrollKit(_id){
    var targetScrollKit;
    for(var i = 0;i<scrollWheelKitManifest.length;i++){
       if(scrollWheelKitManifest[i].targetObject == _id){
          targetScrollKit = scrollWheelKitManifest[i];
       }
    }
    return targetScrollKit;
}
