// Define variables:
var imgCatThumb = "images/kupy/thumbs/";
var imgCatFull = "images/kupy/";
var imgFormat = ".jpg";
var currentSelection = "";
var thumbCss = "thumbIe";

var preloadFlag = false;

// Funkcja interfejsu galerii
function changeState(imgid,action)
{
	if(action=='ov')
	{
	$(imgid).style.opacity = 1;
	$(imgid).style.filter = 'alpha(opacity=' + 100 + ')';
	}
	
	if(action=='out')
	{
		if($(imgid).readAttribute('id')!=currentSelection)
		{
		$(imgid).style.opacity = 0.5;
		$(imgid).style.filter = 'alpha(opacity=' + 50 + ')';
		}
	}
	
	if(action=='click')
	{
	var insertedHTML = makeFullImgHTML(imgid);

	currentSelection=imgid;
	
	$('photoContentFour').childElements().each(function(img) {
		if(img.readAttribute('id')!=currentSelection)
		{
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=' + 50 + ')';
		}
		else
		{
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=' + 100 + ')';
		}
	}
	);
	
	$('photoContentThree').childElements().each(function(img) {
		if(img.readAttribute('id')!=currentSelection)
		{
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=' + 50 + ')';
		}
		else
		{
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=' + 100 + ')';
		}
	}
	);
	
	$('photoContentTwo').childElements().each(function(img) {
		if(img.readAttribute('id')!=currentSelection)
		{
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=' + 50 + ')';
		}
		else
		{
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=' + 100 + ')';
		}
	}
	);
	
	$('photoContentOne').childElements().each(function(img) {
		if(img.readAttribute('id')!=currentSelection)
		{
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=' + 50 + ')';
		}
		else
		{
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=' + 100 + ')';
		}
	}
	);

	if($('fullImg').firstDescendant()!=null)
		 { $('fullImg').update(insertedHTML); }
	else { $('fullImg').insert(insertedHTML); }

	var fullImgPre=new Array('images/kupy/'+imgid+'full.jpg');
	new ImagePreloader(fullImgPre, showContent);
	

	}
}

function showContent() {Effect.SlideDown(currentSelection+'full');}

function makeFullImgHTML(imgid)
{
	var tempString = '<div style="display:none;width:208px;height:208px;" id="'+imgid+'full"><img src="images/kupy/'+imgid+'full.jpg"></div>';
	return tempString;
}

function ImagePreloader(images, doAfter)
{
   // store the call-back
   this.doAfter = doAfter;

   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
 
   // record the number of images.
   this.nImages = images.length;

   // for each image, call preload()
   for ( var i = 0; i < images.length; i++ )
	  this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image)
{
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);

   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;
  
   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;
  
   // assign the .src property of the Image object
   oImage.src = 'images/kupy/'+image;
}

ImagePreloader.prototype.onComplete = function()
{
   this.nProcessed++;
   
   if ( this.nProcessed == this.nImages )
   {
	  this.doAfter();//this.aImages, this.nLoaded
   }
}
ImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}

function doPopulate()
{
	var nThumbsC4 = 69;
	var nThumbsC3 = 99;
	var nThumbsC2 = 124;
	var nThumbsC1 = 136;
	
	for ( var i = 0; i < thumbsArray.length; i++ )
	{
	  var imgid=(thumbsArray[i].truncate(6, ''));
		//populuj
    	if(i<nThumbsC4)
		{
		populator(imgid,'photoContentFour');
		}
		else if(i<nThumbsC3)
		{
		populator(imgid,'photoContentThree');
		}
		else if(i<nThumbsC2)
		{
		populator(imgid,'photoContentTwo');
		}
		else if(i<nThumbsC1)
		{
		populator(imgid,'photoContentOne');
		}
	}
}

function populator(imgid,target)
{

	$(target).insert('<img id="'+imgid+'" onMouseOver="changeState(\''+imgid+'\',\'ov\');" onMouseOut="changeState(\''+imgid+'\',\'out\');" onMouseDown="changeState(\''+imgid+'\',\'click\');" src="images/kupy/thumbs/'+imgid+'.jpg" border="0" class="'+thumbCss+'">');
}

function setThumbsCss(){

	if(navigator.userAgent.indexOf("Firefox")!=-1)
	{
		var versionindex=navigator.userAgent.indexOf("Firefox")+8
		if (parseInt(navigator.userAgent.charAt(versionindex))>=1)
		{
			thumbCss = "thumb";
		}
	}
}


