var scrollSpeed = 8000;
var scrollingSpeed = 100;
var scrollBannerCounter = 1;
var bannerImageCounter = 0;
var bannerImageName = "Podcast_ban1.gif";

function cutTooLongLabel(str) {
    if (str.length > 100) {
        str = str.substr(0, 97);
        var i = 97;
        var x = 97;
        while (i > 0) {
            if (str.charAt(i) == ' ') {
                x = parseInt(i);
                str = str.substr(0, x);
                break;
            }
            i = i - 1;
        }
        str = str + "...";
    }
    return str;
}

function scrollObj(newTitleUrl, newReelID, newPVID, newDoctorName, newDateLocation, isNewSystem, pvName, windowHeight, contentID, redLabel, isDedicated, type, unid) {
    this.titleUrl = cutTooLongLabel(newTitleUrl);
    this.reelID = newReelID;
    this.PVID = newPVID;
    this.doctorName = cutTooLongLabel(newDoctorName);
    this.dateLocation = cutTooLongLabel(newDateLocation);
    this.isNewSystem = isNewSystem;
    this.pvName = pvName;
    this.windowHeight = windowHeight;
    this.redLabel = redLabel;
    this.contentID = contentID;
    this.isDedicated = isDedicated;
    this.type = type;
    this.unid = unid;
}

function setStaticScrollerContent() {
    var newItem;
    newItem = "<br/><br/><center><a style='color: #0878AC; text-decoration: underline' href='http://www.peerviewpress.com' target='_blank'>www.peerviewpress.com</a></center>";

    if (IE) {
        scrollDiv.innerHTML = newItem;
    } else {
        document.getElementById("scrollDiv").innerHTML = newItem;
    }
}

function changeScrollItem(scrollIndex) {
    var newItem;
    var i = scrollIndex;
    // this will test if the banner will be displayed
    if (scrollBannerCounter % 6 == 0) {
        bannerImageCounter++;
        newItem = buildTrackingURL((bannerImageCounter % 14) + 1);
    } else {
        if (scrollingLinksArray[i].type == 'FastCast') {
            newItem = getFastcastURL(scrollIndex);
        } else {
            newItem = getInsightURL(scrollIndex);
        }
    }
    if (IE) {
        scrollDiv.innerHTML = newItem;
    } else {
        document.getElementById("scrollDiv").innerHTML = newItem;
    }
}

function getFastcastURL(scrollIndex) {
    var item = "<br/><a href='#' style='color: #0878AC; text-decoration: underline' onClick='DisplaySlide( \"slideContent\", \"" + scrollingLinksArray[scrollIndex].reelID + "\", \"" + scrollingLinksArray[scrollIndex].PVID + "\", 785 , 580  , 1 , 2000 , 1, " + (scrollingLinksArray[scrollIndex].isNewSystem ? 2 : 1)+ ", pviReferenceID, 1," + scrollingLinksArray[scrollIndex].isDedicated + ");'>" + scrollingLinksArray[scrollIndex].titleUrl + "</a>";
    item += "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[scrollIndex].redLabel + "</font></b>";
    item += "<br/><b>" + scrollingLinksArray[scrollIndex].doctorName + "</b><br/>";
    item += scrollingLinksArray[scrollIndex].dateLocation + "</font><br/>";

    return item;
}

function getInsightURL(scrollIndex) {
    var unid = scrollingLinksArray[scrollIndex].unid;
    var insightBaseURL = "http://ww3.peerviewpress.com/gpimgs/fastcast/";
    var item = '<br/><a href="#" style="color: #0878AC; text-decoration: underline" onClick="openContent(null,null,\'' + insightBaseURL + '/' + unid + '/' + unid + '.html\',\'insight\')">' + scrollingLinksArray[scrollIndex].titleUrl + '</a>';
    item += "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[scrollIndex].redLabel + "</font></b>";
    item += scrollingLinksArray[scrollIndex].dateLocation + "</font><br/>";

    return item;
}


function logScrollingItem(contentid) {
    if (thisHost.indexOf("docguide.com") == -1) {
        Image1 = new Image(1, 1)
        Image1.src = "/fc/LogScrollingItem.do?cid=" + contentid + "&ref=" + pviReferenceID + "&rand=" + Math.round((Math.random() * 9) + 1);
    }
}

