﻿var maxHomeDialogLength = 250;

function Timer() { }
Timer.interval = 20000;
Timer.id = 0;
Timer.on = function (state) {
    if (state == true) {
        clearInterval(Timer.id);
        Timer.id = setInterval("Stories.get(2)", Timer.interval);
    }
    else {
        clearInterval(Timer.id);
    }
}

$(document).ready(function () {

    $("#storiesHomeDialogLeft").dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        minWidth: 600,
        height: 400,
        show: "slide",
        hide: "slide",
        title: "Member Success Story",
        open: function () { Timer.on(false); },
        close: function () { Timer.on(true); }
    });

    $("#storiesHomeDialogRight").dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        minWidth: 600,
        height: 400,
        show: "slide",
        hide: "slide",
        title: "Member Success Story",
        open: function () { Timer.on(false); },
        close: function () { Timer.on(true); }
    });

    $("a.storyReadMoreLeft").click(function () {
        $("div.storiesHomeDialogLeft").dialog("open");
    });

    $("a.storyReadMoreRight").click(function () {
        $("div.storiesHomeDialogRight").dialog("open");
    });

    $("div.storiesHome").hover(
        function () { Timer.on(false); },
        function () {
            if (
                !($("div.storiesHomeDialogLeft").dialog("isOpen")) &&
                !($("div.storiesHomeDialogRight").dialog("isOpen"))
            )
                Timer.on(true); 
        }
    );

    Stories.get(2);
    Timer.on(true);
});

function Stories() { }
Stories.lastId = -1;
Stories.get = function (count) {
    $.ajax({
        url: "/webservices/StoriesService.svc/Stories/{0}/{1}".format(Stories.lastId, count),
        type: "GET",
        dataType: "json",
        success: Stories.getSuccess,
        error: Stories.getError
    });
}
Stories.getSuccess = function (data, textStatus, xmlHttpRequest) {

    Stories.lastId = data[data.length - 1].id;

    $("#storiesHomeContent").hide();

    var shortDescription = (data[0].shortDescription.length > maxHomeDialogLength ?
        data[0].shortDescription.substr(0, maxHomeDialogLength - 1) + "..." :
        data[0].shortDescription);

    $("div.storyLeftContent").html(Stories.successTemplate.format(data[0].imgUrl, shortDescription, data[0].name, "storyReadMoreLeft"));

    $("#dialogImgLeft").attr("src", data[0].imgUrl);
    $("#dialogTextLeft").html(data[0].archives);
    $("#storiesHomeDialogLeft").dialog("option", "title",
            (data[0].name ? "Member Success Story: {0}".format(data[0].name) : "Member Success Story")
        );

    if (data.length > 1) {

        shortDescription = (data[1].shortDescription.length > maxHomeDialogLength ?
        data[1].shortDescription.substr(0, maxHomeDialogLength - 1) + "..." :
        data[1].shortDescription);

        $("div.storyRightContent").html(Stories.successTemplate.format(data[1].imgUrl, shortDescription, data[1].name, "storyReadMoreRight"));

        $("#dialogImgRight").attr("src", data[1].imgUrl);
        $("#dialogTextRight").html(data[1].archives);
        $("#storiesHomeDialogRight").dialog("option", "title",
            (data[1].name ? "Member Success Story: {0}".format(data[1].name) : "Member Success Story")
        );

    }
    else {
        $("div.storyRightContent").html(Stories.emptyTemplate);
    }

    $("div.storiesHome img.buttons").show();
    $("a.storyReadMoreLeft").show();
    $("a.storyReadMoreRight").show();
    $("#storiesHomeContent").show();
}
Stories.getError = function (xmlHttpRequest, textStatus, errorThrown) {
    $("div.storiesHome img.buttons").hide();
    $("a.storyReadMoreLeft").hide();
    $("a.storyReadMoreRight").hide();
    $("div.storyLeftContent").html(Stories.errorTemplate);
    $("div.storyRightContent").html(Stories.emptyTemplate);
}
Stories.successTemplate =
    "<div class=\"text\"><img class=\"left\" alt=\"\" src=\"{0}\" />{1}</div><p class=\"name\"><strong>{2}</strong></p>";
Stories.errorTemplate =
    "<div class=\"text\">{0}</div>".format(com.byreferralonly.constants.CANNOT_ACCESS_STORIES_TEMPLATE);
Stories.emptyTemplate = "&nbsp;";

function getStoriesSuccess(data, textStatus, xmlHttpRequest) {

    lastId = data[data.length - 1].id;


    $("#slider").hide("slide", { direction: "left" }, 1000);
    $("#imgStory").attr("src", data[0].imgUrl);
    $("#storyText").html(shortDescription);
    $("#storyName").html(data[0].name);
    $("#slider").show("slide", { direction: "left" }, 1000);

    $("#storiesRightDialog #dialogImg").attr("src", data[0].imgUrl);
    $("#storiesRightDialog #dialogText").html(data[0].archives);
    $("#storiesRightDialog").dialog("option", "title",
            (data[0].name ? "Member Success Story: {0}".format(data[0].name) : "Member Success Story")
        );
}

