// contains BrowserDetect.js, events.js, lib.js, generic.js, documentTree.js,
// allChecker.js, compactInput.js, scrollAnim.js, cookies.js, browser.js, getObj

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


// written by Dean Edwards, 2005
// with input from Tino Zijdel

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	// assign each event handler a unique ID
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	// create a hash table of event types for the element
	if (!element.events) element.events = {};
	// create a hash table of event handlers for each element/event pair
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = {};
		// store the existing event handler (if there is one)
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	// store the event handler in the hash table
	handlers[handler.$$guid] = handler;
	// assign a global event handler to do all the work
	element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	// delete the event handler from the hash table
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(window.event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) == false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};


//	documentTree.js
//
var DocumentTree={
	l : Array(),
	t : function(n){
	if(1 == n.nodeType){
			this.p(n);
			for(var i=0, c = n.childNodes , l = c.length; i < l; i++){this.t(c[i]);}
		}
	},
	p : function(n){
		if(n.className){
			for(var i =0, m = n.className.split(" "), l = m.length; i < l ;i++){
				if(m[i] != ' ' && m[i] != ''){this.a(m[i],n);}
				}
		}
	},
	a : function(c,e){
		if(!this.l[c]){this.l[c] = new Array();}
		this.l[c].push(e);
	},
	g : function(c){
		if(!this.l[c]){return new Array();}
		return (this.l[c].concat());
	} 
}
document.getElementsByClass = function(c){ return DocumentTree.g(c); }
//addEvent(window,'load',function(){DocumentTree.t(document.body);});

// getInputElementsByType.js
function getInputElementsByType(t,n){
	var e = new Array(),t =  t.toLowerCase();
	if(!n) n = document.body;
	if(!t) t = '';
	if (n && n.getElementsByTagName('input')) {
		for(var i=0, c = n.getElementsByTagName('input'), l = c.length; i < l; i++){
				if(t == c[i].type.toLowerCase() || t == ''){e[e.length] = c[i];}
		}
	}
	return e;
}

