// JavaScript Document
function uppdateraTitel(rubrik) {  document.title = rubrik + ' / Anna Odell';  }

function album_get(){
    if($("s_works_album")){
        var album=$("s_works_album").innerHTML;
    }  else {var album=""; }
    return album;
}

function texts_init(){
    if($("d_texts")) {
        $$("#d_texts a").each(function(el) {  
		    $(el).observe('click', function(event) {
		        event.preventDefault();
				$$("#d_texts a").each(function(el) { $(el).addClassName('album_transparent');   });
		        $(el).removeClassName('album_transparent'); 				
		        var id=$(el).readAttribute('id');       
                id=id.replace("texts_", ''); 
		        new Ajax.Request('inc/text.php?ID='+id,{
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_text').update(obj.responseText);	       }
                    });
		        
		    });
        }); 
    }  
}

function img_adjust2screen(ID) {    
    new Ajax.Request('inc/bild_storlek.php?ID='+ID,{
        asynchronous:true,			   
        onFailure: function(obj) {},
        onComplete: function(obj) {	
            var img_data =obj.responseText.evalJSON();	  
            img_size("img_work",img_data[0],img_data[1]);                                     
        }
    });    
}                
 

function img_size(ID,width,height){       
    if($(ID)){    
       // alert(ID+' '+width+' '+height);       
         var matt=document.viewport.getHeight()*0.80;            
         if(matt>height ){matt=height+'px'; } 
         else{matt+='px';}     
         try  {
                $(ID).setStyle({height: matt});
                $(ID).writeAttribute('alt', height);                 
                $(ID).observe('click',function(event){ 
                    event.preventDefault();                   
                    var h=$(ID).getStyle('height');
                    h=parseInt(h.replace("px",''));
                    var h2=parseInt($(ID).readAttribute('alt'));
                    if(h<h2) {
                        $(ID).setStyle({width: '800px'});      
                        $(ID).setStyle({height: ''});       
                    }
                    else {
                        $(ID).setStyle({height: h2+'px'});      
                        $(ID).setStyle({width: ''});   
                    }                                        
                    $(ID).writeAttribute('alt', h);
					
                });        
                $('d_work').show();			
		        $('d_works').hide();  
          }
        catch(err) {              
       
        }
    }            
}

function work_init(ID){ 
    //$$("#d_content_works a").each(function(el) { $(el).stopObserving('click'); });
    if($("img_work")) { img_adjust2screen(ID);   }      
    if($("a_work_back")) {        
		$("a_work_back").observe('click', function(event) {
		    event.preventDefault();       		       
		    $('d_works').show();
		    $('d_lcol').show();
		    $('d_content').writeAttribute({'id':'d_content_works'});
		    $('d_work').hide();
		    //works_init();
		});       
    }  
    if($("a_work_previous")) {               
		    $("a_work_previous").observe('click', function(event) {
		        event.preventDefault();		        		       
                var id=$("a_work_previous").readAttribute('rel');       
                var album=album_get();
		        new Ajax.Request('inc/work.php?ID='+id+'&album='+album,{
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_work').update(obj.responseText);		                	            
		                work_init(id);    
                    }
                });		   		       	             		        
		    });       
    }  
    if($("a_work_next")) {               
		    $("a_work_next").observe('click', function(event) {
		        event.preventDefault();		        		       
                var id=$("a_work_next").readAttribute('rel');
                var album=album_get();              
		        new Ajax.Request('inc/work.php?ID='+id+'&album='+album,{
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_work').update(obj.responseText);		                	            
		                work_init(id);    
                    }
                });		   		       	             		        
		    });       
    }      
    if($("a_work_all")) {               
		    $("a_work_all").observe('click', function(event) {
		        event.preventDefault();		
		        var album=album_get();  		       
		      	new Ajax.Request('inc/work_all.php?album='+album,{
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_work').update(obj.responseText);		                
		                work_init();
                    }
                });      
		    });       
    }  
}

function works_init(){    
    if($("d_works")) {     
		$('d_work').hide();
		$('d_works').show();
		//$$("#d_works a").each(function(el) {el.stopObserving.('click'); });
        $$("#d_works a").each(function(el) {  
		    $(el).observe('click', function(event) {
		        event.preventDefault();		        
		        var id=$(el).readAttribute('id');       
                id=id.replace("works_image-", ''); 
                var album=album_get();              
               // alert(album);
		        new Ajax.Request('inc/work.php?ID='+id+'&album='+album,{		       
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_work').update(obj.responseText);		                
		                $('d_lcol').hide();
		                $('d_content_works').writeAttribute({'id':'d_content'});		                        
		                work_init(id); 
                    }
                });		        
		    });
        }); 
    }    
}

function album_init(){
    if($("d_albums")) { 
		//$$("#d_albums a").each(function(el) {el.stopObserving.('click'); });
        $$("#d_albums a").each(function(el) {  
		    $(el).observe('click', function(event) {
		        event.preventDefault();		        
		        var id=$(el).readAttribute('rel');     
		        $$("#d_albums a").each(function(el) { $(el).addClassName('album_transparent');   });
		                $(el).removeClassName('album_transparent');     
		        new Ajax.Request('inc/album.php?ID='+id,{
		            asynchronous:true,		   
		            onComplete: function(obj) {		 
		                $('d_works').update(obj.responseText);		                            
		                works_init();
                    }
                });		        
		    });		   
        }); 
    }  
}

function contact_init() {   
    if($('f_contact')){      
        $('f_contact').observe('submit', function(event) {
            event.preventDefault();            
            url='inc/contact_skicka.php'+'?'+$('f_contact').serialize();
	        new Ajax.Updater('d_contact_submit',url,{
				asynchronous:true,
				evalScripts:true,
				onComplete: function(obj) {$('d_contact_submit').update(obj.responseText);}
			});				         
        });        
    }  
}

function meny_init() {   	
    if(location.href.search(/works.php/)!=-1 ) {     
        $('li_works').addClassName('ul_header_active');                  
        var tmp=$('a_works').innerHTML;
        $('li_works').update(tmp);
    }
    else if(location.href.search(/contact.php/)!=-1 ) {   
        $('li_contact').addClassName('ul_header_active');           
        var tmp=$('a_contact').innerHTML;
        $('li_contact').update(tmp);        
    }
    else if( location.href.search(/biography.php/)!=-1 ) {     
        $('li_biography').addClassName('ul_header_active');
        var tmp=$('a_biography').innerHTML;
        $('li_biography').update(tmp);        
    }
    else if( location.href.search(/texts.php/)!=-1 ) {     
        $('li_texts').addClassName('ul_header_active');     
        var tmp=$('a_texts').innerHTML;
        $('li_texts').update(tmp); 
    }    
    else if( location.href.search(/work.php/)!=-1 ) {     
    }    
    else {
        $('li_works').addClassName('ul_header_active');      
        var tmp=$('a_works').innerHTML;
        $('li_works').update(tmp);
    }		
	
}


document.observe("dom:loaded", function() {
    meny_init();
    contact_init();
    texts_init();
    works_init();
    album_init();    	
	if($('a_print')) {
		$('a_print').observe('click', function(event) {
			event.preventDefault();		        
			window.print();
		});   
	}
	
} );   

