var positions = null;
var wipx;
var wipid = 0;
var cur = false;
var curcontainer = false;
var curblock;

window.addEvent('domready', function() {

});

window.addEvent('load', function() {
	data = JSON.decode(data);
	wipx = data.wip;
	data = data.items;
	
	$$('.press').each(function(el) {
		el.getFirst().getFirst().setStyle('margin-top', ($('bod').getSize().y * .5) - 145 + 'px');
	});
	$('profile_box').setStyle('margin-top', ($('bod').getSize().y * .5) - 220);
	checkBlocks();
	
	$('content').setStyle('width',totalw + 'px');
	resize();
});

function loadPress() {
	$$('.press').each(function(el) {
		el = el.getFirst().getFirst();
		if(el.hasClass('checkme')) {
			el.removeClass('checkme');
			el.src = el.get('source');
		}
	});
}

function checkBlocks() {
	
	var scrollx = $('bod').getScroll().x;
	
	$$('.checkme').each(function(el) {
		if(true || (el.getPosition().x >= scrollx && el.getPosition().x < $('bod').getScroll().x + window.getSize().x)) {
			
			el.removeClass('checkme');
			
			if(el.hasClass('image')) {
				el.set('src',el.get('source'));
			}else{		
					
				addListenersToMask(el.getChildren('.mask')[0]);
				var overlay = new Element('div',{ 
													'class':'clearbg fake_link',
													style: 'position: absolute; left: 0px; width: 1000px; height: 210px; padding-top: '+ ((window.getSize().y * .5) - 150) +'px;'
											});
				var holder = new Element('div',{ 
													style: 'width: 100000px; height: 210px; padding-top: '+ ((window.getSize().y * .5) - 150) +'px;'
											});
				var img = new Element('img',{ 
												src: data[el.get('uid')]['kids'][0]['image'],
												style: 'height: 210px; cursor: pointer; width: ' + data[el.get('uid')]['kids'][0]['smallwidth'] + 'px;'
											});
				
				var n = new Element('div',{ 
												'class': 'nameanddescription',
												'style': 'margin-top: 10px'
											});
				n.innerHTML = data[el.get('uid')]['name'];
				var d = new Element('div',{ 
												'class': 'nameanddescription'
											});
				d.innerHTML = data[el.get('uid')]['description'];
				
				if(el.hasClass('loadall')) {
					if(!el.hasClass('loadedall')) {
						data[el.get('uid')]['kids'].each(function(g) {
							var img = new Element('img',{ 
															src: g['image'],
															style: 'height: 210px; cursor: pointer; width: ' + g['smallwidth'] + 'px;'
														});
												
							holder.adopt(img);
						});
						el.addClass('loadedall');
					}
				}else{
					holder.adopt(img);
				}
				
				el.getChildren('.kids')[0].adopt(overlay);
				el.getChildren('.kids')[0].adopt(holder);
				el.getChildren('.kids')[0].adopt(n);
				el.getChildren('.kids')[0].adopt(d);
			}
		}else{
			return;
		}
	});
}

