//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.
MooTools.More = { 'version': '1.2.4.4', 'build': '6f6057dc645fdb7547689183b2311063bd653ddf' };

var Asset = {
	javascript: function(source, properties){
		properties = $extend({
			onload: $empty,
			document: document,
			check: $lambda(true)
		}, properties);
		
		if (properties.onLoad) properties.onload = properties.onLoad;
		
		var script = new Element('script', {src: source, type: 'text/javascript'});

		var load = properties.onload.bind(script), 
			check = properties.check, 
			doc = properties.document;
		delete properties.onload;
		delete properties.check;
		delete properties.document;

		script.addEvents({
			load: load,
			readystatechange: function(){
				if (['loaded', 'complete'].contains(this.readyState)) load();
			}
		}).set(properties);

		if (Browser.Engine.webkit419) var checker = (function(){
			if (!$try(check)) return;
			$clear(checker);
			load();
		}).periodical(50);

		return script.inject(doc.head);
	},

	css: function(source, properties){
		return new Element('link', $merge({
			rel: 'stylesheet',
			media: 'screen',
			type: 'text/css',
			href: source
		}, properties)).inject(document.head);
	},

	image: function(source, properties){
		properties = $merge({
			onload: $empty,
			onabort: $empty,
			onerror: $empty
		}, properties);
		var image = new Image();
		var element = document.id(image) || new Element('img');
		['load', 'abort', 'error'].each(function(name){
			var type = 'on' + name;
			var cap = name.capitalize();
			if (properties['on' + cap]) properties[type] = properties['on' + cap];
			var event = properties[type];
			delete properties[type];
			image[type] = function(){
				if (!image) return;
				if (!element.parentNode){
					element.width = image.width;
					element.height = image.height;
				}
				image = image.onload = image.onabort = image.onerror = null;
				event.delay(1, element, element);
				element.fireEvent(name, element, 1);
			};
		});
		image.src = element.src = source;
		if (image && image.complete) image.onload.delay(1);
		return element.set(properties);
	},

	images: function(sources, options){
		options = $merge({
			onComplete: $empty,
			onProgress: $empty,
			onError: $empty,
			properties: {}
		}, options);
		sources = $splat(sources);
		var images = [];
		var counter = 0;
		return new Elements(sources.map(function(source){
			return Asset.image(source, $extend(options.properties, {
				onload: function(){
					options.onProgress.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				},
				onerror: function(){
					options.onError.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				}
			}));
		}));
	}
};

window.addEvent("load", function(){
    $page = 0;
    $('preloader').tween('opacity',0);
    
    $$('#thumbnails li').each(function(el,i){
    	el.set('tween',{wait:true});
    	el.setStyle('display','block');
    	if(i > 2) el.setStyle('opacity',0);
   		el.addEvent('click', function(){ 
   			$clear(slideshow_timer); 
   			$('preloader').tween('opacity',1);
	
			var loaded_images = new Asset.images($slideshow[i].images, {
				onComplete: function(){
					(function(){
						$('image').empty().adopt(loaded_images);
						$$('#image img')[0].setProperty('class','visible');
						$$('#image img').each(function(img){ img.setProperties({'width':630,'height':315}); });
                        slideshow_timer = (function(){ rotate_images(); }).periodical(5000);
						$$('#desc h3')[0].innerHTML = $slideshow[i].title;
						$$('#desc h4')[0].innerHTML = '<a href="' + $slideshow[i].url + '">' + $slideshow[i].url_title + '</a>';
						$$('#desc p')[0].innerHTML = $slideshow[i].desc;
						$$('#desc p')[0].innerHTML += ' <a href="#" class="look">zobacz&nbsp;&raquo;</a>';
						Cufon.refresh('.cufon');
						$('preloader').tween('opacity',0);
						
						$slideshow[i].big_images.each(function(bigimg,j){
						  $('image').innerHTML += '<a href="'+bigimg+'" title="'+(($slideshow[i].big_desc[j] !== false) ? $slideshow[i].big_desc[j] : '')+'" rel="lightbox[gal'+i+']"></a>';
						});
						
						$$("a").filter(function(el) {
		                   return el.rel && el.rel.test(/^lightbox/i);
	                    }).slimbox({/* Put custom options here */}, null, function(el) {
		                   return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	                    });
	                    
	                    $$('#image img','#desc a.look').addEvent('click', function(e){ 
                            e.stop(); 
                            if($$('#image a')[0]){ 
                                if($$('#image a')[0].getProperty('href').contains('.html')){
                                    window.location = $$('#image a')[0].getProperty('href');
                                }else{
                                    $$('#image a')[0].fireEvent('click'); 
                                }
                            } 
                        });
						
					}).delay(1000);
				}
			});
   		});
	});
    
    var timer = (function(){
        $$('#thumbnails li').each(function(el,i){            
            if(i >= $page * 3 && i < ($page+1) * 3){
                (function(){ el.tween('opacity', 1, 0); }).delay((i % 3) * 150);
            }else{
                if($page < 2 && i >= ($page+1) * 3 && i < ($page+2) * 3 ){
                    (function(){ 
                        el.tween('opacity', 0, 1);
                        el.set('class', (i == 0 || i % 3 == 0) ? 'first' : (i == 1 || i % 3 == 1) ? 'second' : '');
                    }).delay(850 + ((i % 3) * 150));
                }else if($page == 2 && i >= 0 && i < 3 ){
                    (function(){ 
                        el.tween('opacity', 0, 1);
                        el.set('class', (i == 0 || i % 3 == 0) ? 'first' : (i == 1 || i % 3 == 1) ? 'second' : '');
                    }).delay(850 + ((i % 3) * 150));
                }
            }
        });
        
        if($page < 2) $page++;
        else $page = 0;
    }).periodical(8000);
    /* slideshow */
    var slideshow_timer = (function(){ rotate_images(); }).periodical(4500);
});

function rotate_images(){
	var images = $$('#image img');
   	if(images.length > 1){
   		var present = 0;
   		var next = 0;
   		images.each(function(el,i){ if(el.hasClass('visible')) present = i; });
    		
    	if(present < images.length - 1) next = present + 1;
    	else next = 0;
    		
   		images[present].set('tween',{duration:1000});
    	images[next].set('tween',{duration:1000});
    		
    	images[present].tween('opacity',0);
    	images[next].setStyle('opacity',0);
   		images[next].setProperty('class','visible');
    	images[next].tween('opacity',1);
    	(function(){images[present].setProperty('class','');}).delay(600);
    }
}
