var widths = Array();
var is_thumbnails = false;
var biggestw = 0;
var loading_count = 0;
var initial_load = 3;
var server_count = 0;
var index = 0;

window.addEvent('domready', function() {
	$$('.li').addEvents({
		'mouseenter':function() {
			var el = $('level-two-' + this.get('uid'));
			el.set('tween', { duration: 400, transition: Fx.Transitions.Quint.easeOut });
			el.tween('height', el.getFirst().getSize().y + 'px');
		},
		'mouseleave':function() {
			var el = $('level-two-' + this.get('uid'));
			el.set('tween', { duration: 200, transition: Fx.Transitions.Quint.easeOut });
			el.tween('height','0px');
		}
	});
	
	if(galleryjson)
	loadImagesFromJSON();
});

function loadImagesFromJSON() {
	var count = 0;
	var lightbox = new Element('div',{ 'class':'lightbox' });
	lightbox.set('tween', { duration: 500, transition: Fx.Transitions.Quint.easeOut });
	galleryjson.each(function(i) {
		var div = new Element('div',{ 'class':'wraps ' + (i.typ + '_wrap') });
		if(i.typ=='image') {
			
			var timgwrap = new Element('div',{ 'class':'thumb fake_link' });
			var timg = new Element('img',{ 'src':i.thumb, 'class':'fake_link', 'id':count });
			timg.set('h',i.h);
			timg.set('w',i.w);
			timgwrap.adopt(timg);
			timgwrap.addEvent('click',function() {
				galleryJumpTo(timg.get('id'),true);
				loadAPhoto(true);
			});
			lightbox.adopt(timgwrap);
			
			var img = new Element('img',{ 'class':'loadme image' });
			img.set('h',i.h);
			img.set('w',i.w);
			img.set('pos',count++);
			img.set('smallsource',i.small);
			img.set('bigsource',i.large);
			
			if(i.url) {
				var a = new Element('a',{ 'href':i.url });
				div.adopt(a.adopt(img));
			}else{
				div.adopt(img);
			}
		}
		if(i.descript)
		div.adopt(new Element('div',{ 'class':'description', 'html':i.descript }));
		$('photo_wrap').adopt(div);
	});
	$('global').adopt(lightbox);
	initialLoad();
}

function hideThumbs() {
	$$('.lightbox').each(function(el) {
		el.tween('top', -el.getSize().y + 'px');
		//el.tween('top','60px');
	});
	is_thumbnails = false;
}

window.addEvent('resize', function() {
	checkPhotoGalleries();
	galleryJumpTo(index,true);
});

function initialLoad() {
	var totalw = 0;
	var cc = 0;
	
	if($$('.wraps').length > 1)
	setupNavigation();
	
	checkPhotoGalleries();
	loadAPhoto();
	loadAPhoto();
	galleryJumpTo(index,true);
}

function loadAPhoto(dontstop) {
	if(loading_count > 2 || loading_count == -1) return;
	loading_count++;
	var img = $$('.loadme')[0];
	if(!img){ 
		loading_count = -1;
		return;
	}
	img.set('src',window.getSize().y > 900 ? img.get('bigsource').replace('www','www'+(server_count++)) : img.get('smallsource').replace('www','www'+(server_count++)));
	img.removeClass('loadme');
	img.addEvent('load',function() {
		loading_count--;
		if(dontstop) {
			loadAPhoto();
			loadAPhoto();
		}
	});
}

function gotoThumbs() {
	if(is_thumbnails) {
		hideThumbs();
		return;
	}
	$$('.lightbox').each(function(el) {
		el.tween('top','60px');
	});
	
	$('body').setStyle('overflow','auto');
	$$('.photo_wraper').setStyle('overflow','none');
	
	is_thumbnails = true;
	
	return;
	if(is_thumbnails) return;
	is_thumbnails = true;
	checkPhotoGalleries();
	//$$('.thumbnails').setStyle('height',window.getSize().y);
	var parts = $$('.photo_wraper');
	$$('.photo_wraper').setStyle('height',5000);
	$('global').setStyle('height',parts[0].getSize().y-40);
	$('body').setStyle('height',parts[0].getSize().y-40);
	$('body').setStyle('overflow','scroll');
}

function setupNavigation() {	
	
	$$('.image_wrap').each(function(img) {
		img.addEvent('click',function(el) {
			galleryForward();
		});
	});
	
	$$('.left_arrow').addEvent('click',function() {
		galleryBack();
	});
	
	$$('.right_arrow').addEvent('click',function() {
		galleryForward();
	});

	window.addEvent('keydown', function(key) {
		if(key.code==39) {
			galleryForward();
		}
		if(key.code==37) {
			galleryBack();
		}
	});
	show($$('.navtools'));
}

function loadImages(d) {
	(function(){
		$$('.loadme').each(function(el) {
			el.set('src',el.get('source'));
			el.removeClass('loadme');
		});
	}).delay(d);
}

