if (!window.console) {var console = {};}
if (!console.log) {console.log = function() {};}

jQuery.fn.slideLeftHide = function( speed, callback ) { this.animate( { width: "hide", paddingLeft: "hide", paddingRight: "hide", marginLeft: "hide", marginRight: "hide" }, speed, callback ); }
jQuery.fn.slideLeftShow = function( speed, callback ) { this.animate( { width: "show", paddingLeft: "show", paddingRight: "show", marginLeft: "show", marginRight: "show" }, speed, callback ); }

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

var debug = true;
var transition_time = 375;
var easing_method = 'easeInOutQuad';
var easing_method2 = 'easeInOutExpo';
var hold_time = 6000;
var _date = new Date();
var date_today = (parseInt(_date.getMonth())+1).toString()+'/'+_date.getDate()+'/'+_date.getFullYear();

/**
* GLOBAL ELEMENTS
*/
	var default_page = 'home';
	
	var doc = $(document);
	var el_loader;
	var el_nav_slider_track;
	var el_nav_slider;
	var el_nav_highlight;
	var el_nav_li;
	var el_nav_links;
	var el_nav_count = 0;
	var el_content;
	var el_footer;
	
	var stageStatus = 'HIDDEN';
	
	var slideshows = {};
	
/**
* HP ELEMENTS
*/
	slideshows['home'] = {};
	slideshows['home'].timer = false;
	slideshows['home'].mask = false;
	slideshows['home'].slider = false;
	slideshows['home'].nav_L = false;
	slideshows['home'].nav_R = false;
	slideshows['home'].pos = 0;
	
	var el_hp_logo;
	var el_hp_tagline;
	

/**
* CASESTUDIES ELEMENTS
*/
	var el_cs_gallery;
	var el_cs_stage;
	var el_cs_detail;
	var el_cs_detail_close;
	var el_cs_detail_mh;
	var el_cs_detail_title;
	var el_cs_detail_client;
	var el_cs_detail_subnav;
	var el_cs_detail_copy;
	var el_cs_detail_thumbs;
	var el_cs_detail_fullsize;
	
/**
* OFFERINGS ELEMENTS
*/
	var el_offerings;
	var el_offerings_subnav_highlight;
	
	slideshows['offerings'] = {};
	slideshows['offerings'].timer = false;
	slideshows['offerings'].mask = false;
	slideshows['offerings'].slider = false;
	slideshows['offerings'].nav_L = false;
	slideshows['offerings'].nav_R = false;
	slideshows['offerings'].pos = 0;


/**
* STRATEGY ELEMENTS
*/
	var el_strategy;
	var el_strategy_subnav_highlight;
	
	
/**
* CONTACT ELEMENTS
*/
	var el_contact;
	var el_contact_form;
	var el_contact_form_submit;
	

doc.ready(function() {
	Cufon.now();
	start();
});


