var jsReady = false;
var miniMapHoverTimer = null;
var miniMapHoverTimerRunning = false;

function isReady() {
    return jsReady;
}

function pageInit() {
    jsReady = true;
    initScroller();
}

function initScroller(){
    $('#PropertiesScroller').show();
    $('#PropertiesScroller').jScrollPane({showArrows:true, dragMaxHeight:250, scrollbarWidth:14, arrowSize: 14, scrollbarMargin:0, animateTo:true, animateInterval:100, animateStep:3});
    $('#propertiesTable').mouseout(function(){
        jQuery.fn.jScrollPane.onStopDrag;
    });
    
    $('#BigMapDisabler').hide();
    
    if(selectedProperty){
        //$("."+selectedProperty).css({backgroundColor:"#e3e3e3"});
    }
    
    // Set mouse events on properties
    $(".propertiesTable tr").hover(
        function () {
            stopMiniMapHoverTimer();
            $('#PropertiesScroller .'+$(this).attr("class")).css({backgroundColor:"#e3e3e3"});
            sendToActionScript($(this).attr("class"),2);
        },
        function () {
            startMiniMapHoverTimer();
            $('#PropertiesScroller .'+$(this).attr("class")).css({backgroundColor:"#ffffff"});
            sendToActionScript($(this).attr("class"),3);
        }
    );
    
    // Super hack to make sure that flash does not eat the mouseout event
    $("#PropertiesScroller").hover(
        function () {
            $('#BigMapDisabler').show();
        },
        function () {
            $('#BigMapDisabler').hide();
        }
    );
    

}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

function sendToActionScript(value,type) {
    thisMovie("flashMapSO").sendToActionScript(value,type);
    thisMovie("flashMiniMapSO").sendToActionScript(value,type);
}

function flashHovered() {
    setTimeout(function () {
        $('#BigMap').mouseout();
    }, 50);
}


function sendToJavaScript(value)
{
    id = value.substring(value.lastIndexOf(" ")+1);
    cssClassName = "."+id;
    type = value.substring(0, value.indexOf(" "));


    if(type=="rollOver"){
        stopMiniMapHoverTimer();
        thisMovie("flashMiniMapSO").sendToActionScript(id,2);
        $(cssClassName).css({backgroundColor:"#e3e3e3"});
        $('#PropertiesScroller')[0].scrollTo(cssClassName);
    }else{
        startMiniMapHoverTimer();
        $(cssClassName).css({backgroundColor:"#ffffff"});
    }
}

function hoverSelectedProperty() {
    stopMiniMapHoverTimer();
    thisMovie("flashMiniMapSO").sendToActionScript(selectedProperty,2);
}


function startMiniMapHoverTimer() {
    miniMapHoverTimer = setTimeout('hoverSelectedProperty()',1000);
    miniMapHoverTimerRunning = true;
}

function stopMiniMapHoverTimer() {
    if (miniMapHoverTimerRunning)
        clearTimeout(miniMapHoverTimer);
}