function checkPhotoGalleries() {
	var minw = biggestw = 500 + $('menu').getSize().x;
	$$('.wraps').setStyle('height', (window.getSize().y - 63) + 'px');
	
	var h = window.getSize().y;
	var oh = h - 44;
	var i = 0;
	
	$$('.image').each(function(img) {
		img.setStyle('height', (window.getSize().y - 90) + 'px');
		var w = Math.round((parseInt(img.getStyle('height')) * img.get('w')) / img.get('h'));
		if(window.getSize().x - 40 < w) {
			w = window.getSize().x - 40;
			oh = Math.round((w * img.get('h')) / img.get('w'));
			img.setStyle('height', oh + 'px');
			oh = oh - 3;
		}
		img.setStyle('width', w + 'px');
		if(w > biggestw) biggestw = w;
	});
	
	$$('.wraps').setStyle('width', biggestw + 'px');
	
	$$('.gallery').each(function(gal) {
		gal.setStyle('width', (gal.getChildren('.wraps').length * biggestw) + 'px');
	});
	
	if(is_thumbnails) {
		$$('.gallery').tween('margin-left',biggestw);
	}
	
	$$('.thumbnails').setStyle('left', -biggestw + 'px');
	$$('.thumbnails').setStyle('width', biggestw + 100);
	
	var iw = (biggestw - 40) / 5;
	var ih = Math.round((iw * 150) / 192);
	var tc = 1;
	$$('.thumb').each(function(el) {
		if(iw < 0) return;
		var img = el.getFirst();
		el.setStyle('text-align','center');
		
		if(tc % 5 == 0 && tc != 0)
		el.setStyle('margin','0px 0px 10px 0px');
		else
		el.setStyle('margin','0px 10px 10px 0px');
		
		el.setStyle('width', iw + 'px');
		el.setStyle('height', ih + 'px');
		
		if(parseInt(img.get('h')) > parseInt(img.get('w'))) {
			img.setStyle('height','100%');
		}else{
			img.setStyle('width',iw);
			//img.setStyle('height', Math.round((img.getSize().x * img.get('h')) / img.get('w')) + 'px');
		}
		//img.setStyle('margin-left',-(img.getSize().x * .5));
		tc++;
	});
	
	var s = (ih + 20) * Math.ceil($$('.thumb').length/5) > window.getSize().y ? (ih + 20) * Math.ceil($$('.thumb').length/5) : window.getSize().y-60;
	$$('.lightbox').setStyle('width',biggestw);
	$$('.lightbox').setStyle('height',s);
	
	$$('.lightbox').each(function(el) {
		el.getChildren().each(function(c) {
			c.setStyle('float','left');
			//c.setStyle('text-align','center');
			//c.setStyle('width','25%');
		});
	});
	
	$('header').setStyle('width',biggestw);
	$$('.global').setStyle('width',biggestw);
	$$('.global').setStyle('height','100%');
	$$('.photo_wraper').setStyle('width',biggestw);
}

function galleryBack() {
	var gal = $$('.gallery')[0];
	if(index == 0) {
		(function(){
			var gal = $$('.gallery')[0];
			gal.setStyle('margin-left', -gal.getSize().x + 'px');
			galleryJumpTo(gal.getChildren('.wraps').length-1);
		}).delay(500);
		galleryJumpTo(-1);
	}else{
		index = index-1;
		galleryJumpTo(index);
		loadAPhoto(true);
	}
}

function galleryForward() {
	if(is_thumbnails) { galleryJumpTo(0); return; }
	var gal = $$('.gallery')[0];
	index = index+1;
	if(index > gal.getChildren('.wraps').length) return;
	var speed = 500;
	if(index >= gal.getChildren('.wraps').length) {
		//index = 0;
		(function(){
			var gal = $$('.gallery')[0];
			gal.setStyle('margin-left', '900px');
			galleryJumpTo(0);
		}).delay(500);
		
		speed = 500;
	}
	galleryJumpTo(index,false);
	loadAPhoto(true);
}

function galleryJumpTo(i,notween,speed) {
	
	hideThumbs();

	var gal = $$('.gallery')[0];
	index = i = parseInt(i);
	is_thumbnails = false;
	if(!speed) speed = 500;
	gal.set('tween', { duration: speed, transition: Fx.Transitions.Quint.easeOut });
	if(i!= -1 && i < gal.getChildren('.wraps').length)
	$$('.count').set('html', (i+1) + ' / ' + gal.getChildren('.wraps').length);
	if(notween) {
	gal.setStyle('margin-left', (-biggestw * i) + 'px');
	} else {
		gal.tween('margin-left', (-biggestw * i) + 'px');
	}
	
	if(gal.getChildren('.wraps')[i].getChildren()[0].hasClass('loadme'))
	gal.getChildren('.wraps')[i].getChildren()[0].set('src',gal.getChildren('.wraps')[i].getChildren()[0].get('smallsource'));
	
	if(!gal.getChildren('.wraps')[i].hasClass('text_wrap')){
		$('body').setStyle('overflow','hidden');
		$('body').scrollTo(0,0);
		$$('.photo_wraper').setStyle('overflow','hidden');
	}else{
		$('body').setStyle('overflow','auto');
		$$('.photo_wraper').setStyle('overflow','none');
	}
}

function centerItem(content,x,y) {
	if(!x) x = 0;
	if(!y) y = 0;
	content.setStyle('position','absolute');
	var top = ((window.getScroll().y + (window.getSize().y / 2)-(content.getSize().y/2)));
	content.setStyle('top',(top+y)+'px');
	content.setStyle('left',((content.getParent().getSize().x / 2)-(content.getSize().x/2) + x)+'px');
}

function toggle(who) {
	if(who.getStyle('display')=='none') {
		show(who);
	} else {
		hide(who);
	}
}
function show(who) {
	who.setStyle('display','block');
}
function hide(who) {
	who.setStyle('display','none');
}