﻿function bodyLoaded() {
}

$(document).ready(function () {
    document.getElementById("contentContainer").style.minHeight = contentHeight().toString() + "px";

    var config = {
        over: function () {
            $("ul.menuBody", this).slideDown('medium');
        },
        out: function () {
            $("ul.menuBody", this).slideUp('medium');
        },
        timeout: 200, // number = milliseconds delay before onMouseOut    
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
        interval: 100
    };

    $('div.menuButtonContainer').hoverIntent(config);
    setTimeout(function () {
        if (self.startFlashApplication)
            self.startFlashApplication();
    }, 100);
});

function contentHeight() {
    return (pageHeight() - getHeaderAndFooterHeights() - 30);
}

function getHeaderAndFooterHeights() {
    //alert($("#header").height() + $("#menuButtonsContainer").height() + $("#controlsContainer").height() + $("#footer").height());
    return $("#header").height() + $("#menuButtonsContainer").height() + $("#controlsContainer").height() + $("#footer").height() + $("#headerSpacer").height();
    //in css:
    //header = 76
    //menuButtonsContainer = 30
    //controlsContainer = 24
    //footer = 85
}

function pageHeight() {
    return window.innerHeight != null ? window.innerHeight :
    document.documentElement && document.documentElement.clientHeight ?
    document.documentElement.clientHeight : document.body != null ?
    document.body.clientHeight : 450;
}

function pageWidth() {
    return window.innerWidth != null ? window.innerWidth :
    document.documentElement && document.documentElement.clientWidth ?
    document.documentElement.clientWidth : document.body != null ?
    document.body.clientWidth : 450;
}


function playHappyAndMaxIntro() {
    var params = {
        movie: "/Activities/Animations/meet_happy_max.swf",
        allowScriptAccess: "sameDomain",
        quality: "High",
        wmode: "transparent"
    };
    var attrs = { height: "100%", width: "100%" }
    //swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
    swfobject.embedSWF("/Activities/Animations/meet_happy_max.swf", "handmswfContainer", "100%", "100%", "9.0.0", null, null, params, attrs, null);
    document.getElementById("happyAndMaxIntroDiv").style.display = "block";
    setTimeout(hideHappyAndMaxIntro, 40000);
}

function hideHappyAndMaxIntro() {
    document.getElementById("happyAndMaxIntroDiv").style.display = "none";
}


function showTimeoutMessageBox() {
    $find("SessionTimeoutMessageBox").show();
}

function inPopup() {
    return window.opener && window.opener != null;
}

function navigateToActivity(activityKey) {
    var url = activityKey;
    if (url.indexOf("/") < 0)
        url = "/Activities/" + activityKey + ".aspx";
    var child = window.open(url);
    if (!child || child.closed || typeof child.closed == 'undefined') {
        location.href = url;
    }
}

function reportActivityComplete() {
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else // Internet Explorer 5/6
    {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", "/Activities/ActivityComplete.ashx?activityUrl=" + location.href, false);
    xhttp.send("");
    xmlDoc = xhttp.responseXML;
}

function resizeSwf() {
	if (self.startFlashApplication)
		document.getElementById("swfParentContainer").style.height = document.documentElement.clientHeight - 190;
}
var redirectUrl = "";
function reportActivityCompleteAndRedirect() {
    reportActivityComplete();
    closeOrRedirectAfterActivity();
}

function closeOrRedirectAfterActivity() {
    if (inPopup()) {
        if (redirectUrl == null || redirectUrl.length == 0)
            closeWindow();
        else
            location.href = redirectUrl;
    }
    else {
        if (redirectUrl == null || redirectUrl.length == 0)
            location.href = "/Home.aspx";
        else
            location.href = redirectUrl;
    }
}

function closeWindow() {
    window.close();
}

function prepareLinkButtonForDefault(clientID) {
    if (clientID == null || clientID == "")
        return;
    var tag = document.getElementById(clientID);
    if (tag && typeof (tag.click == 'undefined')) {
        tag.click = function() {
            var result = true;
            if (tag.onclick) {
                result = tag.onclick();
            }
            if (typeof (result) == 'undefined' || result) {
                eval(unescape(tag.href));
            }
        }
    }
}

function extendSession() {
    try {
        $.ajax({
            type: "POST",
            url: "/UserManagement/Login.aspx/ExtendSession",
            data: JSON.stringify({}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.

                setTimeout(showTimeoutMessageBox, parseInt(msg.d.toString()));
            }
        });
    } catch (ex) { }
}

function setCultureAndRefresh(culture) {
    eraseCookie("uiculture");
    createCookie("uiculture", culture, 365);
    window.location.reload();
}
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
