// JavaScript Document
function uppdateraTitel(rubrik) {  document.title = rubrik + ' / Truls.dk ';  }
function includeraScript(fil) {  document.write('<script type="text/javascript" src="'    + fil + '"></scr' + 'ipt>'); }
function ladda(loader) {$('laddar').innerHTML = '';$('laddar').show();}   

function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {      return param_value;  }
  else {      return false;  }
}

function hamtaBokstav(e) {
	var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
	return character
}

function tangentbordet() {            
    Event.observe(document, 'keypress', function(event) {
      var element = Event.element(event);         
      if ('INPUT' != element.tagName) {       
        if(event.keyCode!=17 ){         
            if(event.keyCode==27 || event.keyCode==36){  doljBoxar();}; 
            if(event.keyCode==37 ){ }; //vänsterpil
            if(event.keyCode==39 ){ };  //högerpil                
         }
    }});  

}
//----------
function justeraBoxBild(ID,width,height){
    ID=ID.replace("box_innehall",'');                         		          
    if($('box_bild'+ID)){            
         
         var matt=document.viewport.getHeight()*0.80;            
         if(matt>height ){matt=height+'px'; } 
         else{matt+='px';}     
         try  {
                var els=$$('#box_storbild'+ID+' img') ;                        
                    els.each(function(el){  
                        $(el).setStyle({height: matt});
                        $(el).writeAttribute('alt', height);                    
                        $('box_innehall'+ID).show();
                });
                $('box_bild_a'+ID).observe('click',function(event){ 
                                event.preventDefault();
                                var h=$('box_bild'+ID).getStyle('height');
                                h=parseInt(h.replace("px",''));
                                var h2=parseInt($('box_bild'+ID).readAttribute('alt'));        
                                $('box_bild'+ID).setStyle({height: h2+'px'});           
                                $('box_bild'+ID).writeAttribute('alt', h);                           
                });        
                $('box_bild'+ID).show();
                $('laddar').hide();   
                if($('box_bilder-'+ID)) {  if($('box_bilder-'+ID).empty()==true){$('box_bilder-'+ID).hide();$('box_storbild'+ID).setStyle({  width:'100%'}); }    }
                visaBox('box_innehall'+ID,ID);         
          }
        catch(err) {                                         
            $('box_bild'+ID).observe('load',function(event){                                
                var els=$$('#box_storbild'+ID+' img') ;                        
                    els.each(function(el){  
                        $(el).setStyle({height: matt});
                        $(el).writeAttribute('alt', height);                    
                        $('box_innehall'+ID).show();
                });
                $('box_bild_a'+ID).observe('click',function(event){ 
                                event.preventDefault();
                                var h=$('box_bild'+ID).getStyle('height');
                                h=parseInt(h.replace("px",''));
                                var h2=parseInt($('box_bild'+ID).readAttribute('alt'));        
                                $('box_bild'+ID).setStyle({height: h2+'px'});           
                                $('box_bild'+ID).writeAttribute('alt', h);                           
                });     
                $('box_bild'+ID).show();
                $('laddar').hide();   
                if($('box_bilder-'+ID)) {  if($('box_bilder-'+ID).empty()==true){$('box_bilder-'+ID).hide();$('box_storbild'+ID).setStyle({  width:'100%'}); }    }
                visaBox('box_innehall'+ID,ID);            
            });        
        }
    }            
}

function doljBoxar(){var els=$('boxar').childElements();els.each(function(el){$(el).hide();})
$('bakgrund').show();$('boxar').hide();$('laddar').hide();  }

function visaBox(boxID,ID){    
    if($('bakgrund').visible()) {        
        $('bakgrund').hide();
        $('kontakt_innehall').hide();
        $('boxar').show();
        if($(boxID)){$(boxID).show();}           
        $('laddar').hide();
        $('box_bild'+ID).show();
        if($('box_header'+ID)){$('box_header'+ID).show();}
        if($('box_bilder-'+ID)){$('box_bilder-'+ID).show();}
        if($('box_storbild'+ID)){$('box_storbild'+ID).show();}     		
    }	 
}

