﻿jQuery.noConflict();
jQuery(document).ready(function(){
	initNav();
	patchSitemapLayout();
	clearInputs();
	initTabs();
	initPage();
	initCufon();
	initHover('ul.tabset li');
	initHover('.text-container .links-list li');
	initCollapsable(jQuery);
	jQuery('div.gallery-holder').galleryCircle({
		btPrev: '.prev',
		btNext: '.next',
		holderList: '.gallery',
		scrollElParent: 'ul',
		scrollEl: 'li',
		step: true,
		numHolder: false,
		numCreate: false,
		easing: 'linear',
		switchTime: 7000,
		duration : 700
	});
	mobileNav();
	jQuery('.lightbox-opener').simplebox();

	// IMM120810 >>> Required for popup login box on Limited Partners page
	if (jQuery("#lightbox-login").length > 0)
		jQuery('#lb-login-link').click();
	// <<< IMM120810
	// IMM121310 >>> Added to handle form submission for webpdf
	/* IMM010611 >>> Removing form submission code...
	jQuery("ul.pdf li").click( function()
	{
		jQuery("#form1").submit();
	});
	// <<< IMM010611 */
	// <<< IMM121310
	
	jQuery(function() {
        jQuery('div.slogan-box').cycle({
        fx: 'fade',
        speed: 600,
        timeout: 5000,
        sync: false
     });
    });

});

function mobileNav() {
	if (jQuery.browser.mobile && document.getElementById) {
		window.onscroll = function() {
		  document.getElementById('footer').style.position =
			 'absolute';
		  document.getElementById('footer').style.top =
			 (window.pageYOffset + window.innerHeight - 83) + 'px';
		};
	}
}

function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}

function patchSitemapLayout() {
	jQuery('ul#sitemap-list li ul li ul').append('<div style="clear: left;"></div>');
}

function initNav(){
	var set = jQuery('#nav');
	var setHieght = set.parents('div.nav-section').outerHeight(true);
	var winHieght = jQuery(window).height();
	var link = jQuery('#nav > li');
	var dropBox = link.find('div.drop');
	var subLink = dropBox.find('ul > li a');
	var f = true;
	var _href;
	var ind;
	var navPos;
	link.hover(
		function(){
			var _this = jQuery(this);
			if ( !_this.hasClass('hover') ){
				_this.addClass('hover')
			}
		},
		function(){
			var _this = jQuery(this);
			_this.removeClass('hover');
			link.eq(ind).find('div.drop').hide();
			f = true;
		}
	)
	function dropHide(){//don't think this is needed sm_com @todo
		if(f){
			link.eq(ind).find('div.drop').show();
			f = false;
		}
		else {
			link.eq(ind).find('div.drop').hide();
			f = true;
		}
	}
	if( jQuery('body').hasClass('index') ){//homepage
		var dropBox = link.find('div.drop').hide();
		function linkHref(){
			window.location = _href;
		}
		link.bind('click', function(){
			ind = link.index(jQuery(this));
			dropHide();
			return false;
		})
		subLink.bind('click', function(){
			var _this = jQuery(this);
			_this.closest('div.drop').hide();//sm_com added this line to hide the submenu before animating
			_href = jQuery(this).attr("href");
			navPos = winHieght;//sm_com changed this from winHieght - setHeight
			set.parents('div.nav-section').animate({ top: navPos}, { queue: false, duration: 500, complete: function(){
					link.eq(ind).find('div.drop').hide();//don't think this is needed sm_com @todo
					f = true;
					linkHref();
				}
			});
			jQuery('body').css({overflow:'hidden'});//sm_com added this to avoid scrollbar
			return false;
		});
	}else if( jQuery('body').hasClass('fromHome') ){//interior page
		//animate it in if it's hidden on fromHome
		jQuery('div.footer-holder').animate({ height:'83px', top:0 } , 1000 );
	}
}

