var LinguaLibrary = Class.create({
		initialize:function(){
			$('page_up').observe('click',this.page_up.bindAsEventListener(this));
			$('page_down').observe('click',this.page_down.bindAsEventListener(this));
			//$('standard_view').observe('click',this.standard_view.bindAsEventListener(this));
			//$('paged_view').observe('click',this.paged_view.bindAsEventListener(this));
			$('toggle_view').observe('click',this.toggle_view.bindAsEventListener(this));
			
			this.library = $('library');
			this.pages = $$('div.page');
			this.spreads = $$('div.spr_bdr');
			this.__CURRENT_VIEW__ = 'standard';
			
			this.__CURENT_INDEX__ = 0;
		},
		page_down:function(e){
			Event.stop(e);
			
			if ( this.__CURENT_INDEX__ + 1 > (this.pages.size() - 1) )
				this.__CURENT_INDEX__  = (this.pages.size() - 1);
			else
				this.__CURENT_INDEX__++;
			
			this.select_page(this.__CURENT_INDEX__ );			
			window.scroll(0,0);
		},
		page_up:function(e){
			Event.stop(e);
			
			if ( this.__CURENT_INDEX__ - 1 < 0 )
				this.__CURENT_INDEX__  = 0;
			else
				this.__CURENT_INDEX__--;
			
			this.select_page(this.__CURENT_INDEX__ );
			window.scroll(0,0);
		},
		toggle_view:function(e){
			Event.stop(e);
			this.__CURENT_INDEX__ = 0;

      //cancel all current effects..
      Effect.Queue.each(function(effect) { effect.cancel(); });

			if ( this.__CURRENT_VIEW__ == 'paged' ) {
				this.__CURRENT_VIEW__ = 'standard';
				$('minmax').writeAttribute('src','/l3_zoomin.png');
				this.standard_view();
			} else if ( this.__CURRENT_VIEW__ == 'standard' ) {
				this.__CURRENT_VIEW__ = 'paged';
				$('minmax').writeAttribute('src','/l3_zoomout.png');
				this.paged_view();
			}
			
		},
		select_page:function(dex){
			//this.spreads.reject(function(s){ return s.select('div.page').select(function(s){ return s == this.pages[dex] }.bind(this)) }.bind(this) ).invoke('hide');
			this.spreads.invoke('hide');
			this.pages.reject(function(r){ return r == this.pages[dex] }.bind(this)).invoke('hide');
			
			this.pages[dex].up('div.spr_bdr').show();
			this.pages[dex].show();
		},
		paged_view:function(){
			
			
			
			$$('div.page').each(function(p){ //prescale them all..
					p.setStyle({'position':'relative', 'left':'0', 'margin':'12px auto 0 auto'});

					new Effect.Scale(p.down('div.fbdr').readAttribute('id'),140,{delay:0.2,scaleMode:{ originalHeight: 782, originalWidth: 615}});
					new Effect.Scale(p.readAttribute('id'),140,{delay:0.2,scaleMode:{  originalHeight: 782, originalWidth: 616}});
					new Effect.Scale(p.down('div.ibr').readAttribute('id'),140,{delay:0.2,scaleMode:{  originalHeight: 701, originalWidth: 532}});	

					new Effect.Morph(p.down('div.numbering').readAttribute('id'), {delay:0.3,duration:0.001,style:'height:22px; width:22px; top:1054px; left:422px;'});
					new Effect.Morph(p.down('div.numbering').down('p'), {delay:0.3,duration:0.001,style:'font-size:108%;'});

					Effect.multiple(p.down('div.text').select('p').pluck('id'), Effect.Morph, {delay:0.75,duration:0,style:'font-size: 150%; line-height:136%;'} );
					
					p.select('img').each(function(i){
								
								var imw = parseInt(parseInt(i.readAttribute('standardwidth'))*1.6);
								if ( imw > 740 )
									imw = 740;
								var imh = parseInt(parseInt(i.readAttribute('standardheight'))*1.6);
								if ( imh >  1000 )
									imh = 1000;
								
								i.writeAttribute('width',imw).writeAttribute('height',imh);
								i.setStyle({'width':imw+'px','height':imh+'px'});
								
					});
					
					
					

			});
			$$('div.spr_bdr').invoke('setStyle',{'height':parseInt(1.4*790)+'px'});
			this.spreads.invoke('hide');
			$$('#page_up, #page_down').invoke('show');
			this.library.setStyle({'top':'30px'});
			$('logo').hide();
			window.scroll(0,0);
			
			this.select_page(this.__CURENT_INDEX__);
			
		},
		standard_view:function(){
			
			

			$('logo').show();
			
			$$('#page_up, #page_down').invoke('hide');
			$$('div.page').invoke('setStyle',{'position':'absolute','margin':0});

			/*each(function(p){
					p.setStyle({'position':'absolute', 'left':p.readAttribute('x')+'px', 'top':p.readAttribute('y')+'px'});
			});*/
			$$('div.spr_bdr').invoke('setStyle',{'height':(800)+'px'});
			this.spreads.invoke('show');
			this.pages.invoke('show');
			//$$('div.numbering').invoke('setStyle',{'height':'16px','width':'16px'});
			this.library.setStyle({'top':'0px'});
			resetDefaultStyles();
			
			window.scroll(0,0);

			
		}
	
});