/**
* INIT STUFF
**/
	
	function start() {
		initGlobal();
		scrapePageFromURL();
	}
	
	function initGlobal() {
		
		$(['images/bg.jpg','images/bg2.jpg','images/mh_logo.png','images/mh_logo_white.png']).preload();
		
		initVars();
		initEventHandlers();
		initHiddenElements();
		initCufon();
	}
	
	
	function initHiddenElements() {
		$('.hide').each(function() {
			$(this).hide().removeClass('hide');
		});
	}
	
	function initVars() {
		
		//doc = $(document);
		
		el_hp_logo = $('#mh h1 A');
		
		el_loader = $('#loader');
		el_nav_slider_track = $('#nav_slider_track');
		el_nav_slider = $('#nav_slider');
		el_nav_highlight = $('#nav_slider .highlight');
		el_nav_links = $('ul.nav A');
		el_nav_li = $('ul.nav li');
		el_nav_count = el_nav_links.size();
		el_footer = $('#footer');
		
		el_content = $('#content');
		el_footer = $('#el_footer');
		
		el_hp_tagline = $('h2#tagline A');
		slideshows['home'].mask = $('#feature .mask');
		slideshows['home'].slider = $('#feature .mask .slider');
		slideshows['home'].nav_L = $('A#feature_nav_L');
		slideshows['home'].nav_R = $('A#feature_nav_R');
		
		el_cs_gallery = $('#casestudies .gallery');
		el_cs_stage = $('#stage');
		el_cs_detail = $('#casestudies_detail');
		el_cs_detail_close = $('#casestudies_detail A.close');
		el_cs_detail_mh = $('#casestudies_detail .mh');
		el_cs_detail_title = $('#casestudies_detail .mh .title');
		el_cs_detail_client = $('#casestudies_detail .mh .client');
		el_cs_detail_subnav = $('#casestudies_detail .sections2');
		el_cs_detail_copy = $('#casestudies_detail .copy');
		el_cs_detail_thumbs = $('#casestudies_detail .thumbs');
		el_cs_detail_fullsize = $('#casestudies_detail .fullsize');
		
		
		el_offerings = $('#offerings');
		el_offerings_subnav_highlight = $('#offerings .sections2 .highlight');
		
		slideshows['offerings'].mask = $('#offerings .mask');
		slideshows['offerings'].slider = $('#offerings .mask .slider');
		slideshows['offerings'].nav_L = $('A#offerings_slider_nav_L');
		slideshows['offerings'].nav_R = $('A#offerings_slider_nav_R');
		
		el_strategy = $('#strategy');
		el_strategy_subnav_highlight = $('#strategy .sections2 .highlight');
		
		el_contact = $('#contact');
		el_contact_form = $('#contact form');
		el_contact_form_submit = $('#contact form fieldset.submit A');
		
	}
	
	function initCufon() {
		Cufon.replace('ul.nav A', { fontFamily: 'TradeGothic', hover: true }); //textShadow: '0 0 3px #ffffff',
		Cufon.replace('h2#tagline A,#news h3,#news p,#casestudies .sections p,#casestudies li.title,#casestudies p,#casestudies_detail h2 .title,#casestudies_detail p,#agency p,#careers p,#careers li,#offerings p,#offerings table.subtopics td,#strategy p,#contact .sections p,#contact p,#contact form legend,#contact fieldset.submit A', {fontFamily: 'TradeGothic' });
		Cufon.replace('h2#tagline span,#news h4 A,#casestudies h2,#casestudies li.client,#casestudies_detail h3 A,#agency h2,#careers h2,#careers h3,#careers h4, #offerings h2,#offerings h3,#strategy h2,#strategy h3,#contact h2', { fontFamily: 'TradeGothicCondensed' });
		Cufon.replace('#casestudies_detail h2 .client', { fontFamily: 'TradeGothicLight' });
		//Cufon.replace('h2', { fontFamily: 'Gotham' });
	}
	
	


