var frontend = function () {
	return {
		setup: function () {			
			$(window).resize(function() {
				frontend.ui.doSize();
			});
			
			frontend.ui.doSize();

			frontend.ui.bind_links();
		}
	}
} ();

frontend.ui = function () {
    return {
        bind_links: function () {
            $('a[rel=onyx-ajax]').unbind('click').click(frontend.ajax.trigger);
            $('a[rel=event-ajax]').unbind('click').click(frontend.ui.event_preview);


            $('.event-list-item').unbind().hover(frontend.events.previewRollover, frontend.events.previewRollout);
            $('.large-home').unbind('focus').focus(frontend.ui.inputFocus);
            $('.large-home').unbind('blur').blur(frontend.ui.inputBlur);
            $('.onyx-front-ajax').unbind('click').click(frontend.events.previewOn);
            $('.close-link').unbind('click').click(frontend.events.previewOff);

            $('textarea[name=homeSuggest]').unbind('keyup').keyup(frontend.ui.suggestType);
            $('input[name=homeSearch]').unbind('keyup').keyup(frontend.ui.searchType);

            $('#close-highlights').unbind('click').click(frontend.ui.close_highlights);

            $('#suggest-smit').unbind('click').click(frontend.ui.doSuggest);

            $('.cell-inactive').css('opacity', .2);

        },
        doSuggest: function () {
            var sug = $('textarea[name=homeSuggest]').val();
            var name = $('input[name=suggestionName]').val();
            var email = $('input[name=suggestionEmail]').val();

            if (sug.length >= 14 && name.length >= 3 && email.length >= 10) {
                options = { data: { suggestion: sug, sName: name, sEmail: email} };
                frontend.ajax.request($('#suggest-url').attr('href'), options);
            }
            else {
                alert("Sorry, please ensure your suggestion is longer than 14 letters,\nYour name longer than 3,\nand your e-mail longer than 14!");
            }

        },
        searchType: function (e) {
            var str = $('input[name=homeSearch]').val();

            if ($.trim(str).length > 1) {
                if (e.which >= 48 && e.which <= 90 || e.which >= 96 && e.which <= 111 || e.which == 190 || e.which == 188 || e.which == 189 || e.which == 8 || e.which == 46) {
                    options = { data: { query: str} };

                    frontend.ajax.request($('#search-url').attr('href'), options, true);
                }
            }
        },
        close_highlights: function () {
            $('#picks-block').prependTo('#center-side');
            $('#calendar-block').show();
            $('#home-block').show();
        },
        highlights_right: function () {
            if ($(window).width() <= 1100) {
                $('#picks-block').prependTo('#right-side');
                $('#calendar-block').hide();
            }

            $('#home-block').show();
            $('#picks-block').removeClass('picks-active');

            //$('#need-highlights').hide();
        },
        event_preview: function (e) {
            frontend.events.previewOn();
            frontend.ajax.trigger(e, true);

            return false;
        },
        update_element: function (jsonResponse) {
            $('#' + jsonResponse.domElement).html(jsonResponse.domHtml);
            frontend.ui.bind_links();
        },
        update_subelement: function (jsonResponse) {
            $('#' + jsonResponse.domSubElement).html(jsonResponse.domSubHtml);
            frontend.ui.bind_links();
        },
        after_action: function (jsonResponse) {
            switch (jsonResponse.responseSubType) {
                case "close":
                    frontend.ui.overlay.close();
                    break;
                case "update":
                    frontend.ui.update_subelement(jsonResponse);
                    break;
                case "preview":
                    frontend.ui.preview.show(jsonResponse);
                    break;
                case "highlights":
                    frontend.ui.highlights_right();
                    break;
                case "suggested":
                    $('#make-suggestion').hide();
                    break;
                case "picks-active":
                    $('#picks-block').addClass('picks-active');

                    if ($(window).width() <= 1100) {
                        $('#picks-block').prependTo("#left-side");
                        $('#home-block').hide();
                    }
                    break;
            }
        },
        suggestType: function () {
            var str = $('textarea[name=homeSuggest]').val();

            if ($.trim(str) != "") {
                var rows = $('textarea[name=homeSuggest]').val().length / 30;

                rows = Math.ceil(rows);

                if (rows == 0) {
                    $('textarea[name=homeSuggest]').attr('rows', 1);
                }
                else {
                    $('textarea[name=homeSuggest]').attr('rows', rows);
                }

                if (str.length >= 14) {

                    $('#suggest-are-you-ready').show();
                    $('#suggest-smit').show();
                }
                else {
                    $('#suggest-smit').hide();
                    $('#suggest-are-you-ready').hide();
                }

                str = str.replace("<", "&lt;");
                str = str.replace(">", "&gt;");

                $('#suggestion-text').html('"' + str + '"');

                $('#suggest-block').show();
                $('#calendar-block, #vote-block').hide();
            }
            else {
                $('#suggestion-text').html('');
                $('#suggest-block').hide();
                $('#calendar-block, #vote-block').show();
            }
        },
        inputFocus: function () {
            var str = 'type to search...';

            if ($(this).attr('name') == 'homeSuggest') {
                str = 'type to suggest...';
            }

            if ($(this).val() == str) {
                $(this).val('').addClass('focus');
            }
        },
        inputBlur: function () {
            var str = 'type to search...';

            if ($(this).attr('name') == 'homeSuggest') {
                str = 'type to suggest...';
            }

            if ($.trim($(this).val()) == '') {
                $(this).val(str).removeClass('focus');
            }
        },
        doSize: function () {
            var width = $(window).width();

            $('#page').removeClass();

            if (width <= 1500) {
                $('#page').attr('class', 'width-1280');
            }
            if (width <= 1270) {
                $('#page').attr('class', 'width-1270');
            }
            if (width <= 1100) {
                $('#page').attr('class', 'width-1100');

                if ($('.picks-active').length > 0) {
                    $('#picks-block').prependTo("#left-side");
                    $('#home-block').hide();
                }
            }

            if (width > 1100) {
                $('#picks-block').prependTo("#center-side");
                $('#home-block').show();
                $('#calendar-block').show();
            }

            if (width > 1500) {
                $('#page').attr('class', 'width-large');
            }

            $('.cell').css('height', $('.cell').width());
        }
    }
} ();

