Event.observe(window,'load',
	function() {
		featured_thumbs();
		portfolio_box();
	}
);

Event.onDOMReady(
	function() {
		attachments_list();
	}
);

function featured_thumbs() {
	if($$('div[class~=right] div[class~=thumbs] div[class~=thumb] img')) {
		$$('div[class~=right] div[class~=thumbs] div[class~=thumb] img').each(
			function(img) {
				img.style.cursor = 'pointer';
				Event.observe(img, 'click', 
					function() {
						img.up('div[class~=thumbs]').down('div[class~=selected]').removeClassName('selected');
						img.up('div[class~=thumb]').addClassName('selected');
						new Effect.Fade($('featured_hero').down('img'), { duration: 0.5, queue: 'front' });
						setTimeout("$('featured_hero').down('img').remove()", 501);
						setTimeout("$('featured_hero').innerHTML = '<img src=\"" + img.src.replace(/small/,'large') + "\" style=\"display: none\" />';",502);
						setTimeout("new Effect.Appear($('featured_hero').down('img'), { duration: 0.5, queue: 'end' });",503);
					}
				);
			}
		);
	}
}

function attachments_list() {
	if($$('ul[class~=all-attachments]')) {

		var co_txt = 'Change Orders';
		var pa_txt = 'Pay Applications';
		var sc_txt = 'Schedule';
		var oc_txt = 'Owner Contract';
		var sl_txt = 'Sublist';
		var pp_txt = 'Progress Pictures';
		var misc_txt = 'Others';
		
		var p = document.createElement('p');
		p.innerHTML = co_txt;
		co_txt = p;

		var p = document.createElement('p');
		p.innerHTML = pa_txt;
		pa_txt = p;

		var p = document.createElement('p');
		p.innerHTML = sc_txt;
		sc_txt = p;

		var p = document.createElement('p');
		p.innerHTML = oc_txt;
		oc_txt = p;

		var p = document.createElement('p');
		p.innerHTML = sl_txt;
		sl_txt = p;

		var p = document.createElement('p');
		p.innerHTML = pp_txt;
		pp_txt = p;

		var p = document.createElement('p');
		p.innerHTML = misc_txt;
		misc_txt = p;
		
		$$('ul[class~=all-attachments]').each(
			function(list) {
				var cos = document.createElement('li');
				cos.appendChild(co_txt);
				cos.className = 'co_list';
				var cos_ul = document.createElement('ul');
				cos.appendChild(cos_ul);
				var cos_length = 0;

				var pas = document.createElement('li');
				pas.appendChild(pa_txt);
				pas.className = 'pa_list';
				var pas_ul = document.createElement('ul');
				pas.appendChild(pas_ul);
				var pas_length = 0;

				var scs = document.createElement('li');
				scs.appendChild(sc_txt);
				scs.className = 'sc_list';
				var scs_ul = document.createElement('ul');
				scs.appendChild(scs_ul);
				var scs_length = 0;

				var ocs = document.createElement('li');
				ocs.appendChild(oc_txt);
				ocs.className = 'oc_list';
				var ocs_ul = document.createElement('ul');
				ocs.appendChild(ocs_ul);
				var ocs_length = 0;

				var sls = document.createElement('li');
				sls.appendChild(sl_txt);
				sls.className = 'sl_list';
				var sls_ul = document.createElement('ul');
				sls.appendChild(sls_ul);
				var sls_length = 0;

				var pps = document.createElement('li');
				pps.appendChild(pp_txt);
				pps.className = 'pp_list';
				var pps_ul = document.createElement('ul');
				pps.appendChild(pps_ul);
				var pps_length = 0;

				var miscs = document.createElement('li');
				miscs.appendChild(misc_txt);
				miscs.className = 'misc_list';
				var miscs_ul = document.createElement('ul');
				miscs.appendChild(miscs_ul);
				var miscs_length = 0;
				
				list.immediateDescendants().each(
					function(item) {
						if(item.down('a')) {

							var filename = item.down('a').innerHTML;
							var link = item.down('a');

							if(filename.indexOf('co_') != -1) {
								cos_ul.appendChild(item);
								cos_length++;
							} else if(filename.indexOf('pa_') != -1) {
								pas_ul.appendChild(item);
								pas_length++;
							} else if(filename.indexOf('sc_') != -1) {
								scs_ul.appendChild(item);
								scs_length++;
							} else if(filename.indexOf('oc_') != -1) {
								ocs_ul.appendChild(item);
								ocs_length++;
							} else if(filename.indexOf('sl_') != -1) {
								sls_ul.appendChild(item);
								sls_length++;
							} else if(filename.indexOf('pp_') != -1) {
								pps_ul.appendChild(item);
								pps_length++;
							} else {
								miscs_ul.appendChild(item);
								miscs_length++;
							}

							item.appendChild(link);
						}
					}
				);

				
				list.innerHTML = '';
				
				if(ocs_length > 0) {
					list.appendChild(ocs);
					ocs_ul.style.display = 'none';
				}

				if(cos_length > 0) {
					list.appendChild(cos);
					cos_ul.style.display = 'none';
				}

				if(pas_length > 0) {
					list.appendChild(pas);
					pas_ul.style.display = 'none';
				}

				if(scs_length > 0) {
					list.appendChild(scs);
					scs_ul.style.display = 'none';
				}

				if(pps_length > 0) {
					list.appendChild(pps);
					pps_ul.style.display = 'none';
				}

				if(sls_length > 0) {
					list.appendChild(sls);
					sls_ul.style.display = 'none';
				}

				if(miscs_length > 0) {
					list.appendChild(miscs);
					miscs_ul.style.display = 'none';
				}

				list.getElementsBySelector('li').each(
					function(new_item) {
						new_item.down('a').innerHTML = new_item.down('a').innerHTML.replace(/[a-z][a-z]_/,'');
						if(new_item.down('p')) {
							Event.observe(new_item.down('p'),'click',
								function() {
									new_item.down('ul').toggle();
								}
							);
						}
					}
				);
				
				list.makePositioned();
			}
		);
	}
}

