﻿Ext.namespace('MSDImages');

MSDImages.HomePageApp = function() {

    //private space
    var tagSearch, freeSearch, btnSearch, btnTagSearch;

    function initFields() {

//        new Ext.Button({
//        applyTo: 'viewImages',
//            text: 'View images',
//            handler: function() {
//                window.location = "/search/index";
//            }
//        });

        tagSearch = new MSDImages.AutoCompleteComboBox({
            renderTo: "tagsearch",
            url: "/search/tagsearch",
            id: "Keywords"
        });

        freeSearch = new Ext.form.TextField({
            applyTo: 'freeText',
            allowBlank: true,
            blankText: 'Please enter a search term.',
            invalidText: 'Please enter a search term',
            msgTarget: 'under',
            width: 200
        });

        btnSearch = new Ext.Button({
            renderTo: 'btnSearch',
            id: 'extBtnSearch',
            text: 'Search',
			width: 80,
            handler: function() {
            if (validateSearchField()) {
                    MSDImages.Utils.Log('FreeTextSearch', freeSearch.getValue());
                    document.forms['freeTextForm'].submit();
                }
            }
        });

        btnTagSearch = new Ext.Button({
            applyTo: 'btnTagSearch',
            id: 'extBtnTagSearch',
            text: 'View Images',
			width: 80,
            handler: function() {
            if (validateKeywordField()) {
                    MSDImages.Utils.Log('DiseaseSearch', tagSearch.getValue());
                    document.forms['tagForm'].submit();
                }
            }
        });
    }

    function addCarousel(id) {
        new Ext.ux.Carousel(id, {
            interval: 3,
            autoPlay: false,
            itemSelector: 'a.carousel-item',
            pauseOnNavigate: true,
            freezeOnHover: true,
            navigationOnHover: false
        });
    }

    function validateKeywordField() {
        return tagSearch.isValid();
    }

    function validateSearchField() {
        return freeSearch.isValid();
    }

    //public space
    return {
        init: function() {
            initFields();
            addCarousel('topRatedImagesWrap');
            addCarousel('mostDownloadedImages');
        },

        validateKeywordSearch: function() {
            return validateKeywordField();
        },

        validateFreeSearch: function() {
            return validateSearchField();
        },

        viewImage: function(container, url, rating, imageId, numberOfVotes) {
            return MSDImages.Utils.showFigure(container, url, rating, imageId, null, numberOfVotes);
        }
    }

} ();