/************************************************************************************
	CUFON 
*************************************************************************************/
Cufon.replace('#masthead h1, #masthead p', { fontFamily: 'GainHead' });
Cufon.replace('#master_container .heading, #masthead .cta', { 
	fontFamily: 'GainBody', 
	hover: true 
});
Cufon.replace('#header #main_nav a.top', { 
	fontFamily: 'GainBody', 
	hover: true 
});

window.setInterval( function() {Cufon.refresh('#header #main_nav a.top');}, 0 );


/************************************************************************************
	CAROUSEL 
*************************************************************************************/
var Carousel = new Class({
	Implements: Chain,
	initialize: function(carouselID, autoPlay) {
		this.carousel = $(carouselID);
		this.autoPlay = (autoPlay == null) ? false : autoPlay;
		this.paginationLinks = this.carousel.getElements('.pagination a');
		this.prevLink = (this.carousel.getElement('.prev_button')) ? this.carousel.getElement('.prev_button') : false;
		this.nextLink = (this.carousel.getElement('.next_button')) ? this.carousel.getElement('.next_button') : false;
		this.carouselWindow = this.carousel.getElement('.window');
		this.carouselItems = this.carousel.getElements('.carousel_item');
		this.numCarouselItems = this.carouselItems.length;
		this.currCarouselNum = 0;
		this.animating = false;
		
		this.carouselItems[0].setStyle('display', 'block');
		
		this.carouselWindow.set('tween', {
			duration: 400, 
			transition: Fx.Transitions.Quad.easeOut,
			onStart: function(){
				this.animating = true;
			}.bind(this),
			onComplete: function(){
				this.callChain();
			}.bind(this)
		});
		
		this.paginationLinks.addEvent('click', function(e) {
			var e = new Event(e);
			var linkNum = 0;
			this.paginationLinks.filter(function(item, index){
			    if (item == e.target) {
					linkNum = index;
				}
			});
			this.goTo(linkNum);
			
			$clear(this.autoRotate);
			
			return false;			
		}.bind(this));
		
		if(this.prevLink) { this.prevLink.addEvent('click', this.goPrevious.bind(this)); }
		if(this.nextLink) { this.nextLink.addEvent('click', this.goNext.bind(this)); }
		
		if(this.autoPlay) { this.autoRotate = this.goNext.periodical(8000, this); }		
	},
	
	goTo : function(num) {
		if (! this.animating) {
			this.chain(
				function(){this.carouselWindow.tween('opacity', 0)},
				function(){this.carouselItems[this.currCarouselNum].setStyle('display', 'none'); this.callChain();},
				function(){this.carouselItems[num].setStyle('display', 'block'); this.callChain();},
				function(){this.paginationLinks[this.currCarouselNum].getParent().removeClass('active'); this.callChain();},
				function(){this.paginationLinks[num].getParent().addClass('active'); this.callChain();},
				function(){this.currCarouselNum = num; this.callChain();},
				function(){this.carouselWindow.tween('opacity', 1)},
				function(){this.animating = false}
			);
			this.callChain();
		}
		return false;
	},
	
	goPrevious : function() {
		if (! this.animating) {
			if ((this.currCarouselNum - 1) < 0) {
				nextNum = this.numCarouselItems - 1;
			}
			else {
				nextNum = this.currCarouselNum - 1;
			}
			this.goTo(nextNum);
		}		
		return false;
	},
	
	goNext : function() {
		if (! this.animating) {
			if ((this.currCarouselNum + 1) > (this.numCarouselItems - 1)) {
				nextNum = 0;
			}
			else {
				nextNum = this.currCarouselNum + 1;
			}
			this.goTo(nextNum);
		}		
		return false;
	}

});



/************************************************************************************
	SUB NAV DROP DOWN MENUS 
*************************************************************************************/
var subNavTimer;

