/********************************************************
*	(C)2006-2007 Creuna AB
*		
*	This script set external pages to be targeted in
*   a new window and also fixes a lot of compatibility
*   issues with the CSS standard for Internet Explorer
*	  
*********************************************************/

var objPageInitializer = new function(){ 

	this.OnLoad = function(){
		objPageInitializer.InitzializeLinks();
		
	}

	this.InitzializeLinks = function(){//add to this for external links to be opened in new window etc
        
	    var anchors = document.getElementsByTagName("a");
	    for (var i=0; i<anchors.length; i++) {
		    var anchor = anchors[i];
		    var href = anchor.getAttribute("href");
		    var title = null;
		    
		    if(href != null){
		        var rel         = anchor.getAttribute("rel");
				var file		= this.IsFileLink(href, rel);
				
				var external	= this.IsExternalLink(href, rel);
				if(external || file){
					if(anchor.target != null){
						anchor.target = "_blank";
						if(this.IsIE()){
							if(file){
								anchor.className += " file";
								this.AddExtensionClass(anchor, href);
							}else{
								anchor.className += " external";
							}
						}
					}
				}
		    }
	    }		    
    }
    
    this.IsExternalLink = function(href, rel){
		if(document.location.pathname != null){
			var host = document.location.href.substring(0, document.location.href.indexOf(document.location.pathname));
			if(href.indexOf(host) == 0){
				return false;
			}
		}
		var s = href.substring(0, 1);
		return (rel == "external" || (s != "/" && s != "#"));
	}

	this.IsFileLink = function(href, rel){
		var extension = "";
		var index1 = href.lastIndexOf(".");
		var index2 = href.indexOf("?");
		if(index1 > 0 && index2 < 0){
			extension = href.slice(index1 + 1);
			if(extension.indexOf("/") > 0)
				extension = "";
		}		
		return (rel == "file" || (extension != "" && extension != "htm" && extension != "html" && extension != "aspx"));
	}
	

	/**************************************
	IE Repairing Scripts START
	**************************************/
	this.AddExtensionClass = function(anchor, href){
		var index = href.lastIndexOf(".");
		if(index > 0){
			anchor.className += " ext-" + href.slice(index + 1);
		}
	}

	this.IsIE = function(){
		var agt = navigator.userAgent.toLowerCase();
		return ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	}
	
	/**************************************
	IE Repairing Scripts END
	**************************************/
	
}
if(document.addEventListener)
	document.addEventListener("load", objPageInitializer.OnLoad, false);
if(window.addEventListener)
	window.addEventListener("load", objPageInitializer.OnLoad, false);
else if(window.attachEvent)
	window.attachEvent("onload", objPageInitializer.OnLoad);



