/*
 * The following allows backgroundPositionY to be used in animate
 */
(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);

// Get the current value of a background position as an integer (for internet explorer...)
function ieGetPositionValue(coordinate, position){
	var positionInt;
	if(position == 'x'){

	} else if (position == 'y'){
		
	} else {
		return false;
	}
}

// Get current background position value and return it as an integer
function getPositionValue(coordinates, position){
	var positionInt;
	if(position == 'x'){
		var coordinatesChopped = coordinates.split(" ");
		positionInt = coordinatesChopped[0].split("px");
	} else if(position == 'y'){
		var coordinatesChopped = coordinates.split(" ");
		positionInt = coordinatesChopped[1].split("px");
	} else if (position == 'ie'){
		positionInt = coordinates.split("px");
	} else {
		return false;
	}
	return parseInt(positionInt[0]);
}

$(window).load(function(){
    
    // Fade in images so there isn't a color "pop" document load and then on window load
    $('.team_member').fadeIn(500);
    
    //alert($('.team_member').css('backgroundPositionY'));
    /*if($.browser.msie){
    	alert($('.team_member').css('backgroundPositionY'));
    	//alert(getPositionValue($('figure').children('.team_member').css('backgroundPositionY'), 'ie'));
    } else {
    	alert(getPositionValue($('.team_member').css('backgroundPosition'), 'x'));
    }*/
    
    // Bind mouse events for each team member
	$('.team_member').each(function(i){
		//$(this).attr('class', 'figure'+i);
		var positionX;
		var positionY;
    	if($.browser.msie){
    		positionX = getPositionValue($(this).css('backgroundPositionX'), 'ie');
    		positionY = getPositionValue($(this).css('backgroundPositionY'), 'ie');
    	} else {
    		positionX = getPositionValue($(this).css('backgroundPosition'), 'x');
    		positionY = getPositionValue($(this).css('backgroundPosition'), 'y');
    	}
		$(this).bind('mouseenter', function(){
		    $(this).stop().animate({backgroundPosition: positionX+" "+(positionY - 10)+"px", opacity: 1}, {duration: 180});
		});
		$(this).bind('mouseleave', function(){
		    $(this).delay(200).animate({backgroundPosition: positionX+' '+positionY+'px', opacity: 0.84}, {duration:180});
		});
	});
	
	// Bind mouse events for each team navigation item
	$('#team_nav').children('li').each(function(i){
		var thisClass = $(this).attr("class");
		//$('.team').children().css({display: 'block'});
		$(this).bind('click', function(){
			//$('.team').children('li').children('figure').children('div :not(.'+thisClass+', .image_separator)').animate({opacity: '0.4'});
			//$('.team').children('li').children('figure').children('div :not(.'+thisClass+', .image_separator)').animate({backgroundPosition: '0 0'}, {duration:180});
			//$('.team').children().not('.'+thisClass).animate({opacity: '0.4'});
			$('#team_nav').children().removeClass('active');
			if($(this).hasClass("all_team")){
				$('.team').children().css({display: 'inline-block'});
			} else {
				$('.team').children().css({display: 'inline-block'});
				$('.team').children().not('.'+thisClass).css({display: 'none'});
			}
			$(this).addClass('active');
			/* Toggle active class below */
		});
	});
});
