﻿var nextActiveImage=0;
var previousActiveLInk;
var ie=false;
var iPhone=false,iPad=false;
if (navigator.appVersion.indexOf('iPhone')>-1 || navigator.appVersion.indexOf('Android')>-1 || navigator.appVersion.indexOf('iPad')>-1) var touchscreen=true;
if (navigator.appVersion.indexOf('iPhone')>-1) iPhone=true;
if (navigator.appVersion.indexOf('iPad')>-1) iPad=true;
if (document.all) ie=true;

function Gallery() {
	var timeStamp = new Date().getTime().toString();
	var activeSlide,overlay, container, closeButton, miniBrowserContainer, miniBrowser, miniBrowserHeight, miniBrowserLink, scrollBox;
	var fullImageScroll,thumbnailScroll;
	
	var galleryItem = new Array();
	var headTag=document.getElementsByTagName("head")[0];
	var scriptTag=document.createElement("script");
	var metaView=document.getElementById('view');
	scriptTag.setAttribute("type","text/javascript");
	scriptTag.setAttribute("src","/js/iscroll.js");
	headTag.appendChild(scriptTag);
	var touchSheet=document.createElement("link");
	touchSheet.setAttribute("rel", "stylesheet");
	touchSheet.setAttribute("type", "text/css");
	if (touchscreen) {
		touchSheet.setAttribute("href","/css/touchscreen.css?"+timeStamp);
	} else {
		touchSheet.setAttribute("href","/css/gallery.css?"+timeStamp);
	}
	headTag.appendChild(touchSheet);
	
	var gallery=document.getElementById('gallery');
	var galleryLink=gallery.getElementsByTagName('a');
	
	miniBrowserContainer = document.createElement('div');
	miniBrowserContainer.id = "browserContainer";
	miniBrowserContainer.className = "browserContainer";

	miniBrowser = document.createElement('ul');
	miniBrowser.id="galleryMiniBrowser";
	miniBrowser.style.textAlign="center";
	miniBrowser.showing=true;
	miniBrowser.innerHTML=gallery.innerHTML;
	
	
	miniBrowserLink=miniBrowser.getElementsByTagName('a');
	for (i=0;i<miniBrowserLink.length;i++) {
		miniBrowserLink[i].onclick=function() {
			this.openImage;
		}
	}
	function preventDefault(e) { e.preventDefault(); }
	initiateGallery = function() {
		if (touchscreen) document.addEventListener('touchmove', preventDefault, false);
		overlay=document.createElement('div');
		overlay.id="galleryOverlay";
		document.body.appendChild(overlay);
		if (!touchscreen) {
			overlay.style.width="896px";
			overlay.style.height="600px";
			if (ie) {
				overlay.style.top=((document.documentElement.clientHeight/2)-300)+"px";
			} else {
				overlay.style.top=((window.innerHeight/2)-300)+"px";
			}
		}
		container=document.createElement('div');
		container.id="galleryContainer";
		overlay.appendChild(container);
		
		closeButton = document.createElement('img');
		closeButton.id="galleryCloseButton";
		closeButton.onclick = closeImage;
		closeButton.src='/css/images/gfx-button-close.png';
		overlay.appendChild(closeButton);
		
		overlay.appendChild(miniBrowserContainer);
		miniBrowserContainer.appendChild(miniBrowser);

		miniBrowserHeight=(miniBrowser.showing)?miniBrowser.clientHeight : 0;
		if (!touchscreen) {
			var backwardButton = document.createElement('div');
			var forwardButton = document.createElement('div');
			
			backwardButton.innerHTML='◄';
			forwardButton.innerHTML='►';
			backwardButton.className='navigateButton';
			forwardButton.className='navigateButton';
			forwardButton.style.marginLeft='876px';
			
			forwardButton.onclick=function() {
				if (activeSlide<galleryItem.length-1) {
					document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[activeSlide].className='';
					activeSlide++;
					gotoActiveSlide(true);
				}
			}
			backwardButton.onclick=function() {
				if (!activeSlide==0) {
					document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[activeSlide].className='';
					activeSlide--;
					gotoActiveSlide(true);
				}
			}
			overlay.appendChild(backwardButton);
			overlay.appendChild(forwardButton)
		}
		scrollBox=document.createElement('div');
		scrollBox.id="scrollBox";
		container.appendChild(scrollBox);
		overlay.style.opacity=1;
		scrollEndHandler = function(e) {
			document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[activeSlide].className='';
			if (touchscreen) {
				activeSlide=Math.round((scrollBox.style.WebkitTransform.replace("translate3d(","").split("px,")[0]*-1)/window.innerWidth);
			} else {
				activeSlide=Math.round(scrollBox.getElementsByTagName('div')[activeSlide].offsetLeft/896);
			}
			document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[activeSlide].className='active';
		}
		miniBrowserLink=miniBrowser.getElementsByTagName('a');
		for (i=0;i<miniBrowserLink.length;i++) {
			miniBrowserLink[i].className='';
			galleryItem.push(miniBrowserLink[i]);
			var imageContainer=document.createElement('div');
			imageContainer.className='imageContainer';
			imageContainer.style.float='left';
			imageContainer.style.textAlign='center';
			scrollBox.appendChild(imageContainer);
			
			var image=document.createElement('img');
			imageContainer.appendChild(image);
	
			image.onload = function(e) {
				this.origWidth=this.width;
				this.origHeight=this.height;
			}
			image.onchange = function(e) {
				this.origWidth=this.width;
				this.origHeight=this.height;
			}
			image.url=galleryItem[i].href;
			image.src=galleryItem[i].href;
			miniBrowserLink[i].number=i;
			miniBrowserLink[i].onclick=function() {
				document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[activeSlide].className='';
				activeSlide=this.number;
				gotoActiveSlide(false);
				return false;
			}
		}
		if (galleryItem.length>9) browserContainer.style.height="95px";
		var thumbnailWidth=(touchscreen?128:80)
		if (touchscreen) miniBrowser.style.width=(miniBrowserLink.length*thumbnailWidth)+"px";
		setTimeout(function() {
			if (nextActiveImage>=0) { 
				//document.getElementById('galleryMiniBrowser').getElementsByTagName('a')[nextActiveImage].className='active';
				if (typeof previousActiveLink!="undefined") previousActiveLink.className='';
				updateContainerSize();
			}
			if (touchscreen) {
				fullImageScroll = new iScroll('galleryContainer', {snap:true, momentum:false, vScrollbar:false,onScrollEnd:scrollEndHandler});
				thumbnailScroll = new iScroll('browserContainer', {vScroll:false, fadeScrollbar:true,hideScrollbar:true});
			} else {
				scrollBox.style.position='relative';
			}
			gotoActiveSlide(true);
		},450);
	}
	this.nextActiveImage = 0;
	this.previousActiveLink;
	
	updateContainerSize = function(e) {
		if (touchscreen && !iPad) metaView.setAttribute("content","initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5,user-scalable=no");
		window.scrollTo(0,1);
		if (touchscreen) {
			this.clientHeight=window.innerHeight-miniBrowserHeight;
			this.clientWidth=window.innerWidth;
		} else {
			this.clientHeight=520;
			this.clientWidth=896;
			if (galleryItem.length>9) miniBrowser.style.width=(galleryItem.length*80+20)+"px";
		}
		
		if (iPhone) {
			$("#browserContainer").css("bottom","-65px");
			this.clientHeight+=65;
			overlay.style.height=this.clientHeight+"px";
			scrollBox.style.height=this.clientHeight+"px";
			$("#galleryContainer").css("height",this.clientHeight+"px");
		}
		if (touchscreen && !iPhone) miniBrowserContainer.style.height="127px";
		container=document.getElementById('galleryContainer');
		
		scrollBox.style.width=(miniBrowserLink.length*this.clientWidth)+"px";
		scrollBox.style.height=this.clientHeight+"px";
		
		miniBrowserHeight=(miniBrowser.showing)?miniBrowser.clientHeight : 0;
		
		var image=document.getElementById('fullImage');
		var container=document.getElementById('galleryContainer');
		
		var imageCollection=scrollBox.getElementsByTagName('img');
		var imageContainerCollection=scrollBox.getElementsByTagName('div');
		for (a=0;a<imageCollection.length;a++) {
			var imageContainer=imageContainerCollection[a];
			if (typeof imageCollection[a].origWidth=="undefined") {
				imageCollection[a].origWidth=imageCollection[a].width;
				imageCollection[a].origHeight=imageCollection[a].height;
			}
			imageContainer.style.width=this.clientWidth+"px";
			imageContainer.style.height=this.clientHeight+"px";
			imageCollection[a].scaleX=this.clientWidth/imageCollection[a].origWidth;
			imageCollection[a].scaleY=this.clientHeight/imageCollection[a].origHeight;
			imageCollection[a].style.width=this.clientWidth+"px";
			imageCollection[a].style.height=this.clientHeight+"px";
			(imageCollection[a].scaleX < imageCollection[a].scaleY ) ? imageCollection[a].style.height=(imageCollection[a].scaleX*imageCollection[a].origHeight)+"px" : imageCollection[a].style.width=(imageCollection[a].scaleY*imageCollection[a].origWidth)+"px";
			imageContainer.style.opacity=1;
			if (touchscreen) {
				imageCollection[a].style.position='relative';
				imageCollection[a].style.top=Math.round(((window.innerHeight-miniBrowserHeight)/2)-(imageCollection[a].height/2))+"px";
			} 
		}
	}
	var gotoActiveSlide = function(firstTimeRun) {
		if (touchscreen) {
			thumbnailScroll.scrollToElement(miniBrowser.getElementsByTagName('li')[activeSlide], 300);
			fullImageScroll.scrollToElement(scrollBox.getElementsByTagName('div')[activeSlide],300);
		} else {
			var targetX=0-scrollBox.getElementsByTagName('div')[activeSlide].offsetLeft;
			$(scrollBox).animate({left:targetX},300,scrollEndHandler);
		}
	}
	window.onorientationchange = updateContainerSize;
	window.onresize = updateContainerSize;
	var closeImage = function() {
		$('#galleryOverlay').css("opacity",0);
		$('#galleryOverlay').fadeOut({ duration: '0.6',easing: 'swing',complete: function() { document.getElementById('galleryOverlay').parentNode.removeChild(document.getElementById('galleryOverlay'));}});
		document.body.style.overflow="auto";
		metaView.setAttribute("content","user-scalable:yes");
		if (touchscreen) document.removeEventListener('touchmove',preventDefault, false);
	}	
	
	this.openImage = function(e) {
		var URL, obj;
		if (!e) {
			obj=window.event.srcElement.parentNode;
			URL=obj.parentNode.toString();
		} else {
			obj=e.currentTarget;
			URL=obj.href;
		}
		activeSlide=obj.number;
		if (!document.getElementById('galleryOverlay')) {
			initiateGallery();
		}
		window.scrollTo(0,1);
		
		return false;
	}
	for (i=0;i<galleryLink.length;i++) {
		galleryLink[i].onclick=this.openImage;
		galleryLink[i].number=i;
	}
}
var myGallery;
window.onload = function() {
	if (document.getElementById('gallery')) myGallery=new Gallery();
	if (document.getElementById('newsArchives')) {
		process_hashed_url(document.location.href);	/* See if current location has a hash */
	}
	if (document.getElementById('playButton')) {
		var playButton=document.getElementById('playButton');
		var playButtonHover=document.createElement('div');
		var player=document.getElementById('playCapture');
		playButtonHover.id="playButtonHover";
		playButton.appendChild(playButtonHover);
		playButtonHover.onmouseover = function() {
			//$(this).fadeTo('fast',1);
			$(this).animate({ opacity:1},200,'swing');
		};
		playButtonHover.onmouseout = function() {
			//$(this).fadeTo('fast',0);
			$(this).animate({ opacity:0},200,'linear');
		};
	}
}
function Form() {
	this.clearOnFocus = function(obj) {
		obj.defaultValue=obj.value;
		obj.onblur=function() {
			if (this.value=='') {
				this.value=this.defaultValue;
			}
		}
		obj.onfocus=function() {
			if (this.value==this.defaultValue) {
				this.value='';
			}
		}
		obj.value='';
	}
	this.searchOnEnter = function(obj) {
		obj.onkeypress = function(e) {
			if (window.event) {
				var e = new Object();
				e.keyCode = window.event.keyCode;
			}
			if (e.keyCode==13) {
				var language=document.location.href.split("/")[3];
				document.location.href='/'+language+'/Default.aspx?action=search&q='+escape(obj.value);
				return false;
			}
		}
	}
}
var scandiForm = new Form();

