/* the "tape" div that goes through the ticker */
var innerDiv;
var newLeft;
var leftOffset = 5;

/**
 * shiftInner is a recursive function that moves the ticker
 * 
 * @param {Integer} distance - Distance in pixels to move the ticker every shift
 * @param {Integer} time - Time in milliseconds to shift between ticker elements 
 * @param {Integer} pause - Time in milliseconds to wait at a ticker element
 */
function shiftInner(distance, time, pause) {		
	innerDiv.fadeIn(time / 2, function() {
		$(this).animate({borderWidth: "0px"}, pause, function() {
			if (jQuery.support.opacity) {
				$(this).fadeOut(time / 2, function(){
					if (newLeft <= innerDiv.width() * -1 + distance + leftOffset) 
						newLeft = leftOffset;
					else 
						newLeft = $(this).css("left").replace("px", "") - distance;
					
					$(this).animate({
						left: newLeft
					}, 0, function(){
						shiftInner(distance, time, pause);
					});
				});
			} else {
				if (newLeft <= innerDiv.width() * -1 + distance + leftOffset) 
					newLeft = leftOffset;
				else 
					newLeft = $(this).css("left").replace("px", "") - distance;
				
				$(this).animate({
					left: newLeft
				}, 0, function(){
					shiftInner(distance, time, pause);
				});
			}
		});				
	});	
}

function internalAnimate($box, expand) {
	if ($box.attr("id") == "1") {
		if (expand == true) {
			$box.find("p.lie_first").stop(true).animate({
				width: "290px"												
			}, 250, function() {
				$box.find(".lie_second").stop(true).slideDown(250);	
			});	
			
			$box.find("#lie img").css("margin-left", "8px");	
		} else {
			$box.find("p.lie_first").stop(true).animate({
				width: "240px"						
			}, 250);
			
			$box.find(".lie_second").stop(true, true).hide();
			$box.find("#lie img").css("margin-left", "11px");
		}
	} else if ($box.attr("id") == "2") {
		if (expand == true) {
			$box.find("p").stop(true).animate({
				width: "270px",
				fontSize: "11px",				
				marginTop: "25px"				
			});
		} else {
			$box.find("p").stop(true).animate({
				width: "240px",
				fontSize: "11px",
				marginTop: "0px"				
			});
		}
	} else if ($box.attr("id") == "3") {
		if (expand == true) {
			$box.find(".job_top").stop(true).animate({
				width: "290px",
				marginTop: "5px",
				marginBottom: "1px"
			}, 250, function() {				
				$box.find(".job_slider").stop(true).slideDown(250);
			});					
		} else {
			$box.find(".job_top").stop(true).animate({
				width: "240px",
				marginTop: "5px"				
			}, 100);
					
			$box.find(".job_slider").stop(true, true).hide();
		}	
	}
}

$(document).ready(function() {	
	var startWidth = parseInt($(".aBox").css("width").replace("px", ""));
	var startHeight = parseInt($(".aBox").css("height").replace("px", ""));
	var startContHeight = parseInt($(".aBox .pb_content").css("height").replace("px", ""));
	var startLeft = parseInt($(".aBox").css("left").replace("px", ""));
	var startTop = parseInt($(".aBox").css("top").replace("px", ""));							
	
	var growHeight = 40;
	var growWidth = 40;
	var shrinkWidth = growWidth / 2;																				
	
	var animateTime = 200;
					
	/* some box init */
	$(".job_slider").hide();
	$(".lie_second").hide();
					
	$(".aBox").hover(function() {					
		$(this).stop(true).siblings().stop(true).animate({
			width: startWidth - shrinkWidth + "px",
			height: startHeight + "px",
			top: startTop + "px"
		}, animateTime).find(".pb_content").stop(true).animate({
			height: startContHeight + "px"
		}, animateTime);
		
		$(this).animate({
			width: startWidth + growWidth + "px",
			height: startHeight + growHeight + "px",
			top: startTop - (growHeight / 2) + "px" 
		}, animateTime, internalAnimate($(this), true)).find(".pb_content").stop(true).animate({
			height: startContHeight + growHeight + "px"
		}, animateTime);	
									
	}, function() {				
		$(this).animate({
			width: startWidth + "px",
			height: startHeight + "px",
			top: startTop + "px"			
		}, animateTime, internalAnimate($(this), false)).find(".pb_content").animate({
			height: startContHeight + "px"
		}, animateTime);;									
		
		$(this).siblings().animate({ 
			width: startWidth + "px",
			height: startHeight + "px",
			top: startTop + "px"
		}, animateTime);
	});				
	
	var liWidth = 0;				
					
	$("#newsbar ul").wrap("<div class=\"nb_container\"></div>").wrap("<div class=\"nb_inner\"></div>");				
	
	innerDiv = $("div.nb_inner");
	
	innerDiv.css("visibility", "visible");
									
	/* get the total width of the LI elements */
	$("#newsbar li").each(function() {
		liWidth += $(this).width();		
	});
	
	innerDiv.css("width", liWidth).css("left", leftOffset);
																	
	shiftInner(940, 1000, 3000);
});			