/** 
* EVENT HANDLERS
**/

	function clearEventHandlers() {
		
		doc.unbind('stageSET');
		doc.unbind('stageHIDDEN');
		doc.unbind('globalNavSlideDONE');
		doc.unbind('contentDrawDONE');
		
		$('A.newWin').unbind('click');
		
		$('#mh A').each(function() {
			$(this).unbind('click');
		});
		
		if(slideshows['home'].slider) {
			slideshows['home'].slider.find($('A')).each(function() {
				$(this).unbind('click');
			});
		}
		
		if(el_hp_tagline)
			el_hp_tagline.unbind('click');
		
		el_cs_gallery.find($('A')).each(function() {
			$(this).unbind('click');
		});
		
		el_cs_detail.find($('.thumbs A')).each(function() {
			$(this).unbind('click');
		});
		
		if(slideshows['offerings'].slider) {
			slideshows['offerings'].slider.find($('A')).each(function() {
				$(this).unbind('click');
			});
		}
		
		el_offerings.find($('.sections2 A')).each(function() {
			$(this).unbind('click');
		});
		
		el_cs_detail.find($('.mh A')).each(function() {
			$(this).unbind('click');
			$(this).unbind('hover');
		});
		
		el_cs_detail.find($('.thumbs A')).each(function() {
			$(this).unbind('click');
		});
		
		if(el_cs_detail_close)
			el_cs_detail_close.unbind('click');
			
		if(el_contact_form)
			el_contact_form.unbind('submit');
	}
	
	
	
	function initEventHandlers() {
		
		clearEventHandlers();
		
		$('A._blank').click(function() {
			window.open($(this).attr("href"));
			return false;
		});
		
		$('#mh A').each(function() {
			var el = $(this);
			el.click(function() {
				loadPage(el.attr('class'));
				//console.log(el.attr('class'));
				return false;
			});
		});
		
		if(slideshows['home'].slider) {
			
			slideshows['home'].slider.find($('A')).each(function() {
				
				$(this).click(function() {
					var el = $(this);
					var url = el.attr('href').split('/');
					var page = url[url.length-2];
					var id = url[url.length-1];
					
					loadPage(page,id);
					
					return false;
				})
			});
		}
		
		if(el_hp_tagline) {
			el_hp_tagline.click(function() {
				var el = $(this);
				var url = el.attr('href').split('/');
				loadPage(url[url.length-1]);
				return false;
			});
		}
		
		/**
		* These are set in the loadPage() function for context/timing purposes
		*/
			//doc.bind('stageSET',function() { });
			//doc.bind('stageHIDDEN',function() { });
		
		if(el_cs_detail_close) {
			el_cs_detail_close.click(function() {
				loadPage('casestudies');
				return false;
			});
		}
		
		el_cs_gallery.find($('A')).each(function() {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-2];
			var id = url[url.length-1];
			el.click(function() {
				loadPage(page,id);
				return false;
			});
		});
		
		el_cs_detail.find($('.mh A')).each(function() {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-3];
			var pid = url[url.length-2];
			var dir = url[url.length-1];
			el.click(function() {
				casestudiesDetailNextPrev(page,pid,dir);
				return false;
			});
			
			el.hover(
				function() { 
					$(this).addClass('hover');
				}, 
				function() { 
					$(this).removeClass('hover');
				}
			);
		});
		
		el_cs_detail.find($('.sections2 A')).each(function() {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-3];
			var id = url[url.length-2];
			var section = url[url.length-1];
			
			//console.log('page: '+page+' | id: '+id+' |section: '+section);
			
			el.click(function() {
				var els = el_cs_detail.find($('.sections2 h3'));
				els.removeClass('current');
				//el.parent().addClass('current');
				Cufon.replace('#casestudies_detail h3,#casestudies_detail h3 A', { fontFamily: 'TradeGothicCondensed' });
				
				loadContentPane(page,id,section);
				return false;
			});
		});
		
		el_cs_detail.find($('.thumbs A')).each(function(evt) {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-3];
			var pid = url[url.length-2];
			var iid = url[url.length-1];
			
			el.click(function() {
				el.parent().find('A').each(function() { $(this).removeClass('current'); });
				el.addClass('current');
				casestudiesFullsizeImage(page,pid,iid);
				return false;
			});
		});
		
		
		if(slideshows['offerings'].slider) {
			
			slideshows['offerings'].slider.find($('A')).each(function() {
				
				$(this).click(function() {
					var el = $(this);
					var url = el.attr('href').split('/');
					var page = url[url.length-2];
					var id = url[url.length-1];
					
					loadPage(page,id);
					
					return false;
				})
			});
		}
		
		el_offerings.find($('.sections2 A')).each(function() {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-2];
			var id = url[url.length-1];
			
			el.click(function() {
				var els = el_offerings.find($('.sections2 h3'));
				els.removeClass('current');
				//el.parent().addClass('current');
				Cufon.replace('#offerings h3,#offerings h3 A', { fontFamily: 'TradeGothicCondensed' });
				
				el_offerings.find('.projects').attr('class','projects').addClass('projects_'+id);
				
				loadContentPane(page,id);
				return false;
			});
		});
		
		
		el_strategy.find($('.sections2 A')).each(function() {
			var el = $(this);
			var url = el.attr('href').split('/');
			var page = url[url.length-2];
			var id = url[url.length-1];
			
			el.click(function() {
				var els = el_strategy.find($('.sections2 h3'));
				els.removeClass('current');
				//el.parent().addClass('current');
				Cufon.replace('#strategy h3,#strategy h3 A', { fontFamily: 'TradeGothicCondensed' });
				loadContentPane(page,id);
				return false;
			});
		});
		
		
		if(el_contact_form_submit) {
			
			el_contact_form_submit.click(function() {
				
				var errors = false;
				
				el_contact_form.find('.required').each(function() {
					
					$el = $(this);
					$field = $el.find(':input');
					
					if( $field.val() == '') {
						elementColorFlash($field,'#ff8d81',200,null);
						errors = true;
					}
					
					if(  $el.hasClass('email') && !validateEmail($field.val()) ) {
						elementColorFlash($field,'#ff8d81',200,null);
						errors = true;
					}
					
					/*
					if( $el.hasClass('message') &&  $field.val().length < 20) {
						elementColorFlash($field,'#ff8d81',200,null);
						errors = true;
					}
					*/
				});
				
				if(!errors) {
					
					$.post(
						'utilities/sendmail',
						el_contact_form.find(':input').serialize(),
						function(res) {
							//console.log(res);
							
							if(res == 'ok') {
								el_contact_form.html('<fieldset class="thankyou"><legend>Thanks your message has been sent</legend></fieldset>');
								Cufon.replace('#contact form legend');
							} else {
							
							}
						},
						'html'
					);
					
				} else {
					
					// error messaging here
					
				}
				
				return false;
			});
			
		}
	}




