function getById(el) {
	return document.getElementById(el);
}

function getByTag(par,el) {
	var par = (par == '') ? document.body : par;
	if (!par) return new Array();
	return par.getElementsByTagName(el);
}

function addClass(obj,newClass) {
	if(!obj.className.match(new RegExp(newClass)))
		obj.className+=(obj.className.length>0? " ": "") + newClass;
}
	
function removeClass(obj,oldClass) {
	obj.className=obj.className.replace(new RegExp("( ?|^)"+oldClass+"\\b"), "");
}

function getByClass(cla,par,el) {
	var getEls;
	if (/.*native code.*/.test(document.getElementsByClassName)) {
		getEls = function(cla,par,el) { 
			return par.getElementsByClassName(cla);
		}
	}
	else { 
		getEls = function(cla,par,el) { 
			var tagColl = par.getElementsByTagName(el);
			trimedColl = new Array;
			for (var i = 0; tagColl[i]; i++) {
				if(tagColl[i].className.match(new RegExp("( ?|^)"+cla+"\\b")))
					trimedColl[trimedColl.length]=tagColl[i];
			}
			return trimedColl;
		} 
	} 
	getByClass = function(cla,par,el) {
		var par = (!par||(par == '')) ? document.body : par;
		var el = (el == '') ? '*' : el;
		return getEls(cla,par,el); 
	}
	return getByClass(cla,par,el);
}


function getStyle(obj,cssRule) {
	if (document.defaultView && document.defaultView.getComputedStyle) {
		getStyle = function(obj,cssRule) {
			return document.defaultView.getComputedStyle(obj, "").getPropertyValue(cssRule);
		};
	}
	else {
		getStyle = function(obj,cssRule) {
			if (obj.currentStyle) {
				cssRule = cssRule.replace(/\-(\w)/g, function (match, p1) {
					return p1.toUpperCase();
				});
				return obj.currentStyle[cssRule];
			}
		};
	}
	return getStyle(obj,cssRule);
}

function hasBorderRadius() {
	if((document.body.style.BorderRadius !== undefined) || (document.body.style.MozBorderRadius !== undefined) || (document.body.style.WebkitBorderRadius !== undefined))
		hasBorderRadius = function() {return true;};
	else
		hasBorderRadius = function() {return false;};
	return hasBorderRadius();
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

var addEvent = function() {
  if (window.addEventListener) {
    return function(el, type, fn) {
      el.addEventListener(type, fn, false);
    };
  } else if (window.attachEvent) {
    return function(el, type, fn) {
      var f = function() {
        fn.call(el, window.event);
      };
      el.attachEvent('on' + type, f);
    };
  }
}();

function setLinks(linkTags){
	var linkCollection = [];
	for (var j = 0; linkTags[j]; j++) {
		linkCollection[j] = getByTag('',linkTags[j]);
		for (var i = 0; linkCollection[j][i]; i++) {
			if(/bookmark|external|corporate|download|partner/.test(linkCollection[j][i].getAttribute('rel'))) {
				linkCollection[j][i].onclick = function(e) {
					if (!e) var e = window.event;
					if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey)
						return true;
					window.open(this.href);
					return false;
				}
			}
		}
	}
}

function GET_XMLHTTPRequest() {
	var request;
	try{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(ex1){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex2){
			try{
				request = new ActiveXObject("Msxml3.XMLHTTP");
			}
			catch(ex3){
				request = null;
			}
		}
	}
	if(!request && typeof XMLHttpRequest != "undefined"){
		request = new XMLHttpRequest();
	}
	return request;
}

function fixIE() {
	
	var f = getById('footer');
	if(f) {
		var liColl = getByTag(f,'li');
		if(liColl && liColl.length>0) {
			for(var i = 1; liColl[i]; ++i) {
				liColl[i].innerHTML = ' | ' + liColl[i].innerHTML;
			}
		}
	}
	
	var ts = getByClass('town',getById('content'),'div');
	if(ts&&ts[0]) {
		for (var i = 0; ts[i]; ++i) {
			ts[i].onmouseover = function () {
				addClass(this,'hover');
				this.style.zIndex = 999;
			}
			ts[i].onmouseout = function () {
				removeClass(this,'hover');
				this.style.zIndex = -1;
			}
		}
	}
}

function fixLowIE() {
	
}


/* Set quiz
----- */
function setQuiz() {
	var q = getByClass('quiz',getById('container'),'div');

	if(q&&q[0]) {
		var a = getByClass('answer',q[0],'div');
		var labs = getByTag(q[0],'label');
		var rads = getByTag(q[0],'input');
		if(a&&a[0]&&labs&&labs[0]&&rads&&rads[0])
			new Quiz(q[0],labs,rads,a[0]);
	}
}

