// <![CDATA[			
		
var Site = {
	init:function() {
		Site.initLogo();
		Site.initSegments();
		Site.initNewsletter();
		
		if ($('caseStudy') && $('caseStudy').getFirst().get('tag') === 'img') {
			// set up case study rotator:
			var fader = new Fader ('caseStudy');
		}
	},
	rotatorHandler:function(el) {
		switch (el.get('id')) {
				
			case 'img_0':
				window.location.href = '#';				
			break;
			
			case 'img_1':
				window.location.href = '#';
			break;
			
			case 'img_2':
				window.location.href = '#';
			break;
			
			case 'img_3':
				window.location.href = '#';
			break;
			case 'img_4':
				window.location.href = '#';
			break;
			
			case 'img_5':
				window.location.href = '#';
			break;
			
			case 'img_6':
				window.location.href = '#';				
			break;
			
			case 'img_7':
				window.location.href = '#';
			break;
			
			case 'img_8':
				window.location.href = '#';
			break;
			
			case 'img_9':
				window.location.href = '#';
			break;
			case 'img_10':
				window.location.href = '#';
			break;
			
			case 'img_11':
				window.location.href = '#';
			break;
			
			case 'img_12':
				window.location.href = '#';				
			break;
			
			case 'img_13':
				window.location.href = '#';
			break;
			
			case 'img_14':
				window.location.href = '#';
			break;			
			
			default:
				window.location.href = '#';
			break;
		}
	},
	initLogo:function() {
		// apply mouseenter, leave and click
		// events to the logo for navigational
		// and interactivity purposes:
		var logo = $$('#header img')[0];
		
		// set up tweens for the fade:
		logo.set('tween', {
			duration:'short',
			transition:'quad:out'
		}).addEvents({
			'click':function() {
				window.location.replace ('/');
				// window.location.replace('index.php');
			},
			'mouseenter':function() {
				this.tween('opacity', '1', '.5');
			},
			'mouseleave':function() {
				this.tween('opacity', '.7', '1');
			}
		});
	},
	initNewsletter:function() {
		$("newsletter").addEvent("click", function() {
			Site.newsletterForm();
		});
	},
	initSegments:function() {
		if ($('segment_container')) {
			var constructSegments = function(segments) {
				// create elements using the segment class
				// to house and display the data. first we
				// have to figure out how many segments
				// have come through in the response object,
				// as this will determine the css width 
				// property of each div.segment:
				var numSegments = segments.length - 1;
				
				for (var i = numSegments; i >= 0; i--) {
					// create a new element for each segment
					// its width will be determined by the
					// total number of segments sent in
					// response from the JSON query in this
					// function's parent:				
					var segment = new Element('div', {
						'id':'segment_[' + i + ']',
						'class':'segment',
						'styles':{
							'width':((($('segment_container').getStyle('width').toInt()) / segments.length) - 2) //set to 88 once live
						},
						'events':{
							'mouseenter':function() {
								this.setStyle('cursor', 'pointer').tween('background-color', '#FFFFFF', '#E6E6E6');
							},
							'mouseleave':function() {
								this.setStyle('cursor', 'default').tween('background-color', '#E6E6E6', '#FFFFFF');
							},
							'click':function(evt) {								
								// add a click event to the segment element.
								// loads content based on the "action" 
								// column in the table "tbl_whatsnew":								
								if (evt.target.get('tag') === 'img') {
									return;
								} else {
									if (this.getProperty('docID') === null) {
										// if a what's new element has a url and a storyID extant, then just take them
										// to the page where the story originated.										
										if (this.getProperty('url') !== null) {
											window.location.href = this.getProperty('url');
											return;
										} else if (this.getProperty('storyID') !== null) {
											Site.loadWhatsNewStory (this.getProperty('storyID'));
										}
										
									} else {
										Site.loadiPaper(this.getProperty('docID'), this.getProperty('accessKey'));
									}
								}
							}
						}
					});
					
					// set docID and accessKey properties which will be used
					// later in a click event to display content based
					// on these values:
					segment.setProperty('docID', segments[i].docID);
					segment.setProperty('accessKey', segments[i].accessKey);
					segment.setProperty('storyID', segments[i].storyID);
					segment.setProperty('url', segments[i].url);
					
					// make an adjustment if we're at the "last" segment
					// i quote this because we're inserting these in
					// reverse order from the data read:
					if (i === numSegments) {
						segment.setStyle('margin-right', '0');
					}
					
					// insert segment into the DOM:
					segment.inject('segment_container', 'top');
					
					// now create elements for each of the
					// sub elements inside of each segment:
					// heading:
					var segmentHeading = new Element('h2', {
						'class':'headline'					
					}).inject(segment, 'top').set('text', segments[i].heading);
					
					var leftContainer = new Element('div', {
						'styles':{
							'float':'left',
							'width':'35%',
							'height':'100%'
						}
					}).inject(segmentHeading, 'after');
					
						// image:
						var segmentImg = new Element('img', {
							'src':segments[i].thumbnail,
							'width':'70',
							'height':'91'
						}).inject(leftContainer, 'top');
						
					var rightContainer = new Element('div', {
						'styles':{
							'float':'left',
							'width':'65%',
							'height':'100%'
						}
					}).inject(leftContainer, 'after');
					
						// subheading:
						var segmentSubHeading = new Element('span', {
							'class':'subhead'					
						}).inject(rightContainer, 'top').set('text', segments[i].subheading);
				}			
			};
			
			// open a dialog with generateSegments.php:		
			var getSegments = new Request.JSON({
					url:'include/generateSegments.php',
					onComplete:constructSegments.bindWithEvent(this, this.response)
			}).get();
		}
	},
	newsletterForm:function() {
		if(!$('newsletter_viewer')) {
			Site.doMask();
			if($('bluesquare')) $('bluesquare').fade('out');
			
			var documentviewer = new Element('div', {
				'id':'newsletter_viewer',
				'class':'newsletter_viewer',
				'styles':{
					'z-index':'10000'
				}
			}).inject(document.body, 'bottom');
			
			var documentviewer_closebutton = new Element('img', {
				'id':'newsletter_viewer_close_button',
				'src':'img/buttons/closebox.png',
				'alt':'Close this Document',
				'width':'30',
				'height':'30',
				'styles':{
					'position':'absolute',
					'top':documentviewer.getPosition().y - 10,
					'left':documentviewer.getPosition().x - 10,
					'z-index':'10001',
					'width':'30px',
					'height':'30px'
				},
				'events':{
					'mouseenter':function() {
						this.setStyle('cursor', 'pointer');						
					},
					'mouseleave':function() {
						this.setStyle('cursor', 'default');						
					},
					'click':function() {
						Site.undoMask();
						// find any object or embed elements:
						if (Browser.Engine.trident) {
							try {							
								try {
									$('newsletter_viewer').destroy();
									$('newsletter_viewer_close_button').destroy();
								} catch (ex) {
									console.error ('couldn\'t remove viewer: ' + ex.message);
									return;
								}
								
							} catch (ex) {
								console.error (ex.message);
								return;
							}
						} else {
							try {
								documentviewer.destroy();
								documentviewer_closebutton.destroy();									
							} catch (ex) {
								console.error (ex.message);
								return;
							}
						}
						
						if ($('bluesquare')) $('bluesquare').fade('in');
					}
				}
			}).inject(document.body, 'bottom');
			
			// push the story into the box:
			// populate #document_viewer with content gotten from database:
			var getStory = new Request.HTML({
				method:'post',
				url:'/include/getNewsletterForm.php',
				async:true,
				update:$('newsletter_viewer')
			}).send();
			
		} else {
			alert ('You are already reading a document!\nPlease close the viewer before loading a new document.');
			return;
		}
	},
	loadWhatsNewStory:function(storyID) {
		if (!$('document_viewer')) {
			if ($('bluesquare')) $('bluesquare').fade('out');
			
			var documentviewer = new Element('div', {
				'id':'document_viewer',
				'class':'document_viewer',
				'styles':{
					'z-index':'10000'
				}
			}).inject(document.body, 'bottom');
			
			if ($('.smallimgRow')[0]) {
				documentviewer.setStyles({
					'top':$$('.smallimgRow')[0].getPosition().y,
					'left':$$('.smallimgRow')[0].getPosition().x
				});
			} else {
				documentviewer.setStyles({
					'top':$$('.imgRow')[0].getPosition().y,
					'left':$$('.imgRow')[0].getPosition().x
				});
			}
			
			var documentviewer_closebutton = new Element('img', {
				'id':'document_viewer_close_button',
				'src':'img/buttons/closebox.png',
				'alt':'Close this Document',
				'width':'30',
				'height':'30',
				'styles':{
					'position':'absolute',
					'top':documentviewer.getPosition().y - 10,
					'left':documentviewer.getPosition().x - 10,
					'z-index':'10001',
					'width':'30px',
					'height':'30px'
				},
				'events':{
					'mouseenter':function() {
						this.setStyle('cursor', 'pointer');						
					},
					'mouseleave':function() {
						this.setStyle('cursor', 'default');						
					},
					'click':function() {
						// find any object or embed elements:
						if (Browser.Engine.trident) {
							try {							
								try {
									$('document_viewer').destroy();
									$('document_viewer_close_button').destroy();
								} catch (ex) {
									console.error ('couldn\'t remove viewer: ' + ex.message);
									return;
								}
								
							} catch (ex) {
								console.error (ex.message);
								return;
							}
						} else {
							try {
								documentviewer.destroy();
								documentviewer_closebutton.destroy();									
							} catch (ex) {
								console.error (ex.message);
								return;
							}
						}
						
						if ($('bluesquare')) $('bluesquare').fade('in');
					}
				}
			}).inject(document.body, 'bottom');
			
			// push the story into the box:
			// populate #document_viewer with content gotten from database:
			var getStory = new Request.HTML({
				method:'post',
				url:'/include/getNewsItems.php',
				async:true,
				update:$('document_viewer')
			}).send('storyID=' + storyID);
			
		} else {
			alert ('You are already reading a document!\nPlease close the viewer before loading a new document.');
			return;
		}
	},
	loadiPaper:function(docID, accessKey) {
		if (!$('document_viewer')) {
			if (Site.doMask()) {
				if ($('bluesquare')) $('bluesquare').fade('out');
				
				// create the viewer element:
				var scribd_doc = scribd.Document.getDoc (docID, accessKey);
					scribd_doc.addParam ('jsapi_version', 1);
					scribd_doc.addParam ('width', 980);
					scribd_doc.addParam ('height', 650);
					scribd_doc.addParam ('menu', false);
		
					var documentviewer = new Element('div', {
						'id':'document_viewer',
						'class':'document_viewer',
						'styles':{
							'z-index':'10000',
							'overflow':'hidden'							
						}
					}).inject(document.body, 'bottom');
					
					if ($$('.smallimgRow')[0]) {
						documentviewer.setStyles({
							'top':$$('.smallimgRow')[0].getPosition().y,
							'left':$$('.smallimgRow')[0].getPosition().x
						});
					} else {
						documentviewer.setStyles({
							// 'top':$$('.imgRow')[0].getPosition().y,
							// 'left':$$('.imgRow')[0].getPosition().x
							'top':$$('.imgRow-reduced-height')[0].getPosition().y,
							'left':$$('.imgRow-reduced-height')[0].getPosition().x
						});
					}
					
					var documentviewer_closebutton = new Element('img', {
						'id':'document_viewer_close_button',
						'src':'img/buttons/closebox.png',
						'alt':'Close this Document',
						'width':'30',
						'height':'30',
						'styles':{
							'position':'absolute',
							'top':documentviewer.getPosition().y - 10,
							'left':documentviewer.getPosition().x - 10,
							'z-index':'10001',
							'width':'30px',
							'height':'30px'
						},
						'events':{
							'mouseenter':function() {
								this.setStyle('cursor', 'pointer');						
							},
							'mouseleave':function() {
								this.setStyle('cursor', 'default');						
							},
							'click':function() {
								// find any object or embed elements:
								if (Browser.Engine.trident) {
									try {
										if (document.getElementById('document_viewer').children.length > 0) {
											var iPaperViewer = document.getElementById('document_viewer').children(0);
											document.getElementById('document_viewer').removeChild(iPaperViewer);
										}									
										
										try {
											$('document_viewer').destroy();
											$('document_viewer_close_button').destroy();
										} catch (ex) {
											console.error ('couldn\'t remove viewer: ' + ex.message);
											return;
										}
										
									} catch (ex) {
										console.error (ex.message);
										return;
									}
								} else {
									try {
										documentviewer.destroy();
										documentviewer_closebutton.destroy();									
									} catch (ex) {
										if (console) { console.error (ex.message); }
										return;
									}
								}
								
								if (Site.undoMask()) {							
									if ($('bluesquare')) $('bluesquare').fade('in');
								}
							}
						}
					}).inject(document.body, 'bottom');
					
				// write document/story to document_viewer:
				scribd_doc.write ('document_viewer');
			}
		} else {
			alert ('You are already reading a document!\nPlease close the viewer before loading a new document.');
			return;
		}
	},
	getJob:function(el) {
		var selectedJob = $(el).get('value');
		if(selectedJob == '') return false; //For the 'Select An Option' option
		// open the document_viewer and then populate it
		// with the appropriate job description:
		if (!$('document_viewer')) {
			if ($('bluesquare')) $('bluesquare').fade('out');
			
			var documentviewer = new Element('div', {
				'id':'document_viewer',
				'class':'document_viewer'	
			}).inject(document.body, 'bottom');
			
			if ($$('.smallimgRow')[0]) {
				documentviewer.setStyles({
					'top':$$('.smallimgRow')[0].getPosition().y,
					'left':$$('.smallimgRow')[0].getPosition().x
				});
			} else {
				documentviewer.setStyles({
					'top':$$('.imgRow')[0].getPosition().y,
					'left':$$('.imgRow')[0].getPosition().x
				});
			}
			
			var documentviewer_closebutton = new Element('img', {
				'id':'document_viewer_close_button',
				'src':'img/buttons/closebox.png',
				'alt':'Close this Document',
				'width':'30',
				'height':'30',
				'styles':{
					'position':'absolute',
					'top':documentviewer.getPosition().y - 10,
					'left':documentviewer.getPosition().x - 10,
					'z-index':'10001',
					'width':'30px',
					'height':'30px'
				},
				'events':{
					'mouseenter':function() {
						this.setStyle('cursor', 'pointer');						
					},
					'mouseleave':function() {
						this.setStyle('cursor', 'default');						
					},
					'click':function() {
						// find any object or embed elements:
						if (Browser.Engine.trident) {
							try {
								if (document.getElementById('document_viewer').children.length > 0) {
									var iPaperViewer = document.getElementById('document_viewer').children(0);
									document.getElementById('document_viewer').removeChild(iPaperViewer);
								}
								
								try {
									$('document_viewer').destroy();
									$('document_viewer_close_button').destroy();
								} catch (ex) {
									if (console) console.log ('couldn\'t remove viewer: ' + ex.message);
									return;
								}
								
							} catch (ex) {
								if (console) console.log ('couldn\'t remove viewer: ' + ex.message);
								return;
							}
						} else {
							try {
								documentviewer.destroy();
								documentviewer_closebutton.destroy();
							} catch (ex) {
								if (console) console.error ('couldn\'t remove viewer: ' + ex.message);
								return;
							}
						}
						
						if ($('bluesquare')) $('bluesquare').fade('in');
					}
				}
			}).inject(document.body, 'bottom');
			
			// load the job description:
			var loadJobDescription = new Request.HTML({
				url:'/include/getJobDescription.php',
				method:'post',
				async:true,
				update:$('document_viewer')
			}).send('title=' + selectedJob);
			
		} else {
			alert ('You are already viewing a job description.\nPlease close this window before loading an additional job description.');
			return;
		}
	},
	doMask:function() {
		var mask = new Element('div', {
			'id':'mask',
			'styles':{
				// 'position':'absolute',
				'position':'fixed',
				'width':'100%',
				'height':'100%',
				'opacity':'0',
				'background-color':'#181818',
				'z-index':'9999'
			}
		});
		
		mask.set('tween', {
			duration:'normal',
			transition:'quad:out',
			link:'cancel',
			onComplete:function() {
				if (!Browser.Engine.trident) {
					$$('body')[0].setStyle('overflow', 'hidden');
				}
			}
		});		
		
		// insert it into the DOM:		
		mask.inject ($$('body')[0], 'top');
		
		// if it's firefox or something other than ghastly IE, center the overlay:		
		if (!Browser.Engine.trident) Site.centerElement ($('mask'));
		
		// finally make the overlay visible:
		$('mask').tween ('opacity', '0', '0.75');
		
		return mask;
	},
	undoMask:function() {
		if ($('mask')) {
			$('mask').set('tween', {
				duration:'short',
				transition:'quad:out',
				link:'cancel',
				onComplete:function() {
					$('mask').destroy();
					
					if (Browser.Engine.trident) {
						$$('html')[0].setStyles({
							'overflow-x':'hidden',
							'overflow-y':'auto'
						});
					} else {
						$$('body')[0].setStyles({
							'overflow-x':'hidden',
							'overflow-y':'auto'
						});
					}
				}
			}).tween ('opacity', $('mask').getStyle('opacity'), '0');
		}
		
		return true;
	},
	getViewPortDimensions:function() {
		var height = 0;
		var width = 0;
		
		// check for different browser conditions:
		if (self.innerHeight) {
			height = window.innerHeight;
			width = window.innerWidth;
		} else {
			if (document.documentElement && document.documentElement.clientHeight) {
				height = document.documentElement.clientHeight;
				width = document.documentElement.clientWidth;
			} else {
				if (document.body) {
					height = document.body.clientHeight;
					width = document.body.clientWidth;
				}
			}
		}
		
		return {
			height:parseInt(height, 10),
			width:parseInt(width, 10)
		};
		
	},
	centerElement:function(element) {
		var viewport = Site.getViewPortDimensions();
		var left = (viewport.width === 0) ? 50 : parseInt ((viewport.width - $(element).offsetWidth) / 2, 10);
		var top = (viewport.height === 0) ? 50 : parseInt ((viewport.height - $(element).offsetHeight) / 2, 10);
		
		element.setStyles({
			'top':top + 'px',
			'left':left + 'px'
		});
		
		viewport = left = top = null;

	},
	moveDocumentViewer:function(e) {		
		if (!Browser.Engine.trident && $('mask')) {
			Site.centerElement ($('mask'));
		}
		
		if ($('document_viewer')) {		
			// center the documentviewer:
			Site.centerElement ($('document_viewer'));
			
			// move the close button:
			$('document_viewer_close_button').setStyles({
				'top':$('document_viewer').getStyle('top').toInt() - 10,
				'left':$('document_viewer').getStyle('left').toInt() - 10
			});
		}
	}
}

