var vcCurrentPosition = 0;
var vcCurrentOffset = 0;
var vcVideoCount = 0;
var vcDistance = 102;
var vcScrollSpeed = 10;
var vcScrollInterval = 0;

function debugText(txt) {
	document.getElementById("debugText").innerHTML = txt;
}

function vcScrollUntil() {
	if (vcCurrentOffset < vcNewOffset) {
		var intScrollSegment = Math.ceil((vcNewOffset - vcCurrentOffset)/vcScrollSpeed);
		vcCurrentOffset += intScrollSegment;
		document.getElementById("mediaCarouselPlatform").style.cssText = "margin-left:" + vcCurrentOffset + "px";
	} else if (vcCurrentOffset > vcNewOffset) {
		var intScrollSegment = Math.ceil((vcCurrentOffset - vcNewOffset)/vcScrollSpeed);
		vcCurrentOffset -= intScrollSegment;
		document.getElementById("mediaCarouselPlatform").style.cssText = "margin-left:" + vcCurrentOffset + "px";
	} else {
		clearInterval(vcScrollInterval);
		reloadAd();
	}
}

function vcScrollToNewPosition() {
	clearInterval(vcScrollInterval);
	vcScrollInterval = setInterval(vcScrollUntil, 50);
}


function vcIgnore() {
	return false;
}

function vcGoLeft() {
	if (vcCurrentPosition > 0) {
		vcCurrentPosition--;
		vcNewOffset = vcCurrentOffset + vcDistance;
		//vcScrollToNewPosition();
		vcCurrentOffset += vcDistance;
		document.getElementById("mediaCarouselPlatform").style.cssText = "margin-left: " + vcCurrentOffset + "px;";
		if (typeof(window['gDomain']) != 'undefined'){
			dcsMultiTrack('DCSext.VirtualEvent', '1', 'DCSext.rChannel','Inline Video Carousel','WT.cg_n','Inline Video - Carousel','WT.cg_s','','DCSext.DartZone','','DCSext.ContentType','Video');
		}
	}
	vcCheckButtons();
}

function vcGoRight() {
	if ((vcCurrentPosition+3) < vcVideoCount) {
		vcCurrentPosition++;
		vcNewOffset = vcCurrentOffset - vcDistance;
		//vcScrollToNewPosition();
		vcCurrentOffset -= vcDistance;
		document.getElementById("mediaCarouselPlatform").style.cssText = "margin-left: " + vcCurrentOffset + "px;";
		if (typeof(window['gDomain']) != 'undefined'){
			dcsMultiTrack('DCSext.VirtualEvent', '1', 'DCSext.rChannel','Inline Video Carousel','WT.cg_n','Inline Video - Carousel','WT.cg_s','','DCSext.DartZone','','DCSext.ContentType','Video');
		}		
	}
	vcCheckButtons();
}

function vcCheckButtons() {
	document.getElementById("mediaCarouselBtnL").className = "vcActive";
	document.getElementById("mediaCarouselL").onmousedown = buttonPush;
	document.getElementById("mediaCarouselBtnR").className = "vcActive";
	document.getElementById("mediaCarouselR").onmousedown = buttonPush;
	if (vcCurrentPosition == 0) {
		document.getElementById("mediaCarouselBtnL").className = "vcInactive";
		document.getElementById("mediaCarouselL").onmousedown = vcIgnore;
	} else if (vcCurrentPosition == (vcVideoCount - 3)) {
		document.getElementById("mediaCarouselBtnR").className = "vcInactive";
		document.getElementById("mediaCarouselR").onmousedown = vcIgnore;
	}
}

function buttonPush(e) {
	if (!e) var e = window.event;
	if (e.button != 2) {
		if (document.getElementById(this.id).className == "") 
			document.getElementById(this.id).style.cssText = "margin: 2px 0 0 2px;";
	}
}

function buttonRelease(e) {
	if (!e) var e = window.event;
	if (e.button != 2) {
		document.getElementById(this.id).style.cssText = "";
	}
}

function vcInitButtons() {
	document.getElementById("mediaCarouselL").onclick = vcGoLeft;
	document.getElementById("mediaCarouselL").oncontextmenu = vcIgnore;
	document.getElementById("mediaCarouselL").onmousedown = buttonPush;
	document.getElementById("mediaCarouselL").onmouseup = buttonRelease;
	document.getElementById("mediaCarouselR").onclick = vcGoRight;
	document.getElementById("mediaCarouselR").oncontextmenu = vcIgnore;
	document.getElementById("mediaCarouselR").onmousedown = buttonPush;
	document.getElementById("mediaCarouselR").onmouseup = buttonRelease;
}

function vcInit(intNumberOfVideos) {
	vcInitButtons();
	vcVideoCount = intNumberOfVideos;
}

if (document.getElementById("mediaCarouselL")) {
	vcInitButtons();
}
