// ++=========================================================================++
// || DTO ShowCase v2.0.0 - 213
// || Copyright © 2008-2010 Drive Thru Online, Inc. All Rights Reserved.
// || This file may not be redistributed in whole or significant part, or
// || used on web site without licensing of the enclosed code,
// || and software features.
// || http://www.drivethruonline.com | info@drivethruonline.com
// || Downloaded 19:12, Fri Jul 16th 2010
// || 623790213_604952770317
// ++ ========================================================================++

function doOnLoad()
{
    showcaseSearchApp = DTO_ShowcaseSearch_new();
    showcaseSearchApp.gatherConfig();
    showcaseSearchApp.fetchPhrases();
    //called from fetchPhrases()
    //showcaseSearchApp.createApp();
    //showcaseSearchApp.fetchInitial();
    //showcaseSearchApp.redrawAll();
}

/*--------------------------------------------------------------*/

function DTO_ShowcaseSearch()                         {}

function DTO_ShowcaseSearch_new()
{
    var obj = new DTO_ShowcaseSearch();
    DTO_ShowcaseSearch_init(obj);
    return obj;
}

function DTO_ShowcaseSearch_init(obj)
{
    //used for all modes
    obj.config = null;
    obj.mainView = null;
    
    //used for recent modified and new showcase browse mode    
    obj.browseMgr = null;
    
    //used for search mode
    obj.searchMgr = null;
    obj.resultMgr = null;
    obj.phrases = [];
    
    obj.gatherConfig = function()
    {
        this.config = showcaseSearchConfig.config;
        
        showDebug(this.config.dto_debugger);
        debug('reload page');        
    }
    
    obj.baseURL = function()
    {
        return this.config.baseURL;
    }
    
    obj.mode = function()
    {
        return this.config.mode;    
    }
    
    obj.showcaseTypeID = function()
    {
        return this.config.showcaseTypeID;    
    }
    
    obj.gridWidth = function()
    {
        return this.config.gridWidth;
    }
    
    obj.fetchPhrases = function()
    {
        if(this.mode() == 'search')
        {
            var url = this.baseURL() + '/dto_showcase_search.php?do=fetchPhrases';   
            var ajaxCall = DTO_AjaxCall_new(this, 'fetchPhrases');
            ajaxCall.call(url);    
        }
        else
        {
            showcaseSearchApp.createApp();
            showcaseSearchApp.fetchInitial();
            showcaseSearchApp.redrawAll();
        }
    }
    
    obj.ajaxCallback = function(tag, resp)
    {
        if(tag == 'fetchPhrases')
        {
            eval(resp);
            DTO_Phrase_buildLookup(this.phrases);
            showcaseSearchApp.createApp();
            showcaseSearchApp.fetchInitial();
            showcaseSearchApp.redrawAll();
        }    
    }
    
    obj.addPhrase = function(phrase)                {this.phrases.push(phrase);}

    obj.createApp = function()
    {
        var mode = this.mode();
        if(mode == 'search')
        {
            this.searchMgr = DTO_ShowcaseSearchMgr_new(this);
            this.resultMgr = DTO_ShowcaseResultsMgr_new(this);
        
            this.mainView = DTO_ShowcaseMainView_new("showcaseSearchMainView"); 
            this.mainView.createSubviews();
            
            //glue some defaults
            //this.mainView.searchView.vendorView.setSelected(this.searchMgr.selectedVendorID);
        
            //plug outlets together
            this.mainView.searchView.showcaseTypeView.setFormatter(this.searchMgr.showcaseTypeFormatter);
            
    
            this.mainView.searchView.showcaseTypeView.setOnChange(function(value) {var self = obj.searchMgr; self.doShowcaseType(value);});
            this.mainView.searchView.usernameView.setOnChange(function(value)     {var self = obj.searchMgr; self.doUsername(value);});
            this.mainView.searchView.submitButton.setOnClick(function(value)      {var self = obj.searchMgr; self.doSubmit(value);});
        }
        else if(mode == 'modified' || mode == 'new' || mode == 'popular')
        {
            this.browseMgr = DTO_ShowcaseBrowseMgr_new(this);
        
            this.mainView = DTO_ShowcaseBrowseMainView_new("showcaseSearchMainView"); 
            this.mainView.createSubviews();
        }   
    }
    
    obj.fetchInitial = function()
    {
        var mode = this.mode();
        if(mode == 'search')
        {
            this.searchMgr.fetchInitial();
            this.resultMgr.fetchInitial();
        }
        else if(mode == 'modified' || mode == 'new' || mode == 'popular')
        {
            this.browseMgr.fetchInitial();
        }                 
    }
    
    obj.redrawAll = function() 
    {
        debug('redraw');
        this.mainView.clear();
        this.mainView.draw();
    }
    
    obj.doFormSubmit = function()
    {
        //of no use right now
        //debug('doFormSubmit');
    }
    
    obj.allowsShowcaseType = function(showcaseID)
    {
        var check = this.config.showcaseTypesLimits;
        var i, count = (check ? check.length : 0);
        if(count > 0)
        {
            if(showcaseID == 0)
                return false;
            else
            {
                var isOK = false;
                for(i=0; i<count && !isOK; i++)
                {
                    var theID = check[i];
                    if(showcaseID == theID)
                        isOK = true;
                }
            }
            return isOK;
        }
        else
            return true;
    }
    
    obj.hasShowcaseFilter = function()
    {
        var check = this.config.showcaseTypesLimits;
        return (check && check.length > 0 ? true : false);    
    }
}

