/**
 * @author krybak, aciebiera
 */
var wg_photo = new Class({
	initialize:function(el)
	{
		this.element = el;
		this.element.setStyle('z-index', '-1');
		this.effect = new Fx.Styles(this.element, {duration: 500, transition: Fx.Transitions.sineInOut});
	},
	show:function(nextPhoto)
	{
		
		this.element.setStyle('z-index', '2');
	},
	hideandshow:function(nextPhoto)
	{
		this.nextPhoto = nextPhoto;
		this.nextPhoto.element.setStyle('z-index', '1');
		var myChain = new Chain();
		
		this.effect.start({opacity: [1, 0]}).chain(
			function(){ 

				this.nextPhoto.show();
				this.element.setStyles({'z-index' : -1, 'opacity':1} );

			}.bind(this)		  	
		);

	}
});
var wg_wsiz_gallery = new Class({
	initialize:function(el)
	{		
		this.photos = new Array();
		this.infoField = el.getElement('.wg_licznik');
		// Tworzymy liste zdjęć
		el.getElement('.wg_zdjecia').setStyle('position', 'relative');
		var photos = el.getElements('.wg_zdjecia a');
		var imgs = el.getElements('.wg_zdjecia a img');
		var descriptions = new Array();
		imgs.each(function(el){
			descriptions.push(el.alt);
		});
		this.multibox = new MultiBox('', {
			descFromTitle:true,
			content:photos,
			onOpen:function()
			{
				galleries.each(function(gal){
					if(gal.multibox.opened)
						gal.multibox.close();
				});
			},
			onPrevious:this.prev.bind(this),
			onNext:this.next.bind(this),
			descriptions:descriptions
		});
		photos.each(function(photo){
			photo.setStyles({
				position: 'absolute',
				left:'1px',
				top:'2px'
			});
			this.photos.push(new wg_photo(photo));
		}.bind(this));
		// Dodajemy referencje do poprzedniego i następnego zdjęcia

		for(i=0;i<this.photos.length;i++)
		{
			this.photos[i].number = i;
			if(i>0)
				this.photos[i].previous = this.photos[i-1];
			if(i<this.photos.length-1)
				this.photos[i].next = this.photos[i+1];
		}
		this.currentPhoto = this.photos[0];
		this.currentPhoto.show();
		
		// Bindowanie eventów na batonach
		
		var poprz = el.getElement('.wg_poprzedni');
		if(poprz != null)
			poprz.addEvent('click', this.prev.bindWithEvent(this));
		var nast = el.getElement('.wg_nastepny');
		if(nast != null)
			nast.addEvent('click', this.next.bindWithEvent(this));		
		this.updateInfo();		
	},
	prev:function(e)
	{
		if(e && this.multibox.opened)
		{
			this.multibox.options.onPrevious = Class.empty;
			this.multibox.previous();
			this.multibox.onPrevious = this.prev.bind(this);
		}
		if(this.currentPhoto.previous != null)
		{
			this.currentPhoto.hideandshow(this.currentPhoto.previous);
			
			this.currentPhoto = this.currentPhoto.previous;
			this.updateInfo();
		}
	},
	next:function(e)
	{
		if(e && this.multibox.opened)
		{
			
			this.multibox.options.onNext = Class.empty;
			this.multibox.next();
			this.multibox.onNext = this.next.bind(this);
		}		
		if(this.currentPhoto.next != null)
		{
			this.currentPhoto.hideandshow(this.currentPhoto.next);
			
			this.currentPhoto = this.currentPhoto.next;
			this.updateInfo();
		}
	},
	updateInfo:function()
	{
		if(this.infoField != null)
			this.infoField.innerHTML = (this.currentPhoto.number+1) + ' z ' + this.photos.length;
	}
});
var galleries = new Array();
var mb_mail_friend = {};
$(window).addEvent('domready', function(){
	
	$$('.wg_inline-gallery').each(function(el){
		galleries.push(new wg_wsiz_gallery(el));
	});
	
	$$('.e_mail_friend').each(function(el){
		if(el.tagName == "A")
		{
			el.href = "/pl/_layouts/send_mail.aspx?page_url=" + el.baseURI;
			el.rel = "width:525,height:600";
		}
	});
	
	mb_mail_friend = new MultiBox('e_mail_friend', {useOverlay: false, showControls:false});
	
	var g_item = 0;
	$$('.big_gallery').each(function(el){
	
		el.className += " gallery_" + g_item;
		var photos = el.getElements('.wg_item');
		var imgs = el.getElements('.wg_item img');
		var wg_width = "0";
		var wg_height = "0";
		if(imgs.length >0)
		{
			wg_height = imgs[0].height;
			wg_width = imgs[0].width;
			/*var style = "<style type=\"text/css\">ul.gallery_"+g_item+" li a{ width:"+imgs[0].naturalWidth+"px;height:"+imgs[0].naturalHeight+"px;}</style>";
			var tmpStyle = el.ownerDocument.createElement("DIV");
				tmpStyle.innerHTML = style;
				
			if(el.childNodes[0])
				el.insertBefore(tmpStyle, el.childNodes[0]);
			else if(el.parentElement)
				el.parentElement.insertBefore(tmpStyle, el);*/
		}
		var descriptions = new Array();
		
		if(wg_width)
		{
			photos.each(function(el){
				el.style.width = wg_width + "px";
				el.style.height = wg_height + "px";
			});
		}
		imgs.each(function(el){
			descriptions.push(el.alt);
		});
		
		var bg  = new MultiBox('', {
			descFromTitle:true,
			content:photos,
			descriptions:descriptions
		});
		
		g_item++;
		
	});

});

function RenderFlashContent()
{
	$$('.flashcontent').each(
	function(el, pos)
	{
		if(el.id)
		{
			param = el.id.split('::');
			if(param.length >= 4)
			{
				var swf = new SWFObject('/uploaded_flash/player.swf', 'player' + pos, param[1], param[2], '9', param[3]);
				for (p=4; p<param .length; p++) {
	                detail = param [p].split('=');
					swf.addParam(detail[0], detail[1]);
				}
	            if(el.getAttribute('flashvars'))
					swf.addParam('flashvars',el.getAttribute('flashvars'));
	            swf.write(el.id);
            }
		}
	}
	);
}
