﻿/*
* Proffix 2011 Citation JAvascript
* Copyright (c) 2011 2sic Internet Solutions GmbH
* Author: Pascal Schweizer
*
* Requirements:
* jQuery 1.5.1
*/

// Register Proffix Namespace
var root = window;
root["Proffix"] = new Object();

$(function () {
    // Define the Url from the Webservice
    Proffix.WebServiceUrl = "/DesktopModules/ToSicCitation/ProffixWebService.asmx/GetCitations";
    Proffix.FadeSpeed = 1500;
    Proffix.TimeBeforeStart = 5000;
    Proffix.TimeUntilNext = 10000;

    // Default ajax request function (usable for other things)
    Proffix.AjaxRequest = function (WebServiceUrl, SuccessMethod, ErrorMethod) {
        $.ajax({
            type: "POST",
            url: WebServiceUrl,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) { SuccessMethod(msg) },
            error: function (xhr, status, error) { ErrorMethod(xhr, status, error) }
        })
    }

    // This method get the received citations and shows them
    Proffix.ShowCitation = function (msg) {
        for (var i = 0; i < msg.d.lstCitations.length; i++) {
            Proffix.AddCitation(msg.d.lstCitations[i].Quote, msg.d.lstCitations[i].Author, msg.d.lstCitations[i].Location, msg.d.lstCitations[i].Name);
        }
        $("#CitationList .TemplateItem").remove();
        $("#CitationList").attr("proffix:source", msg.d.Source);
        setTimeout(function () {

            $("#CitationList .CitationItem:first").show().addClass("visible");
            $("#Overlay").fadeOut(Proffix.FadeSpeed);


            setInterval(function () {
                Proffix.ShowNext();
            }, Proffix.TimeUntilNext);
        }, Proffix.TimeBeforeStart);
    }

    Proffix.AddCitation = function (Quote, Author, Location, Name) {
        var Citation = $("#CitationList .TemplateItem").clone().removeClass("TemplateItem");
        $(".Quote", Citation).html("&laquo;" + Quote + "&raquo;");
        $(".Author", Citation).html(Author);
        $(".Location", Citation).html(Location);
        $(".Name", Citation).html(Name);
        $("#CitationList").append(Citation);
    }

    // This method shows an error message if webservice request wasn't successfull
    Proffix.ShowErrorMessage = function (xhr, status, error) {
        alert('error');
    }

    // This method calls the ajaxrequest method to call the webservice
    Proffix.LoadCitations = function () {
        Proffix.AjaxRequest(Proffix.WebServiceUrl, Proffix.ShowCitation, Proffix.ShowErrorMessage);
    }

    Proffix.ShowNext = function () {

        // ToDo: Using ClearType Fading

        var Current = ($("#CitationList .CitationItem.visible").length != 0 ? $("#CitationList .CitationItem.visible") : $("#CitationList .CitationItem:first"));

        if (Current.length == 0) Current = $("#CitationList .CitationItem:first");

        var Next = ((Current.next().length) ? ((Current.next().hasClass(".visible")) ? $("#CitationList .CitationItem:first") : Current.next()) : $("#CitationList .CitationItem:first"));

        Current.hide().removeClass("visible");
        $("#Overlay").show();
        Next.show().addClass("visible");
        $("#Overlay").fadeOut(Proffix.FadeSpeed);

    }
});


$(document).ready(function () {
    Proffix.LoadCitations();
});




