var PublicPage = new Class ({
    tabs: null,
    
    content: null,
    
    init: function() {
        this.tabs    = $('tabs').getChildren();
        this.content = $('tabs-boxes').getChildren();
        
        this.tabs.each(function(item, index){
        	item.addEvent('click', function(){
                this.onTabClick(item);
            }.bind(this));
        }, this);
        this.initHistory();
        $$('a.reviews').each(function(rbtn){
        	rbtn.addEvent('click', function(){
        		this.onTabClick(this.tabs[this.tabs.length-1]);
        		return false;
        	}.bind(this));
        }.bind(this));
        $$('a.addmysupplier').each(function(href){
        	href = $(href);
        	var id = href.get('rel');
        	href.addEvent('click', function(event){
        		event.stop();
        		var request = new Request.JSON({
	    			url: '/viewer/addsupplier',
	    			onSuccess: function(json, text){
	    				if (json.message){
	    					new MooDialog.Alert(json.message, {okText: Itc_Lang.ok});
	    				}
	    				if (json.success == true){
	    					this.removeAddSupplier();
	    				}
	    			}.bind(this)
	    		});
	    		request.send('format=json&id=' + id);
	        	return false;
	        }.bind(this));
        }.bind(this));
        var h = location.hash;
        if (h != ''){
        	h = h.substr(1);
        	this.tabs.each(function(item){
        		if (item.getElement('a').get('href').substr(1) == h){
        			this.onTabClick(item);
        		}
        	}.bind(this));
        }
    },
    
    removeAddSupplier: function(){
    	$$('a.addmysupplier').each(function(addmy){
    		addmy.destroy();
    	});
    },
    
    onTabClick: function(el) {
        var num = el.getElement('a').get('href').substr(1),
        	tabId = 'tab-content-' + num,
        	tab  = $(tabId);
        
        this.tabs.each(function(item){
            if (el == item) {
                item.addClass('active');
            } else {
                item.removeClass('active');
            }
            
        }, this);
        
        this.content.each(function(item, index){
            //if (num == index) {
            if (tabId == item.get('id')){
                item.addClass('display-block');
                item.removeClass('display-none');
            } else {
                item.addClass('display-none');
                item.removeClass('display-block');
            }
            
        }, this);
    },
    
    initHistory: function(){
    	return false;
    	var btns = $$('.history');
    	if (1==2 && history.length > 1){
    		var hl = history.length-1;
    		btns.each(function(bt){
    			bt.addEvent('click', function(){
    				history.go(hl - history.length);
    				return false;
    			})
    		});
    	}else{
    		btns.each(function(bt){
    			var cid = bt.getProperty('rel');
    			bt.addEvent('click', function(){
    				var url = '/search';
    				if (cid){
    					url += '?category=' + cid;
    				}
    				location.href = url;
    				return false;
    			})
    		});
    	}
    }
});