/**
* SLIDER
*/

	function doSliderLoop(_type) {
		if(slideshows[_type].mask) {
			if(slideshows[_type].timer != null)
				clearInterval(slideshows[_type].timer);
			
			slideshows[_type].timer = setInterval(function() { autoRight(_type); },hold_time);
		}
	}
	
	function resetSliderLoop(_type) {
		if(slideshows[_type].mask) {
			slideshows[_type].slider.css('left','0px');
			slideshows[_type].pos = 0;
			if(slideshows[_type].timer != null)
				clearInterval(slideshows[_type].timer);
			
			doSliderLoop(_type);
		}
	}
	
	function slide(_type) {
		$width = parseInt(slideshows[_type].mask.css('width'));
		$pos = parseInt(slideshows[_type].slider.css('left'));
		$count = parseInt(slideshows[_type].slider.find('img').size());
		$max = $count * $width;
		$min = 0;
		
		//console.log('slider['+_type+'] image count: '+$count);
		
		if(slideshows[_type].pos >= $count) {
			slideshows[_type].pos = 0;
			if(slideshows[_type].timer != null) {
				clearInterval(slideshows[_type].timer);
				doSliderLoop(_type);
			}
		}
			
		if(slideshows[_type].pos < 0) {
			slideshows[_type].pos = $count-1;
			if(slideshows[_type].timer != null) {
				clearInterval(slideshows[_type].timer);
				doSliderLoop(_type);
			}
		}
		
		$new_pos = ((slideshows[_type].pos * $width) * -1);
		
		//console.log('hp_slider_pos: '+hp_slider_pos+' | width: '+$width+ ' | pos: '+$pos+' | count: '+$count+' | max: '+$max+' | min: '+$min);
		
		slideshows[_type].slider.stop(true,true).animate(
			{ left: $new_pos }, 
			transition_time*2, 
			easing_method2, 
			function() {
      			
 			}  
  		);
	}
	
	function goLeft(_type) {
		slideshows[_type].pos--;
		slide(_type);
		
		clearTimeout(slideshows[_type].timer);
		doSliderLoop(_type);
	}
	
	function autoRight(_type) {
		slideshows[_type].pos++;
		slide(_type);
	}
	
	function goRight(_type,t) {
		slideshows[_type].pos++;
		slide(_type);
		
		clearTimeout(slideshows[_type].timer);
		doSliderLoop(_type);
	}
	
	
	
	
	
	
	
