/** 
 * Gallery Sort menu
 * @version     0.1 7/8/2007 17:07
 * @licensed    http://creativecommons.org/licenses/by-sa/2.5/
 * @author      chaoskaizer <ck@animepaper.net>
 */

var apdom = {};
var debug = true;

ApMambot.interactive = function(elm){  
    $(elm).innerHTML = '<img src="' +this.staticRoute('images/v5/loading_arrow.gif')  +'" alt="Fetching Data..."/>'; 
}

ApModules.isParam = function(elm,container){
	var s = { params: {}, str:$(elm).readAttribute('rev'), id:$(elm).readAttribute('rel'),pars: this.parameters };
      
    if (!s.str) return;
    s.rev = s.str.split(':');
    if (isFirebug && debug) console.log(s.rev);
    
    if (!s.id) s.id = $(ApMambot.localID(elm));
    s.err = 'dropnotice'; 
    s.url = s.rev[3] || '?getgallery'; // s.rev[3] ?getmore=uid       
    
    ApMambot.interactive(s.id);        
    if (s.pars.thistype.include(s.rev[0])) s.params.thistype = s.rev[0];
    if (s.pars.oby.include(s.rev[1])) s.params.oby = s.rev[1];
    if (s.pars.oasc.include(s.rev[2])) s.params.oasc = s.rev[2];
    // static load
    s.params.newt = true; 
    new Effect.BlindDown(s.id); 
    this.tabUpdate(s); 
}

ApMambot.localID = function(elm){
	return elm.href.match(/#(\w.+)/)[1];    
}

ApModules.tabUpdate = function(obj){
    new Ajax.Updater(obj.id, obj.url, { method: 'get',parameters: obj.params });       
}

var Apsortmenu = Class.create();

Apsortmenu.prototype = {
    initialize: function(element){        
        this.selector = $$(element);
        this.act = this.activate.bindAsEventListener(this);
        this.selector.each(this.hook.bind(this));                
    },
    hook: function(elm){
      var nodelist = $A(elm.getElementsByTagName('a'));
          nodelist.each(this.setup.bind(this));  
    },
    setup: function(elm){
        Event.observe(elm,'click',this.act,false);         
        elm.onclick = function(){return false};
    },
    activate: function(trace){
        var link = Event.findElement(trace,"a");
        Event.stop(trace)
        ApModules.isParam(link);
    }
}

ApModules.initTabs = function(objArray){
    objArray.each(function(element){if ($(element)) new Fabtabs(element);});
}

ApModules.sortMenu = function(classname){
    var nodes = $$(classname);
    if (nodes.size() >= 1) new Apsortmenu(classname);
}

ApModules.activate = function(){
    this.initTabs(['showcasetabs','abouttabs']);
    this.sortMenu('.sortmenu');
}

/*
 * Andrew Tetlaw | version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 * Extend AjaxUpdate by chaoskaizer <ck@animepaper.net>
 */
 
 
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
		elm.onclick = function(){return false};
		return false;
	},
	hide : function(elm) {	 
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		new Effect.BlindDown(this.tabID(elm));
		ApModules.isParam(elm);		
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},	
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}

Event.observe(window, 'load',ApModules.activate.bind(ApModules),false);