(function($){
	$(document).ready(function(){
		if ( $('.consultant-popup .gfield input').length != 0 ) {
			$('.consultant-popup .gfield input').each(function(){
				var val = $(this).val();
				if ( val != '' ) {
					$(this).attr('title', val).addClass('blink');
				};
			})
		};
		// Blinking Fields
		$('.blink').live('focus', function() {
		        if(this.title==this.value) {
		            this.value = '';
		        }
		    }).live('blur', function(){
		        if(this.value=='') {
		            this.value = this.title;
		        }
		    });
		
		$('#navigation > div > ul > li').each(function(){
			$(this).find('ul > li:last').addClass('last');
			if ( $(this).find('.dd-wrapper').length != 0 ) {
				var wrap = $(this).find('.dd-wrapper');
				var width = $(this).outerWidth();
				width = Math.round((width - 4) / 2);
				var right = width - 45;
				right = ( $(this).find('> a').text() == 'Company' )? right + 6 : right;
				$(wrap).css('right', right + 'px');
			};
		})
		$('#navigation > div > ul > li').hover(function(){
			if ( $('#navigation > div > ul > li.current-menu-item, #navigation > div > ul > li.current_page_ancestor').not($(this)).length != 0 ) {
				$('#navigation > div > ul > li.current-menu-item, #navigation > div > ul > li.current_page_ancestor').not($(this)).addClass('nofold');
			};


			$('#navigation > div > ul > li').removeClass('hover');
			$(this).addClass('hover');
			if ( $(this).find('.dd-wrapper').length != 0 ) {
				$(this).find('.dd-wrapper').show();
			};
		}, function(){
			$('#navigation > div > ul > li').removeClass('nofold').removeClass('hover');
			if ( $(this).find('.dd-wrapper').length != 0 ) {
				$(this).find('.dd-wrapper').hide();
			};
		})
		$('#secondary-menu ul > li:last, .home-callouts ul > li:last').addClass('last');
		$('.home-callouts ul > li:first').addClass('first');

		// Homepage Sliders
		$('#slider, .logo-rotator').jcarousel({
			auto: 4,
			scroll: 1,
			visible: 1,
			wrap: "both",
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		
		$('.external-link a, .view-site').click(function(){
			var href = $(this).attr('href');
			window.open(href,'externalsitewindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes');
			return false;
		})
		
		// Our Expertise hover 
		$('.our-expertise td').hover(function(){
			$(this).find('.description').stop(true,true).fadeToggle(500);
			$(this).find('span').css('background-position', '0 bottom');
		});

		if ( $('#fader .item').length > 1 ) {
			// Homepage Fader
			$('#fader .item').simpleFade({
				speed: 500,
				duration: 4000,
				onFade: function(i){
					var menu_color = $('#fader .item:eq(' + i + ')').data('menu-color');
					$('#navigation > div').removeClass().addClass('item-' + (i + 1));
				}
			});
		} else if ( $('#fader .item').length == 1 ) {
			$('#navigation > div').removeClass().addClass('item-1');
		};

		


		// Sidebar Archive
		$('#sidebar .list-2 li.main > a').click(function(){
			if ( $(this).parent().find('ul').length) {
				$(this).parent().find('ul').slideToggle();
				$(this).parent().toggleClass('main-active');
				return false;
			} else return;
		});

		if ( $('#sidebar .theme-widget-archives a.active').length != 0 ) {
			$('#sidebar .theme-widget-archives a.active').parents('.main').find('ul').slideToggle();
			$('#sidebar .theme-widget-archives a.active').parents('.main').toggleClass('main-active');
		};

		$('#sidebar ul.widgets ul').each(function(){
			$(this).find('> li:last').addClass('last');
		});
		$('#sidebar ul.widgets > li:last').addClass('widget-last');
		
		$('.free-consult-popup').click(function(){
			var windowHeight = $(window).height();
			var elementWidth = $(this).width();
			var positionY = ($(this).offset().top) - 300; // 315px - popup height, ~15px shadows
			var positionX = (Math.floor($(this).offset().left)) + elementWidth;
			var focusPopup = (Math.floor(windowHeight - positionY))/2;
			var target = $(this).attr('href');
			
			$('<div id="overlay"></div>').appendTo('body');
			if ( $('body').hasClass('iOS') ) {
				$('#overlay').css('height', $(document).height());
			};
			
			$(target).css({
				top: positionY,
				left: positionX
			}).fadeIn();
			
			return false;
		});
		
		$('.consultant-popup .close').click(function(){
			$(this).parents('.consultant-popup').fadeOut(function(){
				$('#overlay').fadeOut(function(){
					$(this).remove();
				});
			});
			return false;
		});
	});

	$(window).load(function(){
		// Home Expertise
		$('.home-expertise .slider').smoothDivScroll({
			autoScrollDirection: "backandforth",
			autoScrollStep: 1, 
			autoScrollInterval: 5,	
			visibleHotSpots: "always",
			scrollStep: 5,
			mouseDownSpeedBooster: 1
		});
		$('.home-expertise .slider .item:last').addClass('last');


		$('.home-expertise .slider img').hover(function(){
			var halfElemHeight = $(this).height()/2;
			$(this).css('top', halfElemHeight * -1);
		}, function(){
		
			$(this).css('top',0);
		});

	});
})(jQuery)