/**
* GLOBAL NAV RELATED
*/
	
	function scrapePageFromURL() {
		var _p = $.param.fragment();
		if(_p == '')
			_p = default_page;
		
		var t = _p.split('/');
		var page = t[0];
		var id = t[1];
		var seg2 = t[2];
		
		if(seg2 != null)
			id += '/'+seg2;
		
		loadPage(page,id);
	}
	
	
	function navHighlightSlide(_p) {
		
		$('ul.nav li').removeClass('current');
		
		$el = $('A.'+_p);
		
		$offset = 38;
		if(_p == 'home')
			$offset  = 89;
		else
			$offset = -222; //-332
			
		$el.parent().addClass('current');
		
		$new_pos = $el.position().left - $offset;
		$overhang = _p == 'home' ? 45 : (parseInt($el.css('margin-left'))/2) +5;
		//if(_p == 'contact')
			//$overhang = 0;
			
		$new_width = $el.width() + $overhang;
		
		el_nav_highlight.stop(true,true).animate(
			{ left: $new_pos+'px', width:  $new_width+'px'},
			Math.round(transition_time*2),
			easing_method2,
			function() {
				//console.log(el_nav_highlight.css('left'));
				//console.log($el.css('margin-left'));
				doc.trigger('globalNavSlideDONE');
				
				if(debug) { console.log("navHighlightSlide('"+_p+"') / OnComplete() "); }
			}
		);
		
		//console.log(el_nav_highlight.css('left'));
		//console.log($target);
	}
	
	
	function initSubNav(_page,_section,_section2) {
		
		var links;
		var link_current;
		var h3_current;
		var highlight;
		var new_left;
		var new_width;
		
		if(_section2 != null) {
		
			links = $('#'+_page+'_detail h3');
			link_current = links.find($('h3.'+_section2+' A'));
			h3_current = link_current.parent();
			highlight = $('#'+_page+'_detail').find($('.highlight'));
			
		} else {
		
			links = $('#'+_page+' h3');
			link_current = links.find($('h3.'+_section+' A'));
			h3_current = link_current.parent();
			highlight = $('#'+_page).find($('.highlight'));
			
		}
		
		links.removeClass('current');
		h3_current.addClass('current');
		
		link_current.hide();
		Cufon.replace(links, { fontFamily: 'TradeGothicCondensed' });
		
		/**
		* need to wait for Cufon to finish rendering in order scrape these values
		*/
			setTimeout(function() {
				new_left = link_current.position().left;
				new_width = link_current.width()+'px';
			
				//console.log('(new_left): '+new_left);
				//console.log('(new_width): '+new_width);
				
				/**
				* only animate the highlight if we can properly
				* pull width and pos values. On page load
				* cufon prevents this without an unacceptable
				* delay
				*/
					if(new_width != '0px') {
						
						highlight.animate(
							{ width : new_width, left : new_left+'px' },
							parseInt(transition_time/2),
							easing_method,
							function() {
								
							}
						);
					}
				
			},50);
		
		link_current.fadeIn(parseInt(transition_time/2),easing_method,function() {
		
		});
		
	
	}



