/* eyeCandy (przezroczyste zaokraglenia) by mh (c) 2007 v1.0 */

var useEyeCandy = true;
var eyeCandyDebug = false;

var eyeCandy; // obiekt eyeCandy

var EyeCandy = function(mainEl, mainElBgHeight, cssClass, blockMargin)
{
	if (!cssClass) cssClass = 'eyeCandy';
	// odstep w pionie miedzy blokami (domyslnie 5px)
	if (!blockMargin) blockMargin = 5;
	
	this.mainEl = mainEl;
	this.mainElBgHeight = mainElBgHeight;
	this.cssClass = cssClass;
	this.blockMargin = blockMargin;
	
	/*this.mainElOffsetTop = $(this.mainEl).offset().top;*/
	
	$(this.mainEl).addClass(this.cssClass + 'Bg');
	$(this.mainEl).addEyeCandy(this);
}

$.fn.extend({
	eyeCandy: function(eObj){
		var eObj = eObj ? eObj : eyeCandy;
		if (!eObj) return this;
		
		var ignoreRest = false;
		
		return this.each(function(){
			if (ignoreRest)
			{
				$(this).removeEyeCandy(eObj);
				return;				
			}
			// pominiecie tych ktorych nie trzeba przerabiac
			var thisOffsetTop = $(this).offset().top;				
			if (thisOffsetTop - eObj.mainElOffsetTop >= eObj.mainElBgHeight)
			{
				ignoreRest = true;
				$(this).removeEyeCandy(eObj);
				return;
			}
			
			var thisHeight = $(this).height();
			
			// uwaga na sztywno! - w celu szybszego dzialania
			// przesuniecia kolejnych elementow zaokraglen wzgledem poczatku bloku
			var shift = new Array(0, 0, 2, 2, thisHeight - 6, thisHeight - 6, thisHeight - 4, thisHeight - 4);
			
			$(this).addEyeCandy(eObj);
			
			if ($(this).find('.blockFiller').size() == 0)
			{
				$(this).find('.blockL').prepend($('<div class="blockFiller blockFillerTL"></div>'
					+ '<div class="blockFiller blockFillerTL blockFillerTS"></div>'
					+ '<div class="blockFiller blockFillerTR blockFillerTS"></div>'
					+ '<div class="blockFiller blockFillerTR"></div>'
					+ '<div class="blockFiller blockFillerBL blockFillerBS"></div>'
					+ '<div class="blockFiller blockFillerBR blockFillerBS"></div>'
					+ '<div class="blockFiller blockFillerBL"></div>'
					+ '<div class="blockFiller blockFillerBR"></div>'));
			}
			
			var i = 0;
			$(this).find('.blockFiller').each(function(){
				$(this).css('background-position', '0px ' + (eObj.mainElOffsetTop - thisOffsetTop - shift[i]) + 'px');
				// debug
				if (eyeCandyDebug) $(this).html((eObj.mainElOffsetTop - thisOffsetTop - shift[i]) + 'px').css({'color':'red','border':'1px solid blue','font-size':'10px'});
				i++;
			});
			
			// wczesniejsze ustalenie czy ignorowac kolejne bloki
			if (thisOffsetTop + thisHeight + eObj.blockMargin - eObj.mainElOffsetTop >= eObj.mainElBgHeight)
			{
				ignoreRest = true;
			}
		});
	},
	
	addEyeCandy: function(eObj){
		var eObj = eObj ? eObj : eyeCandy;
		if (!eObj) return this;
		
		return this.each(function(){
			$(this).children().addClass(eObj.cssClass);
		});
	},
	
	removeEyeCandy: function(eObj){
		var eObj = eObj ? eObj : eyeCandy;
		if (!eObj) return this;
		
		return this.each(function(){
			$(this).children().removeClass(eObj.cssClass);
		});
	}	
});