function scrollItem(nextIndex) {
    var newItem;
    switch (scrollFrame) {
        case 1:
            newItem = "<br/><a href='#' style='color: #0878AC; text-decoration: underline' onClick='DisplaySlide( \"slideContent\", \"" + scrollingLinksArray[currentScrollIndex].reelID + "\", \"" + scrollingLinksArray[currentScrollIndex].PVID + "\", 785 , 580 , 1 , 2000 , 1, " + (scrollingLinksArray[currentScrollIndex].isNewSystem ? 2 : 1)+ ", pviReferenceID, 1)'>" + scrollingLinksArray[currentScrollIndex].titleUrl + "</a>";
            newItem = newItem + "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[currentScrollIndex].redLabel + "</font></b>";
            newItem = newItem + "<br/><b>" + scrollingLinksArray[currentScrollIndex].doctorName + "</b><br/>";
            newItem = newItem + scrollingLinksArray[currentScrollIndex].dateLocation + "</font><br/>";
            break;
        case 2:
            newItem = "<br/><b>" + scrollingLinksArray[currentScrollIndex].doctorName + "</b><br/>";
            newItem = newItem + scrollingLinksArray[currentScrollIndex].dateLocation + "</font>";
            newItem = newItem + "<br/><a href='#' style='color: #0878AC; text-decoration: underline' onClick='DisplaySlide( \"slideContent\", \"" + scrollingLinksArray[nextIndex].reelID + "\", \"" + scrollingLinksArray[nextIndex].PVID + "\", 785 , 580  , 1 , 2000 , 1, " + (scrollingLinksArray[nextIndex].isNewSystem ? 2 : 1)+ ", pviReferenceID, 1)'>" + scrollingLinksArray[nextIndex].titleUrl + "</a></b>";
            newItem = newItem + "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[currentScrollIndex].redLabel + "</font></b>";
            break;
        case 3:
            newItem = "<br/>" + scrollingLinksArray[currentScrollIndex].dateLocation + "</font>";
            newItem = newItem + "<br/><a href='#' style='color: #0878AC; text-decoration: underline' onClick='DisplaySlide( \"slideContent\", \"" + scrollingLinksArray[nextIndex].reelID + "\", \"" + scrollingLinksArray[nextIndex].PVID + "\", 785 , 580  , 1 , 2000 , 1, " + (scrollingLinksArray[nextIndex].isNewSystem ? 2 : 1)+ ", pviReferenceID, 1)'>" + scrollingLinksArray[nextIndex].titleUrl + "</a></b>";
            newItem = newItem + "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[currentScrollIndex].redLabel + "</font></b>";
            newItem = newItem + "<br/><b>" + scrollingLinksArray[nextIndex].doctorName + "</b><br/>";
            break;
        case 4:
            newItem = "<br/><a href='#' style='color: #0878AC; text-decoration: underline' onClick='DisplaySlide( \"slideContent\", \"" + scrollingLinksArray[nextIndex].reelID + "\", \"" + scrollingLinksArray[nextIndex].PVID + "\", 785 , 580 , 1 , 2000 , 1, " + (scrollingLinksArray[nextIndex].isNewSystem ? 2 : 1)+ ", pviReferenceID, 1)'>" + scrollingLinksArray[nextIndex].titleUrl + "</a>";
            newItem = newItem + "&nbsp;&nbsp;<b><font color='red'>" + scrollingLinksArray[nextIndex].redLabel + "</font></b>";
            newItem = newItem + "<br/><b>" + scrollingLinksArray[nextIndex].doctorName + "</b><br/>";
            newItem = newItem + scrollingLinksArray[nextIndex].dateLocation + "</font><br/>";

            intervalID = setInterval('switchContent();', scrollSpeed);
            currentScrollIndex = nextIndex;
            logScrollingItem(scrollingLinksArray[currentScrollIndex].contentID);
            clearInterval(switchIntervalID);
            break;
    }
    if (IE) {
        scrollDiv.innerHTML = unescape(newItem);
    } else {
        document.getElementById("scrollDiv").innerHTML = unescape(newItem);
    }
    scrollFrame = scrollFrame + 1;
}

function switchContent() {
    scrollBannerCounter++;
    //step forward
    switchBanner();
    if (showScrollerItems) {

        if (scrollBannerCounter % 6 == 0) {
            clearInterval(intervalID);
            if (currentScrollIndex > scrollingLinksArray.length - 1) currentScrollIndex = 0;
            changeScrollItem(currentScrollIndex);
            intervalID = setInterval('switchContent();', scrollSpeed);
            logScrollingItem(scrollingLinksArray[currentScrollIndex].PVID, "PVP");

        } else {
            nextIndex = currentScrollIndex + 1;
            if (nextIndex > scrollingLinksArray.length - 1) nextIndex = 0;
            if (scrollingLinksArray.length > 1) {
                clearInterval(intervalID);
                scrollFrame = 1;
                switchIntervalID = setInterval('scrollItem(' + nextIndex + ');', scrollingSpeed);
            }
            reelID = scrollingLinksArray[currentScrollIndex].reelID;
            PVID = scrollingLinksArray[currentScrollIndex].PVID;
        }


    } else {
        setStaticScrollerContent();
    }
}

function next() {
    scrollBannerCounter++;
    //step forward when click next
    clearInterval(intervalID);
    currentScrollIndex = currentScrollIndex + 1;
    if (currentScrollIndex > scrollingLinksArray.length - 1) currentScrollIndex = 0;
    changeScrollItem(currentScrollIndex);
    intervalID = setInterval('switchContent();', scrollSpeed);
    logScrollingItem(scrollingLinksArray[currentScrollIndex].PVID, "PVP");
}