/**
* CONTENT LOADING
*/

	function loadContentPane(_page,_section,_section2) {
		
		var sections;
		var section_current;
		
		if(_page == 'casestudies' && _section2 != null) {
			sections = $('#'+_page+'_detail .container');
		} else {
			sections = $('#'+_page+' .container');
		}
		
		/**
		* if no section was passed let's figure out what the first section is to default to
		*/
			if(_section == null) {
				$.each($('#'+_page+' .container:first').attr('class').split(' '),function(idx,val) {
					if(val == 'container' || val == 'current' || val == 'first' || val == 'last') {
						// skip generic classes
					} else if(_section == null) {
						_section = val.toString();
					}
				});
			}
		
		/**
		* special usecase for casetudies detail view (we have a 3D url path not 2D)
		*/
			if(_page == 'casestudies' && _section2 != null) {
				section_current = $('#'+_page+'_detail .'+_section2);
			} else {
				section_current = $('#'+_page+' .'+_section);
			}
		
		/**
		* write our unique url for this page
		*/
			var _fragment = _page;
			if(_section != null && _section != '') {
				_fragment += '/'+_section;
			}
			if(_section2 != null && _section2 != '' && _fragment.indexOf(_section2) == -1) { // additional logic to prevent duplicate path elements
				_fragment += '/'+_section2;
			}
			
			var url = $.param.fragment( document.location.href, '#'+_fragment , 2 );
			document.location.href = url;
		
		
		initSubNav(_page,_section,_section2);
		
		sections.hide().removeClass('current');
		
		section_current.addClass('current').stop(true,true).fadeIn(transition_time,easing_method,function() {
			//Cufon.refresh();
		});
	}
	
	
	
	function loadPage(_p,_id,_container) {
		
		//$('body').css('overflow','hidden');
		
		var container,section2,id;
		
		if(_container == null)
			container = el_content;
		else if(typeof(_container) == 'string') {
			if(_container.indexOf('#') != -1 || _container.indexOf('.') != -1)
				container = $(_container);
			else
				container = $('#'+_container);
		}
		else {
			container = _container;
		}
		
		
		var _page = _p+'/content';
		var _fragment = _p;
		
		if(_id != null && _id != '') {
			if(_id.indexOf('/') != -1) {
				var tmp = _id.split('/');
				id = tmp[0];
				section2 = tmp[1];
			}
			else
				id = _id;
			
			_page += '/'+id;
			_fragment += '/'+id;
		}
		
		var url = $.param.fragment( document.location.href, '#'+_fragment , 2 );
		document.location.href = url;
		
		navHighlightSlide(_p);
		
		if(_p == 'casestudies' && (_id != null && _id != '')) 
		{
			//casestudiesDetailTransitionOut();
		}
		
		
		el_footer.hide();
		
		container.stop(true,true).fadeOut(Math.round(transition_time/2),easing_method,function() {
			
			container.hide();
			
			el_loader.stop(true,true).fadeIn(Math.round(transition_time/2),easing_method,function() {
				
				$.post(
					_page,
					{ },
					function(res) {
						
						if(debug) { console.log("loadPage() / AJAX content call onComplete() "); }
						
						container.html(res);
						
						initGlobal();
						
						/**
						* run ancillary routines here for functionality
						* pertaining the load ajax content
						*/
						
							if(_p == 'home') {
								resetSliderLoop('home');
								doSliderLoop('home');
							}
								
							if(_p == 'offerings') {
								resetSliderLoop('offerings');
								doSliderLoop('offerings');
							}
							
							if(_p == 'offerings' || _p == 'strategy') {
								loadContentPane(_p,_id);
							}
							
						
							/**
							* we can't bind here because by this event has already fired by the time we get here
							*/
							//doc.bind('globalNavSlideDONE',function() {
		
								//if(debug) { console.log("loadPage() / doc.bind('globalNavSlideDONE',function()"); }
								
								if(_p == 'casestudies' && (_id != null && _id != '')) {
									
									if(section2 != null && (section2 != 'next' && section2 != 'prev'))
										loadContentPane(_p,_id,section2);
									else
										loadContentPane(_p,_id,'identify');
									
									if(stageStatus == 'HIDDEN') {
										
										if(debug) { console.log("stageStatus == 'HIDDEN' "); }
									
										setStage();
									
										doc.bind('stageSET',function() {
											
											if(debug) { console.log("doc.bind('stageSET',function()"); }
										
											el_loader.stop(true,true).fadeOut(Math.round(transition_time/2),easing_method,function() {
												container.show();
												el_footer.show();
												casestudiesDetailTransitionIn();
												//doc.unbind('stageSET');
											});
										});
										
									} else {
										
										if(debug) { console.log("stageStatus != 'HIDDEN' "); }
									
										el_loader.stop(true,true).fadeOut(Math.round(transition_time/2),easing_method,function() {
											container.show();
											el_footer.show();
											casestudiesDetailTransitionIn();
										});
									}
									
								} else {
									
									if(stageStatus == 'SET') {
										
										if(debug) { console.log("stageStatus == 'SET' "); }
									
										hideStage();
									
										doc.bind('stageHIDDEN',function() {
											
											if(debug) { console.log("doc.bind('stageHIDDEN',function()"); }
										
											el_loader.stop(true,true).fadeOut(Math.round(transition_time/2),easing_method,function() {
												container.stop(true,true).fadeIn(transition_time,easing_method,function() {
													el_footer.show();
													//doc.unbind('stageHIDDEN');
												});
											});
										});
									
									} else {
										
										if(debug) { console.log("stageStatus != 'SET' "); }
									
										el_loader.stop(true,true).fadeOut(Math.round(transition_time/2),easing_method,function() {
										
											if(debug) { console.log(" el_loader.stop(true,false).fadeOut() "); }
											
											container.stop(false,false).fadeIn(transition_time,easing_method,function() {
												
												if(debug) { console.log("container.stop(true,false).fadeIn() "); }
												
												el_footer.show();
											});
										});
									}
								}
								
							//}); // doc.bind('globalNavSlideDONE'
						
						
					},
					'html'
				);
			});
		});
		
	}