function initCufon() {
	Cufon.replace('.heading-block h1', { fontFamily: 'Gotham Light' });
	Cufon.replace('.info-line h2', { fontFamily: 'Gotham Book' });
	Cufon.replace('#sidebar .title-block h3', { fontFamily: 'Gotham Book' });
	Cufon.replace('.blockquote-holder blockquote q', { fontFamily: 'Gotham Book Light' });
	Cufon.replace('ul.tabset li a', { fontFamily: 'Gotham Book' });
	Cufon.replace('ul.tabset li a.active', { fontFamily: 'Gotham Bold' });
	Cufon.replace('#main .text-container .info-text', { fontFamily: 'Gotham Book Light' });
	Cufon.replace('.text-container .links-list li a', { fontFamily: 'Gotham Book' });
	Cufon.replace('.text-container .links-list li a.current', { fontFamily: 'Gotham Bold' });
	Cufon.replace('.text-container .links-list li a.section', { fontFamily: 'Gotham Bold' });
	Cufon.replace('.gallery li .name', { fontFamily: 'Gotham Medium' });
	Cufon.replace('.gallery li .department', { fontFamily: 'Gotham Light' });
	Cufon.replace('.heading', { fontFamily: 'Gotham Book' });
	Cufon.replace('.text-block .status-info dt', { fontFamily: 'Gotham Bold' });
	Cufon.replace('.text-block .status-info dd', { fontFamily: 'Gotham Book' });
	Cufon.replace('.contact-holder .info-title', { fontFamily: 'Gotham Bold' });
	Cufon.replace('div.industryTeamMembersDiv div.industryTeamMember div.name', { fontFamily: 'Gotham Book' });
	Cufon.replace('.managedNewsletter h1', { fontFamily: 'Gotham Light' });
	Cufon.replace('.managedNewsletter h2', { fontFamily: 'Gotham Light' });
	Cufon.replace('body.managedNewsletter div.sidebarRight div.sidebarRightContentAreaSectionTitle', { fontFamily: 'Gotham Light' });
}

function initHover(h_list, h_class){
	if(!h_class) var h_class = 'hover';
	jQuery(h_list).hover(function(){
		jQuery(this).addClass(h_class);
		Cufon.replace('ul.tabset li.hover a', { fontFamily: 'Gotham Bold' });
	}, function(){
		jQuery(this).removeClass(h_class);
		Cufon.replace('ul.tabset li a', { fontFamily: 'Gotham Book' });
		Cufon.replace('ul.tabset li a.active', { fontFamily: 'Gotham Bold' });
	});
	if(!h_class) var h_class = 'hover';
	jQuery(h_list).hover(function(){
		jQuery(this).addClass(h_class);
		Cufon.replace('.text-container .links-list li.hover a', { fontFamily: 'Gotham Bold' });
	}, function(){
		jQuery(this).removeClass(h_class);
		Cufon.replace('.text-container .links-list li a', { fontFamily: 'Gotham Book' });
		Cufon.replace('.text-container .links-list li a.current', { fontFamily: 'Gotham Bold' });
	});
}

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";

							// refresh cufon
							Cufon.replace('ul.tabset li a', { fontFamily: 'Gotham Book' });
							Cufon.replace('ul.tabset li a.active', { fontFamily: 'Gotham Bold' });
							return false;
						}
					}
				}
			}
		}
	}
}

function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