function portfolio_box() {
	if($$('a[rel*=lightbox]').length > 0) {
		var overlay	= document.createElement('div');
		overlay.setAttribute('id','overlay');
		overlay.style.display = 'none';
		Event.observe(overlay,'click',close_box);
		if(navigator.userAgent.indexOf('MSIE') != -1) {
			overlay.style.width = document.body.clientWidth + 'px';
			if(document.body.clientHeight > document.documentElement.clientHeight) {
				overlay.style.height = document.body.clientHeight + 'px';
			} else {
				overlay.style.height = document.documentElement.clientHeight + 'px';
			}
		}

		var loading = document.createElement('div');
		loading.setAttribute('id','loading');
		overlay.appendChild(loading);

		document.body.appendChild(overlay);
		
		var preloader = document.createElement('div');
		preloader.setAttribute('id','preloader');
		preloader.style.position = 'absolute';
		preloader.style.left = '-10000px';
		preloader.style.top = '-10000px';
		document.body.appendChild(preloader);

		$$('a[rel*=lightbox]').each(
			function(link) {
				Event.observe(link,'click',
					function() {
						open_box(link);
					}
				);
			}
		);
	}
}

animate = false;

function close_box() {
	if($('overlay') && animate == false) {
		$('loading').style.display = 'none';
		var queue = Effect.Queues.get('lightbox');
		queue.each( function(e) { e.cancel() } );
		new Effect.Fade('box_wrap',{ queue: { position: 'parallel', scope: 'lightbox' }, duration: 0.25 });
		new Effect.Fade('box',{ queue: { position: 'parallel', scope: 'lightbox' }, duration: 0.25 });
		new Effect.Fade('overlay',{ queue: { position: 'parallel', scope: 'lightbox' }, duration: 0.25 });
	}
}

function open_box(link) {
	if($('overlay')) {
		var is_vis = false;
		animate = true;

		$('loading').style.display = 'block';

		if($('overlay').visible() && $('box')) {
			new Effect.Fade('box',{queue: { position: 'start', scope: 'lightbox' }, duration: 0.25});
			is_vis = true;
		}
		
		if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('7.0') == -1) {
			$('overlay').style.top = document.documentElement.scrollTop + 'px';
		}

		
		new Ajax.Updater('preloader','/portfolio_loader.php',
			{
				method: 'post',
				parameters: 'img=' + link.href + '',
				onComplete: function() {
					if($('preloader').down('img')) {
						var img = $('preloader').down('img');

						setTimeout(_open_box.bind(null,link,is_vis,img),1000);
						
						setTimeout("animate = false",1000);
					}
				}
			}
		);
	}
}