// compactInput.js
var CompactInput = {
	a : function(){ 
		var e = document.getElementsByClass('compactInput');
		for(var i=0, l = e.length; i < l; i++){
			this.e(e[i]);
		}
	},
	e : function(e){
			var v = e.value;
			e.onfocus = function(){if(v == this.value){this.value= '';}}
			e.onblur =  function(){if(this.value == ''){this.value = v}};
	}
}
// allChecker.js
var AllChecker = {
	a : function(){
		for(var i=0,e = document.getElementsByClass('allChecker'), l = e.length; i < l; i++){this.e(e[i]);}
	},
	e : function(n){
		for(var i=0, c = getInputElementsByType('checkbox',n), l = c.length; i < l; i++){/*c[i].checked = 0,*/ c[i].onchange = this.u, c[i].n = n;}
	},
	u : function(){
		var c = getInputElementsByType('checkbox',this.n), s = this.checked;
		if('checkAll' == this.className){
			for(var i=0, l = c.length; i < l; i++){c[i].checked = s;}
			return;
		}
		for(var i=1, l = c.length; i < l; i++){
			if(!c[i].checked){ 
			s = 0; 
			break;}
		}
		c[0].checked = s;
	}
}
// scrollAnim.js
var ScrollAnim = {
	i : null,		// Interval
	j : false,	// Loop
	c : null,		// Current Node
	h : '',			// location href
	a : function(){ // Attach
		this.h = location.href.replace(location.hash,'');	// Widthout Hash in case it is there
		if(!document.getElementById){ return;} 						// Browser is not W3C safe
		for(i=0,e =document.getElementsByClass('scrollAnim'),l = e.length; i < l; i++){
			if(e[i].href && e[i].hash){e[i].href= 'javascript:ScrollAnim.s(\''+e[i].hash+'\')';}
		}
	},
	s : function(h){ // Scroll
		if(this.i){
			clearInterval(i);
			this.j = false;
			this.i = null;
		}
		if(this.c){ 	// Unset The Current Active Node

		}
		try{
			var n = document.getElementById(h.replace(/#/,''));		// Node we are going to scroll to
		}catch(e){	
			return location.hash = h;
		}
		var d = (this.y(document.getElementById('pageContainer')) + document.getElementById('pageContainer').offsetHeight);
		var w = window.innerHeight;	// All Browsers Except IE
		var y = (this.y(n));				// Current Node Y Pos
		if(y >(d-w)){y = (d-w);}
	  this.o(0,y); 
		this.c = n;		// Set Reference To New Active node
		//location.hash = h;
	},
	o : function(x,y){ // ScrollTo
		if(this.j){
			var l = this.l();
			var t = this.t();
			if(Math.abs(l-x) <= 1 && Math.abs(t-y) <= 1){
				window.scrollTo(x,y);
				clearInterval(this.i);
				this.j = false;
				this.i = null;
			}else{
				window.scrollTo(l+(x-l)/2, t+(y-t)/(1.2));
			}
		}else{
			this.i = setInterval("ScrollAnim.o("+x+","+y+")",100);
			this.j = true;
		}
	},
	y : function(n){ // YPos
		var y = 0;
		while(n.offsetParent){ y += n.offsetTop, n = n.offsetParent;}
		return y;
	},
	l : function(){ // Left
		var l = window.pageXOffset;
		if(!l){l = document.documentElement.scrollLeft  ? document.documentElement.scrollLeft : document.body.scrollLeft;}
		return l;
	},
	t : function(){	// Top
		var t =  window.pageYOffset;
		if(!t){t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;}
		return t;
	},
	w : function(){	// WindowHeight
		var h = window.innerHeight; 
		if(!h){h = document.documentElement.clientHeight? document.documentElement.clientHeight : document.body.clientHeight;}
		return h;
	}
}

// cookies.js
// Cookies by Peter-Paul Koch www.quirksmode.org
function createCookie(name,value,days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function eraseCookie(name) {
        createCookie(name,"",-1);
}

// browser.js
var WindowDimensions = {
	t : '',
  s : function(){
  	var w, h;
	if (self.innerHeight) {
  	      w = self.innerWidth;
  	      h = self.innerHeight;
	} 
	else if (document.compatMode == 'CSS1Compat')
	{
		 w = document.documentElement.clientWidth;
  	     h = document.documentElement.clientHeight;
  	} 
  	else if (document.body) {
  	      w = document.body.clientWidth;
    	  h = document.body.clientHeight;
	}	
		createCookie('windowWidth',w);
		createCookie('windowHeight',h);
  },
  r : function(){
  	clearTimeout(this.t);
		this.t = setTimeout('WindowDimensions.s()',0);
  }
}
		
var Lib = {
  require : function(libName) {
    document.write('<script type="text/javascript" src="javascript/lib/'+ libName +'"></script>');
  },
  load : function() { // Load Libraries
    //this.require('documentTree.js');
    //this.require('getInputElementsByType.js');
    //this.require('compactInput.js');
    //this.require('allChecker.js');
    //this.require('scrollAnim.js');
    //this.require('cookies.js');
    //this.require('browser.js');
    addEvent(window,'load',function(){Lib.startup();});					// fire up...
   	addEvent(window,'resize',function(){WindowDimensions.r()});	// resize
  },
  startup : function(){ // Startup Libraries and other functions
  	DocumentTree.t(document.body);// DocumentTree Travers and Create Class Tree
   	//CompactInput.a();			// attach
   	AllChecker.a();				// attach
   	ScrollAnim.a();				// attach
   	WindowDimensions.s();	// set Window Dimensions
  }
}
Lib.load();



//function to change everything behind ? in url
function changeUrl(newVar){
	currentUrl = document.location.href.split("?");
	document.location = currentUrl[0]+'?'+newVar;
}




//function to center pop-ups
var win = null;
function openWin(page, title, w, h, scroll){

if(navigator.appName=="Microsoft Internet Explorer"){
	win=window.open(page,null,"height=500,width=700,status=no,toolbar=no,menubar=no,location=no");
}
else{
	leftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	topPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+topPosition+',left='+leftPosition+',scrollbars='+scroll+',resizable=true, menubar=yes';
	win=window.open(page, title,settings);
}
}

//function to switch display for given id
function hideAndShow(id){
	var layer = document.getElementById(id);
	if(layer.style.display =="none"){
	layer.style.display="";
	}
	else{
	layer.style.display="none";
	}
	
}

//To change form action on select
function changeActionBottom(){	
			var pageType = document.footerSearch.action=document.footerSearch.zt_o.value;
			var sPage;
			
			switch(pageType){
			
				case "forside":
					sPage = "q_index.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "eiendom":
					sPage = "q_eiendom.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "stilling":
					sPage = "q_stilling.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "bil":
					sPage = "q_motor.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "baat":
					sPage = "q_b%C3%A5t.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "smaattOgStort":
					sPage = "q_sm%C3%A5ttogstort.html";
					document.getElementById('bottomsearchParam').name ="setText";
				break;
				
				case "kart":
					sPage = "kartsok.html";
					document.getElementById('bottomsearchParam').name ="mapSearchString";
				break;				
				
				default:
					sPage = "q_index.html";
					document.getElementById('bottomsearchParam').name ="setText";
			}

			var index = document.footerSearch.zt_o.selectedIndex;
			var value = document.footerSearch.zt_o.options[index];

			document.footerSearch.action=sPage;			
}

//To change form action on select, fix for prettyURL
function changeAction(){	
			var pageType = document.standardSearchForm.action=document.standardSearchForm.zt_o.value;
			var sPage;
			
			switch(pageType){
			
				case "forside":
					sPage = "q_index.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "eiendom":
					sPage = "q_eiendom.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "stilling":
					sPage = "q_stilling.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "bil":
					sPage = "q_motor.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "baat":
					sPage = "q_b%C3%A5t.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "smaattOgStort":
					sPage = "q_sm%C3%A5ttogstort.html";
					document.getElementById('topsearchParam').name ="setText";
				break;
				
				case "kart":
					sPage = "kartsok.html";
					document.getElementById('topsearchParam').name ="mapSearchString";
				break;				
				
				default:
					sPage = "q_index.html";
					document.getElementById('topsearchParam').name ="setText";
			}

			var index = document.standardSearchForm.zt_o.selectedIndex;
			var value = document.standardSearchForm.zt_o.options[index];

			document.standardSearchForm.action=sPage;			
}


// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;
function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}
	