var Form = {
	validate:function() {
		var requiredfields = $$('.required');
		var valid = true;
		
		// parse fields and see if there is data in them:
		$$(requiredfields).each(function(field) {
			if (field.get('value') === '' || field.get('value').length < 1) {
				field.set('tween', {
					duration:'short'					
				}).tween ('background-color', field.getStyle('background-color'), '#FF6666');
				
				valid = false;
			} else {
				// check if it was marked invalid:
				if (field.getStyle('background-color').toLowerCase() === '#ff6666' ) {
					// change the background-color back to mark it valid:
					field.set('tween', {
						duration:'short'
					}).tween ('background-color', '#FF6666', '#FFFFFF');
				}
			}
		});
		
		if (!valid) {
			var showAlert = function() {
				alert ('Please enter data in the marked fields prior to submission.');
			}.delay (500, this);
		}	
		
		return valid;
	},
	submitform:function() {
		var isvalid = Form.validate();
		var form = $('frm_contactform');
		
		if (isvalid) {
			var postdata = form.toQueryString();
			
			// connect to the processing page:
			var processcontactform = new Request({
				url:'/include/processContactForm.php',
				method:'post',
				async:true,
				onSuccess:Form.responseHandler.bindWithEvent(this, this.responseHTML)
			}).send(postdata);
		}
		
		return false;
	},
	responseHandler:function(response) {
		var setupMask = function() {
			var mask = new Element ('div', {
				'id':'mask',
				'styles':{
					'position':'absolute',
					'top':'0px',
					'left':'0px',
					'width':'100%',
					'height':'100%',
					'opacity':'0',
					'background-color':'#181818',
					'z-index':'101'
				}
			}).set ('tween', {
				duration:'short',
				transition:'quad:out'				
			}).inject ($$('body')[0], 'top');
			
			mask.tween ('opacity', 0, '0.75');
			
			return true;
		};
		
		var showMessage = function() {
			var responsecontainer = new Element('div', {
				'id':'responsecontainer',
				'styles':{
					'width':'300px',
					'height':'200px',
					'background-color':'#FFFFFF',
					'border':'2px solid #333',
					'position':'absolute',
					'top':'50%',
					'left':'50%',
					'margin-left':'-150px',
					'margin-top':'-100px',
					'z-index':'102',
					'opacity':'0'
				}
			}).set ('tween', {
				duration:'short',
				transition:'quad:out'
			}).inject ('mask', 'before');
			
			var responsecontainerclosebutton = new Element('img', {
					'id':'response_container_close_button',
					'src':'img/buttons/closebox.png',
					'alt':'Close this Box',
					'width':'30',
					'height':'30',
					'styles':{
						'position':'relative',
						'top':responsecontainer.getPosition().y - 10,
						'left':responsecontainer.getPosition().x - 10,
						'z-index':'9999',
						'width':'30px',
						'height':'30px'
					},
					'events':{
						'mouseenter':function() {
						this.setStyle('cursor', 'pointer');						
						},
						'mouseleave':function() {
							this.setStyle('cursor', 'default');						
						},
						'click':function() {							
							try {
								// remove the elements just added to the DOM:
								$('responsecontainer').destroy();
								$('response_container_close_button').destroy();
								
								if (!$('responsecontainer')) {
									$('mask').set ('tween', {
										duration:'short',
										transition:'quad:out',
										onComplete:function() {
											$('mask').destroy();
										}
									}).tween ('opacity', '0.75', 0);
								}
							} catch (ex) {
								if (console) {
									console.log ('couldn\'t remove viewer: ' + ex.message);
								} else {
									alert ('ERROR! Couldn\'t remove viewer: ' + ex.message + '\nPlease notify the developer at web@designforum.com and reload this page!');
								}
								
								return;
							}
						}
					}
				}).inject (responsecontainer, 'before');														   
			
			responsecontainer.tween ('opacity', 0, 1).set ('html', response);
		};
		
		if (setupMask()) {
			showMessage();
			$('frm_contactform').reset();
		}
	}
}