function addListenersToMask(el) {
	if(!el) return;
	el.addEvent('mouseover',function() {
		$('home-main-project_block_'+el.get('uid')).addClass('project-block-hover');
	});
	el.addEvent('mouseout',function() {
		if(curblock == $('home-main-project_block_'+el.get('uid'))) return;
		$('home-main-project_block_'+el.get('uid')).removeClass('project-block-hover');
	});
	el.addEvent('click',function() {
		
		$('profile_box_holder').removeClass('current');
		
		var container = $('home-main-project_block_'+el.get('uid')+'_kids_container');
		var block = $('home-main-project_block_'+el.get('uid'));
		
		if(el.hasClass('dontexpand')) {
			for(var i=0; i<wipx.length; i++) {
				if(wipx[i] > $('bod').getScroll().x + 10) {
					if(i==wipx.length-1)
					i=0;
					new Fx.Scroll($('bod'), {duration: 200}).start(wipx[i]);
					return;
				}
			}
		}
		
		if(data[container.get('uid')]['isopen']) {
			
			block.removeClass('project-block-hover');
			container.getFirst().removeEvent('click',proceed);
			
			container.getChildren('.tools')[0].dispose();
			data[container.get('uid')]['position'] = 0;
			
			if(!el.hasClass('dontexpand')) {
			
				var c = 0;
				container.getChildren()[1].getChildren().each(function(el) {
					if(c++!=0)
					el.dispose();
				});
			
				el.setStyle('display','block');
			
				cur.removeClass('open');
			
				var myEffect = new Fx.Morph(container, {duration: 200}).start({
				    'width': data[container.get('uid')]['kids'][0]['smallwidth']
				});
			
				var myEffect = new Fx.Morph(container.getChildren()[1], {duration: 200}).start({
				    'padding-top': (window.getSize().y * .5) - 150,
					'height': 210
				});
			
				var myEffect = new Fx.Morph(container.getChildren()[0], {duration: 200}).start({
				    'padding-top': ($('bod').getSize().y * .5) - 150,
					'height': 210
				});
			
				container.getChildren()[1].setStyle('margin-left','0px');
				var myEffect = new Fx.Morph(container.getChildren()[1].getFirst(), {duration: 200}).start({
				    'width': data[container.get('uid')]['kids'][0]['smallwidth'],
					'padding-top': 0,
					'height': 210
				});
			
			}
			cur = false;
		}else{
			
			curblock = block;
			block.addClass('project-block-hover');
			
			new Fx.Scroll($('bod'), {duration: 200}).start(data[container.get('uid')]['myx']);
			
			if(cur) { 
				cur.fireEvent('click');
			}
			
			if(el.hasClass('dontexpand'))
			var tools = new Element('div',{ 'class': 'tools hidden' });
			else
			var tools = new Element('div',{ 'class': 'tools' });
										
			el.setStyle('display','none');
			var left = new Element('div',{ 
											'class': 'fake_link leftarrow arrows'
										});
			left.addEvent('click',function() {
				if(data[container.get('uid')]['position']==0) return;
				data[container.get('uid')]['position']--;
				
				checkMyPosition(container);
			});
			tools.adopt(left);
			
			var pages = new Element('div',{
											'id': 'page_count_' + String(container.get('uid')),
											'class': 'pages',
											'text': '1/' + String(data[container.get('uid')]['kids'].length)
										});
			pages.inject(tools);
			pages.innerHTML = '1/' + String(data[container.get('uid')]['kids'].length);
			
			var right = new Element('div',{ 
											'class': 'fake_link rightarrow arrows'
										});
			right.addEvent('click',function() {
				
				if(data[container.get('uid')]['position'] == data[container.get('uid')]['kids'].length-1) 
				data[container.get('uid')]['position'] = 0;
				else
				data[container.get('uid')]['position']++;
				
				checkMyPosition(container);
				
			});
			tools.adopt(right);
			
			var close = new Element('div',{ 'class': 'fake_link close arrows' });
			close.addEvent('click',function() {
				curblock = false;
				el.fireEvent('click');
			});
			tools.adopt(close);
			
			if(el.hasClass('dontexpand'))
			tools.addClass('hidden');
			
			container.adopt(tools);
			
			curcontainer = container;
			container.getFirst().addEvent('click',proceed);
			
			var c = 0;
			data[container.get('uid')]['kids'].each(function(d) {
				if(c++!=0) {
					var i = new Element('img',{ src: d['image'], style: 'cursor: pointer; height: 10px; vertical-align: top; margin-top: ' + ( d['is_tall'] ? 0 : 40 ) + 'px;' } );
					i.inject(container.getChildren()[1]);
					var f = function() {
						if(el.hasClass('dontexpand')) {
							i.setStyle('height',String(d['is_tall'] ? 210 : 210) + 'px');
							i.setStyle('margin-top','0px');
						}else{
							i.setStyle('height',String(d['is_tall'] ? 420 : 350) + 'px');
						}
					}
					f.delay(250);
				}
			});
			
			if(!el.hasClass('dontexpand')) {
				var myEffect = new Fx.Morph(container, {duration: 200}).start({
				    'width': data[container.get('uid')]['kids'][0]['bigwidth']
				});
			
				var myEffect = new Fx.Morph(container.getChildren()[1], {duration: 200}).start({
				    'padding-top': ($('bod').getSize().y * .5) - 250
				});
				var myEffect = new Fx.Morph(container.getChildren()[0], {duration: 200}).start({
				    'padding-top': ($('bod').getSize().y * .5) - 250,
					'height': 420
				});
		
				var myEffect = new Fx.Morph(container.getChildren()[1].getFirst(), {duration: 200}).start({
				    'width': data[container.get('uid')]['kids'][0]['bigwidth'],
					'padding-top': data[container.get('uid')]['kids'][0]['is_tall'] ? 0 : 40,
					'height': data[container.get('uid')]['kids'][0]['bigheight']
				});
		
				var myEffect = new Fx.Morph(container.getChildren()[1], {duration: 200}).start({
					'height': 420
				});
			}
			
			if(el.hasClass('dontexpand') && !el.hasClass('open')) {
				el.addClass('open');
				right.fireEvent('click');
			}
			
			cur = el;
		}
		data[container.get('uid')]['isopen'] = !data[container.get('uid')]['isopen'];
	});
}