window.addEvent('domready', function() {
	var navItems = $$('#main_nav li');
	
	navItems.each(function(item) {
		if (item.getElement('ul')) {
			item.addEvent('mouseenter', function() {
				subNavTimer = item.addClass.delay(0, item, 'hover');
			});
			item.addEvent('mouseleave', function() {
				item.removeClass('hover');
			 	$clear(subNavTimer);
			});
		}
	});
});



/************************************************************************************
	POP LAYER 
*************************************************************************************/
var popLayer = {
	
	popLayerFade : null,
	popLayerContent : null,
	shim : null,
	center: true,
	shimYOffset: 0,
	shimXOffset: 0,
	
	show : function() {		
		var popLayerFade = $(popLayer.popLayerFade);
		var popLayerContent = $(popLayer.popLayerContent);
		var shim = $(popLayer.shim);
		
		var windowScrollSize = window.getScrollSize();
		var windowSize = window.getSize();
		var windowScroll = window.getScroll().y;
		
		popLayerContent.setStyles({
			display: 'block',
			visibility: 'hidden'
		});
		
		var popLayerContentSize = popLayerContent.getSize();
		
		popLayerFade.setStyles({
			opacity: 0.6,
			height: $('master_container').getScrollSize().y,
			display: 'block'
		});	
		
		var popContentXPos = (windowSize.x / 2) - (popLayerContentSize.x / 2);
		var popContentYPos = (windowSize.y / 2) - (popLayerContentSize.y / 2) + windowScroll;
		popContentYPos = (popContentYPos < 0) ? 125 : popContentYPos;
		
		popLayerContent.setStyles({
			top: (popLayer.center) ? popContentYPos : 125,
			left: popContentXPos,
			visibility: 'visible'
		});
		
		shim.setStyles({
			top: (popLayer.center) ? popContentYPos + popLayer.shimYOffset : (125 + popLayer.shimYOffset),
			left: popContentXPos + (popLayer.shimXOffset),
			height: popLayerContentSize.y - (popLayer.shimYOffset * 2),
			width: popLayerContentSize.x - (popLayer.shimXOffset * 2),
			display: 'block'
		});		
	},
	
	hide : function() {
		var popLayerFade = $(popLayer.popLayerFade);
		var popLayerContent = $(popLayer.popLayerContent);
		var shim = $(popLayer.shim);
		
		popLayerFade.setStyle('display','none');
		popLayerContent.setStyle('display', 'none');
		shim.setStyle('display', 'none');
	}
}

/************************************************************************************
	ENLARGE MAP
*************************************************************************************/
function initEnlargeMap() {
	// initialize google map
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_enlarge"));
		map.setCenter(new GLatLng(40.644700, -74.638800), 16);
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		
		
		geocoder = new GClientGeocoder();
	}
}

function changeMap(name, displayAddress, mapAddress) {
	$$('#popLayer.map_enlarge_layer h2').set('html', name);
	$$('#popLayer.map_enlarge_layer p').set('html', displayAddress);

	var gainIcon = new GIcon(G_DEFAULT_ICON);
	gainIcon.image = "http://www.gaincapital.com/resources/images/maps/map_marker.png";
	gainIcon.shadow = "http://wwww.gaincapital.com/resources/images/maps/map_shadow.png";
	gainIcon.iconSize = new GSize(30, 31);
	gainIcon.iconAnchor = new GPoint(15, 31);
	markerOptions = { icon:gainIcon };
	
	
	geocoder.getLatLng(mapAddress, function(point) {
		if (!point) {
			alert(mapAddress + " not found");
		} else {
			map.setCenter(point, 16);
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);
		}
	});
	
	popLayer.show();
}





function include(file) {
   if (document.createElement && document.getElementsByTagName) {
     var head = document.getElementsByTagName('head')[0];

     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
     script.setAttribute('src', file);

     head.appendChild(script);
   } else {
     alert('Your browser can\'t handle the DOM standard.');
   }
 }