var Fader = new Class({
    Implements:Options,
    options:{
        pause:4000,
        duration:28000,
        loop:true,
        onComplete:Class.empty,
        onStart:Class.empty
    },
    initialize:function(container,options) {
        this.setOptions(options);
        this.container = $(container);
        this.imgs = this.container.getElements('img');
        this.imgs.setStyles({
                'position':'absolute',
                'top':0,
                'left':0,
                'opacity':0
        });        
		
	this.imgs[0].setStyle('opacity', 1);
	this.el = new Element('div', {
		'styles':{
			'position':'relative'
        	}
        });      
		
        this.el.injectInside(this.container);
        this.el.adopt (this.imgs);
        this.next = 0;
        this.start();
    },
    start:function() {	
	// this.show();	
	this.periodical = this.show.bind(this).periodical(this.options.pause);
    },
    stop:function() {
	$clear (this.periodical);
    },
    show:function() {
	if (!this.options.loop && this.next == this.imgs.length - 1) this.stop();
	
	this.next = (this.next === this.imgs.length - 1) ? 0 : this.next + 1;	
	
	var prev = (this.next === 0) ? this.imgs.length - 1 : this.next - 1;
		
	this.imgs[this.next].fade('in');
	this.imgs[prev].fade('out');
    }
});

window.addEvent('domready', Site.init);
window.addEvent('resize', Site.moveDocumentViewer);

// ]]>
