﻿var mediaBoxStatus = 0;

function loadMediaBox() {
    if (mediaBoxStatus == 0) {
        $("#mediaBoxBackground").css({
            "opacity": "0.7"
        });
        $("#mediaBoxBackground").fadeIn("fast");
        $("#mediaBoxContainer").fadeIn("normal");
        mediaBoxStatus = 1;
    }
}

function disableMediaBox() {
    if (mediaBoxStatus == 1) {
        $("#mediaBoxContainer").fadeOut("fast");
        $("#mediaBoxBackground").fadeOut("normal");
        mediaBoxStatus = 0;
    }
}

function centerMediaBox() {
    var imgWidth = $("#mediaBoxImage").width();
    if (imgWidth > 350) $("#mediaBoxContainer").css({ "width": (imgWidth + 50) });

    var contentHeight = 430;
    if ($("#mediaBoxTipAFriend").css("display") == "block") contentHeight = 210;
    if (contentHeight > 0) $("#mediaBoxContainer").height(contentHeight);

    var winHeight = document.documentElement.clientHeight;
    var winWidth = document.documentElement.clientWidth;
    var boxHeight = $("#mediaBoxContainer").height();
    var boxWidth = $("#mediaBoxContainer").width();
    $("#mediaBoxContainer").css({
        "position": "absolute",
        "top": ($(document).scrollTop() + ((winHeight / 2) - (boxHeight / 2) - 100)),
        "left": ((winWidth / 2) - (boxWidth / 2))
    });
    $("#mediaBoxBackground").css({
        "height": $(document).height()
    });
    var navWidth = 250;
    $("#mediaBoxNavigation").css({
        "padding-left": ((boxWidth - navWidth) / 2)
    });
    $("#mediaBoxClose").css({ "width": ($("#mediaBoxContainer").width()) });
}

function hideBoxes() {
    $("#mediaBoxTipAFriendOutput").hide();
    $("#mediaBoxTipAFriend").hide();
    $("#mediaBoxTextContent").hide();
    $("#mediaBoxFileMovieContent").hide();
    $("#mediaBoxWebMovieContent").hide();
    $("#mediaBoxImageContent").hide();
    $("#mediaBoxNavigation").hide();
}

$(document).ready(function() {
    $("#mediaBoxBackground").click(function() { disableMediaBox(); });
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1)
            disableMediaBox();
    });
});

function openMediaBox_BookExtract(bookId) {
    hideBoxes();
    $("#mediaBoxTextContent").show();
    $("#mediaBoxContainer").css({ "width": 500 });

    $.ajax({
        url: "/Site/Services/Article.asmx/GetExtract",
        type: "post",
        contentType: "application/json",
        data: $.toJSON({ bookSourceId: bookId }),
        dataType: "json",
        processData: false,
        success: function(data, textStatus) {
            var dataArray = data.d.split("|");
            $("#mediaBoxHeading").html(dataArray[0]);
            $("#mediaBoxTextContent").html(dataArray[1]);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            var jsonError = $("#mediaBoxTextContent").html(XMLHttpRequest.responseText);
        }
    });

    centerMediaBox();
    loadMediaBox();
}

/* Start - Book SpreadImage */

var i = 1;
var imgUrl = "";
var hasPrevious = false;
var hasNext = false;

