var StronaB = new Class({
	
	Implements: [Events, Options],
	options: {},
	
	/* initialize needed methods
	 ======================================================================= */
	initialize: function(options) {
		this.setOptions(options);
		
		// search box
		$('s').addEvents({
			focus: function(e) {
				if (this.value == 'Wpisz szukane słowo...') {
					this.value = '';
				}
			},
			blur: function(e) {
				if (this.value == '') {
					this.value = 'Wpisz szukane słowo...';
				}
			}
		})
		
		$$('div#reviews dl').addEvents({
			mouseenter: function(e) {
				this.getElement('dd').tween('opacity', [0, 0.7]);
			},
			mouseleave: function(e) {
				this.getElement('dd').fade('out');
			}
		});
        
        //this.fetchAlbums();
	},

	fetchAlbums: function() {
		var req = new Request({ 
			method: 'get', 
			url:    '/wp-content/themes/stronab/lastfm.php',
			onComplete: function(response) {
				var container = $("albums");
				response = JSON.decode(response);
				
				var h3 = new Element('h3');
				h3.appendText('Ostatnio słuchamy');
				
				var ul = new Element('ul');
				
				for (var i = 0; i < response.albums.length; i++) {
					var className = (i + 1) % 6 == 0 ? 'last' : '';
					var image     = response.albums[i].image;
					var artist    = response.albums[i].artist;
					var album     = response.albums[i].album;
					var url       = response.albums[i].url;
					
					var li   = new Element('li', {'class': className});
					var link = new Element('a', {'href': url, 'title': artist + ' – ' + album});
					var img  = new Element('img', {
						'src': image,
						'alt': artist + ' – ' + album,
						'width': 50,
						'height': 50
					});
					
					link.grab(img);
					li.grab(link);
					
					ul.grab(li);
				}
				container.grab(h3);
				container.grab(ul);
				
				$('albums').getElements('ul a').addEvents({
					'click': function() {
						window.open(this.href);
						return false;
					}
				});
			}
		}).send();
	}
});

window.addEvent('domready', function() {
	new StronaB();
});

function sidebarPhotos(data) {
	var container = document.getElementById('sidebar-photos');
	var html = '<ul>';
	
	for (var i = 0; i < data.photos.photo.length; i++) {
		var className = (i + 1) % 4 == 0 ? 'last' : '';
		var photo     = data.photos.photo[i];
		var url       = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_s.jpg';
		var userUrl   = 'http://www.flickr.com/photos/' + photo.owner + '/' + photo.id;
		
		html += '<li class="' + className + '"><a href="' + userUrl + '"><img src="' + url + '" width="70" alt="" /></a></li>';
	}
	
	html += '</ul>';
	html += '<a id="flickr-url" href="http://www.flickr.com/photos/p_ch/tags/' + escape(container.getAttribute('rel')) + '">Zobacz wszystkie na flickr.com (' + data.photos.total + ') &raquo;</a>'
	
	container.innerHTML += html;
}

function jsonFlickrApi(data) {
	var container = document.getElementById('flickr-gallery');
	var html = '<ul>';
	
	for (var i = 0; i < data.photos.photo.length; i++) {
		var photo   = data.photos.photo[i];
		var url     = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_s.jpg';
		var userUrl = 'http://www.flickr.com/photos/' + photo.owner + '/' + photo.id;
		
		html += '<li><a href="' + userUrl + '"><img src="' + url + '" width="70" alt="" /></a></li>';
	}
	
	html += '</ul>';
	html += '<a id="flickr-url" href="http://www.flickr.com/photos/tags/' + escape(container.getAttribute('rel')) + '">Zobacz wszystkie na flickr.com (' + data.photos.total + ') &raquo;</a>'
	
	container.innerHTML = html;
}


