
function readGallery(maincontent){
var images = xGetElementsByTagName("img",maincontent);
activateMouseOverSlides(images);
}

function activateMouseOverSlides(tr){
for (i=0; i<tr.length; i++) {
	tr[i].screenImage = new Image();
	tr[i].screenImage.src = preloadScreenImage(tr[i].src);
	tr[i].style.cursor="pointer";
	tr[i].onmousedown = zoomImage;
	}
}

function zoomImage(){
var image = this.screenImage.src;
var zoombox = xGetElementById('ZoomImage');
var img = zoombox.childNodes[1];
var caption = this.getAttribute('alt') || '';
img.swap = function(){
	xOpacity('main_content',0.4);
	zoombox.childNodes[2].innerHTML = caption;//h6: caption
	this.src = image;
	xShow('ZoomImage');
	switchZoomedImage(false);
	}
clearTimeout(AD.zoomtimer); 
switchZoomedImage(true);
}

function switchZoomedImage(show){
var img =  xGetElementById('ZoomImage').childNodes[1];
var dir = (show)?-1:1;
var end = (dir==1)?100:0;
var X;
if (AD.zoomcounter*dir<end*dir){
	AD.zoomcounter += dir*(1+Math.abs(end-AD.zoomcounter)/10);
	if(AD.zoomcounter*dir>=end*dir) AD.zoomcounter = end;	
	X = AD.zoomcounter/100;
	xOpacity('ZoomImage', X);	
	AD.zoomtimer = setTimeout(function(){switchZoomedImage(show)},30);
	} else {clearTimeout(AD.zoomtimer); if (img.rst) img.rst(); else if (show) img.swap();}
}

function closeImage(){	
var img =  xGetElementById('ZoomImage').childNodes[1];
img.rst = function(){
		this.rst=false;
		xOpacity('main_content',1);
		if (IE) xGetElementById('main_content').style.filter = null;
		xHide('ZoomImage');
		}
clearTimeout(AD.zoomtimer); 
switchZoomedImage(true);
}

function displayGoogleMap(map){
xShow('ZoomImage');
xGetElementById('ZoomImage').childNodes[1].src = map;
clearTimeout(AD.zoomtimer);
switchZoomedImage(false);
}

function preloadScreenImage(ele){
var path = ele.split('/');
var image="";
for (j=0;j<path.length-1;j++) image += path[j]+"/";
image +="full_size/"+path[path.length-1];
return image;
}

function displayMainPic(pic,image){
pic.swap = function(){
	this.src = image;
	switchMainPic(this, false)
	}
clearTimeout(AD.zoomtimer); 	
switchMainPic(pic, true)		
}

function switchMainPic(pic, show){
var dir = (show)?-1:1;
var end = (dir==1)?100:0;
var X;
if (AD.imagecounter*dir<end*dir){
	AD.imagecounter += dir*(1+Math.abs(end-AD.imagecounter)/10);
	if(AD.imagecounter*dir>=end*dir) AD.imagecounter = end;	
	X = AD.imagecounter/100;
	xOpacity(pic, X);
	xShow(pic);
	AD.zoomtimer = setTimeout(function(){switchMainPic(pic,show)},20);
	} else {clearTimeout(AD.zoomtimer); if (show) pic.swap();}
}

preloadSelectionTours = function() {
xClip('tour_selection',0,0,0,0);
HomePics = xGetElementsByClassName('screenpic',xGetElementById('positionable_layer'),'img');
AD.picindex = 1 + Math.floor(Math.random()*(HomePics.length-1));//picront dont count!
HomePics[AD.picindex].onload = displayHomePic;
for (i=1; i<HomePics.length; i++) HomePics[i].src = preloadScreenImage(HomePics[i].src);
var pause =  xGetElementById('pause').firstChild;
pause.onmouseover = function(){AD.pauserotation=true;AD.radiotimer=50;xDisplay('message','block');}
pause.onmouseout = function(){AD.pauserotation=false;xDisplay('message','none')}
var iframe = document.createElement('iframe');
iframe.setAttribute('width',0);iframe.setAttribute('height',0);iframe.setAttribute('frameborder',0);
iframe.setAttribute('name','homepics_iframe');iframe.setAttribute('id','homepics_iframe');
iframe.setAttribute('src','tours_lists/selection_tours.html');
document.body.appendChild(iframe);
ele  = new Object();
ele.readSelectedPage = readSelectionTours;
if (IE) waitFrameLoading(ele,iframe); else iframe.onload = readSelectionTours;
}