/*--------------------------------------------------------------*/

function DTO_ShowcaseSearchMgr()                            {}

function DTO_ShowcaseSearchMgr_new(theApp)
{
    var obj = new DTO_ShowcaseSearchMgr();
    DTO_ShowcaseSearchMgr_init(obj, theApp);
    return obj;
}

function DTO_ShowcaseSearchMgr_init(obj, theApp)
{
    obj.theApp = theApp;

    obj.selectedShowcaseTypeID = -1;
    obj.selectedUsername = '';
    obj.showcaseTypesTemp = [];    //holds types before filterShowcaseTypes() puts them in showcaseTypes
    obj.showcaseTypes = [];
    obj.showcaseCompTypes = [];
    
    obj.classes = [];
    obj.groupings = [];
    obj.attributes = [];
    obj.popups = [];
    obj.searches = null;
    
    obj.showcaseTypeFormatter = DTO_ShowcaseTypePopupFormatter_new(obj.showcaseTypes);
    obj.showcaseCompTypeFormatter = DTO_ShowcaseCompTypePopupFormatter_new(obj.showcaseCompTypes);
    
    obj.fetchInitial = function()
    {
        var url = this.theApp.baseURL() + '/dto_showcase_search.php?do=searchConstants';   
        var ajaxCall = DTO_AjaxCall_new(this, 'searchConstants');
        ajaxCall.call(url);
    }
    
    obj.ajaxCallback = function(tag, resp)
    {
        if(tag == 'searchConstants')
        {
            eval(resp);
            this.filterShowcaseTypes();
            DTO_ShowcaseType_buildLookup(this.showcaseTypes);
            DTO_ShowcaseCompType_buildLookup(this.showcaseCompTypes);
            DTO_ClassInheritance_buildLookup(this.classes);
            DTO_DisplayGrouping_buildLookup(this.groupings);
            DTO_ClassAttribute_buildLookup(this.attributes);
            DTO_AttributeEnum_buildLookup(this.popups);
            if(this.selectedShowcaseTypeID > 0)
                this.doShowcaseType(this.selectedShowcaseTypeID);
            else            
            {
                this.doSearch(null);
                this.theApp.mainView.searchView.draw();
            }
        }
    }
    
    obj.addShowcaseType = function(obj)             {this.showcaseTypesTemp.push(obj);}
    obj.addShowcaseCompType = function(obj)         {this.showcaseCompTypes.push(obj);}
    
    obj.addClass = function(theClass)               {this.classes.push(theClass);}
    obj.addGrouping = function(grouping)            {this.groupings.push(grouping);}
    obj.addAttribute = function(attr)               {this.attributes.push(attr);}
    obj.addPopup = function(popup)                  {this.popups.push(popup);}
    
    obj.doShowcaseType = function(value)                  
    {
        this.selectedShowcaseTypeID = value;     
        this.doSearch(value);
        this.calcSearch();
    }
    
    obj.doUsername = function(value)                  
    {
        this.selectedUsername = value;     
        this.doSearch(value);
    }
    
    obj.doOption = function(searchAttr)
    {
        this.doSearch(searchAttr);
    }
    
    obj.doSubmit = function(value)                  
    { 
        this.doSearch(value);
    }
    
    obj.doSearch = function(value)
    {
        var optStub = (this.searches ? DTO_SearchAttribute_uncompress(this.searches) : '');
        var q = this.theApp.baseURL() + '/dto_showcase_search.php?do=search'
              + '&showcaseTypeID=' + this.selectedShowcaseTypeID
              + '&username=' + escape(this.selectedUsername)
              + (optStub.length ? '&' : '') + optStub;
        var ajaxCall = DTO_AjaxCall_new(this.theApp.resultMgr, 'search');
        ajaxCall.call(q);
    }
    
    obj.calcSearch = function()
    {
        var matchAttr = [];
        var oldSearches = this.searches;
        
        if(this.selectedShowcaseTypeID  > 0)
        {
            var i, count = this.attributes.length;
            for(i=0; i<count; i++)
            {
                var attr = this.attributes[i];
                var showcaseCompType = DTO_ShowcaseCompType_cachedWithClassID(attr.classID);
                if(showcaseCompType && showcaseCompType.showcaseTypeID == this.selectedShowcaseTypeID)
                    matchAttr.push(attr);
            }
        }
        
        var onUIChange = function(searchAttr){var self = obj; self.doOption(searchAttr);}
        this.searches = DTO_SearchAttribute_compress(matchAttr, onUIChange);
        if(oldSearches)
            this.copySearchValues(oldSearches, this.searches);
        this.theApp.mainView.searchView.setOptionsArray(this.searches);
        this.theApp.mainView.searchView.draw();
    }
    
    obj.copySearchValues = function(searchOld, searchNew)
    {
        var i, count = (searchNew ? searchNew.length : 0);
        for(i=0; i<count; i++)
        {
            var search = searchNew[i];
            var old = DTO_SearchAttribute_forClassAttr(searchOld, search);
            search.setValue(old ? old.getValue() : null);
        }
    }
    
    obj.filterShowcaseTypes = function()    
    {
        var i, count = (this.showcaseTypesTemp ? this.showcaseTypesTemp.length : 0);
              
        if(this.theApp.hasShowcaseFilter())
        {
            var allowed = 0;
            for(i=0; i<count; i++)
            {
                var typeO = this.showcaseTypesTemp[i];
                if(this.theApp.allowsShowcaseType(typeO.id))
                    allowed++;
            }
            for(i=0; i<count; i++)
            {
                var typeO = this.showcaseTypesTemp[i];
                if(typeO.id == 0 && allowed > 1)
                    this.showcaseTypes.push(typeO);
                else if(this.theApp.allowsShowcaseType(typeO.id))
                    this.showcaseTypes.push(typeO);
            }
        }
        else
        {
            for(i=0; i<count; i++)
            {
                var typeO = this.showcaseTypesTemp[i];
                if(typeO.id == 0 && count > 2)
                    this.showcaseTypes.push(typeO);
                else
                    this.showcaseTypes.push(typeO);
            }
        }
        
        if(this.showcaseTypes.length > 0)
            this.selectedShowcaseTypeID = this.showcaseTypes[0].id;
        else
            this.selectedShowcaseTypeID = 1;    //bogus ID
    }
}