function Quiz(prt,labs,rads,ans) {
	this.prt = prt;
	this.labs = labs;
	this.rads = rads;
	this.answer = ans;
	this.cId = -1;
	for(var i = 0; labs[i]; ++i) {
		labs[i].cId=i;
		labs[i].onclick = function (that) {
			return function() {
				that.reveal(this);
				return false;
			}
		}(this);
	}
	for(var i = 0; rads[i]; ++i) {
		rads[i].cId=i;
		rads[i].onclick = function (that) {
			return function() {
				if(that.cId==-1) {
				//	that.rads[this.cId].checked=true;
					that.reveal(this.parentNode)
				}
				else {
					that.rads[that.cId].checked=true;
				}
				this.blur();
				//return false;
			}
		}(this);
	}
}

Quiz.prototype = {
	reveal: function(obj) {
		if(this.cId == -1) {
			addClass(this.answer,'open');
			this.cId=obj.cId;
		}
		this.light(this.cId);

		for(var i = 0; this.labs[i]; ++i) {
			this.labs[i].onclick = function () {
				return false;
			}
		}
		
		this.timeOut = setTimeout(function (that) {
			return function () {
				that.light();
			}
		}(this), this.speed);
		
		return false;
	},
	light: function(cId) {
		var r = this.rads[this.cId];
		if(r&&(r.checked!=true))
			r.checked=true;
	}
}

/* Carto
----- */
function setCarto() {
	var m = getByClass('demo',getById('content'),'div');
	if(m&&m[0]) {
		var s = getByClass('selection',m[0],'div');
		var t = getByClass('towns',m[0],'div');
		if(s&&s[0]&&t&&t[0]) {
			new Carto(s[0],t[0]);
		}
	}
}

function Carto(sContainer,tContainer) {
	this.container = tContainer;
	var towns = getByClass('town',tContainer,'div');
	this.towns = towns;
	var f = getByTag(sContainer,'form');
	this.frm = f[0];
	var s = getByTag(sContainer,'select');
	this.selectors = s;
	this.frm.onsubmit = function(that) {
		return function() {
			that.toggle();
			return false;
		}
	}(this);
}

Carto.prototype = {
	toggle: function() {
		for(var i = 0; this.towns[i]; ++i) {
			removeClass(this.towns[i],'current');
		}
		for (var i = 0; this.selectors[i]; ++i) {
			var v = this.selectors[i].value;
			if (/town/.test(v)) {
				var s = v.split(' ');
				for(var j = 0; s[j]; ++j) {
					addClass(getByClass(s[j],this.container,'div')[0],'current');
				}
			}
		}
		return false;
	}
}

/* 
----- */
function roundJobs() {
	if(hasBorderRadius()==true) return;
	var jobs = getByClass('jobs','','');
	if(jobs&&jobs[0]) {
		for(var i = 0; jobs[i]; ++i) {
			addClass(jobs[i],'roundedJobs');
			
			var b = getByClass('block',jobs[i],'div');
			if(b&&b[0]){
				for(var j = 0; b[j]; ++j){
					b[j].innerHTML = '<div class="innerBlock">'+b[j].innerHTML+'</div>';
				}
			}
		}
	}
	
	var job = getByClass('jobSplit','','');
	if(job&&job[0]) {
			addClass(job[0],'roundedJob');
			var b = getByClass('job',job[0],'div');
			if(b&&b[0]){
				b[0].innerHTML = '<div class="innerJob">'+b[0].innerHTML+'</div>';
			}
			var l = getByClass('jobList',job[0],'div');
			if(l&&l[0]){
				l[0].innerHTML = '<div class="innerList">'+l[0].innerHTML+'</div>';
			}
	}
}

/* Init
-------------------- */
var init = function() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if(!document.getElementsByTagName)
		return;
	addClass(getById('page'),'scripted');
	setQuiz();
	setCarto();
	setLinks(['a','area']);
	roundJobs();
	/*@cc_on @*/
	/*@if (@_win32)
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat( RegExp.$1 );
			if(rv < 7) fixLowIE();
			if(rv < 8) fixIE();
		}
	/*@end @*/
}

if (/WebKit/i.test(navigator.userAgent)) {
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			init();
		}
	}, 10);
}

else if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, null);
}

else {
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete")
			init();
	};
	/*@end @*/
	window.onload = init;
}
