﻿function Site() {
};

var site = new Site();

var scrollSpeed = 40;   // Speed in milliseconds
var step = 1;           // How many pixels to move per step
var current = 0; 		// The current pixel row
var imageWidth = 960; 	// Background image width
var headerWidth = 3000; // How wide the header is.

//The pixel row where to start a new loop
var restartPosition = -(imageWidth - headerWidth);

Site.prototype.retrieveData = function (path, parameters, funStart, funEnd, funHandleData) {
    funStart();

    // retrieve JSON result
    $.getJSON(
        path,
        parameters,
        function (data) {
            // handle incoming data
            funHandleData(data);

            funEnd();
        }
    );
};

Site.prototype.initIndex = function (baseUrl) {
    $('#updater').hide();

    $('#email').focus();

    $('#submit').click(function (event) {
        event.preventDefault();
        site.retrieveData(baseUrl + "/Home/Email",
            {
                email: $('#email')[0].value,
                question: $('#question')[0].value
            },
            function () {
                $('#email-form').hide();
                $('#updater').show();
            },
            function () {
                $('#updater').hide();
                $('#email-form').show();
            },
            function (data) {
                $('#response > div').remove();
                $('#response').append("<div style='padding: 2px;'>" + data + "</div>");
                $('#response > div').highlightFade({ speed: 1500 });
            });
    });
};

Site.prototype.initHeader = function () {
    //Calls the scrolling function repeatedly
    var init = setInterval("site.scrollBackground()", scrollSpeed);
};

Site.prototype.scrollBackground = function () {
    //Go to next pixel row.
    current -= step;

    //If at the end of the image, then go to the top.
    if (current == restartPosition) {
        current = 0;
    }

    //Set the CSS of the header.
    $('#head').css("background-position", current + "px 0");
};

Site.prototype.showComments = function (baseurl, id) {
    $.ajax({
        url: baseurl + "/blog/comments",
        data: { id: id },
        dataType: "html",
        type: "GET",
        cache: false,
        beforeSend: function (XMLHttpRequest) {
            $("#updater").show();
            $("#comments-container").show();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $("#comments-container").empty();
            $("#comments-container").html("Sorry, there were issues grabbing the comments. They weren't that important anyway.");
        },
        success: function (data) {
            $(".comments").hide();
            $("#comments-container").empty();
            $("#comments-container").html(data);
            $("#name").focus();
            site.wirePostComment(baseurl);
        }
    });
};

Site.prototype.wirePostComment = function (baseurl) {
    $('#submit').click(function (event) {
        event.preventDefault();
        $.ajax({
            url: baseurl + "/blog/postcomment",
            data:
            {
                source: $("#source-name").val(),
                postId: $("#post-id").val(),
                name: $("#name").val(),
                comment: $("#comment").val()
            },
            dataType: "html",
            type: "POST",
            cache: false,
            beforeSend: function () {
                $('#comment-form').hide();
                $('#comment-updater').show();
            },
            error: function () {
                $('#comment-updater').hide();
                $('#comment-form').show();
            },
            success: function (data) {
                //site.showComments(baseurl, id);
                $("#comments-container").empty();
                $("#comments-container").html(data);
                $("#name").focus();
                site.wirePostComment(baseurl);
            }
        });
    });
};