function openMediaBox_BookSpreadImage(bookId) {
    hideBoxes();
    $("#mediaBoxImageContent").show();
    $("#mediaBoxNavigation").show();
    $("#mediaBoxContainer").css({ "width": 500 });
    centerMediaBox();
    loadMediaBox();

    $("#mediaBoxImage").attr("src", "/Site/Themes/GOD/Images/loading.gif");
    $("#mediaBoxImage").ready(function() { centerMediaBox(); });

    $.ajax({
        url: "/Site/Services/Article.asmx/GetBookSpreadImage",
        type: "post",
        contentType: "application/json",
        data: $.toJSON({ bookSourceId: bookId, imageId: i }),
        dataType: "json",
        processData: false,
        success: function(data, textStatus) {
            if (data.d.length > 0) {
                var dataArray = data.d.split("|");
                $("#mediaBoxHeading").html(dataArray[0]);
                $("#mediaBoxImage").attr("src", dataArray[1]);
                $("#mediaBoxImage").ready(function() { centerMediaBox(); });

                if (dataArray[2] == "1") {
                    $("#mediaBoxPreviousImage1").show();
                    $("#mediaBoxPreviousImage2").show();
                } else {
                    $("#mediaBoxPreviousImage1").hide();
                    $("#mediaBoxPreviousImage2").hide();
                }

                if (dataArray[3] == "1") {
                    $("#mediaBoxNextImage1").show();
                    $("#mediaBoxNextImage2").show();
                } else {
                    $("#mediaBoxNextImage1").hide();
                    $("#mediaBoxNextImage2").hide();
                }
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { var jsonError = alert(XMLHttpRequest.responseText); }
    });

    $("#mediaBoxPreviousImage1").attr("href", "javascript:previous_BookSpreadImage('" + bookId + "')");
    $("#mediaBoxPreviousImage2").attr("href", "javascript:previous_BookSpreadImage('" + bookId + "')");
    $("#mediaBoxNextImage1").attr("href", "javascript:next_BookSpreadImage('" + bookId + "')");
    $("#mediaBoxNextImage2").attr("href", "javascript:next_BookSpreadImage('" + bookId + "')");
}

function next_BookSpreadImage(bookId) { i++; openMediaBox_BookSpreadImage(bookId); }

function previous_BookSpreadImage(bookId) { i--; openMediaBox_BookSpreadImage(bookId); }

/* End - Book SpreadImage */

/* Start - Book MovieWeb */
function openMediaBox_BookWebMovie(bookId) {
    hideBoxes();
    $("#mediaBoxWebMovieContent").show();
    $("#mediaBoxWebMovieContent").html('<img src="/Site/Themes/GOD/Images/loading.gif" />');
    $("#mediaBoxContainer").css({ "width": 654 });
    centerMediaBox();
    loadMediaBox();

    $.ajax({
        url: "/Site/Services/Article.asmx/GetMovieWeb",
        type: "post",
        contentType: "application/json",
        data: $.toJSON({ bookSourceId: bookId }),
        dataType: "json",
        processData: false,
        success: function(data, textStatus) {
            if (data.d.length > 0) {
                var dataArray = data.d.split("|");
                $("#mediaBoxHeading").html(dataArray[0]);
                $("#mediaBoxWebMovieContent").html(dataArray[1]);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { var jsonError = alert(XMLHttpRequest.responseText); }
    });
}
/* End - Book MovieWeb */

/* Start - Tipsa en vän */
function openMediaBox_TipAFriend() {
    hideBoxes();
    $("#mediaBoxHeading").html("");
    $("#tipafriend_toemail").val("");
    $("#tipafriend_toname").val("");
    $("#mediaBoxTipAFriendOutput").html("");
    $("#mediaBoxTipAFriend").show();
    $("#mediaBoxContainer").css({ "width": 500 });
    centerMediaBox();
    loadMediaBox();
}

function send_TipAFriend() {
    var from_email = $("#tipafriend_fromemail").val();
    var from_name = $("#tipafriend_fromname").val();
    var to_email = $("#tipafriend_toemail").val();
    var to_name = $("#tipafriend_toname").val();
    var raw_url = location.href;
    var msg = '';

    if (!validateEmail(from_email)) { msg += 'Din e-postadress är inte giltig.'; }
    if (from_name === '') { msg += 'Du har inte skrivit in ditt namn.'; }
    if (!validateEmail(to_email)) { msg += 'Mottagarens e-postadress är inte giltig.'; }
    if (to_email === '') { msg += 'Du har inte skrivit in mottagares namn.'; }

    if (msg != '') {
        alert(msg);
    } else {
        $("#tipafriend_toemail").val("");
        $("#tipafriend_toname").val("");

        $("#mediaBoxTipAFriend").hide();
        $("#mediaBoxTipAFriendOutput").show();
        $("#mediaBoxTipAFriendOutput").html("<img src='/Site/Themes/GOD/Images/loading.gif' style='margin: 0 auto 0 auto;' />");

        $.ajax({
            url: "/Site/Services/Article.asmx/TipAFriend",
            type: "post",
            contentType: "application/json",
            data: $.toJSON({ fromEmail: from_email, fromName: from_name, toEmail: to_email, toName: to_name, url: location.href }),
            dataType: "json",
            processData: false,
            success: function(data, textStatus) { $("#mediaBoxTipAFriendOutput").html(data.d); },
            error: function(XMLHttpRequest, textStatus, errorThrown) { var jsonError = $("#mediaBoxTipAFriendOutput").html(XMLHttpRequest.responseText); }
        });
    }
}

/* Slut - Tipsa en vän */

/* Start - Person film */
function openMediaBox_PersonFileMovie(personId) {
    hideBoxes();
    $("#mediaBoxFileMovieContent").show();
    $("#mediaBoxFileMovieContent").html('<img src="/Site/Themes/GOD/Images/loading.gif" />');
    $("#mediaBoxContainer").css({ "width": 654 });
    centerMediaBox();
    loadMediaBox();

    $.ajax({
        url: "/Site/Services/Person.asmx/GetMovieFile",
        type: "post",
        contentType: "application/json",
        data: $.toJSON({ personId: personId }),
        dataType: "json",
        processData: false,
        success: function(data, textStatus) {
            if (data.d.length > 0) {
                var dataArray = data.d.split("|");
                $("#mediaBoxHeading").html(dataArray[0]);
                $("#mediaBoxFileMovieContent").html(dataArray[1]);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { var jsonError = alert(XMLHttpRequest.responseText); }
    });
}

function openMediaBox_PersonWebMovie(personId) {
    hideBoxes();
    $("#mediaBoxWebMovieContent").show();
    $("#mediaBoxWebMovieContent").html('<img src="/Site/Themes/GOD/Images/loading.gif" />');
    $("#mediaBoxContainer").css({ "width": 654 });
    centerMediaBox();
    loadMediaBox();

    $.ajax({
        url: "/Site/Services/Person.asmx/GetMovieWeb",
        type: "post",
        contentType: "application/json",
        data: $.toJSON({ personId: personId }),
        dataType: "json",
        processData: false,
        success: function(data, textStatus) {
            if (data.d.length > 0) {
                var dataArray = data.d.split("|");
                $("#mediaBoxHeading").html(dataArray[0]);
                $("#mediaBoxWebMovieContent").html(dataArray[1]);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { var jsonError = alert(XMLHttpRequest.responseText); }
    });
}

/* End - Person film */
