var _isInitialized = false;
var _popupImage = null;
var _popupContainer = null;
var _popupLink = null;
var _musicVideoFrame = null;
var _videoPlayer = null;
var _isQuickTime = false;
var _usePageX = false;
var _mouseX = 0;
var _mouseY = 0;
var _popupOffsetX = 0;
var _popupOffsetY = 0;
var _oldId = "";
var _oldVideoId = "";
var _direction = "right";
var _domain = "kweezletown"
var _enableSound = true;
var _pageHref = '';

function homePageLoad() {
    if (window.document.referrer.indexOf(_domain) == -1) {
        playSound(firstload, "start");
    }
}

function clickSound(obj) {
    playSound(obj, 'start');
    return false;
}
function stopSound(obj) {
    playSound(obj, 'stop');
    return false;
}
function rollSound(obj) {
    playSound(obj, 'start');
    return true;
}
function playSound(obj, state) {
    if (_enableSound) {
        obj.gotoAndPlay('/soundclip',state);
    }
}
            
function rollover(obj, state) {
    if (!_isInitialized) {
        init();
    }
    var id = obj.id;
    setPopup(state, id);
    _oldId = id;
}

function setPopup(state, id) {
    if (state == "on" && id != _oldId) {
        var popupHref = "";
        _popupImage.src = getPopupImage(id);
        popupHref = getPopupLink(id);
        if (popupHref.length == 0) {
            _popupLink.href = "#";
            _popupLink.onClick = "return false;"
        } else {
            _popupLink.href = popupHref;
        }
        _popupContainer.style.top = getPopupY() + "px";
        _popupContainer.style.left = getPopupX() + "px";
        _popupContainer.style.display = "block";
    } else {
         if (state == "off") {
            _popupContainer.style.display = "none";
         }
    }
}

function getPopupImage(id) {
    var image = "images/popup_";
    _direction = "right";
    switch (id) {
        case "link-house":
            image += "findmore";
            break;
        case "blue-house":
            image += "abc";
            break;
        case "orange-house":
            image += "rhyme-time";
            break;
        case "pink-house":
            image += "store";
            _direction = "left";
            break;
        case "purple-house":
            image += "show-and-tell";
            break;
        case "school-house":
            image += "dictionary";
            break;
        case "rainbow":
            image += "music";
            break;
        case "sun":
            image += "music";
            break;
        case "ray-goldie":
            image += "goldie";
            break;
        case "ray-calamity-1":
            image += "calamity";
            break;
        case "ray-calamity-2":
            image += "calamity";
            break;
        case "ray-potatoes":
            image += "potatoes";
            break;
        default:
            image += "images/px";
    }
    return  image + ".gif";
}

function getPopupLink(id) {
    var popupHref = "";
    switch (id) {
        case "link-house":
            popupHref = "more_information.htm"
            break;
        case "blue-house":
            popupHref = "abc_challenge.htm";
            break;
        case "orange-house":
            popupHref = "rhyme_time.htm";
            break;
        case "pink-house":
            popupHref = "general_store.htm";
            break;
        case "purple-house":
            popupHref = "show_and_tell.htm";
            break;
        case "school-house":
            popupHref = "sing_and_sign_abc.htm";
            break;
        case "rainbow":
            popupHref = "sun.htm";
            break;
        case "sun":
            popupHref = "sun.htm";
            break;
        default:
            popupHref = "";
    }
    return popupHref;
}

function init() {
    if (document.captureEvents) {
        document.captureEvents(Event.MOUSEMOVE);
        _usePageX = true;
    }
    document.onmousemove = setMouseCoords;
    _popupImage = document.getElementById('img-popup');
    _popupContainer = document.getElementById('popup-container');
    _popupLink = document.getElementById('popup-link');
    if (document.getElementById("music-video-frame")) {
        _musicVideoFrame = document.getElementById("music-video-frame");
    }
    if (document.getElementById("video-frame")) {
        if (window.frames["video-frame"].document.player != null) {
            _videoPlayer = window.frames["video-frame"].document.player;
        }
    }
    _isInitialized = true;
}

function setMouseCoords(e) {
    if (_usePageX) {
        _mouseX = e.pageX;
        _mouseY = e.pageY;
    } else {
        e = window.event;
        _mouseX = e.clientX;
        _mouseY = e.clientY;
    }
}

function getPopupX() {
    x = _mouseX - _popupOffsetX;
    if (x < 0) {
        x = 0;
    }
    if (_direction == "left") {
        x = _mouseX;
    } else {
        x = _mouseX - _popupOffsetX;
    }
    if (x < 0) {
        x = 0;
    }
    return x;
}

function getPopupY() {
    y = _mouseY - _popupOffsetY;
    if (y < 0) {
        y = 0;
    }
    return y
}

function changeVideo(obj, fileext) {
    var id = obj.id;
    if (isNewId(id)) {
        setVideo(id, fileext);
        _oldVideoId = id;
    }
}

function setVideo(id, fileext) {
    document.getElementById("music-video-frame").src = getVideoHref(id, fileext);
}

function getVideoHref(id, fileext) {
    var videoHref = "choose_";
    switch (id) {
        case "ray-goldie":
            videoHref += "goldie";
            break;
        case "ray-calamity-1":
            videoHref += "calamity";
            break;
         case "ray-calamity-2":
            videoHref += "calamity";
            break;
        case "ray-potatoes":
            videoHref += "potatoes";
            break;
        default:
            videoHref = "";
    }
    if (videoHref.length > 0) {
        videoHref += ".htm";
    }
    return videoHref;
}

function isNewId(id) {
     if (id == _oldVideoId) {
        return false;
     } else {
        if (id.substring(0, id.length - 2) == 
                _oldVideoId.substring(0, id.length - 2)) {
            return false;
        } else {
            return true;
        }
     }
}

function playVideo() {
    if (!_isInitialized) {
        init();
    }
    if (_videoPlayer != null) {
        if(isQuickTime()) {
           _videoPlayer.Play();
        } else {
            if (isWindowsMedia()) {
                _videoPlayer.play();
            }
        }
    }
}

function stopVideo() {
    if (!_isInitialized) {
        init();
    }
    if (_videoPlayer != null) {
        if(isQuickTime()) {
            _videoPlayer.Stop();
        } else {
            if (isWindowsMedia()) {
                _videoPlayer.pause();
            }
        }
    }
}

function restartVideo() {
    if (!_isInitialized) {
        init();
    }
    if (_videoPlayer != null) {
        if(isQuickTime()) {
            _videoPlayer.Rewind();
        } else {
            if (isWindowsMedia()) {
              _videoPlayer.stop();
              playVideo();
            }
        }
    }
}

function isQuickTime() {
    return window.frames["video-frame"].location.href.indexOf("_mov.htm") != -1;
}

function isWindowsMedia() {
    return window.frames["video-frame"].location.href.indexOf("_wmv.htm") != -1;
}

function showWait(video, format) {
    document.getElementById("wait-message").style.display = "block";
    _pageHref = "video_" + video + "_" + format + ".htm";
    setTimeout("launchVideo()", 2500);
}

function launchVideo() {
    window.location.replace(_pageHref);
}