function seeArchive(year) {
	if (document.getElementById('newsArchives')) {
		archiveDivs=document.getElementById('newsArchives').getElementsByTagName('div');
		for (i=0;i<archiveDivs.length;i++) {
			archiveDivs[i].style.display='none';
		}
		archiveLinks=document.getElementById('newsArchiveYears').getElementsByTagName('a');
		for (i=0;i<archiveLinks.length;i++) {
			archiveLinks[i].className='';
			hash=archiveLinks[i].toString().split('#');
			if (hash[1]==year) {
				archiveLinks[i].className='active';
			}
		}
		if (document.getElementById(year)) {
			document.getElementById(year).style.display='inline';
		}
	}
}
function process_hashed_url(url) {
	var res = url.match(/#(.+)$/);
	if (!res) {
		//return;	/* Skip if not "hash" link */
		var date = new Date();
		var hash = date.getFullYear();
	} else {
		var hash = res[1];
		window.location.hash=hash;
	}
	seeArchive(hash);
}
function startVideo() {
	$('#player img, #introduction').fadeOut('swing',function() {
		if (touchscreen || !swfobject.hasFlashPlayerVersion("9.0.0")) {
			var videoPlayer=document.createElement('video');
			videoPlayer.width=630;
			videoPlayer.height=354;
			videoPlayer.autoplay="true";
			videoPlayer.setAttribute("autoplay","autoplay");
			videoPlayer.controls="controls";
			videoPlayer.src="/documents%20and%20images/video/Scandibyg.m4v";
			document.getElementById('player').innerHTML="";
			document.getElementById('player').appendChild(videoPlayer);
		} else {
			var flashvars2 = {};
			flashvars2.videoUrl = "/documents%20and%20images/video/Scandibyg.m4v";			
			flashvars2.baseColor = "0x000000";
			flashvars2.scaleToVideoHeight = "false";
			flashvars2.captureTime = "5";
			flashvars2.useTimeDisplay = "true";
			flashvars2.useVolumeBar = "true";
			flashvars2.autoPlay = "true";
			flashvars2.hasFullscreenButton = "true";
			flashvars2.actionColor = "0xcb7f21";
			var attributes = {};
			attributes.id = "fcid";
			var params2 = {};
			params2.wMode = "opaque";
			params2.allowfullscreen = "true";
			params2.bgcolor = "0x00000";
			swfobject.embedSWF("/flash/video/bin/player.swf", "player", "630", "354", "9.0.0", false, flashvars2, params2, attributes);
		}
	});
}
function toggleSection(section,obj) {
	if ($(obj).html()=='-') {
		$(obj).html('+');
		obj.title="Vis";
		obj.sectionHeight=$('#'+section).height();
		$('#'+section).css('overflow','hidden');
		var targetHeight=2;
		if (section=='baeredygtigColumns') targetHeight=190;
		$('#'+section).animate({ height: targetHeight },400,'easeOutCubic');
	} else {
		obj.title="Skjul";
		$('#'+section).animate({ height: obj.sectionHeight },400,'easeOutCubic');
		$(obj).html('-');
	}
}