function box_bytBild(filID,ID){
    if($(boxID)){                
        ladda();        
        new Ajax.Request('includes/box_bild.php?filID='+filID+'&ID='+ID,{
            asynchronous:true,			   
			onFailure: function(obj) {},
			onComplete: function(obj) {					    						
			    $('box_storbild'+ID).update(obj.responseText);	   
                new Ajax.Request('includes/bild_storlek.php?filID='+filID,{
                    asynchronous:true,			   
			        onFailure: function(obj) {},
			        onComplete: function(obj) {									
			            var bilddata = obj.responseText.evalJSON();  			
                        justeraBoxBild('box_innehall'+ID,bilddata[0],bilddata[1]);                          
                    }
                });     				
            }
		});
    }
}

function box_init(ID){    
    $('box_back'+ID).observe('click',function(event){ 
        event.preventDefault();
        doljBoxar();
    });
    var els=$$('#box_bilder-'+ID+' a');                        
    els.each(function(el){  
        $(el).observe('click',function(event){ 
            var filID=$(el).readAttribute('id');                                    
            filID=filID.replace("box_thumbs",'');                                   
            event.preventDefault();
			
			var imgs=$$('#box_bilder-'+ID+' img') ; 
			imgs.each(function(im){
				$(im).setStyle({opacity:''});
			});     
					
            $('box_bild'+filID).setStyle({opacity:100});						
            box_bytBild(filID,ID);
        });    
    })     
    new Ajax.Request('includes/bild_storlek.php?ID='+ID,{
        asynchronous:true,			   
		onFailure: function(obj) {},
		onComplete: function(obj) {						    
		    var bilddata = obj.responseText.evalJSON();     			                 
            justeraBoxBild('box_innehall'+ID,bilddata[0],bilddata[1]); 
			
				  			
        }
    });    			
    /*
    Event.observe('box_innehall'+ID, 'click', function(event) {	
        var element = Event.element(event);       
        var rel=$(element).readAttribute('rel');        
        if (rel != 'bilden') {doljBoxar();}
    });       
    */     
}

function box(ID){
    doljBoxar();
    boxID=ID.replace("box",'box_innehall');
    ID=ID.replace("box",'');    
    if($(boxID)){  visaBox(boxID,ID);    }
    else{
        //ladda();
        //$('laddar').clonePosition('box'+ID);
        $('laddar').show();
        var element=new Element('div',{'id':boxID});
        if($('boxar')){$('boxar').insert({bottom:element});}
        $(boxID).hide();        
        new Ajax.Request('includes/box_innehall.php?ID='+ID,{
            asynchronous:true,
			evalScripts:true,				   
			onFailure: function(obj) {},
			onComplete: function(obj) {									
			    $(boxID).update(obj.responseText);
				box_init(ID);				
				var imgs=$$('#box_bilder-'+ID+' img'); 
				var nr=0;					
				imgs.each(function(im){
					if(nr==0){$(im).setStyle({opacity:100});} 						
					nr++;
				}); 
			}
		});
    }		
}
   
function boxar_init(){
    var els=$$('#verk a');
    els.each(function(el){
        $(el).observe('click',function(event){ID=$(el).readAttribute('id');box(ID);  })        
    })
}

//----------
  document.observe("dom:loaded", function() {
	
    if( location.href.search(/verk.php/)!=-1) {       
        if( location.href.search(/ID=/)!=-1 ) {            
            id=queryString('ID');
            box(id);
        } 
        else { // visaBox(); 
        }         
    }  
    boxar_init();
    tangentbordet();  

    Event.observe(document, 'keypress', function(event){ hamtaBokstav(event);});                  
    Event.observe('a_kontakt', 'click', function(event){ 
        event.preventDefault();
        $('boxar').hide();    
        $('bakgrund').show();       
        $('kontakt_innehall').show();
    });     
	
    Event.observe(document, 'click', function(event){         
        var el=Event.element(event); 
        if(el.tagName!='A') {    $('kontakt_innehall').hide(); }        
    }); 
	
    Event.observe('info', 'click', function(event){ event.preventDefault();$('bakgrund').show();});      
    //Event.observe('click', 'click', function(event){ event.preventDefault();$('bakgrund').show();});       
    $('laddar').hide();    
	    
    new Ajax.Request('includes/bilder_ladda.php',{
            asynchronous:true,		   
			onFailure: function(obj) {},
			onComplete: function(obj) {}
    });
    
} );  
//----------