/*--------------------------------------------------------------*/

function DTO_ShowcaseResultsMgr()                            {}

function DTO_ShowcaseResultsMgr_new(theApp)
{
    var obj = new DTO_ShowcaseResultsMgr();
    DTO_ShowcaseResultsMgr_init(obj, theApp);
    return obj;
}

function DTO_ShowcaseResultsMgr_init(obj, theApp)
{
    obj.theApp = theApp;
    obj.results = [];

    obj.fetchInitial = function()
    {
    }
    
    obj.ajaxCallback = function(tag, resp)
    {
        if(tag == 'search')
        {
            this.results = [];
            eval(resp);
            this.theApp.mainView.resultView.setArray(this.results);
        }
    }

    //catch the search array    
    obj.addShowcase = function(result)                {this.results.push(result);}
    
    obj.addShowcaseImage = function(result)         
    {
        var found = null;
        var count = this.results.length;
        for (var i=0; i<count && !found; i++) 
        {
            var showcase = this.results[i];
            if(showcase.id == result.showcaseID)
                found = showcase;
        }
        if(found)
            found.thumbImage = result;
    }
}

/*--------------------------------------------------------------*/

function DTO_ShowcaseBrowseMgr()                            {}

function DTO_ShowcaseBrowseMgr_new(theApp)
{
    var obj = new DTO_ShowcaseBrowseMgr();
    DTO_ShowcaseBrowseMgr_init(obj, theApp);
    return obj;
}

function DTO_ShowcaseBrowseMgr_init(obj, theApp)
{
    obj.theApp = theApp;
    obj.results = [];

    obj.fetchInitial = function()
    {
        var mode = this.theApp.mode();
        var showcaseTypeID = this.theApp.showcaseTypeID();
        var q = this.theApp.baseURL() + '/dto_showcase_search.php?do=';

        if(mode == 'new')
            q = q + 'newShowcases';
        else if(mode == 'modified')
            q = q + 'modifiedShowcases';
        else if(mode='popular')
            q = q + 'popularShowcases';

        if(showcaseTypeID > 0)
            q = q + '&showcaseTypeID=' + showcaseTypeID; 
        
        var ajaxCall = DTO_AjaxCall_new(this, 'browse');
        ajaxCall.call(q);
    }
    
    obj.ajaxCallback = function(tag, resp)
    {
        if(tag == 'browse')
        {
            eval(resp);
            this.theApp.mainView.setArray(this.results);
        }
    }
    
    obj.addShowcase = function(result)         {this.results.push(result);}
    
    obj.addShowcaseImage = function(result)         
    {
        var found = null;
        var count = this.results.length;
        for (var i=0; i<count && !found; i++) 
        {
            var showcase = this.results[i];
            if(showcase.id == result.showcaseID)
                found = showcase;
        }
        if(found)
            found.thumbImage = result;
    }
}



