Dialog = ({
	defaults: {
		project : 'eb',
		projects : {"eb": 8, "ndsu" : 8, "shaggys" : 3, "jamestown": 8, "west_fargo" : 8, "jtsecond" : 8},
		projectTitles : ["Eide Bailey", "NDSU", "Shaggys", "Jamestown Remodel", "West Fargo Remodel", "Jamestown Remodel 2"],
		projects2: [{	"id" 	: 	"eb", 
						"alt" 	: [	"Eide Bailly Reception", 
									"Eide Bailly Lounge", 
									"Eide Bailly Breakout",
									"Eide Bailly Refreshment Center",
									"Eide Bailly Atrium",
									"Eide Bailly Grand Staircase",
									"Eide Bailly Training Room",
									"Eide Bailly Employee Lounge"]},
									
					{	"id" 	: 	"ndsu", 
						"alt" 	: [	"NDSU Football Offices Reception", 
									"NDSU Football Offices Lounge",
									"NDSU Football Offices History Display",
									"NDSU Football Offices Head Coach Reception",
									"NDSU Football Offices Head Coach Office",
									"NDSU Football Offices Offensive Coordinator Office",
									"NDSU Football Offices Coaches Breakout Area",
									"NDSU Football Offices Auditorium"]},
									
					{	"id" 	: 	"shaggys", 
						"alt" 	: [	"Shaggy's Self Service Dog Wash ", 
									"Shaggy's Self Service Dog Wash",
									"Shaggy's Self Service Dog Wash"]},
									
					{	"id" 	: 	"jamestown", 
						"alt" 	: [	"Jamestown Remodeled Entry and Dining", 
									"Jamestown Remodeled Master Suite",
									"Jamestown Remodeled Kitchen",
									"Jamestown Remodeled Banquette", 
									"Jamestown Remodeled Kitchen Suite",
									"Jamestown Remodeled Great Room",
									"Jamestown Remodeled Master Bath", 
									"Jamestown Remodeled Details"]},
									
					{	"id" 	: 	"west_fargo", 
						"alt" 	: [	"West Fargo Kitchen", 
									"West Fargo Banquette",
									"West Fargo Entry",
									"West Fargo Great Room", 
									"West Fargo Living Area",
									"West Fargo Master Suite",
									"West Fargo Master Suite", 
									"West Fargo Girl's Bath"]},
									
					{	"id" 	: 	"jtsecond", 
						"alt" 	: [	"Jamestown Remodeled Great Room", 
									"Jamestown Remodeled Kitchen",
									"Jamestown Remodeled Kitchen",
									"Jamestown Remodeled Entry", 
									"Jamestown Remodeled Powder Bath",
									"Jamestown Remodeled Master Suite",
									"Jamestown Remodeled Laundry", 
									"Jamestown Remodeled Details"]}
					],
		section : 'commercial'
	},
	show: function(section){
		$('dialogContainer').setStyle({
			'display': 'block',
			'opacity': '0'
		});
		$('dialog').setStyle({
			'display': 'block',
			'opacity': '0'
		});
		if( $('thumb1').firstDescendant().readAttribute('alt') == null || $('thumb1').firstDescendant().readAttribute('alt') == ""){
			var numImgs = Dialog.defaults.projects[Dialog.defaults.project];
			for(i = 1; i <= numImgs; i++){
				Dialog.defaults.projects2.each(function(el,index){
					if( el.id == Dialog.defaults.project ){
						selThumb = $('thumb' + i);
						if ( Prototype.Browser.IE ) selThumb.firstDescendant().setAttribute('alt', Dialog.defaults.projects2[index].alt[i-1]);
						else selThumb.firstDescendant().writeAttribute('alt', Dialog.defaults.projects2[index].alt[i-1]);
					}
				});
			}
			var selThumb = $('thumb1').firstDescendant();
			var altThumb = selThumb.readAttribute('alt');
			var trimThumb = selThumb.readAttribute('src').replace('thumb_','')
			var newImg = 'url(' + trimThumb + ') no-repeat top left';
			$('dialog-content').setStyle({'background': newImg});
			$('project-title').replace('<div id="project-title">' + altThumb + '</div>');
			new Effect.Parallel([ 
				new Effect.Opacity($('dialog-content'), { sync: true, from: 0, to: 1 }) 
				], {
					duration: 1	
			});
		}
		$('project-title').replace('<div id="project-title">' + $('thumb1').firstDescendant().readAttribute('alt') + '</div>');
		if( section != Dialog.defaults.section ){
			Dialog.switchSections(section);
		}
		$('project-selector').clonePosition(Dialog.defaults.project);
		$('project-selector').setStyle({'height':'4px', 'top':'23px'});
		$('thumb-selector').clonePosition('thumb1');
		$('thumb-selector').setStyle({'height':'4px','top':'385px'});
		
		Dialog.resizeOverlay();
		Dialog.setInitialOffset();
		new Effect.Parallel([
			new Effect.Opacity($('dialogContainer'), { sync: true, from: 0, to: .8 }),
			new Effect.Opacity($('dialog'), { sync: true, from: 0, to: 1 })
			], {
			duration: 1
		});
	},
	hide: function(){
		$('dialog-content').setStyle({'background': 'url(images/' + Dialog.defaults.project + '1.jpg) no-repeat top left'});
		$('dialogContainer').setStyle({'display': 'none'});
		$('dialog').setStyle({'display': 'none'});
	},
	move: function( o, n ){ // uses old and new positions
		var oOffset = $(o).offsetLeft;
		var nOffset = $(n).offsetLeft;
		xpos = nOffset - oOffset;
		new Effect.Parallel([
			new Effect.Move('thumb-selector', { x: xpos, mode: 'relative' }), 
			new Effect.Opacity($('dialog-content'), { sync: true, from: 1, to: 0 }) 
			], {
			duration: .5,
			afterFinish: function() {
				Dialog.swapImg(n)	
			}
		});
	},
	swapImg: function(n){
		var selThumb = $(n).firstDescendant();
		var altThumb = selThumb.readAttribute('alt');
		var trimThumb = selThumb.readAttribute('src').replace('thumb_','')
		var newImg = 'url(' + trimThumb + ') no-repeat top left';
		$('dialog-content').setStyle({'background': newImg});
		$('project-title').replace('<div id="project-title">' + altThumb + '</div>');
		new Effect.Parallel([ 
			new Effect.Opacity($('dialog-content'), { sync: true, from: 0, to: 1 }) 
			], {
				duration: 1	
		});	
	},
	swapProject: function(o,n) {
		var nProj = n.readAttribute('id');
		var cProj = Dialog.defaults.project;
		var oOffset = $(o).positionedOffset();
		var nOffset = $(n).positionedOffset();
		xpos = nOffset[0] - oOffset[0];
		if ( Prototype.Browser.IE ) {
			new Effect.Parallel([
				new Effect.Move('project-selector', { x: xpos, mode: 'relative' }), 
				new Effect.Opacity($('dialog-content'), { sync: true, from: 1, to: 0 }),
				new Effect.Opacity('thumbs', { sync: true, from: 1, to: 0 })
				], {
				duration: .5,
				afterFinish: function() {
					Dialog.swapThumbs(n);
					Dialog.setEvents();
					$('thumb-selector').clonePosition('thumb1');
					$('thumb-selector').setStyle({'height':'4px','top':'385px'});
					$('dialog-content').setStyle({'background': 'url(images/' + Dialog.defaults.project + '1.jpg) no-repeat top left'});
					new Effect.Parallel([
						new Effect.Opacity($('dialog-content'), { sync: true, from: 0, to: 1 }),
						new Effect.Opacity($('thumbs'), { sync: true, from: 0, to: 1 })
						], {
							duration: 1
					});
				}
			});
			
		}
		else{
			new Effect.Parallel([ 
				new Effect.Opacity('dialog-content', { sync: true, from: 1, to: 0 }),
				new Effect.Opacity('thumbs', { sync: true, from: 1, to: 0 }),
				new Effect.Move('project-selector', { sync: true, x: xpos, mode: 'relative' }),
				], {
					duration: .5,
					afterFinish: function() {
						Dialog.swapThumbs(n);
						Dialog.setEvents();
						$('thumb-selector').clonePosition('thumb1');
						$('thumb-selector').setStyle({'height':'4px','top':'385px'});
						$('dialog-content').setStyle({'background': 'url(images/' + Dialog.defaults.project + '1.jpg) no-repeat top left'});
						new Effect.Parallel([
							new Effect.Opacity($('dialog-content'), { sync: true, from: 0, to: 1 }),
							new Effect.Opacity($('thumbs'), { sync: true, from: 0, to: 1 })
							], {
								duration: 1
						});
					}
			});
		}
		$(cProj).down().remove();
		var newImg = document.createElement('img');
		newImg.setAttribute('src', 'images/' + cProj + '_bw.jpg');
		$(cProj).insert(newImg);
		
		$(nProj).down().remove();
		var newImg = document.createElement('img');
		newImg.setAttribute('src', 'images/' + nProj + '_color.jpg');
		$(nProj).insert(newImg);
	},
	setInitialOffset: function() {
		var leftOffset = document.viewport.getScrollOffsets().left + ((document.viewport.getDimensions().width/2) - ($('dialog').getWidth()/2)) + "px";

		$('dialog').setStyle({
			left: leftOffset
		});
	},
	switchSections: function(sec){
		if( sec == 'residential' ){
			Dialog.defaults.project = Dialog.defaults.projects2[3].id;
			var range = $A('35');
			Dialog.defaults.section = 'residential';
			$('project-title').replace('<div id="project-title">Bocshe</div>');
		}
		else {
			Dialog.defaults.project = Dialog.defaults.projects2[0].id;
			var range = $A('02');
			Dialog.defaults.section = 'commercial';
			$('project-title').replace('<div id="project-title">Eide Bailey</div>');
		}
		
		$('projects').childElements().each(function(e, index){
			if( $(e).readAttribute('id') != 'project-selector' && $(e).readAttribute('id') != 'choose' ) {
				$(e).remove();
			}
		});
		$('dialog-content').setStyle({'background': 'url(images/' + Dialog.defaults.project + '1.jpg) no-repeat top left'});
		for(i = range[0]; i <= range[1]; i++){
			var newDiv = document.createElement('div');
			var newImg = document.createElement('img');
			var projName = Dialog.defaults.projects2[i].id;
			var projTitle = Dialog.defaults.projectTitles[i];
			if(Prototype.Browser.IE){
				newDiv.setAttribute('id', projName);
				newDiv.setAttribute('title', projTitle);
				if(Dialog.defaults.section == projName) {
					newImg.setAttribute('src', 'images/' + projName + '_color.jpg');
				} else {
					newImg.setAttribute('src', 'images/' + projName + '_bw.jpg');
				}
			}
			else{
				newDiv.writeAttribute('id', projName);
				newDiv.writeAttribute('title', projTitle);
				if(Dialog.defaults.project == projName) {
					newImg.writeAttribute('src', 'images/' + projName + '_color.jpg');
				} else {
					newImg.writeAttribute('src', 'images/' + projName + '_bw.jpg');
				}
			}
			$('projects').insert(newDiv);
			$(projName).insert(newImg);
			$(projName).writeAttribute('class', 'project');
		};
		Dialog.swapThumbs(Dialog.defaults.project);
		Dialog.setEvents(Dialog.defaults.project);
	},
	setEvents: function(proj){
		$('thumbs').childElements().each(function(e){
			if( $(e).readAttribute('id') != 'thumb-selector') {
				Event.observe(e,'click', Dialog.move.bind(Dialog,'thumb-selector', e) );
			}
		});
		if(proj){
			$('projects').childElements().each(function(e){
				if( $(e).readAttribute('id') != 'project-selector') {
					Event.observe(e,'click', Dialog.swapProject.bind(Dialog,'project-selector', e) );
				}
			});
		}
	},
	swapThumbs: function(proj) {
		var selProj = $(proj).readAttribute('id');
		
		Dialog.defaults.project = selProj;
		
		$('thumbs').childElements().each(function(e, index){
			
			if( $(e).readAttribute('id') != 'thumb-selector') {
				$(e).remove();
			}
		});
		var numProjects = Dialog.defaults.projects[selProj];
		for(i = 1; i <= numProjects; i++){
			var newDiv = document.createElement('div');
			var newImg = document.createElement('img');
			if(Prototype.Browser.IE){ // IE BUG Below somewhere
				newDiv.setAttribute('id', 'thumb'+i);
				newImg.setAttribute('src', 'images/thumb_' + Dialog.defaults.project + i +'.jpg');
				Dialog.defaults.projects2.each(function(el,index){
					if( el.id == selProj ){
						newImg.setAttribute('alt', Dialog.defaults.projects2[index].alt[i-1]);
					}
				}); 
			}
			else{
				newDiv.writeAttribute('id', 'thumb'+i);
				newImg.writeAttribute('src', 'images/thumb_' + Dialog.defaults.project + i +'.jpg');
				Dialog.defaults.projects2.each(function(el,index){
					if( el.id == selProj ){
						newImg.writeAttribute('alt', Dialog.defaults.projects2[index].alt[i-1]);
					}
				});
			}
			$('thumbs').insert(newDiv);
			$('thumb'+i).insert(newImg);
			$('thumb'+i).writeAttribute('class', 'thumb');
			$('project-title').replace('<div id="project-title">' + $('thumb1').firstDescendant().readAttribute('alt') + '</div>');
		};
	},
	resizeOverlay: function() {
	    var bodyDims = $(document.body).getDimensions();
	    var browserSize = document.viewport.getDimensions();
	
	    if(bodyDims.height > browserSize.height) browserSize.height = bodyDims.height;
	    if(bodyDims.width > browserSize.width) browserSize.width = bodyDims.width;
	
		var xScroll, yScroll;
			
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		
		$('dialogContainer').setStyle({
			width: pageWidth + 'px',
			height: pageHeight + 'px'
		});
	}
});
document.observe("dom:loaded", function() {
	Event.observe('dialog-close','click', Dialog.hide );
	$('projects').childElements().each(function(n){
		Event.observe(n,'click', Dialog.swapProject.bind(Dialog,'project-selector', n) );
	});
	Dialog.setEvents();
});