function previous() {
    scrollBannerCounter--;
    //step backward when click back
    clearInterval(intervalID);
    currentScrollIndex = currentScrollIndex - 1;
    if (currentScrollIndex < 0) currentScrollIndex = scrollingLinksArray.length - 1;
    changeScrollItem(currentScrollIndex);
    intervalID = setInterval('switchContent();', scrollSpeed);
    logScrollingItem(scrollingLinksArray[currentScrollIndex].PVID, "PVP");
}

function switchBanner() {
    var newItem;
    if (scrollBannerCounter % 6 == 0) {
        clearInterval(intervalID);
        newItem = buildTrackingURL((bannerImageCounter % 14) + 1);
        if (IE) {
            scrollDiv.innerHTML = unescape(newItem);
        } else {
            document.getElementById("scrollDiv").innerHTML = unescape(newItem);
        }
        intervalID = setInterval('switchBanner();', scrollSpeed);
    }

}


function buildTrackingURL2(count) {
    var number;
    var newItem;
    if (count == 1) {
        number = 14776;
    } else if (count == 2) {
        number = 14777;
    } else if (count == 3) {
        number = 14778;
    } else if (count == 4) {
        number = 14779;
    } else if (count == 5) {
        number = 14780;
    }
    bannerImageName = "Podcast_ban" + (count) + ".gif";
    newItem = "<div style=\"padding-top: 11px;\"><a href='http://ww3.peerviewpress.com/gpc.nsf/doc?CreateDocument&n=" + number + "&id=48DDE4A73E09A969852568880078C249&t=00-00-00;AM&l=Other&u=http://www.peerviewpress.com/Podcast.do'><IMG SRC='/podcast/images/" + bannerImageName + "' align='abstop' name='scrollImage' width='365' height='55' border='0'></a></div>";
    return newItem;

}

function buildTrackingURL(count) {
    var number;
    var newItem;

    switch (count) {
        case 1:
            number = 14776;
            bannerImageName = "PA04373FC_Banner.gif";
            destination = "http://www.peerviewpress.com/n177";
            break;
        case 2:
            number = 14777;
            bannerImageName = "PA04309FC_Banner.gif";
            destination = "http://www.peerviewpress.com/n178";
            break;
        case 3:
            number = 14778;
            bannerImageName = "PA04357FC_Banner.gif";
            destination = "http://www.peerviewpress.com/n175";
            break;
        case 4:
            number = 14779;
            bannerImageName = "InSight-banners_n162.gif";
            destination = "http://www.peerviewpress.com/n162";
            break;
        case 5:
            number = 14780;
            bannerImageName = "InSight-banners_n165.gif";
            destination = "http://www.peerviewpress.com/n165";
            break;

        case 6:
            number = 14781;
            bannerImageName = "PA03866FC-n163.gif";
            destination = "http://www.peerviewpress.com/n163";
            break;
        case 7:
            number = 14782;
            bannerImageName = "PA03868FC-n166.gif";
            destination = "http://www.peerviewpress.com/n166";
            break;
        case 8:
            number = 14783;
            bannerImageName = "PA03647FC_n161.gif";
            destination = "http://www.peerviewpress.com/n161";
            break;
        case 9:
            number = 14784;
            bannerImageName = "PA03601FC-n164.gif";
            destination = "http://www.peerviewpress.com/n164";
            break;
        case 10:
			number = 14785;
			bannerImageName = "PA03944FC_Banner.gif";
			destination = "http://www.peerviewpress.com/n171";
			break;
		case 11:
			number = 14786;
			bannerImageName = "PA04017FC_Banner.gif";
			destination = "http://www.peerviewpress.com/n174";
			break;
		case 12:
			number = 14787;
			bannerImageName = "PA03916FC_InSight_Banner.gif";
			destination = "http://www.peerviewpress.com/n172";
			break;
		case 13:
			number = 14788;
			bannerImageName = "PA03946FC_Banner.gif";
			destination = "http://www.peerviewpress.com/n167";
			break;
		case 14:
			number = 14789;
			bannerImageName = "InContext-banner_Template.gif";
			destination = "http://www.peerviewpress.com/c290";
			break;
		
    }

    newItem = "<div style=\"padding-top: 11px;\"><a href='http://ww3.peerviewpress.com/gpc.nsf/doc?CreateDocument&n=" + number + "&id=48DDE4A73E09A969852568880078C249&t=00-00-00;AM&l=Other&u=" + destination + "'><IMG SRC='http://www.peerviewpress.com/podcast/images/" + bannerImageName + "' align='abstop' name='scrollImage' width='365' height='55' border='0'></a></div>";
    return newItem;

}
	