/**
* CASE STUDY DETAIL STUFF
*/

	function casestudiesFormatProjectTitle(section) {
		
		//console.log($.browser);
		
		var el_title;
		var el_client;
		
		if(section == 'cs_gallery') {
			el_title = el_cs_gallery_title;
			el_client = el_cs_gallery_client;
		} else {
			el_title = el_cs_detail_title;
			el_client = el_cs_detail_client;
		}
		
		setTimeout(function() { 
			var pos_offset = 0;
			var pos = el_title.position();
			if(pos.left != null) {
				pos_offset = parseInt(pos.left);
				pos_offset -=  $.browser.opera ? 16 : 0; // opera FF adjustment
				pos_offset -=  ($.browser.msie && parseInt($.browser.version, 10) == 7) ? 137 : 0; // IE7 adjustment
			}
			var width = el_title.width();
			el_client.css( { left: pos_offset+'px', width: width+'px'});
		},300);
	}
	
	function casestudiesFullsizeImage(page,pid,iid) {
		var img = 'images/projects/'+pid+'/'+iid+'_lg.jpg';
		el_cs_detail.find($('.fullsize')).html("<img src=\""+img+"\" alt='' />");
	}
	
	function casestudiesDetailNextPrev(page,pid,dir) {
		$.post(
			'casestudies/projectData',
			{ pid : pid, dir: dir },
			function(res) {
				loadPage(page,res.id);
			},
			'json'
		);
	}
	
	function casestudiesDetailTransitionIn() {
	
			if(debug) { console.log('casestudiesDetailTransitionIn()'); }
			
			el_cs_detail_mh.css('visibility','hidden').show();
			casestudiesFormatProjectTitle('detail');
			el_cs_detail_mh.hide().css('visibility','visible');

			el_cs_detail_close.slideLeftShow(Math.round(transition_time/2),function() {
			el_cs_detail_mh.fadeIn(Math.round(transition_time/2),easing_method,function() {
				el_cs_detail_subnav.fadeIn(Math.round(transition_time/2),easing_method,function() {
					el_cs_detail_copy.fadeIn(Math.round(transition_time/2),easing_method,function() {
						el_cs_detail_thumbs.fadeIn(Math.round(transition_time/2),easing_method,function() {
							el_cs_detail_fullsize.fadeIn(transition_time,easing_method,function() {
								
									if(debug) { console.log('fades done'); }
							})
						});
					});
				});
			});
		});
		
	}
	
	function casestudiesDetailTransitionOut() {
	
			if(debug) { console.log('casestudiesDetailTransitionOut()'); }
			
			el_cs_detail_close.slideLeftHide(Math.round(transition_time/2),function() {
			el_cs_detail_mh.fadeOut(Math.round(transition_time/2),easing_method,function() {
				el_cs_detail_subnav.fadeOut(Math.round(transition_time/2),easing_method,function() {
					el_cs_detail_copy.fadeOut(Math.round(transition_time/2),easing_method,function() {
						el_cs_detail_thumbs.fadeOut(Math.round(transition_time/2),easing_method,function() {
							el_cs_detail_fullsize.fadeOut(transition_time,easing_method,function() {
								
								if(debug) { console.log('fades done'); }
								
							})
						});
					});
				});
			});
		});
		
	}
	
	function setStage() {
		
		if(debug) { console.log("setStage()"); }
		
		//setTimeout(function() { $('body').removeClass('nostage').addClass('stage'); }, 500);
		$('body').addClass('stage');
		
		el_hp_logo.parent().addClass('inverse');
		/*
		el_nav_links.each(function() {
			if(!$(this).parent().hasClass('current'))
				$(this).css('visibility','hidden');
		});
		*/
		
		el_nav_slider_track.stop(true,true).animate(
			{ backgroundColor: '#000000' },
			transition_time,
			easing_method,
			function() {
				
			}
		);
		
		el_nav_slider.stop(true,true).animate(
		{ backgroundColor: '#000000' },
			transition_time,
			easing_method,
			function() {
				
			}
		);
		
		el_cs_stage.stop(true,true).fadeIn(parseInt(transition_time),easing_method,function() {
			doc.trigger('stageSET');
			stageStatus = 'SET';
			
			if(debug) { console.log("doc.trigger('stageSET');"); }
		});
	}
	
	function hideStage() {
		
		if(debug) { console.log("hideStage()"); }
		
		//setTimeout(function() { $('body').removeClass('stage').addClass('nostage'); }, 0);
		$('body').addClass('nostage').removeClass('stage');
		
		el_hp_logo.parent().removeClass('inverse');
		//el_nav_links.css('visibility','visible');
		
		el_nav_slider_track.stop(true,true).animate(
			{ backgroundColor: '#ffffff' },
			transition_time,
			easing_method,
			function() {
				
			}
		);
		
		el_nav_slider.stop(true,true).animate(
			{ backgroundColor: '#ffffff' },
			transition_time,
			easing_method,
			function() {
				
			}
		);
		
		el_cs_stage.stop(true,true).fadeOut(transition_time,easing_method,function() {
			doc.trigger('stageHIDDEN');
			stageStatus = 'HIDDEN';
			
			if(debug) { console.log("doc.trigger('stageHIDDEN');"); }
		});
	}
	
	