function resetDefaultStyles(){
	  $$('div.page, div.ibr, div.fbdr, img.image, p.title, div.numbering, div.numbering p, div.text, div.text p' ).each(function(p){
              var pstyle = {};
              if (p.readAttribute('x') != null)
              	pstyle['left']= p.readAttribute('x')+'px';
              if ( p.readAttribute('y') != null )
              	pstyle['top'] = p.readAttribute('y')+'px';	
              if ( p.readAttribute('width') != null )
              	pstyle['width'] = p.readAttribute('width')+'px';	
              if ( p.readAttribute('height') != null )
              	pstyle['height'] = p.readAttribute('height')+'px';	
              if ( p.readAttribute('fontsize') != null )
              	pstyle['fontSize'] = p.readAttribute('fontsize');	
              if ( p.readAttribute('lineheight') != null )
              	pstyle['lineHeight'] = p.readAttribute('lineheight')+'px';	
              if ( p.readAttribute('fillopacity') != null )
              	p.setOpacity( p.readAttribute('fillopacity') );
              	
              if ( p.readAttribute('standardheight') != null ) {
              	p.writeAttribute('height',p.readAttribute('standardheight'));	
              	pstyle['height'] = p.readAttribute('standardheight')+'px';	
              }
              if ( p.readAttribute('standardwidth') != null ) {
              	p.writeAttribute('width',p.readAttribute('standardwidth'));	
              	pstyle['width'] = p.readAttribute('standardwidth')+'px';	
              }

              p.setStyle(pstyle);
              
    });
  
}


document.observe('dom:loaded',function(){
		resetDefaultStyles();
    
    $$('a').each(function(a){
    			a.observe('focus',function(e){ Event.element(e).blur() }.bindAsEventListener({}));
    });
    
    document.body.observe('mouseover',function(e){
    		CURRENT_HOVER = Event.element(e);
    }.bindAsEventListener({}));

    var popCapper =          function(thar,repl,trans){
    							var popCap = new Element('div',{className:'ajax_modal_bay'});
    							popCap.absolutize().clonePosition((repl||thar),{
    																					setWidth:false,
    																					setHeight:false,
    																					offsetTop:16,
    																					offsetLeft:-16
    																				});
                  if ( repl ) {
                    repl.replace(popCap);
                  } else {
    							  document.body.appendChild(popCap);
                  }
    							popCap.update(trans.responseText);
    							
    							thar.store('myPop',popCap);

                  popCap.select('.close').each(function(c){
                    c.observe('click',function(e){
                      Event.stop(e);
    									if ( popCap ) {//{ // && CURRENT_HOVER != popCap.down('.inner') ) {
	    									popCap.stopObserving();
	    									popCap.remove();
	    									thar.store('myPop',null);
	    								}
                    }.bindAsEventListener({}));
                  });
    
    popCap.select('form.add_comment').each(function(f){

        f.observe('submit', function(e){
          var thar = Event.element(e);
          Event.stop(e);

          new Ajax.Request('/cgi-bin/u.cgi',{
            parameters:thar.serialize(),
    				onComplete:popCapper.curry(thar,thar.up('.ajax_modal_bay'))
          });

        });

    });
                    };
    							
    
    $$('a.comments_link, a.bio_link').each(function(a){
    	a.observe('click',function(e){
    		var thar = Event.element(e);
    		Event.stop(e);
    		
    		var myPop = thar.retrieve('myPop');
    		

    		if ( myPop && myPop.parentNode ) {
				myPop.stopObserving();
						myPop.remove();
						thar.store('myPop',null);    			
						return;
    		}
    		
    		var tuid = thar.readAttribute('alt');
    		new Ajax.Request('/cgi-bin/u.cgi',{
    					parameters:{ 	navigate:( a.hasClassName('comments_link') ? '.ajax view comments.' : '.ajax view bio.' ),
    												userid:tuid,
                            text_id:tuid
    									},
    					onComplete:popCapper.curry(thar,null)
        });
    	}.bindAsEventListener({}));
    });
    
    new LinguaLibrary();
});
