﻿function MoveNavigation()
{
		 var sourceElm = document.getElementById("mergefield_navigation_level2");
		 if(!sourceElm || !sourceElm.childNodes[0]) return;
		 var targetElm = document.getElementById("NewNavigation");
		 if(!targetElm) return;
 
		 targetElm.appendChild(sourceElm);
		 sourceElm.style.visiblity = "visible";
		 sourceElm.style.display = "block";
		 sourceElm.childNodes[0].style.visibility = "visible";
			 Tangora.Layout.Resize();  
}
Tangora.Events.AddHandler(window,"onload",MoveNavigation);



blurAnchors = function(){
  if(document.getElementsByTagName){
    var a = document.getElementsByTagName("a");
    for(var i = 0; i < a.length; i++){
	a[i].onfocus = function(){this.blur()};
    }
  }
}
Tangora.Events.AddHandler (window, "onload", blurAnchors);

function CourseGallery() {
	this._width = 1100;
	this._height = 700;
	this._screenWidth = 1100;
	this._numImages = 1;
	this._images = [];
	this._current = 0;
	this._newWidth = 0;
	this._lbid = "hhggallery";

	this.ScreenWidth();
}

CourseGallery.prototype.Init = function (recid) {

	this._images = $elms('.gallerythumbnail');

	this._numImages = this._images.length;

	for (var i = 0; i < this._numImages; i++) {
		this._images[i].setAttribute('tabindex', i);
	}

	$elm('list_filter81').options[0].text = 'Alle';
	$elm('list_filter81').onchange = cg.ChangeCourse;

	if ($ts.exists(recid)) {
		cg.ShowLightBox(recid);
	}
}

CourseGallery.prototype.Next = function () {

	var image = {};
	cg._current++;
	if (cg._current < cg._numImages) {
		image = cg._images[cg._current];
	} else {
		cg._current = 0;
		image = cg._images[cg._current];
	}

	var recid = image.getAttribute('rel').replace('recid', '');
	var data = $ts.ajax({
		url: '/page150.aspx?recordid150=' + recid,
		type: 'GET',
		async: false
	});

	var result = data.match(/<!--jsonstart-->(.*)<!--jsonend-->/g);
	result = result[0].replace('<!--jsonstart-->', '').replace('<!--jsonend-->', '');
	data = eval(result);
	data = eval(data);
	data = data.records[0];

	$elm('galleryimage' + cg._lbid).src = '/media/imagegenerator/' + cg._newWidth + 'x0/' + data.image;
	var text = $elm('shorttext' + cg._lbid)
	text.innerHTML = '';
	var shorttext = $ts.createElement('em');
	shorttext.appendChild(document.createTextNode(data.shorttext));
	text.appendChild(document.createTextNode(data.holetext + ' '));
	text.appendChild(shorttext);
}

CourseGallery.prototype.Prev = function () {
	var image = {};
	cg._current--;
	if (cg._current > -1) {
		image = cg._images[cg._current];
	} else {
		cg._current = cg._numImages - 1;
		image = cg._images[cg._current];
	}

	var recid = image.getAttribute('rel').replace('recid', '');
	var data = $ts.ajax({
		url: '/page150.aspx?recordid150=' + recid,
		type: 'GET',
		async: false
	});

	var result = data.match(/<!--jsonstart-->(.*)<!--jsonend-->/g);
	result = result[0].replace('<!--jsonstart-->', '').replace('<!--jsonend-->', '');
	data = eval(result);
	data = eval(data);
	data = data.records[0];

	$elm('galleryimage' + cg._lbid).src = '/media/imagegenerator/' + cg._newWidth + 'x0/' + data.image;
	var text = $elm('shorttext' + cg._lbid)
	text.innerHTML = '';
	var shorttext = $ts.createElement('em');
	shorttext.appendChild(document.createTextNode(data.shorttext));
	text.appendChild(document.createTextNode(data.holetext + ' '));
	text.appendChild(shorttext);
}

CourseGallery.prototype.ShowLightBox = function (recid) {

	var elm = $elm('image' + recid);

	cg._current = parseInt(elm.getAttribute('tabindex'));

	var data = $ts.ajax({
		url: '/page150.aspx?recordid150=' + recid,
		type: 'GET',
		async: false
	});

	cg._lbid = cg._lbid + recid;

	var result = data.match(/<!--jsonstart-->(.*)<!--jsonend-->/g);
	result = result[0].replace('<!--jsonstart-->', '').replace('<!--jsonend-->', '');
	data = eval(result);
	data = eval(data);
	data = data.records[0];

	var width = cg._width;
	var height = cg._height;
	if (cg._width > cg._screenWidth) {
		width = cg._width * 1.80;
		height = cg._height * 1.80;
	}

	cg._newWidth = width;

	var root = $ts.createElement('div', { 'id': 'gallerybig' + cg._lbid }, { 'width': width + 'px', 'padding': '4px', 'background': '#ffffff' });
	var container = $ts.createElement('div', { 'id': 'bigcontainer' + cg._lbid });

	var img = $ts.createElement('img', { 'id': 'galleryimage' + cg._lbid, 'alt': data.shorttext });
	img.src = '/media/imagegenerator/' + width + 'x0/' + data.image;
	container.appendChild(img);

	var text = $ts.createElement('p', { 'className': 'galleryshortext', 'id': 'shorttext' + cg._lbid });
	var shorttext = $ts.createElement('em');
	shorttext.appendChild(document.createTextNode(data.shorttext));
	text.appendChild(document.createTextNode(data.holetext + ' '));
	text.appendChild(shorttext);
	container.appendChild(text);

	if (this._numImages > 1) {
		var prev = $ts.createElement('div', { 'id': 'galleryprev' + cg._lbid, 'className': 'galleryprev' }, { 'paddingTop': Math.floor((height - 45) / 2) + 'px', 'height': (height - Math.floor((height - 45) / 2)) + 'px' });
		prev.onclick = cg.Prev;
		var previmg = $ts.createElement('img', { 'src': '/media/HG_Galleri_prev.png', 'alt': 'Forrige' });
		prev.appendChild(previmg);
		container.appendChild(prev);

		var next = $ts.createElement('div', { 'id': 'gallerynext' + cg._lbid, 'className': 'gallerynext' }, { 'paddingTop': Math.floor((height - 45) / 2) + 'px', 'height': (height - Math.floor((height - 45) / 2)) + 'px' });
		next.onclick = cg.Next;
		var nextimg = $ts.createElement('img', { 'src': '/media/HG_Galleri_next.png', 'alt': 'Næste' });
		next.appendChild(nextimg);
		container.appendChild(next);
	}

	root.appendChild(container);

	LightBox.Open(root, null, null, null, null, null, this._lbid);
}

CourseGallery.prototype.ScreenWidth = function () {
	if (document.body && document.body.offsetWidth) {
		this._screenWidth = document.body.offsetWidth;
	}
	if (document.compatMode == 'CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth) {
		this._screenWidth = document.documentElement.offsetWidth;
	}
	if (window.innerWidth && window.innerHeight) {
		this._screenWidth = window.innerWidth;
	}
}

CourseGallery.prototype.ChangeCourse = function(){
	
	document.rediger150.submit();

}

var cg = new CourseGallery();
