/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Copyright � 2008 George McGinley Smith
 * All rights reserved.
 */
 
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d);},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b;},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b;},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b;},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b;},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b;},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b;},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b;},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b;},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b;},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b;},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b;},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b;},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b;},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b;},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b;},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b;},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b;},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b;},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b;},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b;},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b;},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b;},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4;}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b;},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b;},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b;},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b;},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b;},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b;}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b;}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b;}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b;}});


$(document).ready(function(){
		
                
                $('#nav a:not(.home)').click(function(){
			return false;
		});
		
		function indexOf(array, ele){
			for(var i=0; i<array.length; i++){
				if(array[i]==ele){
					return i;
				}
			}
			return -1;
		}
		
		/*if(!Array.indexOf){
			Array.prototype.indexOf = function(obj){
				for(var i=0; i<this.length; i++){
					if(this[i]==obj){
						return i;
					}
				}
				return -1;
			}
		}*/
		
		/*
		 * MAIN CONTENT
		 */
		 
		mainContent = {
			
			cache		: [],
			default_html: $('#header .main-content div').html(),
			duration	: 700,
			ele 		: $('#header .main-content div'),		
			html		: null,	
			active_html	: null,
			nav			: false,
			
			init : function()
			{
					$.getJSON('/content.json.php?action=services', $.proxy(this.parse, this));
			},
			
			checkContent : function(html)
			{
					if(html == this.ele.html()) return false;						
					return true;
			},
			
			getHtml : function(id)
			{
					if(this.cache.length == 0) return false;
					return this.cache[id].html;
			},
			
			getImage : function(id)
			{
					if(this.cache.length == 0) return false;
					return this.cache[id].img;				
			},
			
			getPage : function(url, nav)
			{
					this.nav = nav ? true : false;
					$.getJSON('/content.json.php?action=page&url=' + url, $.proxy(this.parsePage, this));	
			},
			
			parse : function(json)
			{
					this.cache = json;
					
					for(i in this.cache){
						var row = this.cache[i];
						var services = this.cache[i].services;
						var html = '';
						var $ul = $('<ul>');
						
						for(ii in services){
							var service = services[ii];
							var $li = $('<li><a href="/' + service.url + '" id="service' + service.id + '">' + service.title + '</a></li>');
							$li.data('data', service);
							$ul.append($li);
						}
						
						row.html = $('<div><h2>' + row.title + '</h2>' + row.desc + '</div>' );						
						if($ul.find('li').length > 0){
							row.html.append('<h3>' + row.title + ' Services </h3>');
							row.html.append($ul);
						}
						
						row.html.find('li a').click(function(){		
								var data = $(this).parent().data('data');
								contentModal.html(data.desc, data.title);
								return false;
						});
					}					
			},
			
			parsePage : function(json)
			{
					var row = json[0];
					
					this.cache[row.id] = row;
					this.cache[row.id].html = '<h2>' + row.title + '</h2>' + row.desc;	
					
					if(this.checkContent(this.cache[row.id].html)) this.swapContent(this.cache[row.id].html, this.nav);
					mainImage.swapImage(row.img);
					this.nav = false;
			},
			
			reset : function(active)
			{
					if(active){
						this.swapContent(this.active_html);
						return;
					}
					
					if(this.checkContent(this.default_html)) this.swapContent(this.default_html);
			},
			
			swapContent : function(html, nav)
			{
					this.html = html;
					if(nav) this.active_html = html;					
					this.swapContentHide();
			},
			
			swapContentHide : function()
			{			
					var props 	= {opacity: 0, top: 300};
					var opts 	= {duration: this.duration, queue: false, easing: 'easeInQuad', complete: $.proxy(this.swapContentShow, this)};	
					
					this.ele.animate(props, opts);			
			},
			
			swapContentShow : function()
			{
					var styles	= {left: 0, top: -30};
					var props	= {opacity: 1, left: 0, top: 0};
					var opts	= {duration: this.duration, queue: false, easing: 'easeOutQuad'};
					
					this.ele.html(this.html).css(styles).animate(props, opts);
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * MAIN BACKGROUND IMAGE
		 */
		
		mainImage = {
			
			cache		: [],
			default_img : $('#header .main-bg img'),
			duration	: 1200,
			ele			: $('#header .main-bg'),
			image_base 	: '/images/crop/945,432/projects/',
			
			displayImage : function(src)
			{
					var $img = $('<img src="' + src + '"/>');
					var styles	= {opacity: 0, position: 'absolute', left: 0, right: 0};
					var props	= {opacity: 1};
					var opts	= {duration: this.duration, queue: false, easing: 'easeOutQuad'};
					
					$img.css(styles).appendTo(this.ele).animate(props, opts);
			},
			
			loadImage : function(src)
			{				
					var img = new Image();
					var obj = this;
					
					img.src = src;
					img.onload = function(){
							obj.displayImage(src);
					};
					
					this.cache[src] = img;
			},
			
			reset : function()
			{					
					this.swapImage(this.default_img.attr('src'), true);
			},
			
			swapImage : function(src, pathed)
			{
					var src = pathed ? src : this.image_base + src;
					
					if(this.cache[src]){
						this.displayImage(src);	
					}else{
						this.loadImage(src);
					}
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * PROJECTS AREA
		 */
		
		projects_obj = {
			
			ele					: $('#projects'),
			loaded				: false,
			projects			: [],
			project_counter		: 0,
			active				: false,
			load_style			: 9,	// integer or 'all'
			selected_id			: null,
			
			init : function()
			{
					$.getJSON('/content.json.php?action=projects', $.proxy(this.loadProjects, this));
			},
			
			loadProject : function()
			{
					var obj 	= this;
					var i 		= this.project_counter;
					var project = this.projects[i];
					
					if(i > 0 && this.load_style == 1) this.projects[i-1].ele.unbind('complete');				
					if(i >= this.projects.length) return false;								
					project.ele.bind('complete', function(){ obj.loadProject();});				
					project.loadImage();				
					this.project_counter++;
			},
			
			loadProjects : function(json)
			{
					for(i in json){
						this.projects.push(new project_obj(json[i], this.ele));						
					}
					
					if(this.load_style == 'all'){
						for(i=0; i<this.projects.length; i++){					
							this.projects[i].loadImage();	
						}
					}else{
						for(i=0; i<this.load_style; i++){
							this.loadProject();							
						}
					}	
			},
			
			fadeOutProjects : function()
			{
					for(i in this.projects){
						this.projects[i].fadeOut();	
					}				
			},
			
			fadeProjects : function(id)
			{		
					if(id == -1) id = this.selected_id;
			
					if(id){		
						for(i in this.projects){							
							//if(!this.projects.hasOwnProperty(i)) continue;
							if(!this.projects[i].data.pages) continue;
							if(indexOf(this.projects[i].data.pages, id) > -1){
							//if(this.projects[i].data.pages.indexOf(id) > -1){//this.projects[i].page_id == id){
								this.projects[i].fadeIn();
							}else{
								this.projects[i].fadeOut();
							}
						}
					}else{
						for(i in this.projects){							
							//if(!this.projects.hasOwnProperty(i)) continue;
							this.projects[i].fadeIn();	
						}
					}
			}
		
		};		
		
		// --------------------------------------------------
		
		/*
		 * INDIVIDUAL PROJECT
		 */
		
		project_obj = function(data, parent){
			
				this.data = data;		
				this.page_id = this.data.page_id;
				this.parent = parent;	
				this.cache = [];
				this.ele = $('<a href="#"><span>' + this.data.title + '</span></a>');
				
				this.ele.attr('href', '/project/' + this.data.url + '/' + this.data.id ).css('opacity', 0);
				this.ele.click($.proxy(this.load, this));
		};	
		
		project_obj.prototype = {
			
			container			: $('#project'),
			content				: null,
			image_base			: '/images/sprite/105,105/projects/',
			
			complete : function(){	this.ele.trigger('complete');},
			
			displayContent : function(json)
			{
					if(json) this.content = json[0];
					
					var html = '<div class="project"><h2>' + this.content.title + '</h2>' + this.content.desc + '</div>';
					
					mainContent.swapContent(html);
					
					this.displayImages();
			},
			
			displayImage : function()
			{
					var styles 	= {backgroundImage: "url(" + this.image_base + this.data.img + ")", opacity: 0};
					var props	= {opacity: 1};
					var opts	= {duration: 1300, queue: false, complete: $.proxy(this.complete, this)};
					
					this.ele.css(styles);
					this.ele.find('span').css(styles);	
					
					if(projects_obj.active) this.ele.find('span').css('opacity', 1);
										
					this.ele.animate(props, opts);
			},
			
			displayImages : function()
			{				
					var obj = this;
					this.container.html('<div></div><a href="#back" class="back">&laquo;back</a>').css({opacity: 0, display: 'block'}).animate({opacity: 1});
					this.container.find('div').css('opacity', 0.75);
					this.container.find('.back').css('opacity', 0);
					
					if(this.cache.length > 0){
						for(i in this.cache){
							var img = this.cache[i];
							img.ele.appendTo(this.container);
							img.ele.click(function(){
									imageModal.load($(this).attr('href'), img.data.title, img.data.desc);	
									return false;
							});
						}
					}else{
						for(i in this.content.images){
							var img = new Image();						
							img.ele = $('<a href="/images/projects/' + this.content.images[i].file + '">' + this.content.images[i].title + '</a>').css('opacity', 0);
							img.data = this.content.images[i];
							img.src = this.image_base + this.content.images[i].file;
							img.onload = function(){
									var styles 	= {backgroundImage: "url(" + this.src + ")"};
									var props 	= {opacity: 1};
									var opts	= {duration: 1000, queue: false};
									
									this.ele.appendTo(obj.container).css(styles).animate(props, opts);
									
									var me = this;
									
									this.ele.click(function(){
											imageModal.load($(this).attr('href'), me.data.title, me.data.desc);	
											return false;
									});
							};
							this.cache.push(img);
						}
					}
					
					this.container.find('.back').css({backgroundImage: 'url(/images/site/back.png)'}).animate({opacity: 1},{duration: 200, queue: false}).click(function(){
							obj.remove();
							return false;
					});
			},
			
			fadeIn : function()
			{
					this.ele.animate({opacity: 1}, {duration: 300, queue: false});	
					this.ele.find('span').animate({opacity: 0}, {duration: 300, queue: false});
			},
			
			fadeOut : function()
			{	
					this.ele.animate({opacity: .5}, {duration: 300, queue: false});	
					this.ele.find('span').animate({opacity: 1}, {duration: 300, queue: false});	
			},
			
			hide : function()
			{
					this.container.css('display', 'none');	
			},
			
			loadImage : function()
			{					
					var img = new Image();
					var obj = this;					
					
					this.ele.appendTo(this.parent);
					
					img.src = this.image_base + this.data.img;
					img.onload = function(){
							obj.displayImage();
					};
			},
			
			load : function()
			{
					if(this.content){
						this.displayContent();
					}else{
						$.getJSON('/content.json.php?action=project&url=' + this.data.url, $.proxy(this.displayContent, this));
					}
				
					projects_obj.active = this;
					projects_obj.fadeOutProjects();
				
					return false;
			},
			
			remove : function()
			{
					this.container.animate({opacity: 0}, {duration: 500, queue: false, complete: $.proxy(this.hide, this)});
										
					mainContent.reset(true);
					
					projects_obj.active = false;
					projects_obj.fadeProjects(-1);
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * GENERIC MODAL INIT
		 */
		
		modal = {
			
			loader		: $('<div id="loader"/>'),
			modal		: $('<div id="modal"><h4><a href="#close" class="close">close &times;</a><span></span></h4><div></div><p></p></div>'),
			overlay		: $('<div id="overlay"/>'),	
			
			init : function()
			{
					this.overlay.css({opacity: 0, width: $(window).width(), height: $(window).height()}).appendTo(document.body);
					this.loader.css({opacity: 0, display: 'none', left: ($(window).width() - 100 ) / 2 , top: ($(window).height() - 40 ) / 2}).appendTo(document.body);
					this.modal.css({opacity: 0}).appendTo(document.body);			
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * IMAGE MODAL
		 */
		 
		imageModal = {
			
			max_height	: $(window).height() - 180,
			max_width	: $(window).width() - 160,
			
			init : function()
			{
					this.loader 	= $('#loader');
					this.modal 		= $('#modal');
					this.overlay 	= $('#overlay');				
					this.close_btn 	= this.modal.find('.close');
					this.contents	= this.modal.find('>div');
					this.desc		= this.modal.find('>p');
					this.title		= this.modal.find('>h4>span');
					
					this.contents.css({overflow: 'hidden'});	
					
					this.close_btn.click($.proxy(this.close, this));
					this.overlay.click($.proxy(this.close, this));
					
					//this.loader.css('display', 'block');					
					
					$(window).resize($.proxy(this.adjust, this));
			},
			
			adjust : function()
			{
					this.max_width	= $(window).width() - 160;
					this.max_height	= $(window).height() - 140;
					
					this.overlay.css({width: $(window).width(), height: $(window).height()});
					if(this.modal.is(':visible')) this.center(true);				
			},
			
			center : function(animate)
			{
					var left = ( $(window).width() - this.modal.outerWidth() ) / 2;
					var top = ( $(window).height() - this.modal.outerHeight() ) / 2;		
					
					if(animate){
						this.modal.animate({left: left, top: top}, {duration: 200, queue: false, easing: 'easeInOutQuad'});
					}else{
						this.modal.css({left: left, top: top});	
					}					
			},
			
			close : function()
			{
					this.overlay.animate({opacity: 0}, {duration: 600, queue: false});	
					this.modal.animate({opacity: 0}, {duration: 600, queue: false, complete: $.proxy(this.hide, this)});					
			},
			
			display : function()
			{
					this.modal.css({display: 'block'}).animate({opacity: 1}, {duration: 1000, queue: false});
					this.loader.delay(100).animate({opacity: 0});					
			},
			
			hide : function()
			{
					this.overlay.css({display: 'none'});
					this.modal.css({display: 'none'});
			},
			
			load : function(url, title, desc)
			{
					this.title.text(title);
					this.desc.text(desc);				
					this.overlay.css({display: 'block'}).animate({opacity: 0.5}, {duration: 600, queue: false});
					this.loader.css({display: 'block'}).animate({opacity: 0.7}, {duration: 600, queue: false});
					
					var obj = this;
					var img = new Image();			
					img.src = url;
					img.onload = function(){
						// adjust width if too wide
							var org_w = this.width;				
							while(obj.max_width < this.width){
								this.width--;	
							}			
						// adjust height to match width change
							var ratio = this.width/org_w;				
							if(ratio != 1) this.height = this.height * ratio;						
						// adjust height if too tall
							var org_h = this.height;
							while(obj.max_height < this.height){
								this.height--;
							}
						// adjust width to match height change						
							ratio = this.height/org_h;
							if(ratio != 1) this.width = this.width * ratio;
						// append image
							obj.contents.css({width: this.width, height: this.height});
							obj.contents.html($('<img src="' + url + '" width="' + this.width + '" height="' + this.height + '"/>'));						
						// display image
							obj.center();
							obj.display();
					};
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * CONTENT MODAL
		 */
		 
		contentModal = {
			
			width : 400,
			width_html : 400,
			
			init : function()
			{
					this.loader 	= $('#loader');
					this.modal 		= $('#modal');
					this.overlay 	= $('#overlay');	
					this.close_btn 	= this.modal.find('.close');
					this.contents	= this.modal.find('>div');
					this.desc		= this.modal.find('>p');
					this.title		= this.modal.find('>h4>span');
					
					
					this.close_btn.click($.proxy(this.close, this));
					this.overlay.click($.proxy(this.close, this));
					
					$(window).resize($.proxy(this.adjust, this));				
			},
			
			adjust : function()
			{					
					this.overlay.css({width: $(window).width(), height: $(window).height()});
					if(this.modal.is(':visible')) this.center(true);				
			},
			
			center : function(animate)
			{
					var left = ( $(window).width() - this.modal.outerWidth() ) / 2;
					var top = ( $(window).height() - this.modal.outerHeight() ) / 2;		
					
					if(animate){
						this.modal.animate({left: left, top: top}, {duration: 200, queue: false, easing: 'easeInOutQuad'});
					}else{
						this.modal.css({left: left, top: top});	
					}					
			},
			
			close : function()
			{
					this.overlay.animate({opacity: 0}, {duration: 600, queue: false});	
					this.modal.animate({opacity: 0}, {duration: 600, queue: false, complete: $.proxy(this.hide, this)});					
			},
			
			display : function()
			{
					this.modal.css({display: 'block'}).animate({opacity: 1}, {duration: 1000, queue: false});
					this.loader.delay(300).animate({opacity: 0});					
			},
			
			hide : function()
			{
					this.overlay.css({display: 'none'});
					this.modal.css({display: 'none'});
			},
			
			html : function(html, title)
			{
					this.overlay.css({display: 'block'}).animate({opacity: 0.5}, {duration: 600, queue: false});
					this.loader.css({display: 'block'}).animate({opacity: 0.7}, {duration: 600, queue: false});
					
					this.renderHtml(html, null, title);				
			},
			
			load : function(url)
			{
					this.overlay.css({display: 'block'}).animate({opacity: 0.5}, {duration: 600, queue: false});
					this.loader.css({display: 'block'}).animate({opacity: 0.7}, {duration: 600, queue: false});
					
					$.getJSON('/content.json.php?action=page&url=' + url, $.proxy(this.render, this));
			},
			loadCommercial : function(url)
			{
					this.overlay.css({display: 'block'}).animate({opacity: 0.5}, {duration: 600, queue: false});
					this.loader.css({display: 'block'}).animate({opacity: 0.7}, {duration: 600, queue: false});
					
                                        this.contents.css({width: 500})
					$.getJSON('/content.json.php?action=page&url=' + url, $.proxy(this.render, this));
			},
			
			loadHtml : function(url, width)
			{
					this.overlay.css({display: 'block'}).animate({opacity: 0.5}, {duration: 600, queue: false});
					this.loader.css({display: 'block'}).animate({opacity: 0.7}, {duration: 600, queue: false});
					
					this.width_html = width;
					$.get(url, $.proxy(this.renderHtml, this));
			},
			
			render : function(json)
			{
					this.contents.css({width: this.width});
					this.contents.html(json[0].desc);
					this.title.text(json[0].title);
					this.desc.text('');
					
					this.contents.css({height: 'auto'});
					this.center();
					this.display();
			},
			
			renderHtml : function(data, success, title)
			{
					var obj = this;
					this.contents.css({width: this.width_html}).html(data);
					this.title.text('');
					if(title) this.title.text(title);
					this.desc.text('');
					
					this.contents.find('form').submit(function(){
							$.ajax({
								url : $(this).attr('action'),
								type : 'post',
								dataType : 'json',
								data : $(this).serialize(),
								context : $(this),
								success : function(json){
										if(json.success){
											$(this).prepend('<p id="msg" class="success">' + json.response + '</p>');
										}else{
											$(this).prepend('<p id="msg" class="error">' + json.response + '</p>');									
										}
										
										setTimeout($.proxy(obj.close, obj), 3000);
								}
							});
							return false;
				   	});
					
					this.contents.css({height: 'auto'});
					this.center();
					this.display();
			}
			
		};
		
		// --------------------------------------------------
		
		/*
		 * INITIALIZE ALL REQUIRED OBJECTS
		 */
	
		mainContent.init();
		projects_obj.init();		
		modal.init();
		imageModal.init();
		contentModal.init();
		
		// --------------------------------------------------
		
		/*
		 * CONTENT INTERACTIONS
		 */
		
		// logo
		$('#home, #nav .home').click(function(){											  
				mainContent.reset();
				mainImage.reset();
				$('#nav .selected').removeClass('selected');
				$('#nav .home').parent().addClass('selected');
				return false;
		});
		
		// navigation		
		$('#nav a:not(.home)').each(function(){
				var $this = $(this);
				var uri = $this.attr('href').split('/');
				uri.shift();
				$this.data('uri', uri);
		}).click(function(){				
				var $this = $(this);								
			
				if($this.data('uri')[0] == 'service'){
					var id = $this.data('uri')[2];
					var html = mainContent.getHtml(id);
					var img = mainContent.getImage(id);
					
					projects_obj.selected_id = id;
					projects_obj.fadeProjects(id);
					if(mainContent.checkContent(html)) mainContent.swapContent(html, true);
					if(!$this.hasClass('selected')) mainImage.swapImage(img);
				}else{
					projects_obj.fadeProjects();
					mainContent.getPage($this.attr('href').substr(1), true);
				}				
				
				$('#nav .selected').removeClass('selected');
				$this.parent().addClass('selected');
				
				if(projects_obj.active)  projects_obj.active.remove();
				
				return false;
		});
		
		// footer links (privacy, legal)
		$('#footer .footright .modal').click(function(){			
				contentModal.load($(this).attr('href').substr(1));
				return false;
		});

                $('#top-bar .commercial-btn').click(function(){			
				contentModal.load($(this).attr('href').substr(1));
				return false;
		});
		
		// request quote 
		$('#footer .quote, .request-quote-btn a, .contact-us-btn').click(function(){
				contentModal.loadHtml($(this).attr('href'), 312);		
				return false;
		});
		
});
