// ============================================== //

		// EASING FUNCTIONS //

// ============================================== //

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing, {
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		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;
	},
	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;
	}
});

(function($){
	
	// ============================================== //

		// TOOL TIP FUNCTIONS //

	// ============================================== //
	
	$.fn.setToolTip = function(isClient){
		
		var offsetBorder = 550;
		
		if ($('#people_list').length) {  
			offsetBorder = 0;
		};
		
		showToolTip = function(target, ttLeft, ttTop, isClient){
				//
				if(isClient){
					var container = $('#tt_client_container');
				}else{
					var container = $('#tt_container');
				}
				//
				if($.browser.msie){
				}else{
					container.stop().animate({'opacity':0},0.001);
				};	
				//
				container.find('div.tt_content').html(target.data('tt_html'));
				//
				var winOffSet = $('#master').offset();
				var winOffSetLeft = winOffSet.left;	
				var linkWidth = target.width();
				var linkHeight = target.height();
				var windowWidth = container.width();
				var windowHeight = container.height();
				if(isClient){
					var topPos = ttTop-windowHeight-10;
				}else{
					var topPos = ttTop-70+(linkHeight/2);
				}
				//
				var mOffset = $('#master').offset();
				//
				
				if((ttLeft-mOffset.left)<offsetBorder){
					if(isClient){
						container.css({'left':(ttLeft), 'top': topPos+10});
						container.removeClass('tt_container_client');
						container.addClass('tt_container_client_left');
					}else{	
						container.removeClass('tt_container_basic');
						container.addClass('tt_container_basic_left');
						container.css({'left':(ttLeft+linkWidth), 'top': topPos+10});
					};
				}else{
					if(isClient){
						container.css({'left':(ttLeft-windowWidth+(linkWidth/2)), 'top': topPos+10});
						container.removeClass('tt_container_client_left');
						container.addClass('tt_container_client');
					}else{	
						container.removeClass('tt_container_basic_left');
						container.addClass('tt_container_basic');
						container.css({'left':(ttLeft-windowWidth), 'top': topPos+10});
					};
				};
				//
				if ($('#people_list').length) {
					$('#tt_container').removeClass('tt_container_basic').addClass('tt_container_thin');
					//alert(ttLeft);
					container.css({'left':(ttLeft-220), 'top': topPos+20});
				};
				//
				//
				if($.browser.msie){
					container.stop().show();
				}else{
					container.stop().show().animate({
					  opacity: 1,
					  top: '-=10'
					},200);
				};	
		};
		
		hideToolTip = function(isClient){
			if(isClient){
				var container = $('#tt_client_container');
			}else{
				var container = $('#tt_container');
			}
			
			if($.browser.msie){
				container.hide();
			}else{
				container.stop().animate({
					'opacity': 0,
					top: '-=10'
				},300, function(){
					$(this).hide();
				});
			};	
		};
		
		return this.each(function(){
			var target = $(this);
			//
			var targetText = target.attr('title');
			target.data('tt_html', targetText).attr('title', '');
			//
			target.mouseenter(function(){
				var aOffSet = target.offset();
				//
				showToolTip($(this), aOffSet.left, aOffSet.top, isClient);	
			}).mouseleave(function(){
				hideToolTip(isClient);					   
			}).bind('click', function(){
				if($(this).attr('href')=='#'){
					return false;	
				};
			});
		});
		
	};
	
	$.fn.initToolTip = function(){
		var toolTipHtml = '<div id="tt_container" class="tt_container_basic"><div class="tt_content"><\/div><div class="tt_base"><\/div><\/div>';
		$('body').prepend(toolTipHtml);
		var target = $('div#tt_container')
		//target.find('div.tt_content').html('<h3>Belinda</h3><p>25 years in the PR business. Admires creativity, passion and ambition. Dislikes complacency.</p>')
		if($.browser.msie){
			target.hide();
		}else{
			target.animate({'opacity':0},0.001);
		};
		$('a.tt_link').each(function(i){
			$(this).setToolTip();
		});
		//
		if($('#services_list')){
			var hasSubUl = false;
			//
			$('#services_list').find('li').each(function(i) {
				//
				$(this).find('a').mouseenter(function(){
					var tipId = '#'+$(this).attr('title');
					$(tipId).trigger('mouseenter');	
					$(this).find('span').css({'display': 'inline'});
				}).mouseleave(function(){
					var tipId = '#'+$(this).attr('title');
					$(tipId).trigger('mouseleave');
					$(this).find('span').css({'display': 'none'});
				}).bind('click', function(){
					return false;
				});
				//
				if($(this).find('ul').length){
					$(this).find('a').mouseenter(function(){
						$(this).css({'text-decoration': 'underline'});
					}).mouseleave(function(){
						$(this).css({'text-decoration': 'none'});
					});
					hasSubUl=true;
				};
				//
				if(i==$('#services_list').find('li').length-1){
					if(hasSubUl){
						$().initImagePane($('#services_list'));
					};
				};								 
			});
		};
	};

	// ============================================== //

		// IMAGE PANE FUNCTIONS //

	// ============================================== //
	
	$.fn.initImagePane = function(target){		
		//
		_getPageSize = function() {var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;};var windowWidth, windowHeight;if (self.innerHeight) {if(document.documentElement.clientWidth){windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;};windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;};if(yScroll < windowHeight){pageHeight = windowHeight;} else { pageHeight = yScroll;};if(xScroll < windowWidth){	pageWidth = xScroll;		} else {pageWidth = windowWidth;};arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};
		//
		_getPageScroll = function() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) {yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) {yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;};arrayPageScroll = new Array(xScroll,yScroll);return arrayPageScroll;};
		//
		////
		//
		positionPane = function(){
			// Get page sizes
			var arrPageSizes = _getPageSize();
			// Style overlay and show it
			$('#jquery-overlay').css({
				width:		arrPageSizes[0],
				height:		arrPageSizes[1]
			});
			// Get page scroll
			var arrPageScroll = _getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + ((arrPageSizes[3]-$('#lightbox-container').height())/2),
				left:	arrPageScroll[0]
			});
			//
		};
		
		showImagePane=function(target){
			$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container"><div id="lightbox-content"><\/div><\/div><\/div>');
			//
			var arrPageSizes = _getPageSize();
			//
			$('#jquery-overlay').css({
				backgroundColor:	'#fff',
				opacity:			0,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).animate({opacity: 0.7}, 250);
			// Get page scroll
			var arrPageScroll = _getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 5),
				left:	arrPageScroll[0]
			}).show();
			//
			$('#lightbox-container').click(function() {
				return false;						
			});
			//
			$(window).resize(function() {
				positionPane();
			});
			var t;
			$(window).scroll(function () { 
				clearTimeout(t);
				t = setTimeout(function() {positionPane()}, 300); 
			});
			//
			// remove first LI if it has text
			if(target.parent().find('ul').find('li').eq(0).find('h3').length > 0){	
				var thisImageText = target.parent().find('ul').find('li').eq(0).html();
				target.parent().find('ul').data('imageText', thisImageText).find('li').eq(0).remove();
			};
			var imageText = target.parent().find('ul').data('imageText')
			//
			var imageContent = '<div id="lightbox-images"><ul>'+target.parent().find('ul').html()+'<\/ul><\/div>';
			$('#lightbox-content').html(imageContent+imageText);
			//
			if(target.parent().find('ul').find('li').length > 1){
				$('#lightbox-images').after('<div id="lightbox_scroll_nav"><p><a href="#" title="View previous image" class="prev"><\/a><\/p><p><a href="#" title="View next image" class="next"><\/a><\/p><\/div>');
				setPaneBrowser();
			};
			$('#lightbox-content').append('<a href="#" title="Close window" class="paneClose"><\/a>');
			// Assigning click events in elements to close overlay
			$('#jquery-overlay,#jquery-lightbox,#lightbox-container-content a,a.paneClose').click(function() {
				clearTimeout(t);
				closeImagePane();
				return false;	
			});
			//
			$('#lightbox-content').hide();
			positionPane();
			$('#lightbox-container').animate({width: 530},function() {
				$('#lightbox-content').show();
			});
			//
		};
		//
		closeImagePane=function(){
			$('#jquery-lightbox').remove();
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
			//
			$(window).unbind('resize');
			$(window).unbind('scroll');
		};
		//
		setPaneBrowser = function(){
			var target = $('#lightbox-images').find('ul');
			var liTotal = target.find('li').size();
			var liWidth = target.find('li').eq(0).width();
			var ulWidth = liWidth*liTotal;
			//
			target.css('width' , ulWidth);
			target.data('currentState', 0);
			//
			$('a.next').bind('click', function(){
				scrollImage(1);
				return false;
			});
			$('a.prev').bind('click', function(){
				scrollImage(-1);
				return false;
			}).hide();
			//
			scrollImage = function(direction){
				var scrollState = target.data('currentState')+direction
				target.data('currentState', scrollState);
				var scrollPos = -1*(scrollState*liWidth);
				//
				target.animate({left: scrollPos}, 800, 'easeInOutExpo');
				//
				if(scrollState==0){
					$('a.prev').hide();
				}else{
					$('a.prev').show();
				};
				if(scrollState==(liTotal-1)){
					$('a.next').hide();
				}else{
					$('a.next').show();
				};
			};
		};	
		//
		target.find('li').find('ul').each(function(i) {
			//var thisHtml = $(this).parent().find('a').eq(0).html()+' <span style="display:none;">- click for more<\/span>';
			//$(this).parent().find('a').eq(0).html(thisHtml);
			//
			$(this).parent().find('a').bind('click', function(){
				showImagePane($(this));
				return false;
			});
		});
		//
	};
	
	// ============================================== //

		// PEOPLE PAGE FUNCTIONS //

	// ============================================== //
	
	$.fn.initPeopleViewer = function(){
		return this.each(function(){
			var target = $(this);
			// number of li's in list
			var liTotal = target.find('li').size();
			var liWidth = target.find('li').eq(0).width();
			var ulWidth = liWidth*liTotal;
			//
			var liGroupTotal = Math.ceil(liTotal/4);
			var liGroupWidth = liWidth*4;
			//
			target.find('ul').css('width' , ulWidth);
			target.find('ul').data('currentState', 0);
			//
			target.find('li').each(function(i) {
				var colourPath = $(this).find('a').attr('rel');				
				var colourHtml = '<img src="'+colourPath+'" class="people_colour" />'
				$(this).find('a').prepend(colourHtml);
				//
				var htmlContent = $(this).find('a').attr('title');
				$(this).find('a').data('people_content', htmlContent);
				$(this).find('a').attr('title', '');
				//
				target.find('img.people_colour').animate({'opacity':0},0.001, function(){
					$(this).show();
				});
				//
				////class='intro'
				//
				$(this).find('a').mouseenter(function(){
					$(this).find('img').eq(0).animate({'opacity':1},200);		   
				}).mouseleave(function(){
					if($(this).attr('class')!='people_active'){
						$(this).find('img').eq(0).animate({'opacity':0},200);	
					}
				}).bind('click', function(){
					$('#people_text').html($(this).data('people_content'));
					$('#people_main').attr('src', $(this).attr('href'));
					$(this).parent().parent().find('a.people_active').removeClass('people_active').find('img').eq(0).animate({'opacity':0},200);
					$(this).addClass('people_active');
					return false
				});
			});
			//
			if(liTotal>4){
				$('#people_list').after('<div id="people_scroll_nav"><p><a href="#" title="View more people" class="prev"></a></p><p><a href="#" title="View more people" class="next"></a></p></div>');	
			};
			//
			$('a.next').bind('click', function(){
				scrollPeople(1);
				return false;	
			});
			$('a.prev').bind('click', function(){
				scrollPeople(-1);
				return false;	
			});
			scrollPeople = function(direction){
				var scrollState = target.find('ul').data('currentState')+direction
				target.find('ul').data('currentState', scrollState)
				var scrollPos = -1*(scrollState*liGroupWidth);
				target.find('ul').animate({left: scrollPos}, 800, 'easeInOutExpo');
				//
				if(scrollState==0){
					$('a.prev').hide();
				}else{
					$('a.prev').show();
				}
				if(scrollState==(liGroupTotal-1)){
					$('a.next').hide();
				}else{
					$('a.next').show();
				}
				//
			};
			// set first in list
			target.find('li').eq(0).find('a').addClass('people_active').find('img').eq(0).animate({'opacity':1},0.001);
			//
			//target.find('ul').animate({left: -liGroupWidth}, 800, 'easeInOutExpo');
		});
	};
	
	
	// ============================================== //

		// CLIENTS TICKER TAPE FUNCTIONS //

	// ============================================== //
	
	$.fn.initTicker = function(){
		
		return this.each(function(){
			var target = $(this);
			var ulWidth = 0;
			var liHtml = target.find('ul').html();
			var tickerSpeed = 60000;
			//
			target.find('li').each(function(i){
				ulWidth+=$(this).width();
			});
			target.find('ul').css({'width':(ulWidth*2)});
			target.find('ul').html(liHtml+liHtml);
			//
			target.animate({scrollLeft: 0}, 0.001, 'linear');
			//
			scrollTicker = function(tickerSpeed){
				target.animate({scrollLeft: ulWidth}, tickerSpeed, 'linear', function(){
					target.animate({scrollLeft: 0}, 0.001, 'linear', function(){
						scrollTicker(tickerSpeed);
					});																   
				});
			};
			//
			
			target.mouseenter(function(){
				target.stop();
			}).mouseleave(function(){
				var speedRatio = tickerSpeed*(target.scrollLeft()/ulWidth);
				var newSpeed = Math.round(tickerSpeed-speedRatio);
				scrollTicker(newSpeed);
			});
			
			//
			scrollTicker(tickerSpeed);
			$().initTickerToolTip();
			//
			
			var clientLength = $('#client_content div.client_container div.client_logos').length;
			
			if(clientLength>1){
				//
				var tempHtml = '<div id="client_scroll_nav"><p><a href="#" class="prev" title="View previous clients">Prev<\/a><\/p><p><a href="#" class="next" title="View more clients">Next<\/a><\/p><\/div>'
				$('#client_content div.client_container').append(tempHtml);	
				$('a.prev').hide();
				$('#client_content div.client_container div.client_logos').hide();
				$('#client_content div.client_container div.client_logos').eq(0).show();
				//
				$('a.next').bind('click', function(){
					showClient(1);
					return false;	
				});
				$('a.prev').bind('click', function(){
					showClient(-1);
					return false;	
				});
				//
				$('#client_content').data('currentState', 0)
				//
				showClient = function(direction){
					var scrollState = $('#client_content').data('currentState')+direction;
					$('#client_content').data('currentState', scrollState);
					// show client
					$('#client_content div.client_container div.client_logos').hide();
					$('#client_content div.client_container div.client_logos').eq(scrollState).show();
					//
					if(scrollState==0){
						$('a.prev').hide();
					}else{
						$('a.prev').show();
					}
					if(scrollState==(clientLength-1)){
						$('a.next').hide();
					}else{
						$('a.next').show();
					}
				};
			};

		});
		
	};
	
	$.fn.initTickerToolTip = function(){
		var toolTipHtml = '<div id="tt_client_container" class="tt_container_client"><div class="tt_content"><\/div><div class="tt_client_base"><\/div><\/div>';
		$('body').prepend(toolTipHtml);		
		var target = $('div#tt_container')
		if($.browser.msie){
			target.hide();
		}else{
			target.animate({'opacity':0},0.001);
		};
		$('#client_ticker ul li a').each(function(i){
			$(this).setToolTip(true);
		});
	};
	
		  
})(jQuery);

//
$(document).ready(function(){
	if($('a.tt_link').length) { 
		$().initToolTip();	
	};
	if($('#people_list').length){
		$('#people_list').initPeopleViewer();
	};
	if($('#client_ticker').length){
		$('#client_ticker').initTicker();
	};
});