frontend.events = function () {
    return {
        previewOn: function () {
            $('#event-view-ajax').html('');
            $('#event-view-block').stop().css({ opacity: 0, top: 50, display: 'block' })
									.animate({ opacity: 1, top: 92 }, 500, 'easeOutQuint');
        },
        previewOff: function () {
            $('#event-view-block').stop().animate({ opacity: 0, top: 50 }, 500, 'easeOutQuint', function () { $(this).hide(); $('#event-view-ajax').html(''); });
            return false;
        },
        previewRollover: function () {
            var height = $(this).find('.name').height() + 40;
            height += $(this).find('.event-list-item-extended').height();
            $(this).stop().animate({ height: height }, 500);

            $(this).find('.event-list-category-indicator').height(height);
        },
        previewRollout: function () {
            $(this).stop().animate({ height: 38 }, 500);
        }
    }
} ();

frontend.ajax = function () {
    var currentRequest, loadTimer;
    return {
        trigger: function (source, loading) {
            $('input[name=homeSearch]').val('type to search...').removeClass('focus');

            if ($(source.target).attr('href') != undefined) {
                frontend.ajax.request($(source.target).attr('href'), null, loading);
            }
            else {
                frontend.ajax.request($(source.target).parents('a[href]').attr('href'), null, loading);
            }
            return false;
        },
        request: function (url, options, hideLoading) {
            frontend.ajax.beingLoadingSequence(null, null, null, hideLoading);

            if (currentRequest) {
                currentRequest.abort();
            }

            config = {
                url: url,
                type: "POST",
                cache: false,
                dataType: 'json',
                success: frontend.ajax.delegateResponse
            };

            if (options)
                jQuery.extend(config, options);

            currentRequest = $.ajax(config);
        },
        beingLoadingSequence: function (a, f, o, hideLoading) {
            loadTimer = setTimeout(function () { frontend.ajax.showLoading(a, f, o, hideLoading) }, 500);
        },
        showLoading: function (a, f, o, hideLoading) {
            $('#loading').show();
            if (hideLoading == null) {
                $('#overlay, #loading-indicator').css({ opacity: 0, display: "block" }).animate({ opacity: .6 }, 2000);
            }
        },
        delegateResponse: function (jsonResponse) {
            clearTimeout(loadTimer);

            $('#loading, #overlay, #loading-indicator').hide();

            switch (jsonResponse.responseType) {
                case "overlay":
                    frontend.ui.overlay.delegate(jsonResponse);
                    frontend.ui.after_action(jsonResponse);
                    break;
                case "alert":
                    alert(jsonResponse.alertMessage);
                    break;
                case "update":
                    frontend.ui.update_element(jsonResponse);
                    frontend.ui.after_action(jsonResponse);
                    break;
            }
        }
    };
} ();

$(function () {
	frontend.setup()
});
