
var allwidth = 0;

function calcWidth() {
	jQuery('#promoimages .promopic').each(function() {
		if (parseInt(jQuery(this).css('top'))==0 || isNaN(parseInt(jQuery(this).css('top')))) { allwidth += jQuery('img',this).attr('width') /*+ 1*/; }
	});
	allwidth -= jQuery('#promoimg').width();
}

$(function() 
{
	jQuery("#promo").remove();
	jQuery("#promoimg").css('visibility','visible');
	
	calcWidth();
		
	var currentPos;
	var speed = 15;
    var promowidth = jQuery('#promoimg').width();
	var maxScroll = allwidth;
	var currentX;
	var middleWidth = 150;
	var move = false;
	var ease = 1;
	var currentDirection;
	var iwanttoexit;
	
	jQuery('#promoimages').mouseenter(function() { move = true; /*hideBorders();*/ });
	
	jQuery('#promoimages').mouseleave(function() { move = false; myFadeIn("#promoimages .promopic img"); /*showBorders();*/ });
	

	
	jQuery('#promoimg').mousemove(function(e){
		currentX = e.pageX - jQuery(this).offset().left;
	});
	
	var timer = setInterval(enterFrame, 1000/30);
	function enterFrame(){
		
		if(move){
                if(currentX < promowidth/2 - middleWidth){
				ease = 1;
				currentDirection = "left";
				scrollContainer(-1);
			}
			else if(currentX > promowidth/2 + middleWidth){
				ease = 1;
				currentDirection = "right";
				scrollContainer(1);
			}
			else{
				if(currentDirection == "left"){
					ease = ease/3;
					scrollContainer(-ease);
				}
				else if (currentDirection == "right"){
					ease = ease/3;
					scrollContainer(ease);
				}
			}
		}
	}
	
	function scrollContainer(direction){

        if(jQuery('#promoimages').position().left < (7 - speed) && direction<0) {
			jQuery('#promoimages').css('left', jQuery('#promoimages').position().left - (direction * speed) + "px" );		
		}
		else if (jQuery('#promoimages').position().left < 7 && direction<0) {
			jQuery('#promoimages').css('left', "7px" );		
		}
		else if (direction>0 && jQuery('#promoimages').position().left > -maxScroll + speed) {
			jQuery('#promoimages').css('left', jQuery('#promoimages').position().left - (direction * speed) + "px" );

		}
		else if (direction>0 && jQuery('#promoimages').position().left > -maxScroll) {
			jQuery('#promoimages').css('left', (-maxScroll-8) );

		}
		else{
			//jQuery('#promoimages').css('left', jQuery('#promoimages').position().left - (direction * speed) + "px" );
		}
	}

	function myFadeOut(el) {
		jQuery(el).stop().animate({'opacity':'0.3'},200);
	}
	
	function myFadeIn(el) {
		jQuery(el).stop().animate({'opacity':'1'},200);
	}
	
	/*function hideBorders() {
		jQuery("#promoimages .promopic").css('borderRightColor','#222');
		jQuery("#promoimages .promopic").css('borderBottomColor','#222');
	}
	
	function showBorders() {
		setTimeout(function() {
			jQuery("#promoimages .promopic").css('borderRightColor','#ccc');
			jQuery("#promoimages .promopic").css('borderBottomColor','#ccc');
			}, 100);
	}*/


	jQuery("#promoimages .promopic img").hover(function() {
		jQuery(this).addClass("showing");
		myFadeOut("#promoimages .promopic img:not(.showing)");
		myFadeIn(this);
	}, function() {
		jQuery(this).removeClass("showing");
	});
	

});

