/*
-------------------------------------------------------------------
Raynes Associates - Site 2009 -- JavaScript functions
authors:   maggie@maggiethayer.com
		   andrewpgordon@gmail.com
		   web@spacewalk.com
		   Coco Raynes Associates, Inc.
version:  2009.10.10
-------------------------------------------------------------------
*/

Cufon.replace('h2', { fontFamily: 'Univers Condensed' });
Cufon.replace('h3', { fontFamily: 'Univers Condensed' });
Cufon.replace('h4', { fontFamily: 'Univers Condensed' });
Cufon.replace('#navbar li#nav-phone-number', { fontFamily: 'Univers Condensed' });
Cufon.replace('#navbar li#nav-phone-number', { fontFamily: 'Univers Condensed' });
Cufon.replace('#footer p', { fontFamily: 'Univers Condensed' });
Cufon.replace('div.portfolio-maindesc', { fontFamily: 'Univers Condensed' });
Cufon.replace('#accessibility-slideshow-holder', { fontFamily: 'Univers Condensed' });
Cufon.replace('#content.about-our-firm div', { fontFamily: 'Univers Condensed' });
Cufon.replace('#accessibility-venues-list', { fontFamily: 'Univers Condensed' });
Cufon.replace('#navbar li#submenu > a', { fontFamily: 'Univers Condensed' });
Cufon.replace('#navbar li#submenu ul li a', { fontFamily: 'Univers Condensed', hover: true });
Cufon.replace('#body-maindesc', { fontFamily: 'Univers Condensed' });
Cufon.replace('#project-visual-identity', { fontFamily: 'Univers Condensed' });
Cufon.replace('#client-categories-list', { fontFamily: 'Univers Condensed', hover: true });
/* > a  */

/* START JQUERY-DEPENDENT CODE */

