var a_current, a_currentParent, a_currentParentPos, a_currentIndex, a_textItem, a_imageContext, a_textContext, a_animating = false;

var f_current, f_currentIndex, f_Pos, f_textItem, f_backgroundPos, f_animating = false;

function registerSliderEvents (imageContext, textContext) {
	a_imageContext = jQuery(imageContext);
	a_textContext = jQuery(textContext);
	jQuery("div img", a_imageContext).click(function() {
		if(a_animating) {
			return;
		}
		a_current = this;
		a_currentParent = jQuery(a_current).parent();
		a_currentIndex = -1;
		
		var tmpWidth = 0;
		jQuery("div img", a_imageContext).each(function(indx, el) {
			if(el != a_current) {
				tmpWidth += jQuery(el).width();
				jQuery(el).fadeOut();
			} else { 
				a_currentIndex = indx;
				a_currentParentPos = tmpWidth;
			}
		});
		jQuery(a_currentParent).animate(
			{ left : '0px' },
			"slow",
			"linear",
			function() {
				a_textItem = jQuery("div", a_textContext).eq(a_currentIndex);
				jQuery(a_textItem)
					.css('left', jQuery(a_currentParent).width() + 10 + "px")
					.css('top', "0px")
					.fadeIn("slow");
			}
		);
	});
}

function resetSlider () {
	if(a_animating) {
		return;
	}
	a_animating = true;
	jQuery(a_textItem).fadeOut("slow", function() {
		jQuery(a_currentParent).animate(
			{ left : a_currentParentPos + 'px' },
			"slow",
			"linear",
			function () {
				jQuery("div img:hidden", a_imageContext).each(function(indx, el) {
					jQuery(el).fadeIn("normal", function() {
						a_animating = false;
					});
				});
			}
		);
	});
}

function registerFreelancerEvents() {
	jQuery("#freelancers div.freelancer").click(function() {
		if(f_animating) {
			return;
		}
		if(f_current == this) {
			return;
		}
		f_animating = true;
		f_current = this;
		f_Pos = 0;
		var tmpWidth = 0;
		jQuery("#freelancers div.freelancer").each(function(indx, el) {
			if(el != f_current) {
				jQuery(el).fadeOut("fast");
				tmpWidth += 236;
			} else { 
				f_currentIndex = indx;
				f_Pos = tmpWidth;
			}
		});
		jQuery("#freelancers").animate(
			{ "height" : "438px" }
			, "slow"
			, "linear");
			
		f_backgroundPos = jQuery(f_current).css("background-position");
		
		jQuery(f_current).animate(
			{ left : "0px" },
			"slow",
			"linear",
			function() {
				jQuery(f_current).animate(
					{ backgroundPosition: "0px 0px", width : "293px", height : "438px" },
					"slow",
					"linear",
					function() {
						f_textItem = jQuery("#freelancer-text div").eq(f_currentIndex);
						f_animating = false;
						jQuery(f_textItem)
							.css('left', "303px")
							.css('top', "-5px")
							.fadeIn("slow");
					}
				);
			}
		);
		
	});
}

function resetFreelancers () {
	if(f_animating) {
		return;
	}
	f_animating = true;
	jQuery(f_textItem).fadeOut("slow", function() {
		jQuery(f_current).animate(
			{ backgroundPosition: f_backgroundPos, width : "200px", height : "200px" },
			"slow",
			"linear",
			function () {
				jQuery("#freelancers").animate(
				{ "height" : "200px" }
				, "slow"
				, "linear");
				jQuery(f_current).animate(
					{ left : f_Pos + 'px' },
					"slow",
					"linear",
					function() {
						jQuery("#freelancers div.freelancer:hidden").each(function(indx, el) {
							jQuery(el).fadeIn("normal", function() {
								f_animating = false;
								f_current = undefined;
							});
						});
					}
				);
			}
		);
	});
}
