/*-----------------------------------------------------------------------------------------------

 tandberg arkitekter mnal 2008
 website by eDIR/ ee (Espen Schjelderup, Espen Hofsvang) © Copyright 2008

 scripts: global

-------------------------------------------------------------------------------------------------
 browser detection
-----------------------------------------------------------------------------------------------*/
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: [
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		}
	],
	dataOS : []
};

BrowserDetect.init();

/*-----------------------------------------------------------------------------------------------
 sifr config
-----------------------------------------------------------------------------------------------*/
var efont = {
	src: '../../system/efont.swf',
	wmode: 'transparent'
};
sIFR.activate(efont);
sIFR.replace(efont, { selector: 'h1', css: '.sIFR-root { color: #313335; font-size: 15px; font-weight: normal; text-align: right; }', offsetLeft: '3', tuneHeight: '-2', sharpness: '-80', thickness: '10' });

/*-----------------------------------------------------------------------------------------------
 e_scroll config
-----------------------------------------------------------------------------------------------*/
e_scroll.slideDur = 400;

function scrollup(x) {
	e_scroll.scrollBy(x,0,200);
	return false;
}

function scrolldown(x) {
	e_scroll.scrollBy(x,0,-200);
	return false;
}

e_scroll.scrollBy = function(eCtx, x, y, dur) {
	if ( e_scrollObjs[eCtx] ) {
		e_scrollObjs[eCtx].glideBy(x, y, dur);
	}
}

e_scroll.prototype.glideBy = function(dx, dy, dur) {
	if ( !document.getElementById || this.sliding ) {
		return;
	}
	this.slideDur = dur || e_scroll.slideDur;
	this.destX = this.destY = this.distX = this.distY = 0;
	this.eY = document.getElementById(this.eCon);
	this.startX = this.x; this.startY = this.y;

	if (dy < 0) {
		this.distY = (this.startY + dy >= -this.maxY)? dy: -(this.startY  + this.maxY);
	}
	else if (dy > 0) {
		this.distY = (this.startY + dy <= 0)? dy: -this.startY;
	}
	if (dx < 0) {
		this.distX = (this.startX + dx >= -this.maxX)? dx: -(this.startX + this.maxX);
	}
	else if (dx > 0) {
		this.distX = (this.startX + dx <= 0)? dx: -this.startX;
	}
	this.destX = this.startX + this.distX; this.destY = this.startY + this.distY;
        this.slideTo(this.destX, this.destY);
}

e_scroll.prototype.slideTo = function(destX, destY) {
	this.per = Math.PI/(2 * this.slideDur); this.sliding = true;
	this.slideStart = (new Date()).getTime();
	this.aniTimer = setInterval(this.animString + ".doSlide()",10);
	this.on_slide_start(this.startX, this.startY);
}

e_scroll.prototype.doSlide = function() {
	var elapsed = (new Date()).getTime() - this.slideStart;
	if (elapsed < this.slideDur) {
		var x = this.startX + this.distX * Math.sin(this.per*elapsed);
		var y = this.startY + this.distY * Math.sin(this.per*elapsed);
		this.shiftTo(this.eY, x, y); this.on_slide(x, y);
	}
	else {
		clearInterval(this.aniTimer); this.sliding = false;
		this.shiftTo(this.eY, this.destX, this.destY);
		this.eY = null; this.on_slide_end(this.destX, this.destY);
	}
}

e_scroll.prototype.on_slide_start = function() {}
e_scroll.prototype.on_slide = function() {}
e_scroll.prototype.on_slide_end = function() {}

/*-----------------------------------------------------------------------------------------------
 internet explorer < 7 fixes
-----------------------------------------------------------------------------------------------*/
if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) {
	$(function() {
		$("#bar").css({ "height":"36px", "bottom":"-1px"});
		$(".symbol").hover(function () {
			$(this).css("background-position", '0 -60px');
		}, function () {
			$(this).css("background-position", '0 0');
		});
	});
};

