//31 fotos verfuegbar
var image_count = 31;
var hideTop = false;
var showLoading = true;

function get_images(count) {
  images = "";
  indizes = new Array();
  for (i = 1; i <= image_count; i++) {
    indizes.push(i);
  }
  //index wurde erstellt
  //mische den index
  var indizes2 = new Array();
  while (indizes.length > 0){
     var zufall = Math.floor(Math.random() * indizes.length);
     index = indizes[zufall];
     indizes.splice(zufall, 1);
     indizes2.push(index);
  }  

  for (i = 0; i < count; i++) {
    images += get_image_url(indizes2[i]);
  }
  return images;
}

function ramdomize_images() {
  div = document.getElementById('tgal');
  div.innerHTML = get_images(3);
  
  divb = document.getElementById('mgal');
  divb.innerHTML = get_images(2);
}

function get_image_url(id) {
	return ("<div class=\"pic\"><IMG SRC=\"images/small/" + id + ".jpg\" width=\"108\" height=\"81\" onclick=\"ramdomize_images();\" style=\"cursor:pointer\"/></div>");
}


//laedt news wenn keine direkte seite angegeben ist.
function getDefaultFile() {
	if (getURLParam('page') != "") {
		getFile(getURLParam('page'));
	} else {
		getFile('news.html');
	}
}

function getFile(pURL) {
   showLoadingIcon(true);
  
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, IE 7, etc 
      xmlhttpFile=new XMLHttpRequest();
      xmlhttpFile.onreadystatechange=postFileReady;
      xmlhttpFile.open("GET", noCache(pURL), true);
      xmlhttpFile.send(null);
      
      hideTop = (pURL.indexOf("fotos/") == 0);
   } else if (window.ActiveXObject) { //IE 6
      xmlhttpFile=new ActiveXObject('Microsoft.XMLHTTP'); 
      if (xmlhttpFile) {
         xmlhttpFile.onreadystatechange=postFileReady;
         xmlhttpFile.open('GET', noCache(pURL), true);
         xmlhttpFile.send();
      }
   }
}

// function to handle asynchronous call
function postFileReady() {
   if (xmlhttpFile.readyState==4) { 
      if (xmlhttpFile.status==200) {
         //if user clicked on different url after fotos
         div = document.getElementById('text_mitte');
         div.innerHTML=xmlhttpFile.responseText;
         var x = div.getElementsByTagName("script");
         for(var i=0;i<x.length;i++) {
             eval(x[i].text);
         }
         
         if (hideTop) {
            document.getElementById('gal').style.display="none";
            document.getElementById('bgal').style.display="none";
         } else {
            document.getElementById('gal').style.display="block";
            document.getElementById('bgal').style.display="block";
         }
         
         showLoadingIcon(false);
      }
   }
}

// get the big picture
function getFoto(pURL) {
   showLoadingIcon(true);
   countSmallFoto(pURL);
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, IE7, etc 
      xmlhttpFoto=new XMLHttpRequest();
      xmlhttpFoto.onreadystatechange=postFotoReady;
      xmlhttpFoto.open("GET", noCache(pURL), true);
      xmlhttpFoto.send(null);
   } else if (window.ActiveXObject) { //IE 6
      xmlhttpFoto=new ActiveXObject('Microsoft.XMLHTTP'); 
      if (xmlhttpFoto) {
         xmlhttpFoto.onreadystatechange=postFotoReady;
         xmlhttpFoto.open('GET', noCache(pURL), true);
         xmlhttpFoto.send();
      }
   }
}

// function to handle asynchronous call
function postFotoReady() {
  if (xmlhttpFoto.readyState==4) { 
      if (xmlhttpFoto.status==200) { 
         div = document.getElementById('foto');
         div.innerHTML=xmlhttpFoto.responseText;
         showLoadingIcon(false);
      }
   }
}

function countFoto(img) {
   iframe = document.getElementById('counter');
   iframe.src = "showspacefittingimage.html?pic="+img;
}

function countSmallFoto(img) {
   iframe = document.getElementById('counter');
   iframe.src = "fotos/foto.html?pic="+img;
}


// read the contact information
function getContact() {
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
      contactReq=new XMLHttpRequest();
      contactReq.onreadystatechange=postContactReady;
      contactReq.open("GET", noCache("contact.html"), true);
      contactReq.send(null);
   } else if (window.ActiveXObject) { //IE 
      contactReq=new ActiveXObject('Microsoft.XMLHTTP'); 
      if (contactReq) {
         contactReq.onreadystatechange=postContactReady;
         contactReq.open('GET', noCache("contact.html"), true);
         contactReq.send();
      }
   }
}

function postContactReady() {
  if (contactReq.readyState==4) { 
      if (contactReq.status==200) { 
         document.getElementById('contact').innerHTML=contactReq.responseText
      }
   }
}


function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }

  return unescape(unescape(strReturn));
}

function noCache(uri){
  return uri.concat(/\?/.test(uri)?"&":"?","noCache=",(new Date).getTime(),".",Math.random()*1234567);
}


function loginRedirect() {
  getFile("fotos/thumbnails.php?user="+document.login.user.value+"&password="+document.login.password.value);
  return true;
}

function changeImages() {
	ramdomize_images();
	window.setTimeout("changeImages()", 15000);
}

function showLoadingIcon(show) {
  if (show) {
    showLoading = true;
  	window.setTimeout("showLoadingIcon2()", 500);
  } else {
    document.getElementById('loading').style.visibility = "hidden";
    showLoading = false;
  }
}

function showLoadingIcon2() {
  if (showLoading) {
    document.getElementById('loading').style.visibility = "visible";
  }
}
