   var isMSIE = /*@cc_on!@*/false;

   shuffle = function(o){
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
   };

   var gal_image_data = new Array();
   var gal_image_data_count = 0;
   var gal_display_image_data = new Array();
   var gal_display_image_img = new Array();
   var gal_imgWait = new Array()
   var galobj=false;
   var galcells=false;
   var gal_totalLoaded=0;
   var http_request = false;
   var newimage = new Image(85,85);

   function gal_debug(text) {
// 	 d = document.getElementById('debug');
// 	 d.innerHTML=d.innerHTML+"<br/>"+text;
   }

   function gal_debugs(a) {
// 	 d.innerHTML='';
// 	 d = document.getElementById('debug');
// 	 d.innerHTML+=a[0]+' '+a[1]+' '+a[2]+'<br/>';
   }

   function gal_makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) {
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = gal_loadContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function gal_loadContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {

            var xmldoc = http_request.responseXML;

            var root = xmldoc.getElementsByTagName('data').item(0);

            for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
               var node = root.childNodes.item(iNode);
               if(node.childNodes.length==0) continue;
               gal_image_data[gal_image_data_count] = new Array();
               gal_image_data[gal_image_data_count][0]=gal_image_data_count;
               gal_image_data[gal_image_data_count][1]=node.childNodes.item(0).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][2]=node.childNodes.item(1).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][3]=node.childNodes.item(2).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][4]=node.childNodes.item(3).childNodes.item(0).data;
               gal_image_data_count++;
            }

			galobj = document.getElementById('gal');
	 	    galcells = galobj.getElementsByTagName('TD');

	 		if(gal_image_data_count<galcells.length) {
			 	alert('Not enought images. Got '+gal_image_data_count+' of '+galcells.length+'.')
			 	return false;
		    }

		    for(var i=0;i<galcells.length;i++) {
		       gal_display_image_img[i] = new Image(85,85);
		    }

            gal_fill_images();
         } else {
            alert('There was a problem with the request.');
         }
      }
   }

   function gal_fill_images() {
     var gal_tmp_array = new Array();

     for(var i=0;i<gal_image_data_count;i++) {
       gal_tmp_array[i]=i;
     }

	 gal_totalLoaded=0;

     while(gal_display_image_data.length<galcells.length) {
       var g=0;
       gal_tmp_array=shuffle(gal_tmp_array);
       while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
       gal_display_image_data[gal_display_image_data.length]=gal_image_data[gal_tmp_array[g]];
     }

     for(var i=0;i<galcells.length;i++) {
   	    gal_display_image_img[i].src=gal_display_image_data[i][1];
     	gal_loadImage(i);
     }
   }

   function gal_fill_image(slot) {

     var gal_tmp_array = new Array();

     for(var i=0;i<=gal_image_data_count;i++) {
       gal_tmp_array[i]=i;
     }

     var g=0;
     gal_tmp_array=shuffle(gal_tmp_array);
     while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
     gal_display_image_data[slot]=gal_image_data[gal_tmp_array[g]];
	 gal_display_image_img[slot].src=gal_display_image_data[slot][1];
     gal_loadImage(slot);
   }


   function gal_inArray(a, v) {
   	 for(var x=0;x<a.length;x++) {
   	 	if(a[x]==undefined) continue;
		if(a[x][0]==v) return true;
	 }
   	 return false;
   }

   function gal_loadImage(slot) {
     if(!gal_display_image_img[slot].complete){
		gal_imgWait[slot]=setTimeout('gal_loadImage('+slot+')', 250);
	 }
	 else{
	   gal_totalLoaded++;
	   gal_spreadImage(slot);
	   if(gal_totalLoaded==galcells.length) {
	   	 setTimeout('gal_fadeImage()', 5000);
	   }
	 }
   }

   function gal_reloadImage(slot) {
     if(!newimage.complete){
		gal_imgWait[slot]=setTimeout('gal_reloadImage('+slot+')', 250);
	 }
	 else{
	   gal_processFadeImage(slot,80);
	 }
   }

   function gal_processSpreadImage(slot,v) {
      gal_display_image_img[slot].width=v;
      gal_display_image_img[slot].height=v;
      if(v<85) {
    	gal_imgWait[slot]=setTimeout('gal_processSpreadImage('+slot+','+(v+20)+')', 40);
      }
   }

   function gal_processFadeImage(slot,v) {
      gal_display_image_img[slot].width=v;
      gal_display_image_img[slot].height=v;
      if(v>0) {
    	gal_imgWait[slot]=setTimeout('gal_processFadeImage('+slot+','+(v-20)+')', 40);
      }
      else {
        galcells[slot].removeChild(galcells[slot].firstChild);
        gal_display_image_img[slot].src=gal_display_image_data[slot][1];
     	setTimeout('gal_fadeImage()', 5000);
        gal_spreadImage(slot);
      }
   }

   function gal_spreadImage(slot) {
      gal_display_image_img[slot].width=0;
      gal_display_image_img[slot].height=0;
      var ahref = document.createElement("a");
      ahref.href=gal_display_image_data[slot][4];
      ahref.setAttribute("title",gal_display_image_data[slot][3]);
      galcells[slot].appendChild(ahref);
      gal_display_image_img[slot].setAttribute("alt",gal_display_image_data[slot][3]);

      if(isMSIE) {        gal_display_image_img[slot].onmouseover = function() {
			gal_ie_toggleopacity(gal_display_image_img[slot],false);
		}        gal_display_image_img[slot].onmouseout = function() {
			gal_ie_toggleopacity(gal_display_image_img[slot],true);
		}
      }
   	  ahref.appendChild(gal_display_image_img[slot]);
   	  gal_processSpreadImage(slot,5);
   }

   function gal_fadeImage() {
      var slot = Math.ceil(9*Math.random())-1;
      var gal_tmp_array = new Array();
      for(var i=0;i<gal_image_data_count;i++) {
        gal_tmp_array[i]=i;
      }
      var g=0;
      gal_tmp_array=shuffle(gal_tmp_array);
      while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
      newimage.src=gal_display_image_data[slot][1];
      gal_display_image_data[slot]=gal_image_data[gal_tmp_array[g]];
      gal_reloadImage(slot,80);
   }

   function gal_ie_toggleopacity(elm, val) {      var f='DXImageTransform.Microsoft.alpha';
      if (elm.filters[f]) {
		elm.filters[f].enabled = val;
  	  } else {
		elm.style.filter = 'progid:' + f + '(opacity=20,enabled=true)';
	  }
   }
