﻿var currentPageID = 10;
var currentProductID = -1;
var currentProductStatus = "";
var waitForProductBuy = false;
var waitForProductDownload = false;
var toTranslate = new Array(10, 20, 30, 40, 50, 70, 0, 80, 90, 60);
var labelsToTranslate = new Array("t1", "t2");

// Load the default page
function init() {

    //History
    Sys.Application.add_navigate(onStateChanged);

    var startPageID = 10;

    //If a PageID is specified in the URL, load this page
    var urlPageID = getURLParameter("Page");
    var urlProductID = getURLParameter("Product");
    var urlVersion = getURLParameter("Version");
    var urlDownload = getURLParameter("Download");

    if (urlProductID == "" && urlVersion == "" && urlDownload == "") {
        if (urlPageID != "")
            startPageID = urlPageID;

        requestContent(startPageID)
    }
    else if (urlProductID != "") {
        requestProduct(urlProductID, true);
    }
    else if (urlVersion != "") {
        requestProduct(urlVersion, false);
        requestVersions(urlVersion);
    }
    else if (urlDownload != "") {
        requestDownload(urlDownload);
    }

    //Fill all static boxes with the appropriate content
    ALMTools_Webseite.WSStaticContent.GetAllBoxes(callbackFillBoxes);

    checkSilverlight(this);
}


//Requests the WebService to get the content for the given PageID
function requestContent(pageID) {
    ALMTools_Webseite.WSStaticContent.GetPageContent(pageID, callbackDisplayContent);
    currentPageID = pageID;
    currentProductID = -1;

    //Translate the static labels after loading content
    for (var i = 0; i < labelsToTranslate.length; i++) {
        ALMTools_Webseite.WSStaticContent.GetTranslation(labelsToTranslate[i], callbackTranslate);
    }

}


//Get the RSS content for the given PageID
function requestRssContent(pageID, rssID) {
    ALMTools_Webseite.WSStaticContent.SetRssID(rssID);
    ALMTools_Webseite.WSStaticContent.GetPageContent(pageID, callbackDisplayContent);
    currentPageID = pageID;
    currentProductID = -1;

    //Translate the static labels after loading content
    for (var i = 0; i < labelsToTranslate.length; i++) {
        ALMTools_Webseite.WSStaticContent.GetTranslation(labelsToTranslate[i], callbackTranslate);
    }
}


// Gets the requested content and sets the page title
function callbackDisplayContent(result) {

    document.getElementById("Label1").innerHTML = result[0];
    setPageTitle(result[1]);

    //History
    setHistory(currentPageID);

    //ToDo: Ändern! Da die Seite nicht die ID 20 hat
    if (currentPageID == 20) {

        //Die Benutzerdaten in die Felder laden
        //Die Produktinfos anzeigen. Idee: Oben noch ein DropDown Menü in dem alle verfügbaren Produkte angezeigt werden. Das auch 
        //HIER befüllen, falls bereits ein Produkt ausgewählt wurde, dieses markieren und bereits alle Preis einfügen
        //HIer soll iregndwann trotzdem was passieren
    }

    //Hides all boxes (and later show only the boxes for the given page)
    ALMTools_Webseite.WSStaticContent.GetAllBoxes(callbackHideBoxes);
    $(document).ready(function () {
        $("#fw_orb_top").tipTip({ maxWidth: "400px", keepAlive: true, delay: 100, backgroundClass: "red", defaultPosition: "top", forceDirection: true });
        $("#fw_orb_left").tipTip({ maxWidth: "400px", keepAlive: true, delay: 100, backgroundClass: "lightblue", defaultPosition: "left" });
        $("#fw_orb_right").tipTip({ maxWidth: "400px", keepAlive: true, delay: 100, backgroundClass: "purple", defaultPosition: "right" });
        $("#fw_orb_bottom").tipTip({ maxWidth: "400px", keepAlive: true, delay: 100, backgroundClass: "green", defaultPosition: "bottom" });
    });

}


