var baster_obj = null;
var baster_journal = null;

var Baster = new Class({
	initialize:function() {
		this.initImageMouseOvers();
		this.initFooter();
		if(!$chk(window.free_pos)) { // free_pos is only defined in admin mode
			this.initScroller();
			this.loadIndex();
			this.initPopups();
			this.initClickedBocks();
		}
	}
	
	,initClickedBocks:function() {
		$$('.black').getElements('a').each(function(el){
			el.addEvent('click',function(ev) {
					var a = this;
					var id = this.getParent('div.black').get('id');
					var req = new Request({
						url:"/control/baster/ajax/remember_click/item/" +id
						,onComplete:function(ev) {
							document.location = a.get('href');
						}
					}).post();
					ev.stop();
			});
		});
	}
	
	,initImageMouseOvers: function() {
		this.file_types = $$('.type_file');
		if(!this.file_types) return;
		this.file_types.each(function(el) {
			if(!el.hasClass("hover_enabled")) {
				return;
			}
			
			el.addEvent('mouseenter',function(ev) {
				if(!this.getElement('.img_color')) return;
				var col = this.getElement('.img_gray');
				col.set('tween',{duration:200});
				//col.fade("hide");
				//col.setStyle('display','block');
				//col.removeClass('img_show');
				col.fade(0);
				//this.getElement('.img_gray').removeClass('img_show');
			});
			el.addEvent('mouseleave',function(ev) {
				if(!this.getElement('.img_color')) return;
				var col = this.getElement('.img_gray');
				col.set('tween',{duration:200});
				col.fade(1);
				
				//this.getElement('.img_gray').addClass('img_show');
				//this.getElement('.img_color').removeClass('img_show');
			});
		});
	}
	
	,initFooter: function() {
		var footers = $$('.footer');
		var prev_pos = 0;
		$$('.index_content').each(function(index_el,dx) {
			var max_y = -999;
			index_el.getElements('.free_pos').each(function(free_el) {
				var txt_h = (free_el.getElement('.text')) ? free_el.getElement('.text').getSize().y : 0;
				var new_h = free_el.getPosition().y+free_el.getSize().y +txt_h;
				max_y = (new_h > max_y) ? new_h : max_y;
			});
			prev_pos = index_el.getPosition().y;
			if(max_y > 0) {
				footers[dx].setStyle('top',max_y-prev_pos +"px");
			}
		});
	}
	
	,initScroller: function() {
		if(!this.isOnProjectPage()) {
			return;
		}
		this.enableScrolling();
		var me = this;
		this.trigger_depth = 80;
		this.disable_wheel = false;
		this.is_scrolling = false;
		this.can_scroll = $$('.index_content').length > 1;
		
		var reset_scrolling = function() {
			var y = $$('.index_content')[0].getPosition().y;
			$$('.index_content')[0].destroy();
			$$('.index_content')[0].setStyle('top',y+'px');
			me.can_scroll = $$('.index_content').length > 1;
			window.scrollTo(0,0);
			var delay_enable = function() {
				me.is_scrolling = false;
				me.disable_wheel = false;
			};
			delay_enable.delay(200);
		}
		
		window.addEvent('mousewheel',function(ev) {
			if(me.is_scrolling || me.disable_wheel) {
				ev.stop();
				ev.preventDefault();
				return false;
			}
			var d = me.index_container.getPosition().y - window.getScroll().y;
			if(d < me.trigger_depth && me.can_scroll) {
				return false;
			}
		});
		
		var auto_scroll = function() {
			if(me.is_scrolling) return;
			me.is_scrolling = true;
			var scroll = new Fx.Scroll(window,{
				duration:500
				,onComplete:function(){ reset_scrolling(); }
				,onCancel:function() { reset_scrolling();	}
			});
			scroll.start(0,me.index_container.getPosition().y);
		};
		
		window.addEvent('mousewheel',function() {
			if(me.is_scrolling || ! me.can_scroll || !me.isScrollingEnabled()) return;
			var d = me.index_container.getPosition().y - window.getScroll().y;
			var diff = me.trigger_depth - d;
			if(diff > 0) {
				window.scrollTo(window.getScroll().y, window.getScroll().y-diff);
			}
			
			if(d < me.trigger_depth) {
				//$$('.index_content')[0].fade(0);
				$$('.index_content')[0].fade("hide");
				me.disable_wheel = true;
				auto_scroll.delay(300);
			}
		});
	}
	,disableScrolling: function() {	this.scrolling_enabled = false; }
	,enableScrolling: function() {	this.scrolling_enabled = true; }
	,isScrollingEnabled: function() { return this.scrolling_enabled; }
	
	,loadIndex: function() {
		if(!this.isOnProjectPage()) {
			return;
		}
		var me = this;
		//var curr_h = this.getPageHeight() + 111;
		var curr_h = this.getPageHeight() + 7; // 78 = 122
		var containers = $$(".index_content");
		this.index_container = containers[containers.length-1];
		this.index_container.setStyle('top',curr_h +"px");
		var req = new Request.HTML({
			url:"/"
			,append:this.index_container
			,onComplete:function(result) {
				me.initImageMouseOvers();
				me.initFooter();
				me.initClickedBocks();
				// enable preloading...
				if(window.image_preloader) {
					window.image_preloader.showImagesAfterLoadingWithXHR();
				}
			}
		}).send();
	}
		
	,getPageHeight: function() {
		var max_h = -1;
		$$('.free_pos').each(function(el){
			if(el.getPosition().y + el.getDimensions().y > max_h) {
				max_h = el.getPosition().y + el.getDimensions().y;
			}
		});
		if(document.getSize().y > max_h) {
			//max_h = document.getSize().y;
		}
		return max_h;
	}
	,isOnProjectPage: function() {
		return document.location.pathname != "/" && document.location.pathname != '/journal';
	}
	
	,initPopups: function() {
		var me = this;
		this.popup_open = false;
		var b = document.getElement('body');
		var container = $('popup_container');
		var resize_popup_image = function(popup_el) {
			var image = popup_el.getElement('img');
			
			// check if we already stored the real image size
			var image_size = image.retrieve("start_size");
			if(!image_size) {
				image_size = image.getSize();
				if(image_size.x != 0) { // when the image is not yet loaded the width will be 0
					image.store("start_size", image.getSize());
				}
			}
			// scale the image when it's bigger then theh visible area
			var window_size = window.getSize();
			if(image_size.x > window_size.x || image_size.y > window_size.y)  {
				var percx =window_size.x /  image_size.x;
				var percy = window_size.y / image_size.y;
				var perc = percx;
				if(percx > percy) {
					perc = percy;
				}
				image.setStyles({width:(image_size.x * perc) +"px", height:(image_size.y * perc)+"px"});
				
			}
			image.setStyles({
				'margin-left':-(image.getSize().x * 0.5).toInt() +"px"
				,'margin-top':window.getScroll().y + ((window.getSize().y *0.5)-image.getSize().y * 0.5) 
			});
			
		}
		var resize_overlay = function(popup_el) {
			var image = popup_el.getElement('img');
			var bg = "url(" +image.get('rel') +")";
			popup_el.setStyles({
				display:'block'
				,width:window.getSize().x * 1.0 + 15
				,height:8000
				,backgroundImage:bg
				,bottom:"0px"
				,top:"0px"
				
			});
			
			container.setStyles({
				width:window.getSize().x
				,height:7000 +"px"
			});
		}
		
		$$('.popup_toggler').each(function(el) {
			var popup_el = el.getParent('div').getElement('.popup');
			if(!popup_el) return;
			var pi = popup_el.getElement('img');
			
			//popup_el.inject(b,"top");
			popup_el.inject(container);
			resize_overlay(popup_el);
			pi.store('popup_el',popup_el);
			pi.addEvent('load',function(piev) {
				var popup_el = this.retrieve('popup_el');
				popup_el.show();
				resize_popup_image(popup_el);
				popup_el.hide();
			});
			
			popup_el.setStyle('display','none');
			var close_popup = function(oPopup) {
				var pop = oPopup;
				return function(ev) {
					if(me.popup_open) {
						pop.hide();	
						container.hide();
						b.removeClass("overlay");
						$('pages').show();
						me.popup_open = false;
						me.enableScrolling();
					}
				}
			};
			popup_el.addEvent('click',close_popup(popup_el));
			
			// handle clip on small image to open big one.
			el.store("popup_el",popup_el);
			el.addEvent('click',function(ev) {
				var popup = this.retrieve("popup_el");
				if(!me.popup_open) {
					me.disableScrolling();
					container.show();	
					popup.setStyle('opacity',0.0);
					popup.show();
					popup.fade("in");
					b.addClass("overlay");
					resize_overlay(popup_el);
					resize_popup_image(popup_el)
					//$('pages').hide();
					me.popup_open = true;
					me.opened_popup = popup;
				}
			});
		});
		window.addEvent('resize',function(ev) {
			if(me.popup_open) {
				resize_popup_image(me.opened_popup);
				resize_overlay(me.opened_popup);
			}
		});
	}
});

// JS related to the blog/journal.
var BasterJournal = new Class({
	initialize:function() {
		this.initScroller();
	}
	,initScroller: function() {
		this.container = $('blog_entries');
		if(!this.container) return;
		this.max_page = this.container.get('class').split("num_pages_")[1];
		var me = this;
		this.page = 2;
		this.scroll = new ScrollLoader({
			area:100
			,onScroll:function() {
				if(me.page > me.max_page) {
					return;
				}	
				var scroll = this;
				this.detach();
				$('journal_spinner').fade(1);
				$('logo').fade("hide");
				var req = new Request.HTML({
					url:"/journal/page/"+me.page
					,append:me.container
					,onComplete:function(result) {
						if(me.page < me.max_page) {
							scroll.attach();
							me.page++;
						}
						var h = function() {
							$('logo').fade("in");
						}
						var t = function() {
							$('journal_spinner').fade(0);
							h.delay(400);	
						};
						t.delay(400);
					}
				}).send();
			}
		});
	}
});

document.addEvent('domready',function(ev) {
	baster_obj = new Baster();
	baster_journal = new BasterJournal();
});