$(document).ready(function(){
jQuery.easing.def = "easeOutQuart"; //default easing function

	$('#navbar li.bg a').mouseenter( 
		function() {
			if( !$(this).parent().children("ul").is(":animated") ){ 
				if (!$(this).is('.you-are-here') && !$(this).parent().parent().parent().children('a.you-are-here').length) {
					$('#navbar li.bg a.you-are-here').parent().children('ul').slideUp("fast");
				} 
				$(this).parent().children('ul').slideDown("fast");
			}
		}
	);
	$('#navbar li.bg').mouseleave( function() { if($(this).children('a:not(.you-are-here)').length) { $(this).children('ul').slideUp("fast");}});
	$('#navbar li.bg a.you-are-here').parent().children('ul').slideDown("fast");
	$('#navbar li.bg a:not(.you-are-here)').mouseenter( function() { if (!$(this).parent().parent().parent().children('a.you-are-here').length) {$('#navbar li a.you-are-here').parent().children('ul').slideUp("fast");}});
	$('#navbar').mouseleave( function() {$('#navbar li.bg a.you-are-here').parent().children('ul').slideDown("fast"); });
	
	if (window.location.hash.length==0) $('#navbar li.bg:has(a.you-are-here) ul li.p:first-child a').addClass('you-are-here');
	
	Cufon.refresh();
	$(window).load(function() {
		$("#page").fadeIn(200);
		$("#footer").fadeIn(200);
		var tallest = 0;
		group=$('#navbar li.bg ul#submenu');
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		$('#navbar').height(tallest+60);
		//$('#navbar li ul').height(tallest);
		
		// Scroll .outer-viewport initially if there's a hash (#string) in the url 
		$.localScroll.hash({
			target: '.outer-viewport',
			axis: 'x',
			easing: 'easeOutQuart', //easing equation above
			duration: 2000,
			onBefore:function( e, anchor, $target ){
                //highlight the proper project navigation item when url hash causes a scroll to a project
                var theURL = window.location;
                var theHash = window.location.hash;
                var dropdown=$('#navbar li.bg a.you-are-here').parent().children('ul').children('li').children('a');
                if (dropdown.filter( function() { return ($(this).attr('href')==window.location); }).length) {
                	dropdown.filter( function() { return ($(this).attr('href')==window.location); }).addClass("you-are-here"); 
                } else {
                	dropdown.filter(function (index) { return index==0; }).addClass("you-are-here");
                }
                Cufon.refresh();
			}
		});
		if ($('.home').length) {
			homeImageRotate();
			//setTimeout("homeImageRotate();",2000);
		}
	});
	
		
	/* open anchors with class="new-window" in a new window, rather than using the non-validating target="_blank" */
	$('a.new-window').click(function(){
		window.open(this.href);
		return false;
	});	
			
	
	// SCROLL IMAGE STRIPS UPON MOUSEMOVE
	// Requires the uls and the surrounding divs to have these class names. There can be only one ul in the div.
	
	var hoverIntentConfig = {    
     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
     interval: 200, // number = milliseconds for onMouseOver polling interval    
     over: function(){}, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: function(){} // function = onMouseOut callback (REQUIRED)    
	};

		
	$("div.image-list-wrapper").mousemove(function(e){
		var div = $(this);
		var ul = $(this).find("ul");
		var lastLi = ul.find('li:last-child');
		var ulPadding = 100;
		var divWidth = div.width();
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	    
		div.css({overflow: 'hidden'});
	    
		if(ulWidth > divWidth) {
			//$(this).css({"padding-left":"16px"});
		}
	   div.scrollLeft(left);
	});
			
	// SCROLL INNER CONTENT HORIZONTALLY UPON MOUSEMOVE
	// Requires the uls and the surrounding divs to have these class names. There can be only one ul in the div.
	$("#content.about-our-firm .inner-viewport").mousemove(function(e){
		var div = $(this);
		var ul = $(this).find("ul");
		/* we look for an id here because Splitter's columnizing divs mess up finding li:last-child */
		var lastLi = ul.find('li#last-li');
		var ulPadding = 15;
		var divWidth = div.width();
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	    
		div.css({overflow: 'hidden'});

		if(ulWidth > divWidth || left < 0) {
			//$(this).css({"padding-left":"16px"});
		}
		if(left < 0) {
			left = -(left); /* fix for the fact that we're sometimes getting a negative left value */
		}
	    div.scrollLeft(left);
	});
	$("div.image-list-wrapper").mouseleave(function(e){
		var div = $(this);
		var ul = $(this).find("ul");
		var lastLi = ul.find('li:last-child');
		var ulPadding = 100;
		var divWidth = div.width();
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	    if (e.pageX>(div.offset().left+divWidth)) {
			div.scrollLeft(ulWidth-divWidth);
		} else if (e.pageX<div.offset().left) {
			div.scrollLeft(0);
		} 
	});
	$("#content.about-our-firm .inner-viewport").mouseleave(function(e){
		var div = $(this);
		var ul = $(this).find("ul");
		var lastLi = ul.find('li#last-li');
		var ulPadding = 15;
		var divWidth = div.width();
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		if (e.pageX>(div.offset().left+divWidth)) {
			div.scrollLeft(ulWidth-divWidth);
		} else if (e.pageX<div.offset().left) {
			div.scrollLeft(0);
		} 
	});
	
	
	/*
	-------------------- localScroll and serialScroll calls -------------------*/
	
	// add you-are-here class to clicked navigation anchors
    $('#navbar li ul li a').click(function(){ 
            $('#navbar li ul li a').removeClass('you-are-here'); 
            $(this).addClass('you-are-here'); 
            Cufon.refresh();
            $('div.portfolio-image-wrapper').css({"padding-left":"0"}).scrollLeft(0); // the scroll probably wasn't left at 0 by the user
    });
    	
	/* USE LOCALSCROLL TO SCROLL THE PORTFOLIO PROJECTS */
	$('#navbar').localScroll({
		target: '#portfolio-slideshow-holder',
		axis: 'x',
		hash: true,
		duration: 1000,
		easing:'easeOutQuart' //easing plugin
	});
	
	/* USE LOCALSCROLL TO SCROLL THE ACCESSIBILITY-PRODUCTS VENUES */
	$('#accessibility-venues-list').localScroll({
		target: '#accessibility-slideshow-holder',
		axis: 'x',
		hash: true,
		duration: 1000,
		easing:'easeOutQuart' //easing plugin
	});
		
	/* USE SERIALSCROLL TO SCROLL "ABOUT OUR FIRM" CLIENT-LIST ENTRIES */

	var $clientCatNav = $('ul#client-categories-list li a');
	$clientCatNav.eq(0).addClass('you-are-here'); //add "selected" class to first nav item (0) upon page load
	
	//$('div#content').serialScroll({
	$('#content.client-list div.outer-viewport').serialScroll({
		items:'div.client-list-category-wrapper',
		navigation: $clientCatNav, // var declared above
		offset: 0, // when scrolling to photo, stop this amount before reaching it (from the left)
		start: 0, //as we are centering it, start at the 2nd
		duration: 500,
		force: true,
		stop: true,
		lock: false,
		cycle: false, // scroll back once you reach the end
		easing:'easeOutQuart', //easing equation above
		jump: false, //click on the images to scroll to them
		// from Ariel Fleshler
		onBefore:function(e,el,$p,$i,pos){
			$clientCatNav.removeClass('you-are-here');
			$clientCatNav.eq(pos).addClass('you-are-here');
			$('#content.client-list div.inner-viewport').css({"padding-left":"0"}).scrollLeft(0); // the scroll probably wasn't left at 0 by the user
		}
	});
		
});//end document.ready

/* END JQUERY-DEPENDENT CODE */
var currentHomeImage=0;
function homeImageRotate() {
	if (currentHomeImage>2) $('#body-maindesc').hide();
	$('.body-outer').scrollTo($('.body-inner a > img').get(currentHomeImage),1000,{easing:'easeOutQuart' });
	currentHomeImage++;
	if ($('.body-inner a > img').length<=currentHomeImage) {
		currentHomeImage=0;
	}
	setTimeout("homeImageRotate();",2000);
}