function proceed() {
	var container = curcontainer;
	if(data[container.get('uid')]['position'] == data[container.get('uid')]['kids'].length-1) 
	data[container.get('uid')]['position'] = 0;
	else
	data[container.get('uid')]['position']++;
	checkMyPosition(container);
}

function checkMyPosition(container) {
	
	$('page_count_' + container.get('uid')).innerHTML = String(data[container.get('uid')]['position'] + 1) + '/' + String(data[container.get('uid')]['kids'].length);
	
	if(!container.hasClass('dontexpand')) {
		var myEffect = new Fx.Morph(container, {duration: 200}).start({
		    'width': data[container.get('uid')]['kids'][data[container.get('uid')]['position']]['bigwidth']
		});
	
		var myEffect = new Fx.Morph(container.getChildren()[1], {duration: 200}).start({
		    'margin-left': -data[container.get('uid')]['kids'][data[container.get('uid')]['position']]['offset']
		});
	}
}

function resize() {
	if($('pressbox')) {
		$('pressbox').setStyle('padding-top', parseInt((($('bod').getSize().y - 170 * 4) * .5)) + 'px');
	}
}

$('bod').addEvent('resize', function() {
	
	resize();
	
	$$('.project-block').each(function(el) {
		if(!el.hasClass('press') && !el.hasClass('profile')) {
			if(el.getPosition().x >= $('bod').getScroll().x && el.getPosition().x < $('bod').getScroll().x + window.getSize().x) {
				el.getChildren('.kids')[0].getFirst().setStyle('padding-top', ($('bod').getSize().y * .5) - (data[el.get('uid')]['isopen'] ? 250 : 150) + 'px');
			}
		}
	});
	
	$$('.press').each(function(el) {
		el.getFirst().getFirst().setStyle('margin-top', ($('bod').getSize().y * .5) - 145 + 'px');
	});
	
	$('profile_box').setStyle('margin-top', ($('bod').getSize().y * .5) - 220);
	
});

$('bod').addEvent('scroll',function(){
	checkBlocks();
});

window.addEvent('resize',function() {
	resize();
});

window.addEvent('scroll',function(){
	checkBlocks();
});

function checkImages() {
	$$('.image').each(function(el) {
		if(el.get('myx') > $('bod').getScroll().x + $('bod').getSize().x) return;
		
		if(el.get('myx') >= $('bod').getScroll().x) {
			el.set('src',el.get('source'));
			el.removeClass('image');
		}
	});
}

function show(who) {
	who.setStyle('display','block');
}

function hide(who) {
	who.setStyle('display','none');
}
