// JScript source code

//contains calls to silverlight.js, example below loads Page.xaml
var control = null;

function createSilverlight()
{
	Silverlight.createObjectEx({
		source: "Page.xaml",
		parentElement: document.getElementById("SilverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "900",
			height: "600",
			version: "1.1",
			enableHtmlAccess: "true",
			isWindowless: "true"
		},
		events: {}
	});
	
	// Give the keyboard focus to the Silverlight control by default
    document.body.onload = getFocus;
    
    control = document.getElementById('SilverlightControl');
	//window.setInterval("tick()", 5000);
	initializeMouseWheel();
}

function getFocus()  {
  var silverlightControl = document.getElementById('SilverlightControl');
  if (silverlightControl)
  silverlightControl.focus();
}

function initializeMouseWheel(){
	if(window.addEventListener){
		window.addEventListener('DOMMouseScroll',wheelHandler, false);
	}
	window.onmousewheel = document.onmousewheel = wheelHandler;
	control.onmouseover = mouseOverHandler;
	control.onmouseout = mouseOutHandler;
}

function wheelHandler(event){
	//alert('hello');
	var delta = 0;
	if(!event){
		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;
        //Sign is only reversed in windows FF
        if(navigator.userAgent.indexOf("Macintosh") != -1){
           delta=delta * 3;
        }
    }
	var consume = false;
    
    // If delta is nonzero, handle it.
    // Basically, delta is now positive if wheel was scrolled up,
    // and negative, if wheel was scrolled down.
    try{
        if (delta){
            consume = control.content.MouseWheelAgent.handleMouseWheelEvent(delta);
	    }
            
        // Prevent default actions caused by mouse wheel.
        // That might be ugly, but we handle scrolls somehow
        // anyway, so don't bother here..
        if (consume) {
		    if (event.preventDefault)
			    event.preventDefault();
    	        
		    event.returnValue = false;
	    }
	}catch(exc)
	{
	    //do nothing
	}
}

function mouseOverHandler(event){
    try{
	    control.content.MouseWheelAgent.handleMouseOverEvent();
	}catch(exc)
	{
	    //do nothing
	}
}

function mouseOutHandler(event){
    try{
        control.content.MouseWheelAgent.handleMouseOutEvent();
    }catch(exc)
    {
        //do nothing
    }
}

function tick(){
    control.content.TimeTicker.tick();
}