/*-----------------------------------------------------------------------------------------------
 toggle om menu/content
-----------------------------------------------------------------------------------------------*/
function toggleom() {
	var bar = document.getElementById('bar');
	var footer = document.getElementById('footer');
	var ommenu = document.getElementById('ommenubar');
	var om = document.getElementById('om');

	if ( bar.style.display != 'block' ) {
		footer.style.display = 'block';
		bar.style.display = 'block';
		ommenu.style.display = 'block';
		wMove('ommenubar',0,0,6);
	}
	else {
/* 		setTimeout("ommenu.style.display = '';",100); */
		footer.style.display = '';
		ommenu.style.display = '';
		bar.style.display = '';
		om.style.display = '';
		wMove('om', 0,800,1);
		wMove('ommenubar',0,35,6);
	}
};

function openom(omcat, omid, list) {
	wMove('om', 0,800,1);
	var omcontainer = document.getElementById('om');
	var arrows = document.getElementById('omarrows');
	var omentries = omcontainer.getElementsByTagName('div');
	omcontainer.style.display = '';
	for( i=0; i<omentries.length; i++ ) {
		var omcheck = omentries[i].getAttribute('id');
		if (omcheck != omcat) {
			omentries[i].style.display = '';
		}
		else {
			wMove('om', 0,0,10);
			omcontainer.style.display = 'block';
			omentries[i].style.display = 'block';
			sIFR.replace(efont, { selector: 'h2', css: '.sIFR-root { background-color: #3663ad; color: #ffffff; font-size: 16px; font-weight: normal; text-align: left; }',	offsetTop: '-4', sharpness: '30', thickness: '-100' });
			if (list) {
				arrows.style.visibility = 'visible';
				var eID = new e_scroll(omcat, omid, null);
			}
			else {
				arrows.style.visibility = '';
			}
		}
	}
};

/*-----------------------------------------------------------------------------------------------
 credits pulse
-----------------------------------------------------------------------------------------------*/
$(function() {
	$("#credits a").hover(function () {
		$(this).css("background-position", '0 -35px');
		var $fade = $(this);
		if ($fade.is(':animated')) {
			$fade.stop().fadeTo(200, '1.0');
		}
		else {
			$(this).fadeTo(200, '1.0');
		}
	}, function () {
		$(this).css("background-position", '0 -35px');
		var $fade = $(this);
		if ($fade.is(':animated')){
			$fade.stop().fadeTo(400,'0.6');
		}
		else {
			$(this).fadeTo(400, '0.6');
		}
	});
});


/*-----------------------------------------------------------------------------------------------
 alphapng, externalurl - png alpha opacity support for ie6-, substitute for target="_blank"
-----------------------------------------------------------------------------------------------*/
var alphaExt = function() {

	var blankgif = "graphics/x.gif";

	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		}
		else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}

	function fnLoadPngs() {
		var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
		for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
			if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
				fnFixPng(obj);
				obj.attachEvent("onpropertychange", fnPropertyChanged);
			}
		}
	}

	function fnPropertyChanged() {
		if (window.event.propertyName == "style.backgroundImage") {
			var el = window.event.srcElement;
			if (!el.currentStyle.backgroundImage.match(/x\.gif/i)) {
				var bg	= el.currentStyle.backgroundImage;
				var src = bg.substring(5,bg.length-2);
				el.filters.item(0).src = src;
				el.style.backgroundImage = "url('" + blankgif + "')";
			}
		}
	}

	function fnFixPng(obj) {
		var bg	= obj.currentStyle.backgroundImage;
		var src = bg.substring(5,bg.length-2);
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		obj.style.backgroundImage = "url('" + blankgif + "')";
	}

	function externalUrl() {
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	}

	return {
		init: function() {
			if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
				addLoadEvent(fnLoadPngs);
			}
			addLoadEvent(externalUrl);
		}
	}
}();

alphaExt.init();