/*--- gallery function ---*/
jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.prev',
		btNext: 'a.next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		numHolder: false,
		numCreate: false,
		step: false,
		innerMargin: 0,
		curPage: false,
		onClick: null,
		easing: 'linear',
		switchTime: false,
		duration : 1500
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _next = jQuery(_options.btNext, _this).length ? jQuery(_options.btNext, _this) : false;
		var _prev = jQuery(_options.btPrev, _this).length ? jQuery(_options.btPrev, _this) : false;
		var _holderList = jQuery(_options.holderList, _this);
		var _scrollElParent = jQuery(_options.scrollElParent, _holderList);
		var _scrollEl = jQuery(_options.scrollEl, _scrollElParent);
		var _numHolder = false ;
		if (_options.numHolder) _numHolder = jQuery(_options.numHolder, _this).length ? jQuery(_options.numHolder, _this) : false;
		var _step, _t = null;
		var _widthSum = 0;
		_scrollEl.each(function(){_widthSum += jQuery(this).outerWidth(true);})
		var _startPosition = _scrollEl.index(_scrollEl.filter('.active'));
		if (_startPosition==-1) _startPosition=0;
		_scrollEl.removeClass('active');
		var _easing = _options.easing;

		if (!_options.step) _step = _holderList.innerWidth();
		var _margin = _widthSum;
		_scrollElParent.append(_scrollEl.clone(true));
		_scrollElParent.prepend(_scrollEl.clone(true));

		var _offsetStartPosition =0;
		_offsetStartPosition = culcOffset(_startPosition);

		_scrollElParent.css('marginLeft', (-_margin+_options.innerMargin-_offsetStartPosition));

		//auto rotation
		if (_options.switchTime) {
			_t = setTimeout(function(){
				nextSlides();
			},_options.switchTime);
		}
		
		//button next "click"
		if (_options.btNext) {
			_next.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					nextSlides();
				}
				return false;
			});
		}

		//button prev "click"
		if (_options.btPrev) {
			_prev.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					prevSlides();
				}
				return false;
			});
		}

		//curent position
		function getCurElIndex(){
			var _curMargin = parseInt(_scrollElParent.css('marginLeft')) + _widthSum - _options.innerMargin;
			for(i=0; i < _scrollEl.length; i++){
				if (_curMargin == 0) return i;
				if (_curMargin <= _options.innerMargin) _curMargin += _scrollEl.eq(i).outerWidth(true);
				else _curMargin -= _scrollEl.eq(i).outerWidth(true);
				if (_curMargin == _options.innerMargin) return i+1;
			}
		}

		// offset of gallery if when activ element not first at start 
		function culcOffset(_ind){
			var _tmpcounter=0;
			var _pos=0;
			while (_tmpcounter < _ind){
				_pos += _scrollEl.eq(_tmpcounter).outerWidth(true);
				_tmpcounter++;
			};
			return _pos;
		}

		//go next slide
		function nextSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				_step = (_scrollEl.eq(_curElIndex).outerWidth(true) * 4);
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin += _step;
			
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin >= _widthSum*2) {
					_margin = _widthSum + (_margin - _widthSum*2);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		//go prev slide
		function prevSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				if (_curElIndex == 0) _curElIndex= _scrollEl.length;
				_step = (_scrollEl.eq(_curElIndex-1).outerWidth(true) * 4);
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin -= _step;
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin < _widthSum) {
					_margin = _widthSum*2 - (_widthSum - _margin);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		// Number list Create
		jQuery.fn.galleryCircle.numListCreate = function(_numHolder, _scrollEl){
			var _numListElC = '';
			for(var i=0; i<_scrollEl.length; i++){
				_numListElC += '<li><a href="">'+(i+1)+'</a></li>';
			}
			jQuery(_numHolder).html('<ul>'+_numListElC+'</ul>');
		};

		// Number list Activate
		jQuery.fn.galleryCircle.numListActive = function(_numHolder, _scrollEl){
			_curElIndex = getCurElIndex();
			if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);
			if (_numHolder) {
				jQuery('a',_numHolder).removeClass('active');
				jQuery('a',_numHolder).eq(_curElIndex).addClass('active');
			}
		};

		//click on control elemens
		function numClick() {
			jQuery(_options.numHolder, _this).find('a').click(function(){
				if (_t) clearTimeout(_t);
				var _aList = jQuery(_options.numHolder, _this).find('a');
				var _index = _aList.index(jQuery(this));
				_margin = _widthSum + _index * _scrollEl.outerWidth(true);
				_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
					if (_margin >= _widthSum*2) {
						_margin = _widthSum + (_margin - _widthSum*2);
					}
					_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
					_aList.removeClass('active').eq(_index).addClass('active');

					//autoslide
					if (_options.switchTime) {
						_t = setTimeout(function(){
							nextSlides();
						},_options.switchTime)
					}
				}});
				return false;
			});
		};

		// init creating num list
		if (_options.numCreate) jQuery.fn.galleryCircle.numListCreate(_numHolder, _scrollEl);

		// pagination first init (example Page 2/6)
		if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);

		// init activate num list item and init numClick()
		if (_options.numHolder) {
			jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);
			numClick();
		}
	});
}
;(function(jQuery) {
	jQuery.fn.simplebox = function(options) {
		return new Simplebox(this, options);
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				duration: jQuery.browser.msie && jQuery.browser.version<9 ? 0 : 300,
				linkClose: 'a.close, a.btn-close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '#wrapper',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = jQuery(context);
			this.select = jQuery(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function($this, el){
			el.click(function(){
				if (jQuery(this).attr('href')) $this.toPrepare(jQuery(this).attr('href'));
				else $this.toPrepare(jQuery(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = jQuery('body').width();
			if (jQuery(this.options.wrapper).width() > this.winWidth) this.winWidth = jQuery(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = jQuery(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if (jQuery.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = jQuery(window).height();
			this.winScroll = jQuery(window).scrollTop();
			
			this.popupTop = this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			this.faderHeight = jQuery(this.options.wrapper).outerHeight();
			if (jQuery(window).height() > this.faderHeight) this.faderHeight = jQuery(window).height();
			
			this.popup.css({
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			this.fader.css({
				width: this.winWidth,
				height: this.faderHeight
			});
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function($this, el, flag){
			el.click(function(){
				jQuery('body > div.outtaHere').removeClass('optionsDivVisible').addClass('optionsDivInvisible')
				$this.popup.fadeOut($this.options.duration, function(){
					$this.popup.css({left: '-99999px'}).show();
					if (jQuery.browser.msie) $this.select.css({visibility: 'visible'});
					$this.submitBtn.unbind('click');
					$this.fader.unbind('click');
					$this.btnClose.unbind('click');
					jQuery(window).unbind('resize');
					if (flag) $this.fader.fadeOut($this.options.duration);
					else {
						if ($this.submitBtn.attr('href')) $this.toPrepare($this.submitBtn.attr('href'));
						else $this.toPrepare($this.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function ($this){
			$this.fader.fadeIn($this.options.duration, function(){
				$this.popup.fadeIn($this.options.duration);
			});
			jQuery(window).resize(function(){
				$this.calcWinWidth();
				$this.popup.animate({
					left: $this.winWidth/2 - $this.popup.outerWidth(true)/2
				}, {queue:false, duration: $this.options.duration});
				$this.fader.css({width: $this.winWidth});
			});
		},
		initFader: function(){
			if (jQuery(this.options.divFader).length > 0) this.fader = jQuery(this.options.divFader);
			else{
				this.fader = jQuery('<div class="'+this.options.divFader+'"></div>');
				jQuery('body').append(this.fader);
				this.fader.css({
					position: 'absolute',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));

function initCollapsable($) {
	var $collapsables = $('.expandable').hide().addClass('collapsed');
	var $openers = $('.opener').click(function(e) {
		var toggle = $(e.currentTarget).closest('.collapsableToggle');
		var $exp = toggle.siblings('ul.expandable');
		if($exp.hasClass('collapsed')) {
			$exp.slideDown('fast',function() {
				toggle.removeClass('collapsed').addClass('expanded');
				$exp.removeClass('collapsed').addClass('expanded');
			});
		} else {
			$exp.slideUp('fast',function() {
				toggle.addClass('collapsed').removeClass('expanded');
				$exp.addClass('collapsed').removeClass('expanded');
			});
		}
		return false;
	});
}

