if (!VISA) { VISA = {}; }
if(!VISA.ui){VISA.ui = {} };

/* VISA.ui.tabs
* purpose: handles the tabs click
* param: el, a specific id or class that you would like to search the "tabs".  If null or not found, search the document
* param: callBackBeforeShow, a function that will be called after the content is shown.  If not found, ignore
* Tab structure: tab-content must be on the same level as the "tabs" class
    * <div class="tabs"><ul><li><a href="#ddd">tab1</a></li><li><a href="#tab-2">tab 2</a></li></ul></div>
    * <div class="tab-content selected">selected goes here</div>
*/
(function(){

    VISA.ui.tabs = function(el, callBackAfterShow) {

        var self = this;

        self.tabConfigs = {
            TABCLASS: ".tabs",
            TABCONTENT: ".tab-content",
            SELECTED: ".selected"
        };

        this.init = function(){
           var target = (typeof el !== "undefined")?el:document;
           if($(self.tabConfigs.TABCLASS, target).length >0){
                $(self.tabConfigs.TABCLASS + ' li').bind('click', function(event){
                    self._renderSortMenu($(this), event);
                    event.preventDefault();
                });
            }
        };

        /**
		 * Unhides the sort menu options in the sort options
		 * then adds listeners to each of the items in it
		 * @protected
		 */

        this._renderSortMenu = function(oTab, event){

			var oSortMenu = $(oTab).closest('ul');
            var oSortRoot = oSortMenu.parent().parent();

            var sortParams = $(event.target).attr('href');
            //set the clicked tab as selected
            var sortCallback = function(){
                var li = $(event.target).closest('li');
                var curSlctd = $(li).parent().find(self.tabConfigs.SELECTED);
                if(curSlctd[0]){
                    curSlctd[0].removeAttribute('class'); //for a safari bug
                }
                $(curSlctd).removeClass(self.tabConfigs.SELECTED.replace(/./, ''));
                $(li).addClass(self.tabConfigs.SELECTED.replace(/./, ''));

                if(typeof callBackAfterShow != "undefined" && jQuery.isFunction(callBackAfterShow)){
                    callBackAfterShow.apply(self);
                }
            };
            self.sortContent(oSortRoot, sortParams, sortCallback);

		};

		/**
		 * Sorts module content list
		 * @param url {String} url to make xhr connection to
		 * @param sortParams {String} sort params to be passed to connection object
		 * @param contentLst {Dom Element} Content list to sort
		 * @param callback {Function} Function to invoke when sort is done
		 */
		this.sortContent = function(oSortRoot, sortParams, callback){

            var content = $(sortParams, oSortRoot);
            if(content){
                var curSelected = $(oSortRoot).find(self.tabConfigs.TABCONTENT + self.tabConfigs.SELECTED);
                $(curSelected).removeClass(self.tabConfigs.SELECTED.replace(/./, ''));

                $(oSortRoot)
                    .find(sortParams)
                    .addClass(self.tabConfigs.SELECTED.replace(/./, ''));

                if(jQuery.isFunction(callback)){
                    callback.apply(self);
                }
            }

		};

        this.init();
    }
})();


