var loadURL;
var loadCap;
var loadUid;
var loadIndex;
var loadW;
var loadH;
var preloadURL;

// images
imgs = new Array();
	
//thumbs
var thumbId = 0;
var currImg = null;
var imgShown = false;

function registerImg(theURL, theCap, theUid, theIndex, w, h){

	if(imgs[theUid+""] == undefined){
		imgs[theUid+""] = new Array();
		
	}
	if (imgs[theUid+""][theIndex] == undefined){
		imgs[theUid+""][theIndex] = new Array(theURL, theCap, w, h);
	}
}
	
// define id for thumbnails
function defineId(id){
	thumbId = id;
}

function popDiv(theURL, theCap, theUid, theIndex, w, h, thumbImage){
	loadURL = theURL;
	loadCap = theCap;
	loadUid = theUid;
	loadIndex = theIndex;
	loadW = w;
	loadH = h;
	
	var showPic = $('showPic');
	var coverPic = $('coverPic');
	imgShown = true;
	if(!$chk(showPic)){
		coverPic   = new Element('div', {id: 'coverPic'});
		coverPic.inject($(document.body), 'bottom');
		coverPic.set('styles', {opacity: .9});
		
		showPic   = new Element('div', {id: 'showPic'});
		showPic.inject($(document.body), 'bottom');		
	}
	var coverAll = $('blogCoverAll');
	if(!$chk(coverAll)){
		coverAll   = new Element('div', {id: 'blogCoverAll'});
		coverAll.inject($(document.body), 'bottom');
		coverAll.set('styles', {opacity: .5, display: 'block'});
	} else {
		coverAll.set('styles', {display: 'block'});
	}
	

	if(thumbImage){
		var a = document.getElementById(thumbImage);
		aImg  = a.getElementsByTagName("img");
		fillPopDiv(aImg[0].src, theCap, theUid, theIndex, w, h)
	}
			
	
		  	
  	startLoader(theURL);
}

function positionPic(){
	var scroll = window.getScroll(), size = window.getSize();
	var l = (size.x/2) - (loadW/2) - 23 + scroll.x;
	var t = (size.y/2) - (loadH/2) -23 + scroll.y
	var wPad = loadW+46;
	var hPad = loadH+46;
	$('showPic').setStyles({left: l, top: t, width: loadW+23});
	$('coverPic').setStyles({left: l, top: t, width: wPad, height: hPad});
}
		  
function fillPopDiv(theURL, theCap, theUid, theIndex, w, h){
  	var nummer = theIndex+1;
	var anzahl = imgs[theUid].length;
	var popD = document.getElementById("showPic");
	$('blogCoverAll').setStyles({zIndex: 1002});
	
	position();

  	var str = "";
	str += '<div id="hidePic">';
	str += '<a href="javascript:hidePic();">';
	str += '<img src="/fileadmin/user/daskorn/templates/grafik/closeblog.gif" border="0" alt="close"/>';
  	str += "</a>";
  	str += "</div>";
  	str += "<div>";
  	
  	if((theIndex + 1) < imgs[theUid].length){
  		var newIndex = theIndex + 1;
  	} else {
  		var newIndex = 0;
  	}
  	
  	var newURL = imgs[theUid][newIndex][0];
	var newCap = imgs[theUid][newIndex][1];
	var newW = imgs[theUid][newIndex][2];
	var newH = imgs[theUid][newIndex][3];
	
	str += "<a href=\"javascript:popDiv('"+ newURL+"', '"+ newCap+"', '"+theUid+"', "+(newIndex)+", "+newW+", "+newH+");\">";;	  	
  	str += "<img border='0' src='" + theURL + "' alt='' width='"+w+"' height='"+h+"'/>";
	str += "</a>";		
  	str += "</div>\n";
	
  	popD.innerHTML = str;
  	popD.style.display = "block";
  	$('coverPic').setStyles({display: 'block'});
}
		  
		  
function startLoader(theURL){
  	var preload = new Image();
	preload.onload = function(){
		fillPopDiv(loadURL, loadCap, loadUid, loadIndex, loadW, loadH);
	  	var preload = new Image();
		preload.src = preloadURL;	
	};
	preload.src = theURL;
}

function writePrevNext(theUid, theIndex, direction, theString){
  	var s = document.getElementById(direction);
  	if(imgs[theUid][theIndex] == undefined) return;
	var theURL = imgs[theUid][theIndex][0];
  	var theCap = imgs[theUid][theIndex][1];
  	var w = imgs[theUid][theIndex][2];
  	var h = imgs[theUid][theIndex][3];
		  	
  	s.innerHTML="<a href=\"javascript:popDiv('"+ theURL+"', '"+ theCap+"', '"+theUid+"', "+theIndex+", "+w+", "+h+");\">"+theString+"</a>";
}
		  
function hidePic(){
	  var div = document.getElementById("showPic");
	  div.innerHTML = "";
	  div.style.display = "none";
	  $('coverPic').setStyles({display: 'none'});
	  if(!blogShown){
	  	$('blogCoverAll').setStyles({display: 'none'});
	  }
	  $('blogCoverAll').setStyles({zIndex: 1000});
	  imgShown = false;
}
