function nnnImagePreloader(images, callBack)
{
   this.callBack = callBack;
 
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
 
   this.nImages = images.length;
 
   for ( var i = 0; i < images.length; i++ )
      this.preload(images[i]);
}

nnnImagePreloader.prototype.preload = function(image)
{
   var oImage = new Image;
   this.aImages.push(oImage);
  
   oImage.onload = nnnImagePreloader.prototype.onload;
   oImage.onerror = nnnImagePreloader.prototype.onerror;
   oImage.onabort = nnnImagePreloader.prototype.onabort;
  
   oImage.onnnImagePreloader = this;
   oImage.bLoaded = false;
  
   oImage.src = image;
}

nnnImagePreloader.prototype.onComplete = function()
{
   this.nProcessed++;
   if ( this.nProcessed == this.nImages )
   {
      this.callBack(this.aImages, this.nLoaded);
   }
}
nnnImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.onnnImagePreloader.nLoaded++;
   this.onnnImagePreloader.onComplete();
}
nnnImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.onnnImagePreloader.onComplete();
}
nnnImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.onnnImagePreloader.onComplete();
}

function nnnClass(parent, prop) {
  var clazz = function() {
    if (clazz.preparing) return delete(clazz.preparing);
    if (clazz.constr) {
      this.constructor = clazz;
      clazz.constr.apply(this, arguments);
    }
  }
  clazz.prototype = {};
  if (parent) {
    parent.preparing = true;
    clazz.prototype = new parent;
    clazz.prototype.constructor = parent;
    clazz.constr = parent;
  }
  if (prop) {
    var cname = "constructor";
    for (var k in prop) {
      if (k != cname) clazz.prototype[k] = prop[k];
    }
    if (prop[cname] && prop[cname] != Object)
      clazz.constr = prop[cname];
  }
  return clazz;
}

function nnnListener(o, e, a)
{
	if (o.addEventListener)
		o.addEventListener(e, a, false);
	else if (o.attachEvent)
		o.attachEvent("on" + e, a);
	else
		return null;
}


function nnnElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

nnnListener(window, "load", nnnInit);


function nnnInit()
{
	var ads = Array();
	var adElements = nnnElementsByClassName("nnn-ad");
	for(j=0; j<adElements.length; j++)
	{
		ads[j] = new nnnAd(adElements[j]);
	}
	
}

var nnnAd = nnnClass(null, 
{
	div: null,
	items: null,
	thumbs: null,
	thumbImages: null,
	thumbBigImages: null,
	
	hoverTimer: null,
	releaseTimer: null,
	
	opened: null,
	
	dummy: null,
	
	constructor: function(div)
	{
		this.div = div;
		this.items = nnnElementsByClassName("nnn-ad-img", "*", div);
		this.thumbs = this.div.getElementsByTagName("img");
		
		this.opened = Array();
		this.thumbImages = Array();
		this.thumbBigImages = Array();
		
		for(i=0; i<this.thumbs.length; i++)
		{
			this.thumbImages[i] = this.thumbs[i].src;
			this.thumbBigImages[i] = this.thumbs[i].id;

			this.opened[i] = false;
			this.enableActions(this.thumbs[i], i);
		}
		
		new nnnImagePreloader(this.thumbBigImages, function() {} );
	},
	
	enableActions: function(item, i)
	{
		var object = this;
		item.onmouseover = function() { object.hover.apply(object, [item, i]) };
		item.onmouseout = function() { object.release.apply(object, [item, i]) };
	},
	
	hover: function(item, i)
	{
		if (!this.opened[i])
		{
			this.items[i].style.zIndex = 10;
			item.style.zIndex = 10;
			this.div.style.zIndex = 200;
			this.opened[i] = true;
			item.src = this.thumbBigImages[i];
			this.animate(item, -20, -20, 100, 100, 80, function() {  });
		}
	},
	
	release: function(item, i)
	{
		if (this.opened[i])
		{
			this.items[i].style.zIndex = 0;
			this.div.style.zIndex = 100;
			var object = this;
			this.animate(item, 0, 0, 60, 60, 100, function() { setTimeout( function() { item.src = object.thumbImages[i]; object.opened[i] = false; item.style.zIndex = 1; }, 80); });
		}
	},
	
	animate: function(item, newLeft, newTop, newWidth, newHeight, time, callback)
	{
		var el = item;
		if (el == null)
			return;
			
		if (el.style.left=="")
			el.style.left = 0;
		if (el.style.top=="")
			el.style.top = 0;
	 
		var cLeft = parseInt(el.style.left);
		var cTop = parseInt(el.style.top);
		var cWidth = parseInt(el.width);
		var cHeight = parseInt(el.height);
		
		var totalFrames = 1;
		if (time> 0)
			totalFrames = time/10;
	
		var fLeft = newLeft - cLeft;
		if (fLeft != 0)
			fLeft /= totalFrames;
		
		var fTop = newTop - cTop;
		if (fTop != 0)
			fTop /= totalFrames;
		
		var fWidth = newWidth - cWidth;
		if (fWidth != 0)
			fWidth /= totalFrames;
		
		var fHeight = newHeight - cHeight;
		if (fHeight != 0)
			fHeight /= totalFrames;
			
		this.doFrame(item, cLeft, newLeft, fLeft, cTop, newTop, fTop, cWidth, newWidth, fWidth, cHeight, newHeight, fHeight, callback);
	},
	
	doFrame: function(item, cLeft, nLeft, fLeft, cTop, nTop, fTop, cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback)
	{
		var el = item;
		if (el == null)
			return;
	
		cLeft = this.moveSingleVal(cLeft, nLeft, fLeft);
		cTop = this.moveSingleVal(cTop, nTop, fTop);
		cWidth = this.moveSingleVal(cWidth, nWidth, fWidth);
		cHeight = this.moveSingleVal(cHeight, nHeight, fHeight);
	
		el.style.left = Math.round(cLeft) + 'px';
		el.style.top = Math.round(cTop) + 'px';
		el.style.width = Math.round(cWidth) + 'px';
		el.style.height = Math.round(cHeight) + 'px';
		el.width = Math.round(cWidth);
		el.height = Math.round(cHeight);
		
		if(cLeft == nLeft && cTop == nTop && cHeight == nHeight && cWidth == nWidth)
		{
			if (callback != null)
				callback();
			return;
		}
		var object = this;
		setTimeout( function() { object.doFrame(item, cLeft, nLeft, fLeft, cTop, nTop, fTop, cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback) }, 10);
	},
	
	moveSingleVal: function(currentVal, finalVal, frameAmt)
	{
		if (frameAmt == 0 || currentVal == finalVal)
			return finalVal;
		
		currentVal += frameAmt;
		if ((frameAmt> 0 && currentVal>= finalVal)	|| (frameAmt <0 && currentVal <= finalVal))
		{
			return finalVal;
		}
		return currentVal;
	}
});

