var scrollAnim,
	currentIndex = 0,
	scrollTarget = 0,
	numImages = 0,
	imagesPerScreen = 3,
	numScreens = 0;
	//addEventListener = $TORA("common").dom.addEventListener;

$TORA.whenReady(function(){
	var uiReq = $TORA.Requirement("ui");

	uiReq(function(){
		initScroll();
		initControls();
	});
});


// ANIMATION FUNCTIONS

function initScroll(){
	var ui = $TORA("ui"),
		anim = ui.animation,
		dom = $TORA("common").dom,
		getElementsByClassName = dom.getElementsByClassName;
	
	var images = document.getElementById("video-promo-slide");
	numImages = getElementsByClassName.call(images, "related-video").length;
	numScreens = Math.ceil(numImages / imagesPerScreen);
	
	component = ui.UIComponent();
	component.addElement(images);

	var trackInfo = this._$trackInfo = {
			getter: function(htmlElement) {
				return getInteger(htmlElement.style.left);
			},
			setter: function(htmlElement, val) {
				htmlElement.style.left = Math.floor(val)+"px";
			},
			getTarget: function() { return scrollTarget; }
		};

	var clock = anim.Clock(50);

	scrollAnim = anim.TrackingAnimation(component, trackInfo, clock);
	clock.start();
	
    if(currentIndex == 0) {
	  replaceLeft(1);
	}
	if(numScreens == 1) {
	 //replaceLeft(1);
	  replaceRight(1);
	}
};



// CONTROL FUNCTIONS

function initControls(){
	var addEventListener = $TORA("common").dom.addEventListener;
	
	var left = document.getElementById("scroll-left");
	if(left){
		addEventListener.call(left, "mousedown", function(){
			moveLeft();
		});
	}
	
	var right = document.getElementById("scroll-right");
	if(right){
		addEventListener.call(right, "mousedown", function(){
			moveRight();
		});
	}
	
	var replay = document.getElementById("overlay-replay-btn");
	if(replay){
		addEventListener.call(replay, "click", function(){
			playVideo();
		});	
	}
	
	var share = document.getElementById("overlay-share-btn");
	if(share){
		addEventListener.call(share, "click", function(){
			return discoveryShare.share();
		});	
	}
	
	var post = document.getElementById("overlay-facebook-btn");
	if(post){
		addEventListener.call(post, "click", function(){
			postVideoToFacebook();
		});
	}


};

function moveLeft(){
	scroll(1);
}

function moveRight(){
	scroll(-1);
}

function replaceLeft(nums) { //nums is for reverting the images back when scrolling

	if(nums == 1) {
		
		//document.getElementById("scroll-left").style.background = 'url("../images/arrow-left-off.png")';
		document.getElementById("scroll-left").style.visibility = "hidden";
	} else if(nums == 0) {
		
		//document.getElementById("scroll-left").removeAttribute("style"); //style.background = "url('../images/arrow-left.png')";
		document.getElementById("scroll-left").style.visibility = "visible";
	}
}

function replaceRight(nums) {  //nums is for reverting the images back when scrolling
	
	if(nums == 1) {
		//document.getElementById("scroll-right").style.background = "url('../images/arrow-right-off.png')";
		document.getElementById("scroll-right").style.visibility = "hidden";
	} else if(nums == 0) {
		//document.getElementById("scroll-right").removeAttribute("style");//.style.background = "url('../images/arrow-right.png')";
		document.getElementById("scroll-right").style.visibility = "visible";
	}

}

function scroll(to){
	if((currentIndex >= 0 && currentIndex < numScreens-1 && to < 0) 
		|| (currentIndex <= numScreens && currentIndex > 0 && to > 0)){
			
		scrollTarget = scrollTarget + (to * 450);
		currentIndex -= to;
		scrollAnim.start();
		
		if(currentIndex == numScreens - 3) {
			replaceRight(1);
			replaceLeft(0);
		} else if(currentIndex == 0) {
			replaceLeft(1);
			replaceRight(0);
		} else {
			replaceLeft(0);
			replaceRight(0);
		}
	}
}

function getInteger(val) {
	val = parseInt(val);
	return isNaN(val) ? 0 : val;
};

function playVideo(){
	$TORA("video").getPlayer("video-brightcove-player").modules.VIDEO_PLAYER.play();
}



/************************************************************/
$TORA.whenReady(function(){
	var postBtn = document.getElementById("post-vid-to-wall");
	if(postBtn){
		$TORA("common").dom.addEventListener.call(postBtn, "click", function(){
			postVideoToFacebook();
		});
	}
});



function postVideoToFacebook(){
	var title, description, videoWidth, videoHeight, vidSwfSrc, vidImgSrc;

	vidSwfSrc = document.getElementById("vidSwfSrc");
	vidImgSrc = document.getElementById("vidImgSrc");

	var titleNode = document.getElementsByName("title");
	if(titleNode.length)
		title = titleNode[titleNode.length-1].content;

	var descriptionNode = document.getElementsByName("description");
	if(descriptionNode.length)
		description = descriptionNode[descriptionNode.length-1].content;

	var videoWidthNode = document.getElementsByName("video_width");
	if(videoWidthNode.length)
		videoWidth = videoWidthNode[videoWidthNode.length-1].content;

	var videoHeightNode = document.getElementsByName("video_height");
	if(videoHeightNode.length)
		videoHeight = videoHeightNode[videoHeightNode.length-1].content;

	if(title && description && vidSwfSrc && vidImgSrc){
		//we have enough info to post

		var attachment = {};
		var media = {};

		media.type = "flash";
		media.swfsrc = vidSwfSrc.href;
		media.imgsrc = vidImgSrc.href;
		media.expanded_height = videoHeight;
		media.expanded_width = videoWidth;

		attachment.name = title;
		attachment.description = description;
		attachment.media = [media];
		
		/* work around for the ridiculous facebook bug where when the attachment length gets over
		 * a certain amount it switches to a post method and facebook doesnt toString the object properly
		 */
	    attachment.toString = function(){
	        return FB.JSON.stringify(this);
	    };

		var user = $TORA("USER"),
			callback = function() { user.postVideoToFB(attachment); }
		
		if(user.getAuthInfo("HAS_STORY_API")){
			callback(attachment);
		} else {
			$TORA("authentication").fb.logUserIn(user, callback);	
		}
	}
};

