var Article = Class.create({
    lightview: false,
    initialize: function() {
        
		
		this.url 			   = '/modules/articles/front/server.php';
		this.destination_id    = "contentcontent";
		this.destination_el    = $(this.destination_id);
		this.source_id		   = "article_container_full";
		this.source_class	   = "article_container_full";
		this.source_el 		   = null;

    },
	create:function(){
		

              
           /* if(this.source_el){
                this.source_el.show(); 
            }
            else{*/
			if (this.source_el) {
			
			}
			else {
				this.source_el = new Element('div', {
					'id': this.source_id,
					'class': this.source_class
				});
				Element.extend(this.source_el);
				this.source_el.innerHTML = '';
				this.source_el.clonePosition(this.destination_el, {
					'setTop': false,
					'setHeight': false,
					'setWidth': false,
					'setLeft': false
				});
								
				Element.insert(this.destination_el,{'after':this.source_el} );
				

			}
            //}
			
            if(this.destination_el){
                this.destination_el.hide();
            }			                

            scroll(0,0);    
            
            return this.source_el; 		
	},
	
    close: function(){
            if(this.destination_el){
                this.destination_el.show();
            }
                                                             
            if(this.source_el){
                this.source_el.hide();                                                    
            } 
			scroll(0,0); 
    },
    open: function(moduleinstance,vid,detail){

		var el = this.create();
		   
        var params=$H(
        {
            moduleinstance:moduleinstance,
            vid:vid,
            detail:detail,
            sid:Math.random()
        }
        ).toQueryString();
		
		el.ajaxUpdate(   
			this.url, {
        		method: 'get',
        		parameters: params, 				
				onSuccess: function(transport){
					el.show();
				}
			}
		);

    }
});


function Tab(id){
    this.element=$(id);
    this.tabItems=[];
    this.tablistItems=[];
    this.activeTab=1;
    this.toggleTimer=null;
    this.activeCallback=null;
    this.init();
};

Object.extend(Tab.prototype,{
    init:function(){
        if(this.element){
            
	
            var ul=document.createElement('ul'),li,a;
            var tab=this.element.firstChild,i=1;
            Element.extend(tab);
			var first = true; 
            while(tab){
         
                if(tab.nodeType==1){
            
                    this.tabItems[i]=tab;
					//var title = tab.readAttribute('rel');
		
                    li = document.createElement('li');
                    a = document.createElement('a');
                    Element.extend(a);
                    Element.extend(li);
                    a.setAttribute('href',window.location.href+'#'+tab.id);
                    a.appendChild(document.createTextNode(i));
                    a.observe('click',function(e){
                        e.preventDefault();
                    });
                    a.observe('mouseover',this.setActive.bind(this,i,true));
                    
                    li.appendChild(a);
                    if(i!=this.activeTab){
                        tab.style.display='none';
                    }
                    else{ 
                        li.addClassName('active');
                    }
					
					if(first){
						li.addClassName('first');
					}
                     
                    this.tablistItems[i]=li;
                    ul.appendChild(li);
                    i++;
					first = false;
                }
                tab=tab.nextSibling;

            }
            //this.element.appendChild(ul);
			this.element.insertBefore(ul,this.element.firstChild);
            this.element.observe('mouseout',this.startToggle.bind(this));
            this.element.observe('mouseover',this.stopToggle.bind(this));
            this.startToggle();
            Event.observe(window,'unload',this.cleanUp.bind(this));
        }
    },
    cleanUp:function(){
        this.stopToggle();
        this.tablistItems=null;
        this.tabItems=null;
        this.element=null;
        this.activeCallback=null;
    },
    setActive:function(i,doCallback,e){
            if(i!=this.activeTab){
                if(this.activeTab){
                    this.tabItems[this.activeTab].style.display='none';
                    this.tablistItems[this.activeTab].removeClassName('active');
                }
                this.activeTab=i;
                this.tabItems[this.activeTab].style.display='';
                this.tablistItems[this.activeTab].addClassName('active');
                if(this.activeCallback&&doCallback)
                    this.activeCallback(i,false);
            }
            if(e){
                e.preventDefault();
            }
    },
    toggle:function(){
        var i=this.activeTab+1;
        if(!this.tabItems[i]){
            i=1;
        }
    
        this.setActive(i,true);
        this.startToggle();
    },
    startToggle:function(){
        this.stopToggle();
        this.toggleTimer=setTimeout(this.toggle.bind(this),Math.round(40000/(this.activeTab+1)));
    },
    stopToggle:function(){
        if(this.toggleTimer){
            clearTimeout(this.toggleTimer);
            this.toggleTimer=null;
        }
    }
});



