var xmlfoto = new Array();
var fotoObjectContainer = new Object();

fotoGadget = function(numitems) {
   this.response = false;
   this.elementFoto = false;
   this.elementFotoName = false;
   this.elementFotoCounter = false;

   this.direction = 'next';
   this.numitems = numitems;
   this.ie6 = false;
   this.nowPercent = 0;
   this.prozentWert = 0;

   this.getFotos = function(mode, instance, viewformat, filter, direction, userId, order, id_gadget) {
      xmlfoto[instance] = false;
      if(this.numitems == 1)
         return false;

      fotoMode[instance] = mode;

      if(countFotos[instance] <= 1) {
         document.getElementById('nextButtonFoto_' + instance).style.visibility = 'hidden';
         document.getElementById('prevButtonFoto_' + instance).style.visibility = 'hidden';
         document.getElementById('nextButtonFoto_' + instance).style.display = 'none';
         document.getElementById('prevButtonFoto_' + instance).style.display = 'none';

         return false;
      }

      if (typeof XMLHttpRequest != 'undefined') {
         xmlfoto[instance] = new XMLHttpRequest();
      }
      if (!xmlfoto[instance]) {
         // Internet Explorer 6 und aelter...
         try {
            xmlfoto[instance]  = new ActiveXObject("Msxml2.XMLHTTP");
            this.ie6 = true;
         } catch(e) {
            try {
               xmlfoto[instance]  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
               xmlfoto[instance]  = null;
            }
         }
      }

      chromeName = 'chrome';
      chromeName = chromeName.toString();
      ie7Name = 'msie 7';
      ie7Name = ie7Name.toString();
      var agent = navigator.userAgent.toLowerCase();
      if(this.ie6 || agent.indexOf(chromeName.toLowerCase()) > 0 || agent.indexOf(ie7Name.toLowerCase()) > 0) {
         uri = "/ext/Foto/GetFotosFromArray/&instance=" + instance + "&viewformat=" + viewformat + "&order=" + order + "&filter=" + filter + "&id_user=" + userId + "&numitems=" + numitems + "&explorer=1&id_gadget=" + id_gadget;
         if(agent.indexOf(ie7Name.toLowerCase()) > 0)
        	 xmlfoto[instance].open('POST', uri, true);
         else
        	 xmlfoto[instance].open('POST', uri, false);
      } else {
         uri = "/ext/Foto/GetFotosFromArray/&instance=" + instance + "&viewformat=" + viewformat + "&order=" + order + "&filter=" + filter + "&id_user=" + userId + "&numitems=" + numitems + "&explorer=0&id_gadget=" + id_gadget;
         xmlfoto[instance].multipart = true;
         xmlfoto[instance].open('POST', uri, true);
      }

      fotoObjectContainer = this;
      var e = 0;
      xmlfoto[instance].onreadystatechange = function(event) {
         if(xmlfoto[instance].readyState == 4) {
            response = xmlfoto[instance].responseText.toString();
            if(response == '') {
               return false;
            }

            response = response.replace(/--rn9012--/g, '');
            response = response.replace(/--rn9012/g, '');
            response = response.replace(/\s*Content-type:\s+text\/html/g, '');
            response = response.replace(/Content-type:.*\n/g, '');
            try {
               eval(response);
            } catch(err) {
               alert(err.errorMessage + " " + err.line + " Errors occured, please check the latest Fotos.\n" + err.description + "\n");
            }

            // Die Events der Pager Buttons festlegen.
            var prevButton = document.getElementById('prevButtonFoto_' + instance);
            if(prevButton) {
               prevButton.fotoObject = fotoObjectContainer;
               prevButton.onclick = function() {
                  prevButton.fotoObject.switchElementFoto('prev', instance);
               }
            }
            var nextButton = document.getElementById('nextButtonFoto_' + instance);
            if(nextButton) {
               nextButton.fotoObject = fotoObjectContainer;
               nextButton.onclick = function() {
                  nextButton.fotoObject.switchElementFoto('next', instance);
               }
            }

            if(fotoObjectContainer.ie6 || agent.indexOf(chromeName.toLowerCase()) > 0 || agent.indexOf(ie7Name.toLowerCase()) > 0) {
               return true;
            }

            if(xmlfoto[instance].status == 200 && countFotos[instance] <= 5) {
            	fotoGadgetObject[instance].switchElementFoto('tryagain', instance);
            }
         }
      }
      xmlfoto[instance].send(null);

      return true;
   }

   this.switchElementFoto = function(direction, instance) {
      if(direction == 'tryagain') {
         this.setFotoDisplay(direction, instance);
         return false;
      }
      if(fotoMode[instance] == 'full') {
         if(this.numitems > 0) {
            if(direction == 'next') {
               fotoIndex[instance]++;
               if(fotoIndex[instance] >= this.numitems || fotoIndex[instance] >= countFotos[instance])
                  fotoIndex[instance] = 0;
            }
            if(direction == 'prev') {
               fotoIndex[instance]--;
               if(fotoIndex[instance] < 0) {
                  if(this.numitems > countFotos[instance])
                     fotoIndex[instance] = countFotos[instance] - 1;
                  else
                     fotoIndex[instance] = this.numitems - 1;
               }
            }
         } else {
            if(direction == 'next') {
               fotoIndex[instance]++;
               if(fotoIndex[instance] == countFotos[instance])
                  fotoIndex[instance] = 0;
            }
            if(direction == 'prev') {
               fotoIndex[instance]--;
               if(fotoIndex[instance] <= 0)
                  fotoIndex[instance] = countFotos[instance] - 1;
            }
         }
      }
      if(fotoMode[instance] == 'list') {
         if(direction == 'next') {
            fotoIndex[instance]++;
            if(fotoIndex[instance] == countFotos[instance])
               fotoIndex[instance] = 0;
         }
         if(direction == 'prev') {
            fotoIndex[instance]--;
            if(fotoIndex[instance] < 0)
               fotoIndex[instance] = countFotos[instance] - 1;
         }
      }
      this.setFotoDisplay(direction, instance);

      return true;
   }

   this.setFotoDisplay = function(direction, instance) {
      startpunkt = 0;
      endpunkt = 0;
      startstring = false;
      endstring = false;

      shortTitle = false;

      elementFoto = document.getElementById('photoElement_' + instance);
      elementFotoName = document.getElementById('fotoName_' + instance);
      elementFotoCounter = document.getElementById('fotosCounter_' + instance);

      var prevButton = document.getElementById('prevButtonFoto_' + instance);
      var nextButton = document.getElementById('nextButtonFoto_' + instance);

      if(arrFotos[instance]) {
         if(!arrFotos[instance][fotoIndex[instance]] || arrFotos[instance][fotoIndex[instance]] == '') {
            this.showLoading(instance);
            this.loopLoadBar(arrFotos[instance].length, countFotos[instance], instance);
            setTimeout("fotoGadgetObject[" + instance + "].switchElementFoto('tryagain', '" + instance + "');", 2000);

            return false;
         }
      } else {
         if(prevButton) {
            functionPrev = prevButton.onclick; 
            prevButton.onclick = false;
         }
         if(prevButton.onclick) {
            functionNext = nextButton.onclick;
            nextButton.onclick = false;
         }

         this.showLoading(instance);

         setTimeout("fotoGadgetObject[" + instance + "].switchElementFoto('tryagain', '" + instance + "');", 2500);
         return false;
      }

      if(!prevButton.onclick)
         prevButton.onclick = functionPrev;
      if(!nextButton.onclick)
         nextButton.onclick = functionPrev;

      if(fotoMode[instance] != 'list') {
         if(!arrFotoNames[instance]) {
            setTimeout("fotoGadgetObject[" + instance + "].switchElementFoto('tryagain', '" + instance + "');", 800);
            return false;
         }
         startpunkt = fotoIndex[instance] + 1;
         startstring = startpunkt.toString(10);
         endpunkt = parseFloat(countFotos[instance]);
         this.numitems = parseFloat(this.numitems);
         if(this.numitems > 0 && endpunkt > this.numitems) {
            endpunkt = this.numitems;
         }
         endstring = endpunkt.toString(10);
         if(startstring.length < 3) {
            if(startstring.length == 1)
               startstring = "00" + startstring;
            if(startstring.length == 2)
               startstring = "0" + startstring;
         }
         if(endstring.length < 3) {
            if(endstring.length == 1)
               endstring = "00" + endstring;
            if(endstring.length == 2)
               endstring = "0" + endstring;
         }

         if(arrFotoNames[instance]) {
        	 if(arrFotoNames[instance][fotoIndex[instance]]) {
		         if(arrFotoNames[instance][fotoIndex[instance]].length > 20) {
		            shortTitle = arrFotoNames[instance][fotoIndex[instance]].substring(0, 20);
		            shortTitle = shortTitle + "...";
		            arrFotoNames[instance][fotoIndex[instance]] = shortTitle;
		         }
        	 }
         }

         elementFotoName.innerHTML = arrFotoNames[instance][fotoIndex[instance]];
         elementFotoCounter.innerHTML = startstring + "/" + endstring;
         elementFoto.innerHTML = arrFotos[instance][fotoIndex[instance]];

         this.hideLoading(instance);
      }
      if(fotoMode[instance] == 'list') {
         startpunkt = fotoIndex[instance] + 1;
         startstring = startpunkt.toString(10);
         endpunkt = countFotos[instance];
         endstring = endpunkt.toString(10);
         if(startstring.length < 3) {
            if(startstring.length == 1)
               startstring = "00" + startstring;
            if(startstring.length == 2)
               startstring = "0" + startstring;
         }
         if(endstring.length < 3) {
            if(endstring.length == 1)
               endstring = "00" + endstring;
            if(startstring.length == 2)
               endstring = "0" + endstring;
         }
         this.loopLoadBar(100, 100, instance);
         elementFotoCounter.innerHTML = startstring + "/" + endstring;
         elementFoto.innerHTML = arrFotos[instance][fotoIndex[instance]];

         this.hideLoading(instance);
      }

      if(document.getElementById('fotoRating_' + instance)) {
         initRatingMo('fotoRating_' + instance);
         dhtml.init();
      }

      return true;
   }

   this.showLoading = function(instance) {
      chromeName = 'chrome';
      chromeName = chromeName.toString();
      ie7Name = 'msie';
      ie7Name = ie7Name.toString();
      var agent = navigator.userAgent.toLowerCase();
      if((this.ie6 || agent.indexOf(chromeName.toLowerCase()) > 0 || agent.indexOf(ie7Name.toLowerCase()) > 0)) {
    	  document.getElementById('boxLoadingOldFoto_' + instance).style.visibility = 'visible';
      } else {
		   if(document.getElementById('boxLoadingFoto_' + instance).style.display == '' || document.getElementById('boxLoadingFoto_' + instance).style.display == 'none') {
	    	  document.getElementById('boxLoadingFoto_' + instance).style.display = 'block';
	          document.getElementById('loadBar_' + instance).style.display = 'block';
	          document.getElementById('showVal_' + instance).style.display = 'block';
	       }
      }
      return true;
   }

   this.hideLoading = function(instance) {
      chromeName = 'chrome';
      chromeName = chromeName.toString();
      ie7Name = 'msie';
      ie7Name = ie7Name.toString();
      var agent = navigator.userAgent.toLowerCase();
      if((this.ie6 || agent.indexOf(chromeName.toLowerCase()) > 0 || agent.indexOf(ie7Name.toLowerCase()) > 0)) {
    	  document.getElementById('boxLoadingOldFoto_' + instance).style.visibility = 'hidden';
      } else {
         if(document.getElementById('boxLoadingFoto_' + instance).style.display == 'block')
            document.getElementById('boxLoadingFoto_' + instance).style.display = 'none';
         document.getElementById('loadBar_' + instance).style.display = 'none';
         document.getElementById('showVal_' + instance).style.display = 'none';
      }
      return true;
   }

   this.loopLoadBar = function(now, count, instance) {
      chromeName = 'chrome';
      chromeName = chromeName.toString();
      ie7Name = 'msie';
      ie7Name = ie7Name.toString();
      var agent = navigator.userAgent.toLowerCase();
      if((this.ie6 || agent.indexOf(chromeName.toLowerCase()) > 0 || agent.indexOf(ie7Name.toLowerCase()) > 0)) {
    	  return false;
      }
      if(count == 0)
		count = 1;
      teiler = count / 100;
      prozent = now / teiler;
      if(prozent >= 100) {
         document.getElementById('loadBar_' + instance).style.width = '100%';
         document.getElementById('showVal_' + instance).innerHTML = '100%';
         fotoGadgetObject[instance].hideLoading(instance);
         return;
      }
      wert = Math.ceil(prozent);
      if(wert < 1)
         wert = 1;

      if(this.prozentWert > wert)
    	  return false;
      this.prozentWert = wert;

      document.getElementById('loadBar_' + instance).style.width = wert + '%';
      document.getElementById('showVal_' + instance).innerHTML = wert + '%';
      now = Math.ceil(now + teiler);
      if(this.prozentWert < 100)
    	  setTimeout("fotoGadgetObject[" + instance + "].loopLoadBar(" + now + ", " + count + ", '" + instance + "');", 2500);

      return false;
   }
}

function wrapGetFotos(mode, cg_i_instance, cg_e_viewformat, cg_e_filter, userflag, userid, cg_e_order, direction, numitems, countPics, id_gadget, start) {
   if(numitems == 1)
      return false;
   // document.getElementById('boxLoadingOldFoto_' + cg_i_instance).style.visibility = 'visible';
   countFotos[cg_i_instance] = countPics;
   fotoGadgetObject[cg_i_instance] = new fotoGadget(numitems);
   fotoGadgetObject[cg_i_instance].showLoading(cg_i_instance);
   fotoGadgetObject[cg_i_instance].loopLoadBar(1, 100, cg_i_instance);
   fotoGadgetObject[cg_i_instance].getFotos(mode, cg_i_instance, cg_e_viewformat, cg_e_filter, userflag, userid, cg_e_order, id_gadget);
   if(direction == 'next')
      fotoGadgetObject[cg_i_instance].switchElementFoto('next', cg_i_instance);
   else
      fotoGadgetObject[cg_i_instance].switchElementFoto('prev', cg_i_instance);
}

function submitFormFoto(name) {
   document.getElementById('action').value = name;
   document.fotoManageProcessForm.submit();
}