//Fills all static boxes with the appropriate content
function callbackFillBoxes(result) {
    for (var i = 0; i < result.length; i++) {
        ALMTools_Webseite.WSStaticContent.GetBoxContent(result[i], callbackBoxContent);
    }
}

//Fills the box with the HTML ID result[1] with the appropriate content
function callbackBoxContent(result) {
    if (result[0] != "-")
        document.getElementById("box" + result[1] + "_Label3").innerHTML = result[0];
}


//Changes the language in the Session and translates the labels and content
function setLanguage(language) {
    //For the DataProtection Field
    if (language == "en") {
        document.getElementById('88').style.display = "inline";
        document.getElementById('boxreferenzen_Label2').innerHTML = "References";
    }
    else {
        document.getElementById('88').style.display = "none";
        document.getElementById('boxreferenzen_Label2').innerHTML = "Referenzen";
    }

    //Call the WebService to change the language in the Session
    ALMTools_Webseite.WSStaticContent.SetLanguage(language);

    //Re-Fill the boxes with the content in the new language
    ALMTools_Webseite.WSStaticContent.GetAllBoxes(callbackFillBoxes);

    //Translate the static labels
    for (var i = 0; i < labelsToTranslate.length; i++) {
        ALMTools_Webseite.WSStaticContent.GetTranslation(labelsToTranslate[i], callbackTranslate);
    }

    //Get the content in the new language
    if (currentPageID != -1)
        requestContent(currentPageID, language);
    else {
        switch (currentProductStatus) {
            default:
            case "":
                requestProduct(currentProductID, true);
                break;
            case "versions":
                requestVersions(currentProductID);
                break;
            case "screenshots":
                requestScreenshots(currentProductID);
                break;
            case "download":
                requestDownload(currentProductID);
                break;
            case "buy":
                requestBuy(currentProductID);
                break;
        }
    }

    //Translate the menu links
    for (var i = 0; i < toTranslate.length; i++) {
        ALMTools_Webseite.WSStaticContent.GetLink(toTranslate[i], callbackLanguage);
    }

}

//Sets the menu link captions with the translated captions
function callbackLanguage(result) {
    document.getElementById(result[1]).innerHTML = result[0];

    //Find the current page title and display it in the browser bar
    if (result[1] == currentPageID)
        setPageTitle(result[0]);
}

//Sets the captions of the static labels with the translated captions
function callbackTranslate(result) {
    if (document.getElementById(result[1]) != null)
        document.getElementById(result[1]).innerHTML = result[0];
}

//Hides all boxes
function callbackHideBoxes(result) {
    hideBoxes(result);

    //Get the boxes for the current page and show them
    ALMTools_Webseite.WSStaticContent.GetBoxesVisible(currentPageID, callbackShowBoxes);
}

//Shows the boxes for the current page
function callbackShowBoxes(result) {
    showBoxes(result);
}

//Beim "Jetzt kaufen" Button
function requestBuy(productID) {
    ALMTools_Webseite.WSRegistrationAndOrder.getBuy(productID, callbackDisplayBuySite);
    setHistoryBuy(productID);

    //ALMTools_Webseite.contentWS.getBuy(productID, l, callbackDisplayContent);

    /*
    if (document.getElementById("lblProductDescription") == null) {
    waitForProductBuy = true;
    requestProduct(productID, false, l);
    }
    else {
    ALMTools_Webseite.contentWS.getBuy(productID, l, callbackDisplayProductDescription);
    currentPageID = 80;
    currentProductID = productID;
    }
    */
}

function callbackDisplayBuySite(result) {
    document.getElementById("Label1").innerHTML = result;
}


//SERVICES (Navigation)
function requestService(serviceID) {
    ALMTools_Webseite.WSProductData.GetService(serviceID, callbackDisplayBuySite);
}

function setProductDownloadID(downloadID) {
    ALMTools_Webseite.WSRegistrationAndOrder.setDownloadID(downloadID);
}

function SetClientLanguage() {
    //    var lang = navigator.language || navigator.userLanguage;
    //    if (language == "de") {
    //        setLanguage(language);
    //    }
}

//Change language 
function ChangeLanguageByOrderProducts() {

    //Funktion definieren
}
