var lastRequest;
var projectIndex = 0;
var projectType = "Logo";

$(document).ready(function() {
    $("#join").click(function(event) {
        event.preventDefault();
        emailRegex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
        if (emailRegex.test($("#footer #email").val())) {
            SubscribeMailingList($("#footer #email").val());
        }
        else {
            $("#confirmation .indicator").hide();
            $("#confirmation .complete .message").html("Please enter a<br />valid email address.");
            $("#confirmation").modal({ onClose: modalClose });
        }
    });

    $("#confirmation .complete a").click(function() {
        $("#confirmation").hide();
        return false;
    });

    $("#footer #email").focus(function() { if ($(this).val() == "your email address") $(this).val(""); });
    $("#footer #email").blur(function() { if ($(this).val() == "") $(this).val("your email address"); });

    $("#accordion").accordion({ header: "h2", active: false, animated: "bounceslide", autoHeight: false, alwaysOpen: false });
    $(".ui-accordion").bind("accordionchange", function(event, ui) {
        AccordionChange(ui.newContent);
    });

    $("a#logo").click(function() {
        $("#accordion").accordion("activate", false);
        return false;
    });

    $("a#sm2-1").click(function() {
        $("#sm2 a").removeClass("current");
        $(this).addClass("current");
        projectIndex = 0;
        projectType = "Logo";
        ProjectChanged();
        return false;
    });

    $("a#sm2-2").click(function() {
        $("#sm2 a").removeClass("current");
        $(this).addClass("current");
        projectIndex = 0;
        projectType = "Print";
        ProjectChanged();
        return false;
    });

    $("a#sm2-3").click(function() {
        $("#sm2 a").removeClass("current");
        $(this).addClass("current");
        projectIndex = 0;
        projectType = "Website";
        ProjectChanged();
        return false;
    });

    $("a#sm2-4").click(function() {
        $("#projects #projectLoading").show();
        $("#sm2 a").removeClass("current");
        $(this).addClass("current");

        if (lastRequest && lastRequest.readyState != 4) {
            lastRequest.abort();
        }

        lastRequest = $.ajax({
            type: "POST",
            url: "Default.aspx/GetPage",
            data: "{'id':'5'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                $("#projects .subsection-content").setTemplateURL('inc/templates/content/clients.html', null, { filter_data: false });
                $("#projects .subsection-content").processTemplate(data.d);
                $("#projects .paging").hide();
                $("#projects #projectLoading").hide();
            }
        });
        return false;
    });

    $("#projects a#next").click(function(event) {
        event.preventDefault();
        projectIndex++;
        ProjectChanged();
    });

    $("#projects a#prev").click(function(event) {
        event.preventDefault();
        projectIndex--;
        ProjectChanged();
    });

    if (jQuery.url.param("page")) {
        var pageId = parseInt(jQuery.url.param("page"));
        switch (pageId) {
            case 1:
                $("#accordion").accordion("activate", 0);
                break;
            case 2:
                $("#accordion").accordion("activate", 2);
                break;
            case 3:
                $("#accordion").accordion("activate", 3);
                break;
            case 4:
                $("#accordion").accordion("activate", 4);
                break;
            case 5:
                $("#accordion").accordion("activate", 1);
                break;
        }
    }
});

function AccordionChange(content) {
    if (lastRequest && lastRequest.readyState != 4) {
        lastRequest.abort();
    }

    if (content[0]) {
        var ids = content[0].id.split('_');
        switch (ids[0]) {
            case "page":
                lastRequest = $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetPage",
                    data: "{'id':'" + ids[1] + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        SetPageContent(content, data.d);
                    }
                });
                break;
            case "projects":
                SetProjectContent();
                break;
        }
    }
}

function SetPageContent(container, data) {
    container.setTemplateURL('inc/templates/content/page.html', null, { filter_data: false });
    container.processTemplate(data);
}

function SetProjectContent() {
    if (lastRequest && lastRequest.readyState != 4) {
        lastRequest.abort();
    }

    lastRequest = $.ajax({
        type: "POST",
        url: "Default.aspx/GetProject",
        data: "{'projectType':'" + projectType + "','projectIndex':'" + projectIndex + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            $("#projects .subsection-content").setTemplateURL('inc/templates/content/project.html', null, { filter_data: false });
            $("#projects .subsection-content").processTemplate(data.d["First"]);
            var projectCount = data.d["Second"];
            $("#projects .paging #which").text(projectIndex + 1);
            $("#projects .paging #count").text(projectCount);

            if (projectCount > 1) {
                if (projectIndex + 1 < projectCount) {
                    $("#projects a#next").show();
                }
                else {
                    $("#projects a#next").hide();
                }

                if (projectIndex > 0) {
                    $("#projects a#prev").show();
                }
                else {
                    $("#projects a#prev").hide();
                }

                $("#projects .paging").show();
            }
            else {
                $("#projects .paging").hide();
            }

            $("#projects #projectLoading").hide();
        }
    });
}

function ProjectChanged() {
    $("#projects #projectLoading").show();
    SetProjectContent();
}

function SubscribeMailingList(email) {
    $("#confirmation .indicator").show();
    $("#confirmation .complete").hide();
    $("#confirmation").modal({ onClose: modalClose });

    lastRequest = $.ajax({
        type: "POST",
        url: "Default.aspx/SubscribeMailingList",
        data: "{'email':'" + email + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            $("#confirmation .indicator").hide();
            $("#confirmation .complete .message").html(data.d);
            $("#confirmation .complete").show();
        }
    });
}

function modalClose(modal) {
    modal.data.fadeOut('slow', function() {
        modal.container.hide('slow', function() {
            modal.overlay.slideUp('slow', function() {
                $.modal.close();
            });
        });
    });
}