var overLessonLink = false;
var overLessonMenu = false;
var rating = 5;


// Register the onLoad function
if (window.addEventListener) {
    window.addEventListener ("load", onLoad, false);
}
else if (window.attachEvent) {
    window.attachEvent ("onload", onLoad);
}
else {
    window.onload = onLoad;
}


function onLoad () {
    // Only show the rating widgets if it's an online version of the page otherwise we won't be able to create an xmlHTTPRequest object anyway (same origin policy)
    var onlineURL = "http://stakeout.sourceforge.net";
    if (document.baseURI) {
        var documentURL = document.baseURI.substr (0, onlineURL.length);
        if (documentURL == onlineURL) {
            // We're online, allow them to submit feedback
            var ratingWidget = document.getElementById ("ratingWidget");
            ratingWidget.setAttribute ("style", "display: block;");
        }
    }
}


function getX (element) {
    var x = 0;
    while (element) {
        x += element.offsetLeft;
        element = element.offsetParent;
    }
    return x;
}


function getY (element) {
    var y = 0;
    while (element) {
        y += element.offsetTop;
        element = element.offsetParent;
    }
    return y;
}


function OnLessonLinkOver () {
    overLessonLink = true;
    setTimeout (ShowHideLessonMenu, 50);
}


function OnLessonLinkOut () {
    overLessonLink = false;
    setTimeout (ShowHideLessonMenu, 50);
}


function OnLessonMenuOver () {
    overLessonMenu = true;
    setTimeout (ShowHideLessonMenu, 50);
}


function OnLessonMenuOut () {
    overLessonMenu = false;
    setTimeout (ShowHideLessonMenu, 50);
}

 
function ShowHideLessonMenu () {
    if (overLessonLink || overLessonMenu) {
        var lessonLink = document.getElementById ("lessonLink");
        var lessonMenu = document.getElementById ("lessonMenu");
        var menuLeft = getX (lessonLink);
        var menuTop = getY (lessonLink) + lessonLink.offsetHeight - 2;
        lessonMenu.setAttribute ("style", "display: inline; position: absolute; left: " + menuLeft + "; top: " + menuTop + ";");
        // We have to hide the applet because it gets z-order precedence over the menu
        var introApplet = document.getElementById ("introApplet");
        var introAppletReplacement = document.getElementById ("introAppletReplacement");
        if (introApplet != null && introAppletReplacement != null) {
            introApplet.setAttribute ("style", "display: none;");
            introAppletReplacement.setAttribute ("style", "display: block;");
        }
    }
    else {
        var lessonMenu = document.getElementById ("lessonMenu");
        lessonMenu.setAttribute ("style", "display: none;");
        var introApplet = document.getElementById ("introApplet");
        var introAppletReplacement = document.getElementById ("introAppletReplacement");
        if (introApplet != null && introAppletReplacement != null) {
            introApplet.setAttribute ("style", "display: inline;");
            introAppletReplacement.setAttribute ("style", "display: none;");
        }
    }
}


function ChangeRating (control, eventInfo) {
    // The first star will always be filled so no need to change it
    var star2 = document.getElementById ("rating2");
    var star3 = document.getElementById ("rating3");
    var star4 = document.getElementById ("rating4");
    var star5 = document.getElementById ("rating5");
    if (control.id == "rating1") {
        rating = 1;
        star2.src = "../image/starEmpty.png";
        star3.src = "../image/starEmpty.png";
        star4.src = "../image/starEmpty.png";
        star5.src = "../image/starEmpty.png";
    }
    else if (control.id == "rating2") {
        rating = 2;
        star2.src = "../image/starFilled.png";
        star3.src = "../image/starEmpty.png";
        star4.src = "../image/starEmpty.png";
        star5.src = "../image/starEmpty.png";
    }
    else if (control.id == "rating3") {
        rating = 3;
        star2.src = "../image/starFilled.png";
        star3.src = "../image/starFilled.png";
        star4.src = "../image/starEmpty.png";
        star5.src = "../image/starEmpty.png";
    }
    else if (control.id == "rating4") {
        rating = 4;
        star2.src = "../image/starFilled.png";
        star3.src = "../image/starFilled.png";
        star4.src = "../image/starFilled.png";
        star5.src = "../image/starEmpty.png";
    }
    else if (control.id == "rating5") {
        rating = 5;
        star2.src = "../image/starFilled.png";
        star3.src = "../image/starFilled.png";
        star4.src = "../image/starFilled.png";
        star5.src = "../image/starFilled.png";
    }
}


function SubmitRating () {
    var request = new XMLHttpRequest ();
    var url = document.baseURI;
    var lessonIdStart = url.indexOf ("lid");
    var lessonIdEnd = url.indexOf ("/", lessonIdStart);
    var lesson = url.substr (lessonIdStart + 3, lessonIdEnd - lessonIdStart - 3);
    request.open ("GET", "http://stakeout.sourceforge.net/iid5/rate.php?lesson=" + lesson + "&rating=" + rating, false);
    request.send (null);

    var response = document.getElementById ("ratingResponse");
    response.text = request.responseText;
    response.innerHTML = request.responseText;
    response.setAttribute ("style", "display: block;");
}

function SubmitComments () {
    var request = new XMLHttpRequest ();
    var url = document.baseURI;
    var pageIdEnd = url.lastIndexOf ("/");
    var pageIdStart = url.lastIndexOf ("/", pageIdEnd - 1);
    var pageId = url.substr (pageIdStart + 1, pageIdEnd - pageIdStart - 1);
    var nameBox = document.getElementById ("nameBox");
    var name = nameBox.value;
    var emailBox = document.getElementById ("emailBox");
    var email = emailBox.value;
    var feedbackBox = document.getElementById ("feedbackBox");
    var feedback = feedbackBox.value;
    request.open ("GET", "http://stakeout.sourceforge.net/iid5/feedback.php?pageId=" + pageId + "&name=" + name + "&email=" + email + "&feedback=" + feedback, false);
    request.send (null);

    window.location = 'http://stakeout.sourceforge.net/iid6/index.php';
}
