        $(document).ready(function() {

            $('.twitter').hover(function() {
                $('.twitter').stop().animate({ bottom : "-4px"}, 100);
                return false;
            }, function() {
                $('.twitter').stop().animate({ bottom : "-60px"}, 600);
                return false;
            });
            // Navigation Hovers
            $('header nav li a').css({backgroundPosition : '0px -75px'});
            $('header nav li a').hover(function() {
                $(this).stop().animate({ backgroundPosition : '0 0'}, 90);
            }, function() {
                $(this).stop().animate({ backgroundPosition : '0 -75px'}, 90);
            });
            // Navigation Active Trail Fixes & Hovers
            $('header nav li a.active-trail').css({backgroundPosition : '0px 0px'});
            $('header nav li a.active-trail').hover(function() {
                $(this).stop().css({color : '#FFFFFF'}).animate({ backgroundPosition : '0 0'}, 90);
            }, function() {
                $(this).stop().css({color : '#000000'}).animate({ backgroundPosition : '0 -75px'}, 90);
            });
            // Social Links Hover
            $('#social a').fadeTo('fast', 0.5);
            $('#social a').hover(function() {
                $(this).stop().fadeTo('fast', 1.0);
            }, function() {
                $(this).stop().fadeTo('fast', 0.5);
            });
            
            // Initiate colorbox for quickview so something happens while ajax is going
            $('.work-lightbox a').click(function(){
            	$('#cboxOverlay').fadeTo('fast',0.85);
            });
                       
    		// Let's do some cool stuff on the work details page
   			var quickTab = $('#quicktabs-portfolio_gallery .quicktabs-tabpage').length;
   			if ( quickTab > 1 ) {
   				// Do Nothing
   			}
   			else {
   			   	$('#quicktabs-portfolio_gallery #quicktabs-tabpage-portfolio_gallery-1').removeClass('quicktabs-hide');
   			   	$('#quicktabs-portfolio_gallery .quicktabs-tabs li').addClass('active');
   			}
      
			// Client Logo Hovers
 			$('.client-logo').hover(function() {
 				$(this).find('.client-logo-bw').stop(true,true).fadeOut(); 				
 				$(this).find('.client-logo-color').stop(true,true).fadeIn();
 			}, function() {
 				$(this).find('.client-logo-color').stop(true,true).fadeOut();
 				$(this).find('.client-logo-bw').stop(true,true).fadeIn();  			
 			});
 			
 			// Client Tooltips
 			$('.view-client-content-bk li a').hover(function(){
 				var clientText = $(this).text();
 				$(this).parent().append("<span class='client-tooltip'>"+clientText+"</span>");
 				$(this).parent().find('span').fadeTo('fast',.65);	
 			}, function() {
 				$(this).parent().find('span').fadeTo('fast',0);
 				$(this).parent().find('span').remove();	
 			});
            // Quicktabs Hide
            
            $('#quicktabs-portfolio_gallery').delay(300).fadeTo('fast',1.0);
            $('#quicktabs-case_study_gallery').delay(300).fadeTo('fast',1.0);
            
            
            
            // Logo Fade
            $('.inner #logo').append('<span class="logo-over"></span>');
            $('.inner #logo').hover(function() {
                $('.inner #logo .logo-over').stop().fadeTo('fast', 1.0);            
                $('.inner #logo img').stop().fadeTo('fast', 0);
            }, function() {
                $('.inner #logo .logo-over').stop().fadeTo('fast', 0);            
                $('.inner #logo img').stop().fadeTo('fast', 1.0);
            });
            // Disable link on past employees
			$('.former-employee a').contents().unwrap();
            
            // Hide random <br> tags in DT views
            $('.view-display-id-block br').remove();

			// Work Filters & Page
				// Set our UL variables
				$('.menu-block-2').find('ul:first').attr('id','parentFilter');
				$('.menu-block-2').find('ul ul').addClass('childFilter');
				
				
				// Rewrite the Portfolio Links to Anchors
            	$(".menu-block-2 a").each(function() {
              	  this.href = this.href.replace("http://kps3.com/work/portfolio/", "#");
            	});
				// Inject All Value for Work Types
				$('#parentFilter').prepend('<li class="all"><a href="#">All Types of Work</a></li>');
				$('.childFilter').prepend('<li class="childAll"><a class="active" href="#">Pick a subcategory</a></li>');
				// Hide Values Except First Value on Work Type
				$('#parentFilter li a').hide();
				// Show First Value in Work Type
				$('#parentFilter li.all a').addClass('active').show();
				
				
				// Show Rest of Menu on Hover
				$('#parentFilter li a').not('.childFilter li a').hover(function() {
					$('#parentFilter li a').not('.childFilter li a').show().width('232px');
				}, function() {
					$('#parentFilter li a').not('.active').not('.childFilter a').hide();
				});
				
				
				$('#parentFilter li a').click(function() {
					$('.childFilter li a').hide();
					$('.childFilter').hide();
					$('.childFilter .childAll a').removeClass('active');
					$('#parentFilter li a').removeClass('active');
					$(this).not('.childFilter li a').addClass('active');
					$(this).next('.childFilter').show();					
					$(this).next('.childFilter').find('.childAll a').show();
					$('#parentFilter li a').not('.active').not('.childFilter li a').hide();
					$('.childFilter .childAll a').addClass('active');
				}, function() {
					return false;
				});
				
				$('.childFilter .childAll a').hover(function() {
					$(this).removeClass('active');
					$(this).addClass('active');
					$('.childFilter li a').show();
					}, function() {
					$('.childFilter li a').not('.active').hide();								
				});
				
				$('.childFilter li a').hover(function() {
					$('.childFilter li a').show();
					}, function() {
					$('.childFilter li a').not('.active').hide();
					if($('.childFilter li a').not($('.childFilter .childAll a')).hasClass('active')) { $('.childFilter .childAll a').hide(); } 
				else {$(this).addClass('active').show(); }			
				});				
						
				
				$('.childFilter li a').click(function() {
					$(this).show();
					$(this).parents('ul').show();
					$('.childFilter li a').removeClass('active');
					$(this).addClass('active');
					$('.childFilter li a').not('.active').hide();
					$(this).parents('ul').prev('a').show().addClass('active');
				}, function() {
					return false;
				});
				
				$('.childFilter .childAll a').click(function() {
					(this).addClass('active');
					}, function() {
					});
				
				
				$('#parentFilter li.all a').click(function() {
					$('.childFilter').hide();
				}, function() {
					return false;
				});	

			// Rewrite Clients 
			$('.view-id-clients li a').each(function() {
				var dirtyText = $(this).text();
				var cleanText = $(this).text().replace(/\s+/g, '-').toLowerCase();
				$(this).addClass(cleanText);
			});
 			
 			$('#block-views-portfolio-block').delay(350).fadeTo('normal',1.0);

			// Rewrite Tags to Class on Portfolio Samples 			
 			$('.portfolio-item-detail').each(function() {
// 				var regularText = $(this).find('.regular-text').text();
 				var filterClass = $(this).find('.regular-text').text().replace('+', '').replace('--','-').replace(/\s+/g, '-').replace(/\@/g,' ').toLowerCase();
 				$(this).find('.work-image-wrap').addClass(filterClass);
 			});
 			$('.regular-text').remove();

			
			// Hovers on Work Portfolio Pieces
			$('.work-image-wrap').hover(function() {
				$(this).find('span').find('a').stop().fadeTo('normal',1.0);
			}, function() {
				$(this).find('span').find('a').stop().fadeTo('normal',0);
			});

            // Rewrite Portfolio Menus
            $(".menu-block-2 ul li a").click(function() {
                var filterVal = $(this).text().replace('+', ' ').replace(/\s+/g, '-').toLowerCase();
				
				if(filterVal == 'all-types-of-work') { $('.portfolio-item-detail').parent().fadeOut().delay(250).fadeIn(250); } 
								
				else {

                	$('.portfolio-item-detail .work-image-wrap').each(function() {
                    	if (!$(this).hasClass(filterVal)) {
                        	$(this).parent().parent().fadeOut(250);
                    	} else {
                        	$(this).parent().parent().delay(250).fadeIn();
                    	}
                	});
  
            	} }, function() {
            		
            		return false;            			
              });
				
			// Let's write the ribbon image in for case studies
			$('.portfolio-item-detail .case-study').prepend('<span class="ribbon"></span>');	
			
			// Let's add a case study filter button
			$('#block-menu-block-2').prepend('<a class="case-study-filter" href="#">Just Show Me The Case Studies</a>');
			
			// Let's show only case studies if that button is clicked and set the other filters back to their starting positions.
			$('.case-study-filter').click(function() {
				$("#parentFilter li a").hide();
				$("#parentFilter li.all a").show();
				$(".childFilter li a").hide();
				$(".childFilter li.childAll a").show();				
				$('.portfolio-item-detail .work-image-wrap').each(function() {
				if (!$(this).hasClass('case-study')) {
					$(this).parent().parent().fadeOut(250);
					} else {
					$(this).parent().parent().delay(250).fadeIn();
					}
				});
			}, function(){
			
				return false;
			});	

										
				// Display the block on load
				$('.menu-block-2').fadeIn('slow');

           $('.tactic-fixed-item').hover(function() {
               $(this).find('.hint').stop().fadeTo('fast', .6).removeClass('hidden');
           }, function() {
              $(this).find('.hint').stop().fadeTo('fast', 0).addClass('hidden');
            });
            $('#block-views-kps3-news-block .view-kps3-news .views-row').hover(function() {
                $(this).find('img').fadeTo('slow', 0.2);
                $(this).find('.views-field-title a').fadeIn('slow');
                $(this).find('.views-field-body').fadeIn('slow');
                $(this).find('.blog-read-more a').fadeIn('slow');
            }, function() {
                $(this).find('img').fadeTo('slow', 1.0);
                $(this).find('.views-field-title a').fadeOut('slow');
                $(this).find('.views-field-body').fadeOut('slow');
                $(this).find('.blog-read-more a').fadeOut('slow');
            });
            $('.portfolio-gallery div').cycle({
				fx: 'fade',
				speed: 'slow',
				timeout: 3000,
				next: '#portfolio-next',
				prev: '#portfolio-prev',
				pauseOnHover: true,
				pause: 1
			});		
 			if(window.location.hash) {
		     	 var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
		     	 var urlFilterVal = hash.indexOf("/");
		     	 var reFilteredVal = hash.substring(urlFilterVal + 1);
		     	 
		     	$('.portfolio-item-detail .work-image-wrap').each(function() {
                   	if (!$(this).hasClass(reFilteredVal)) {
                       	$(this).parent().parent().fadeOut(250);
                    	} else {
                       	$(this).parent().parent().delay(250).fadeIn();
                 	}
              	}); 
		     	 
			  } else {
			     return false;
			  }    

        });

(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);
