//var activateAjax = false;
//alert(test);
if(window.top == window){
	container = true;
}else{
	container = false;
}
var anchors = [];
function loaded(){
    links = $$("a");
    //alert("linking");
   links.each(function(item){
          url = item.getProperty('href');
          target = item.getProperty('target');
         
	
	if(Cake.base == ""){
		
		localUrl = new RegExp("^/","i");
		
	}else{
		localUrl = new RegExp("^"+Cake.base,"i");
	}
	  
          absLocalUrl = new RegExp("^http://"+Cake.host+Cake.base,"i");
	  specialLink = new RegExp("^(#|mailto:|javascript:)","i");
	  newurl = false;
	  onclick = "";
	  
          if((localUrl.test(url)) && (target == '_self' || !target) && (!onclick) && (!specialLink.test(url))  ){
		if(document.all){
			if(url == "/"){
				url = "index";
			}
			newurl = url + ".htm";
			target="content";
		}else{
			newurl = Cake.webroot+"#"+url.substr(Cake.base.length);
			target= "_top";
	    }
	  
	    
          }
	  if(absLocalUrl.test(url) && (target == '_self' || !target) && (!onclick) && (!specialLink.test(url))){
		if(document.all){
			if(url == "/"){
				url = "index";
			}
			newurl = url + ".htm";
			target="content";
		}else{
		
			newurl = Cake.webroot+"#"+url.substr(7+Cake.host.length+Cake.base.length);
			target= "_top";
	     }
	      
          }
	  
	  if(newurl){
		item.setProperty('href',newurl);
		item.setProperty('target',target);
		
	   }
	  	  
    });
    
    $$('form').each(function(item){
        if(!item.hasClass('normal')){
	//	item.addEvent("submit",function(e){putReferer(e.target);new Request.HTML({url:e.target.getProperty('action')+".ajax", update:$('content')}).post();return false});
		item.addEvent("submit",function(e){
			//alert(SWFAddress.getValue());
			putReferer(e.target);
			if(e.target.getProperty('action').substr(-4) != ".htm"){
				e.target.setProperty('action', e.target.getProperty('action')+".htm");
			}
		});
	}
        
    });
    
    docTitle = document.getElement("[name=title]");
    if(docTitle){
	window.top.document.title = "panoplie.org => "+docTitle.get('text');
    }
    
     //anchors.each(function(item){
	//	document.body.grab(new Element("a",{name:item}));
	//    });
    
    
  }
  function putReferer(form){
		
		referer = $(form).getElement("input[name=referer]");
		if(referer){
			referer.setProperty("value",SWFAddress.getValue());
		}else{
			form.grab(new Element("input",{type:"hidden",name:"referer",value:SWFAddress.getValue()}));
		}
		//alert(SWFAddress.getValue());
  }
  function getUrl(url,div){
    if(!div){
      div = "content";
    }
    /*
    req = new Request.HTML({
      update:$(div)
    });
    req.addEvent('complete',function(){
        SWFAddressLinks();
        if($(this.options.update).getElement('[name=title]')){
          if(this.options.update.getElement("[name=title]").get('text')){
            document.title = "panoplie.org => "+this.options.update.getElement("[name=title]").get('text');
          }else{
            document.title = "panoplie.org";
          }
        }else{
          document.title = "panoplie.org";
        }
      
      //$try(function(){document.title = this.options.update.getElement("title").get('text');alert("title : "+document.title);},function(){alert('no title')});
    });
    req.get(url+".ajax?r="+Math.random());
    */
    //alert(url);
    //alert(url+":"+Cake.webroot);
    
    if(url==Cake.webroot){
	url = 'index';
	//alert("going to index");
    }else{
	//alert("going to "+url);
    }
    
    if(!url.substr(-4) != ".htm"){
	url += ".htm";
    }
    //alert(url);
    $(div).setProperty('src',url);
    
  }
  
  function checkHash(){
    
    if(window.top.location.pathname != Cake.base+"/"){
        //alert("case 1: "+Cake.base + "/#"+ window.location.pathname.substr(Cake.base.length));
	window.top.location.href = Cake.base + "/#"+ window.top.location.pathname.substr(Cake.base.length);
	//alert("case 1: "Cake.base + "#"+ window.location.pathname.substr(Cake.base.length));
    }else if(window.top.location.hash){
        getUrl(Cake.base+window.top.location.hash.substr(1));
	//alert("case 2:"+Cake.base+window.top.location.hash.substr(1));
    }else{
	window.top.location.href = "#/";
	//alert("case 3");
    }
  
  }
  
  if(activateAjax){
  
      window.addEvent("domready",function(){
       if(container){
           SWFAddress.addEventListener(SWFAddressEvent.CHANGE,function(e){
		//alert("changed");
              checkHash();
           });
	   
           checkHash();
         }  
	 
         loaded();
	
      });
  
  }
  