function readSelectionTours(test){
var iframe = xGetElementById('homepics_iframe');
clearTimeout(iframe.timer);
var framecontent;
if (iframe.contentDocument) framecontent = iframe.contentDocument; else if (iframe.contentWindow) framecontent=iframe.contentWindow.document;
var main = framecontent.getElementById('main_content');
setExtensionCodes(SELECTION[0].code,main);//needed for hyperlinks to keep correct paths
var tours = xGetElementsByClassName('tour',main,'div');//get tours list
setSelectionNavButtons(tours);
}

function setSelectionNavButtons(tours){
var buttonlayer = xGetElementById('buttonlist');
for (i=0; i<tours.length; i++){
	var radio = document.createElement('a');
	radio.innerHTML = i+1;
	radio.value = i;
	radio.setAttribute('href','#');
	radio.onmousedown = function(){AD.pauserotation=true;displaySelectionTour(tours[this.value]);}
	buttonlayer.appendChild(radio);
	tours[i].radio = radio;
	}	
AD.tourindex = Math.floor(Math.random()*tours.length);
xGetElementById('previous').onmousedown = function(){//left arrow
												AD.pauserotation=true;
												AD.tourindex--;if(AD.tourindex<0) AD.tourindex=tours.length-1;
												displaySelectionTour(tours[AD.tourindex]);
												}
xGetElementById('next').onmousedown = function(){//right arrow
												AD.pauserotation=true;
												AD.tourindex++;if(AD.tourindex == tours.length) AD.tourindex=0;
												displaySelectionTour(tours[AD.tourindex]);
												}
xShow('buttonlayer');
AD.radiotimer=5000;
rotateRadioButtons(tours); 
}

function rotateRadioButtons(tours){
if (!AD.pauserotation){
		displaySelectionTour(tours[AD.tourindex]);
		AD.tourindex++;if(AD.tourindex == tours.length) AD.tourindex=0;
		AD.radiotimer=5000;
		}
AD.radiobuttontimer = setTimeout(function(){clearTimeout(AD.radiobuttontimer);rotateRadioButtons(tours);},AD.radiotimer);
}

function displaySelectionTour(tour){
var selection = xGetElementById('tour_selection');
if (AD.checkedbutton) AD.checkedbutton.className = 'default';
AD.checkedbutton =tour.radio;
AD.tourindex = tour.radio.value;
var details =  xGetElementById('details');
selection.changeSelection = function(){
	this.innerHTML = tour.innerHTML.replace(/src="..\//gi,'src="');//set the correct pass for images (small icons);
	var title = xGetElementsByTagName('h2',this);//add extention to hyperlink
	details.setAttribute('href',title[0].firstChild.href);
	xGetElementById('pause').firstChild.setAttribute('href',title[0].firstChild.href);
	tour.radio.className = 'checked';
	clearTimeout(AD.textotimer);
	flipSelection(this,true);
	}
xDisplay(details,'none');
//xDisplay('pause','none');
clearTimeout(AD.textotimer);
flipSelection(selection,false);
}

function activateCompass(){
xDisplay('details','block');
//xDisplay('pause','block');
displayHomePic();	
}

function flipSelection(ele,show){
var dir = (show)?-1:1;
var end = (dir==1)?100:0;
var H = 300;
var W = 600;
var V,L;
if (AD.selectioncounter*dir<end*dir){
	AD.selectioncounter += dir*(1+Math.abs(end-AD.selectioncounter)/6);
	if(AD.selectioncounter*dir>=end*dir) AD.selectioncounter = end;
	V = Math.round(H*AD.selectioncounter/200);
	L = Math.round(W*AD.selectioncounter/200);
	xClip(ele,V,W-L,H-V,L);
	xDisplay(ele,"block");	
	AD.textotimer = setTimeout(function(){flipSelection(ele,show)},AD.counterspeed);
	} else {
		clearTimeout(AD.textotimer);
		if(!show) AD.textotimer = setTimeout(function(){ele.changeSelection()},1000); else activateCompass();
		}												//show a different homepic in between 2 selections																																								
}

function displayHomePic(){
xGetElementById('picfront').src = HomePics[AD.picindex].src;
AD.picindex++; if (AD.picindex>=HomePics.length) AD.picindex = 1;
}

clearIframe = function() {
var iframe = xGetElementById('homepics_iframe');
if (iframe){
	clearTimeout(AD.screentimer);
	clearTimeout(iframe.timer);
	clearTimeout(AD.randomimagestimer);
	clearTimeout(AD.radiobuttontimer);
	//iframe.src = '';
	}
}

