﻿var Landor = Landor || {};

Landor.Lens = function (canvas, clickHandler)
{
    this._canvas = canvas;
    this._clickHandler = clickHandler;
    this._filterIds = new Array();
    this._values = new Array();

    this._container = null;

    this._Init();
}

Landor.Lens.prototype =
{
    LoadQuestions: function (callback) {
        $.ajax({
            'url': '/data-services/clientservice.svc/getlensitems?nocache=' + Math.random(),
            'context': this,
            'contentType': 'application/json',
            'dataType': 'json',
            'success': function (content) {
                if (null == content || null == content.d) {
                    return;
                }

                this._DisplayContent(content.d);

                if (null != callback) {
                    callback();
                }
            },
            'error': function (jqXHR, textStatus, errorThrown) {
                alert(textStatus);
                alert(errorThrown);
            }
        });
    },

    _AnswerHandler: function (e) {
        var clicked = $(e.target);

        var lastDiv = $('.questions h1').last().attr('id');
        var currentDiv = clicked.parent().parent();

        var answer = clicked.text();
        var answerString = $('.answers').text();

        this._filterIds.push(answer);
        this._values.push(clicked[0].id);

        if (currentDiv.attr('id') != lastDiv) {
            answerString += answer + ' + ';
            currentDiv.fadeOut(100);
            currentDiv.next().fadeIn(500);
        }
        else {
            answerString += answer;
            currentDiv.fadeOut(100);
            $('#submit').css('display', 'block').fadeIn(500);
        };

        $('.answers').text(answerString);
    },

    _LiftOffHandler: function () {
        this._canvas.SetFilters(this._filterIds, this._values);

        if ('#/what-the-l/' != window.location.hash) {
            window.location.hash = '/what-the-l/';
        }
        else {
            $.ajax({
                'url': '/data-services/clientservice.svc/getdocumentcontent',
                'context': this,
                'type': 'POST',
                'contentType': 'application/json',
                'dataType': 'json',
                'data': JSON.stringify({ "url": window.location.hash, "filters": this._filterIds, "values": this._values }),
                'success': function (content) {
                    if (null == content || null == content.d) {
                        return;
                    }

                    if (null != content.d.HtmlContent && 0 != content.d.HtmlContent.length) {
                        this._canvas.DisplayContent(content.d.HtmlContent);
                    }
                    else if (null != content.d.ContentViews && 0 != content.d.ContentViews.length) {
                        this._canvas.LayoutTiles(content.d.ContentViews);
                    }
                },
                'error': function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                    alert(errorThrown);
                }
            });
        }

        this._clickHandler();
        $('h1[id^="question"]', this._container).remove();
    },

    _Init: function () {
        this._container = $(".questions");

        $('.liftoff').click($.proxy(this._LiftOffHandler, this));

    },

    _DisplayContent: function (content) {
        for (var i = 0; i < content.length; ++i) {
            question = $('<h1 id="question' + i + '" class="blackText"></h1>');
            question.append($('<span class="landorBold"><a id="' + content[i].LeftValue + '">' + content[i].Left + '</a></span>'));
            question.append(' OR ');
            question.append($('<span class="landorBold"><a id="' + content[i].RightValue + '">' + content[i].Right + '</a></span>'));

            this._container.append(question);
        }

        $('a', this._container).each($.proxy(function (index, item) {
            $(item).click($.proxy(this._AnswerHandler, this));
        }, this));
    }
}
