﻿
var pluginLoaded = false;
var appLoaded = false;
var blog__contentID = 0;
var blog__culture = 'en-us';
var currentHtmlContent;
var pluginID;
var isNavigating = false;
var baseURL;

function notifyIframeContentResized(height) {
    $get("framedContent").style.height = String.format("{0}px", height);
    notifyClientHtmlResized();
}
function notifyClientHtmlResized() {
    if (!appLoaded || !pluginLoaded) return;
    var slObj = $get(pluginID);
    slObj.content.slRoot.notifyHtmlResized();
}
function xamlRyoushin_pluginLoaded(sender, args) {
    pluginLoaded = true;
    notifyLoaded(true);
}
function app_loaded(sender, args) {
    $addHandler($get("content"), "click", notifyClientHtmlResized);
    appLoaded = true;
    notifyLoaded(true);
    //window_resize();
}
function app_navigate(sender, args) {
    var state = args.get_state();
    if (isNavigating || state == undefined || state.type == null) return;
    pushContent(state["type"], state["value"], state["culture"], true);
}
function notifyLoaded(doTween) {
    if (appLoaded == false || pluginLoaded == false) return;
    var slObj = $get(pluginID);
    slObj.content.slRoot.notifyHtmlLoaded(doTween);
}
function doSetElementOpacity(el, opacity) {
    var domEl = $get(el);
    if (opacity > 1) opacity = 1;
    if (opacity < 0) opacity = 0;
    if (Sys.Browser.agent != Sys.Browser.InternetExplorer) {
        Sys.UI.DomElement.setVisibilityMode($get(el), Sys.UI.VisibilityMode.hide);
        Sys.UI.DomElement.setVisible($get(el), opacity == 1);
    } else {
        if (opacity == 1 && domEl.style.filter && domEl.style.removeAttribute)
            domEl.style.removeAttribute('filter');
        else
            $common.setElementOpacity($get(el), opacity);
    }
}
function unloadContent() {
    Sys.UI.DomElement.setVisible($get(currentHtmlContent), false);
    doSetElementOpacity(currentHtmlContent, 0);
}
function pushContent(key, value, culture, historicized) {
    if (!appLoaded || !pluginLoaded) return;
    var slObj = $get(pluginID);
    slObj.content.slRoot.pushContent(key, value, culture, !historicized);
}
function resizePlugin(height) {
    $get(pluginID).style.height = String.format("{0:D0}px", height);
}
function registerHistoryPoint(type, value, culture) {
    isNavigating = true;
    // TO BE IMPLEMENTED...
    //Sys.Application.addHistoryPoint({ type: type, value: value, culture: culture }, document.title);
    isNavigating = false;
}
function loadContent(content_id, culture) {
    var c_tent = $get("content");
    var framed = $get("framedContent");
    Sys.UI.DomElement.setVisibilityMode(framed, Sys.UI.VisibilityMode.collapse);
    Sys.UI.DomElement.setVisibilityMode(c_tent, Sys.UI.VisibilityMode.collapse);
    if (content_id == blog__contentID && culture.toLowerCase() == blog__culture.toLowerCase()) {
        currentHtmlContent = 'content';
        doSetElementOpacity(c_tent.id, 0);
        Sys.UI.DomElement.removeCssClass(c_tent, "SilverlightHidden");
        Sys.UI.DomElement.setVisible(c_tent, true);
        notifyLoaded(true);
    } else {
        currentHtmlContent = 'framedContent';
        var url = String.format("{2}/pages/iframe.aspx?ID={0}&culture={1}", content_id, culture, baseURL);
        framed.src = url;
        doSetElementOpacity(framed.id, 0);
        Sys.UI.DomElement.setVisible(framed, true);
    }
}
function pushArticle(evt) {
    var nfunc = pushContent;
    if (nfunc && typeof (nfunc) == "function") {
        nfunc("Article", evt.target.articleData.id, evt.target.articleData.culture, false);
    }
    evt.stopPropagation();
    return false;
}
function getPluginBottom() {
    var bounds = Sys.UI.DomElement.getBounds($get(pluginID));
    var tgetHeight = bounds.y + bounds.height;
    //$debug("target height: "+tgetHeight);
    return tgetHeight;
}
function getBodyVisibleHeight() {
    //$debug("body height: " + document.body.offsetHeight);
    return document.body.offsetHeight;
}
function getHtmlElementContentId() {
    return currentHtmlContent;
}
function getContentBottom() {
    var c_tent = $get(currentHtmlContent);
    if (c_tent == null) return 0;
    var bounds = Sys.UI.DomElement.getBounds(c_tent);
    var retval = bounds.y + bounds.height;
    return retval;
}
Sys.Application.add_navigate(app_navigate);
Sys.Application.add_load(app_loaded);
