/**
 * Depends on JQuery 1.4.2
 */

SF.ns('SuccessStory.List', function(){

    var data,
	    state = {},
	    pageBrowser,
	    ALL = 'all',
	    RS = '_ss',
	    itemsPerPage,
	    pbDefault,
	    historyReady;

    /**
     * Initialization
     */
    function init() {
    	if(!(SF.ns('SuccessStory.List')).ready) return;

    	$('#challenge_block').show();
    	$('.tabs > li > a').click(function() {
    		$('.content .desc').hide();
    		$('.tabs > li > a').removeClass('act');
    		$('#'+this.id+'_block').show();
    		$('#'+this.id).addClass('act');
    	});

    	$('.list .desc a').click(function() {
    		var currentLocation = String(document.location);
    		if (currentLocation.indexOf('#') > 0) {
    			var urlParts = currentLocation.split('#');
    			window.location.href = this.href + '#' + urlParts[1];
        		return false;
    		}
    	});


        data = SF.SuccessStory.List.data;
        historyReady = !!(SF.ns('History') && SF.History.ready);

        itemsPerPage = parseInt(data.itemsPerPage);

        pbDefault = {start: 1, count: itemsPerPage};

        //Itialize History
        if(historyReady) {
            SF.History.addCallback('sst', historyCallback);
            setTimeout(function(){ SF.on('SuccessStory:StateChanged', updateURLHash) }, 100);
        }

        //Attach event listeners
        $('#ss_industry').change(function(e){
            SF.fire('SuccessStory:IndustryChanged', SF.extend({ industry: $(this).val() }, pbDefault))
        });

        $('#ss_bsize').change(function(e){
            SF.fire('SuccessStory:BsizeChanged', SF.extend({ bsize: $(this).val() }, pbDefault))
        });

        // initial fire
        setTimeout(function(){SF.fire('SuccessStory:IndustryChanged', SF.extend({ industry: $('#ss_industry').val() }, pbDefault))}, 500);
        setTimeout(function(){SF.fire('SuccessStory:BsizeChanged', SF.extend({ bsize: $('#ss_bsize').val() }, pbDefault))}, 500);

        //Set up page browser
        pageBrowser = new SF.widget.PageBrowser('#ss-pages', data.strings);
        pageBrowser.onChange(updateState);
        SF.on('PageBrowser:update', pageBrowser.update);
        SF.on('PageBrowser:updateSelectedPage', pageBrowser.setSelectedPage);

        SF.on('SuccessStory:IndustryChanged', updateState);
        //We need update industry selector when bsize changes
        SF.on('SuccessStory:IndustryChanged', updateBsizeSelector);

        SF.on('SuccessStory:BsizeChanged', updateState);
        //We need update bsize selector when industry changes
        SF.on('SuccessStory:BsizeChanged', updateIndustrySelector);

        SF.on('SuccessStory:StateChanged', updateList);

        SF.on('SuccessStory:VisibilityUpdated', updateVisibility)

        SF.fire('SuccessStory:StateChanged', pbDefault);
    }

    /**
     * Update state of filters if they are changed.
     * This method listens all events which are fired when filter(s) change(s).
     *
     * @param {Object} obj
     */
    function updateState(obj) {
        for(var key in obj) {
            if(obj[key] === ALL) {
                if(state.hasOwnProperty(key)) {
                    delete state[key];
                }
            } else {
                state[key] = obj[key];
            }
        }

        SF.fire('SuccessStory:StateChanged', state);
    }


    /**
     * Updates the list of success stories according to the current state of filters and page browser.
     *
     * @param {Object} state
     */
    function updateList(state) {
        var byIndustry,
            byBsize,
            itemsToShow = [],
            itemsCount,
            selector;

        //updateSelectors(state);

        //Make array of visible success stories
        //Check for selected industry. If there is industry isn't present then all industries are selected.
        if(state.hasOwnProperty('industry')) {
            byIndustry = data.industries[state['industry']];
        }

        //Check for selected bsize. If there is bsize isn't present then all bsizes are selected.
        if(state.hasOwnProperty('bsize')) {
            byBsize = data.bsizes[state['bsize']];
        }

        //If both filters are set we need to find intersaction of arrays - logical AND
        //If one filter is set its list is used.
        //If nothing is set all items should be displayed.
        if(byIndustry && byBsize) {
            var small,
                big;

            if(byIndustry.length < byBsize.length) {
                small = byIndustry;
                big = byBsize;
            } else {
                small = byBsize;
                big = byIndustry;
            }

            for(var i = 0, len = small.length; i < len; i++) {
                if($.inArray(small[i], big) > -1) {
                    itemsToShow.push(small[i]);
                }
            }
        } else {
            itemsToShow = byIndustry || byBsize;
        }

        //Create array of visible items
        if (typeof itemsToShow == 'undefined') {
            itemsToShow = [];
            for(var uid in data.successStories) {
                itemsToShow.push(uid);
            }
            itemsCount = itemsToShow.length;
            itemsToShow = itemsToShow.slice(state.start - 1, state.start - 1 + state.count);
        } else if(itemsToShow.length == 0) {
            //Show No Items message
            SF.fire('SuccessStory:NoItemsFound');
            itemsCount = 0;
        } else {
            itemsCount = itemsToShow.length;
            itemsToShow = itemsToShow.slice(state.start - 1, state.start - 1 + state.count);
        }

        SF.fire('PageBrowser:update', itemsCount, itemsPerPage, Math.floor((+state.start)/itemsPerPage) + 1);
        SF.fire('SuccessStory:VisibilityUpdated', itemsToShow);
    }


    /**
     * Hides/Shows items according to given array of items
     *
     * @param {Array} itemsToShow
     */
    function updateVisibility(itemsToShow) {
        var selector = [];
        for(var i = 0, len = itemsToShow.length; i < len; i++) {
            selector.push('#' + itemsToShow[i] + RS);
        }
        $('.ss-progress').slideUp('fast');
        $('div.sst-item').not(selector.join(',')).addClass('hidden-h0');
        $(selector.join(',')).removeClass('hidden-h0').removeClass('even').filter(':odd').addClass('even');
    }


    /**
     * Update industry selector when bsize is changed
     *
     * @param {Object} obj
     */
    function updateIndustrySelector(obj) {
        var industries,
            selIndustry,
            select = document.getElementById('ss_industry'),
            o;

        if(!select) return;

        o = select.options;

        //Remember currently selected industry
        selIndustry = o[select.selectedIndex].value;

        if(obj.bsize && obj.bsize !== ALL) {
            industries = data.bsizesWithIndustries[obj['bsize']];

            o.length = 1;
            for(var i = 0, len = industries.length; i < len; i++) {
                o[o.length] = new Option(data.industriesDef[industries[i]]['tag_name'], industries[i], false, selIndustry === industries[i]);
            }

        } else {
            //Show all industries
            o.length = 1;
            for(var key in data.industries) {
                o[o.length] = new Option(data.industriesDef[key]['tag_name'], key, false, selIndustry === key);
            }
        }
    }


    /**
     * Update bsize selector when industry is changed
     *
     * @param {Object} obj
     */
    function updateBsizeSelector(obj) {
        var bsizes,
            selBsize,
            select = document.getElementById('ss_bsize'),
            o;

        if(!select) return;

        o = select.options;

        //Remember currently selected Bsize

        selBsize = o[select.selectedIndex].value;

        if(obj.industry && obj.industry !== ALL) {
        	bsizes = data.industriesWithBsizes[obj['industry']];
            o.length = 1;
            for(var i = 0, len = bsizes.length; i < len; i++) {
                o[o.length] = new Option(data.bsizesDef[bsizes[i]]['tag_name'], bsizes[i], false, selBsize === bsizes[i]);
            }

        } else {
            //Show all bsizes
            o.length = 1;
            for(var key in data.bsizes) {
                o[o.length] = new Option(data.bsizesDef[key]['tag_name'], key, false, selBsize === key);
            }
        }
    }

    /**
     * Is called when URL hash is changed.
     * Compose state object according to URL data and fire event.
     *
     * @param {Object} hashObj
     * @param {String} hashStr
     */
    function historyCallback(hashObj, hashStr){
        var selPage;

        state = { target: 'history' };

        if(hashObj.industry) {
            state.industry = hashObj.industry.split(',')[0];
            $('#ss_industry').val(state.industry);
            updateBsizeSelector({industry: state.industry});
        } else {
            updateBsizeSelector({industry: ALL});
            $('#ss_industry').val(ALL);
        }

        if(hashObj.bsize) {
            state.bsize = hashObj.bsize.split(',')[0];
            $('#ss_bsize').val(state.bsize);
            updateIndustrySelector({bsize: state.bsize});
        } else {
            updateIndustrySelector({bsize: ALL});
            $('#ss_bsize').val(ALL);
        }

        if(hashObj.count && hashObj.start) {
            state.count = +hashObj.count;
            state.start = +hashObj.start;

            selPage = Math.floor(state.start/itemsPerPage) + 1;
        } else {
            SF.extend(state, pbDefault);
            selPage = 1;
        }

        //Update page browser
        SF.fire('PageBrowser:updateSelectedPage', selPage);

        SF.fire('SuccessStory:StateChanged', state);
    }


    /**
     * Call History.load()
     *
     * @param {Object} obj
     */
    function updateURLHash(obj) {
        var hObj = {};

        if(obj.target !== 'history') {
            if(data.industriesDef[obj.industry]) {
                hObj.industry = obj.industry + ',' + data.industriesDef[obj.industry]['tag_name']
            }

            if(data.bsizesDef[obj.bsize]) {
                hObj.bsize = obj.bsize + ',' + data.bsizesDef[obj.bsize]['tag_name'];
            }

            if(obj.start) {
                hObj.start =  obj.start;
            }

            if(obj.count) {
                hObj.count =  obj.count;
            }

            SF.History.load({sst: hObj});
        } else {
            delete state.target;
        }

    }

    SF.addToDOMReady(init);

}());

