var currentGalleryObject=null;
var currentGalleryPositionX;
var currentGalleryPositionY;


function initGallery(){
	gallerydiv=document.createElement("div");
	gallerydiv.id="showgallery";
	gallerydiv.className="hidegalleryclass";
	galleryp=document.createElement("p");
	galleryp.id="gallery_navi";
	naviimg=document.createElement("img");
	naviimg.className="img_navi_left";
	naviimg.src="images/default/left.png";
	naviimg.id="galleryNaviLeft";
	naviimg.alt=naviimg.title="zurück";
	galleryp.appendChild(naviimg);
	titlespan=document.createElement("span");
	titlespan.className="gallery_title";
	titlespan.id="galleryImageTitle";
	titlespan.innerHTML="&nbsp;Titel&nbsp;";
	galleryp.appendChild(titlespan);
	naviimg.className="img_navi_close";
	naviimg.src="images/default/schliessen.gif";
	naviimg.id="galleryClose";
	naviimg.alt=naviimg.title="schliessen";
	naviimg.setAttribute("onClick","hide()");
	galleryp.appendChild(naviimg);
	naviimg.className="img_navi_right";
	naviimg.src="images/default/right.png";
	naviimg.id="galleryNaviRight";
	naviimg.alt=naviimg.title="weiter";
	galleryp.appendChild(naviimg);
	naviimg.id="showgalleryimage";
	naviimg.src="";
	naviimg.alt=naviimg.title="";
	galleryp.appendChild(naviimg);
	gallerydiv.appendChild(galleryp);
	docbody[0].appendChild(gallerydiv);
}



function show(img){
	currentGalleryObject=img;
	galimg=document.getElementById("showgalleryimage");
	galimg.src="images/gallery/images/"+img.src.substring(img.src.lastIndexOf("/")+1);
	galimg.title=galimg.alt=img.title;
	var gallery=document.getElementById("showgallery");
	gallery.className="showgalleryclass";

	galleryNaviLeftElement=document.getElementById("galleryNaviLeft");
	galleryNaviLeftElement.src=hasPrevious(img)?"images/default/left.png":"images/default/left_disabled.png";
	galleryNaviLeftElement.title=galleryNaviLeftElement.alt=hasPrevious(img)?"vorhergehendes Bild":"";
	galleryNaviLeftElement.onclick =hasPrevious(img)? function () { showPrevious(); }:null;

	galleryNaviRightElement=document.getElementById("galleryNaviRight");
	galleryNaviRightElement.src=hasNext(img)?"images/default/right.png":"images/default/right_disabled.png";
	galleryNaviRightElement.title=galleryNaviRightElement.alt=hasNext(img)?"nächstes Bild":"";
	galleryNaviRightElement.onclick =hasNext(img)? function () { showNext(); }:null;

	document.getElementById("galleryImageTitle").innerHTML=img.title;
	gallery.onmousedown=moveGalleryStart;
	galimg.focus();
}


function hide(){
	var gallery=document.getElementById("showgallery");
	gallery.className="hidegalleryclass";
	gallery.onmousemove=null;
	currentGalleryObject=null;
	gallery.onmousedown=null;
	gallery.onmouseup=null;
	gallery.onmouseout=null;
}

function moveGallery(myEvent){
	
	var gallery=document.getElementById("showgallery");
	
	var newCurrentGalleryPositionX=parseInt(myEvent.screenX)-parseInt(currentGalleryPositionX);
	gallery.style.left= newCurrentGalleryPositionX+"px";
	var newCurrentGalleryPositionY=parseInt(myEvent.screenY)-parseInt(currentGalleryPositionY);
	gallery.style.top=newCurrentGalleryPositionY+"px";
	return true;
}

function moveGalleryStart(myEvent){
	var gallery=document.getElementById("showgallery");
	currentGalleryPositionX=gallery.style.left.trimpx();
	currentGalleryPositionX=='' && (currentGalleryPositionX=50);
	currentGalleryPositionY=gallery.style.top.trimpx();
	currentGalleryPositionY=='' && (currentGalleryPositionY=50);
	gallery.onmousemove=moveGallery;
	gallery.onmouseup=moveGalleryEnd;
	gallery.onmouseout=moveGalleryEnd;
	return true;
}

function moveGalleryEnd(myEvent){
	var gallery=document.getElementById("showgallery");
	gallery.onmousemove=null;
	gallery.onmousedown=null;
	gallery.onmouseup=null;
	gallery.onmouseout=null;
	return true;
}

function hasNext(element){
	return	element.nextSibling!=null && element.nextSibling.nodeName=="IMG";
}

function showNext(){
	show(currentGalleryObject.nextSibling);
	return true;
}

function hasPrevious(element){
	return	element.previousSibling!=null && element.previousSibling.nodeName=="IMG";
}

function showPrevious(){
	show(currentGalleryObject.previousSibling);
	return true;
}

  String.prototype.trimpx = function() { 
  		return this.indexOf("px")>-1?this.substr(0,this.length-2):this;
};

function showImage(url){
  imageWindow = window.open(url, "Image", "width=500,height=400,left=100,top=200, dependent=yes, location=no, menubar=no, resizable=yes,status=no, toolbar=no");
  imageWindow.focus();
}