//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var gradePopupStatus = 0;
var yearPopupStatus = 0;

//loading popup with jQuery
function loadPopup(helpWindow){
    //loads popup only if it is currently disabled
    if(yearPopupStatus==0 && gradePopupStatus==0){
        $("#backgroundPopup").css({"opacity": "0.2"});
        $("#backgroundPopup").fadeIn("fast");
        $("#popupContact").fadeIn("fast");
        if(helpWindow==1)
            yearPopupStatus = 1;
        else
            if(helpWindow==2)
                gradePopupStatus = 1;
    }
}

//disabling popup with jQuery
function disablePopup(windowLoad){
    //disables popup only if it is enabled
    if(gradePopupStatus==1 || yearPopupStatus==1){
        $("#backgroundPopup").fadeOut("fast");
        $("#popupContact").fadeOut("fast");
        if(windowLoad==1){
            yearPopupStatus = 0;
            $("#yearHelpPopup").css({"display": "none"});
        }
        if(windowLoad==2){
            gradePopupStatus = 0;
            $("#gradeHelpPopup").css({"display": "none"});
        }
    }
}

//centering popup
function centerPopup(){
    //request data for centering popup
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight/2 - popupHeight/2,
        "left": windowWidth/2 - popupWidth/2
    });
    
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

//activate jQuery listening event when document is ready
$(document).ready(function(){
    // the window load variable determines which help window
    // is to be displayed: 1 = Year/Model, 2 = Grade
    var windowLoad = 0;
    
    //LOADING THE POPUP
    //Hover over a help icon
    $("#yearHelp").mouseover(function(){
        //make the info in the window displayable
        $("#yearHelpPopup").css({"display": "block"});
        //ensure that no windows are currently loaded
        if(windowLoad <> 0){
            disablePopup(windowLoad);
            windowLoad = 0;
        }
        //now set windowLoad to 1 for the year help icon window
        windowLoad = 1;
        //center with css
        centerPopup();
        //load yearHelpPopup window
        loadPopup(windowLoad);
        
        //throw a Google Analytics event
        _gaq.push (['_trackEvent','Help Popup','year-help-popup','Year/Model Help Popup', 10]);
    });
    $("#gradeHelp").mouseover(function(){
        //make the info in the window displayable
        //make the info in the window displayable
        $("#gradeHelpPopup").css({"display": "block"});
        //ensure that no windows are currently loaded
        if(windowLoad <> 0){
            disablePopup(windowLoad);
            windowLoad = 0;
        }
        //now set windowLoad to 1 for the year help icon window
        windowLoad = 2;
        //center with css
        centerPopup();
        //load gradeHelpPopup window
        loadPopup(windowLoad);
        
        //throw a Google Analytics event
        _gaq.push (['_trackEvent','Help Popup','grade-help-popup','Grade/Model Help Popup', 10]);
    });
    
    //CLOSING THE POPUP
    //Click the X event
    $("#popupContactClose").click(function(){
        disablePopup(windowLoad);
    });
    //click out event
    $("#backgroundPopup").click(function(){
        disablePopup(windowLoad);
    });
    //Press escape event
    $(document).keypress(function(e){
        if(e.keyCode==27 && (gradePopupStatus==1 || yearPopupStatus==1)){  
            disablePopup(windowLoad);
        }
    });
});

