﻿
$(document).ready(function() {
    //Retrieve Browser Type
    var userBrowser = BrowserType();
    //IE 6 fix
    if (userBrowser == "msie6") {
        $("div.community").css({ marginLeft: "48px" });
        $("div.spinControl, div.stopControl, div.helpControl").css({ marginLeft: "7px" });
    }

    //Initialize only if scenes exist for the virtual Tour
    if ($("a.vtSceneList").length > 0) {
        initVTScenes();
        initVTControls();
    }

    //Override server-side click events for standalone implementation
    var isStandAlone = $.urlParam("standalone");
    if (isStandAlone != "") {
        $("div.closeWindow").hide();
        $("div.container").css({ paddingBottom: "10px", paddingTop: "10px", border: "1px solid black" });
        $("a.vtSceneList").click(function() {
            var lnk = $(this);
            if (lnk.text() != $("div.sceneTitle").html()) {
                var redirect;
                if ($.urlParam("CAV") != "")
                    redirect = window.location.toString().replace($.urlParam("CAV"), lnk.attr("id"));
                else
                    redirect = window.location + "&CAV=" + lnk.attr("id");

                window.location.href = redirect;
            }
            return false;
        });
    }
});

//Initialize controls on lower right of viewer
function initVTControls() {
    $("div.spinControl, div.spinText").click(function() {
        spinIpix();
    });

    $("div.stopControl, div.stopText").click(function() {
        stopIpix();
    });

    $("div.helpControl, div.helpText").click(function() {
        helpWin();
    });
}

function initVTScenes() {
    //Load first scene
    $("a.vtSceneList").css({ color: "#898A85" });

    var scene = $("a.vtSceneList:first");
    if (ipix > 0)
        scene = $("a#" + ipix);

    scene.css({ color: "#A0564D" });
    $("div.sceneTitle").html(scene.text());

    InitVT();
}

function InitVT() {
    //Recreate the applet each time
    var vtContainer = $("div.ipixContainer");
    var vtControls = $("div.ipixControls").parent();

    //Check java version
    var version = PluginDetect.getVersion("Java", jarPath);
    var versionCheck = PluginDetect.isMinVersion("Java", minJava, jarPath)


    if (!navigator.javaEnabled()) {
        vtContainer.empty();
        vtContainer.html("<div class=\"ipixError\">" + noJavaMsg + "</div>");
        vtControls.hide();
    }
    else if (versionCheck != 1) {
        vtContainer.empty();
        vtContainer.html("<div class=\"ipixError\">" + wrongJavaVerMsg + "</div>");
        vtControls.hide();
    }
    else {
        vtControls.show();
    }

}

function stopIpix() {
    document.applets['IpixViewer'].control("stop", null);
}


function spinIpix() {
    document.applets['IpixViewer'].control("spin", null);
}

function helpWin() {
    window.open(helpURL, "Help", "width=580,height=548,toolbar=no,scrollbars=yes,resizable=no");
}

function closeWin() {
    parent.CloseVirtualTourOverlay();
}

//Function to retrieve Browser Type
function BrowserType() {
    var _browser;
    jQuery.each(jQuery.browser, function(i, val) {
        switch (i) {
            case "msie":
                if (val) _browser = "msie" + jQuery.browser.version.substr(0, 1);
                break;
            case "mozilla":
                if (val) _browser = i;
                break;
            case "safari":
                if (val) _browser = i;
            default:
                break;
        }
    });
    return _browser;
}

$.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return ""; }
    return results[1] || "";
} 