function _open_box(link, is_vis, img) {
	if($('box')) {
		$('box').remove();
	}
	if($('box_wrap')) {
		$('box_wrap').remove();
	}
	
	if(img.width > 600) {
		img.width = 600;
		img.height = Math.round(img.height * (600 / img.width));
	}

	var img_width = img.width;
	var img_height = img.height;
	var box = document.createElement('div');
	box.setAttribute('id','box');

	var box_prev = document.createElement('div');
	if(link.up('li[class~=thumbnail]').previous('li[class~=thumbnail]')) {
		box_prev.className = 'box_prev box_prev_on';
		Event.observe(box_prev,'click',
			function() {
				open_box(link.up('li[class~=thumbnail]').previous('li[class~=thumbnail]').down('a[rel*=lightbox]'));
			}
		);
	} else {
		box_prev.className = 'box_prev';
	}
	box.appendChild(box_prev);

	var box_left = document.createElement('div');
	box_left.id = 'box_left';
	box_left.appendChild(img);
	box_left.style.width = img_width + 'px';
	box_left.style.height = img_height + 'px';
	box.appendChild(box_left);

	var box_right = document.createElement('div');
	box_right.className = 'box_right';

	var title = document.createElement('h3');
	title.innerHTML = link.title;
	box_right.appendChild(title);

	if(link.next('p[class~=teaser]') != 'undefined') {
		var desc = document.createElement('p');
		desc.innerHTML = link.next('p[class~=teaser]').innerHTML;
		box_right.appendChild(desc);
	}

	box.appendChild(box_right);

	var box_next = document.createElement('div');
	if(link.up('li[class~=thumbnail]').next('li[class~=thumbnail]')) {
		box_next.className = 'box_next box_next_on';
		Event.observe(box_next,'click',
			function() {
				open_box(link.up('li[class~=thumbnail]').next('li[class~=thumbnail]').down('a[rel*=lightbox]'));
			}
		);
	} else {
		box_next.className = 'box_next';
	}
	
	box.appendChild(box_next);

	var box_close = document.createElement('div');
	box_close.className = 'box_close';
	Event.observe(box_close,'click',close_box);
	box.appendChild(box_close);
	
	box.style.display = 'none';
	
	var box_wrap = document.createElement('div');
	box_wrap.setAttribute('id','box_wrap');
	

	box_wrap.appendChild(box);
	if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('7.0') == -1) {
		box_wrap.style.top = document.documentElement.scrollTop + 'px';
	}
	Event.observe(box_wrap,'click',close_box);
	document.body.appendChild(box_wrap);

	var box_width = img_width + 200 + (12 * 4) + (53 * 2);
	
	$('box').style.width = box_width + 'px';
	$('box').style.marginLeft = '-' + Math.round(box_width / 2) + 'px';
	
	if($('box').down('div[id~=box_left]').getHeight() > $('box').down('div[class~=box_right]').getHeight()) {
		$('box').down('div[class~=box_right]').style.height = (($('box').down('div[id~=box_left]').getHeight() / 2) + 24) + 'px';
	}
	

	if($('box').down('div[class~=box_right]').getHeight() > $('box').down('div[id~=box_left]').getHeight()) {
		$('box').down('div[id~=box_left]').style.height = (($('box').down('div[class~=box_right]').getHeight() / 2) + 24) + 'px';
	}
	
	var box_height = Math.round(img_height / 2) + 24;
	
	if(box_height < document.body.down('div[class~=wrapper]').getHeight()) {
		if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('7.0') == -1) {
			$('box').style.marginTop = ((box_height /2 ) - 24) + 'px';
		} else {
			$('box').style.marginTop = '-' + (box_height + 24) + 'px';
		}
	} else {
		$('box').style.marginTop = '10px';
		$('overlay').style.height = (box_height * 2 + 24) + 'px';
	}

	if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('7.0') == -1) {
		box_prev.style.height = (img_height) + 'px';
		box_next.style.height = (img_height) + 'px';
	} else {
		box_prev.style.height = (img_height - 24) + 'px';
		box_next.style.height = (img_height - 24) + 'px';
	}
	
	if(is_vis == false) {
		new Effect.Appear('overlay',{queue: { position: 'parallel', scope: 'lightbox' },duration: 0.5,from:0.0,to:0.6});
		new Effect.Appear('box',{queue: { position: 'parallel', scope: 'lightbox' }, duration: 0.5});
	} else {
		new Effect.Appear('box',{queue: { position: 'front', scope: 'lightbox' }, duration: 0.5});
	}
	
	Event.observe('box','click',
		function() {
			animate = true;
			setTimeout("animate = false",100);
		}
	);
}