//global variables
var stop_flag=0;
var speed = 10; //move speed 10 millionseconds
var space = 10; //move width (px) each time
var pagewidth = 650; //page width = image width
var fill = 0; // whole move
var movelock = false;
var MoveObjTimerID;
var direction="right";
var Comp_1 = 0;
var AutoplayObjTimerID=null;

//find document element object by id
function GetObj(objName)
{
	if(document.getElementById)
	  	return eval('document.getElementById("'+objName+'")');
	else
		return eval('document.all.'+objName);
}
/*
************************************
*automatic scroll - default to left
************************************
*/

function AutoPlay()
{
	clearInterval(AutoplayObjTimerID);
	AutoplayObjTimerID=setInterval('Scroll_RightGo();Scroll_RightStop();',7000)
}

/*
************************************
*manually scroll to left
************************************
*/
function Scroll_LeftGo()
{
	if(movelock)
		return;
	clearInterval(AutoplayObjTimerID);
	movelock=true;
	direction="left";
	MoveObjTimerID=setInterval('scrollLeft();',speed);
}

/*
***********************************
*stop manually scroll to left
***********************************
*/
function Scroll_LeftStop()
{
	if(direction == "right")
		return

	clearInterval(MoveObjTimerID);
	if((GetObj('AdContent_ID').scrollLeft-fill)%pagewidth!=0)
	{
		Comp_1=fill-(GetObj('AdContent_ID').scrollLeft%pagewidth);
		CompScr_1();
	}else{
		movelock=false;
	}
	//AutoPlay()
 }

/*
**************************************
*manually scroll left - image move
**************************************
*/
function scrollLeft()
{
	if(GetObj('AdContent_ID').scrollLeft<=0)	
		GetObj('AdContent_ID').scrollLeft=GetObj('AdContent_ID').scrollLeft+GetObj('List1_1').offsetWidth
	
	GetObj('AdContent_ID').scrollLeft-=space
}

/*
*************************************
*manually scroll right
*************************************
*/
function Scroll_RightGo()
{
	clearInterval(MoveObjTimerID);
	if(movelock)
		return;
	clearInterval(AutoplayObjTimerID);
	movelock=true;
	direction="right";
	scrollRight();MoveObjTimerID=setInterval('scrollRight()',speed)
}

/*
***********************************
*stop manually scroll right
***********************************
*/
function Scroll_RightStop(stop)
{
	if(direction == "left")
	   return;
	clearInterval(MoveObjTimerID);
	if(GetObj('AdContent_ID').scrollLeft%pagewidth-(fill>=0?fill:fill+1)!=0){
		Comp_1=pagewidth-GetObj('AdContent_ID').scrollLeft%pagewidth+fill;
		CompScr_1();
	}else{
		movelock=false;
	}

	if(stop>0)
	   stop_flag=stop;

	if(stop_flag == 0)
		AutoPlay();
}

/*
************************************
*manually scroll right-image move
********************************8***
*/

function scrollRight()
{
	if(GetObj('AdContent_ID').scrollLeft>=GetObj('List1_1').scrollWidth)	
		GetObj('AdContent_ID').scrollLeft=GetObj('AdContent_ID').scrollLeft-GetObj('List1_1').scrollWidth
	
	GetObj('AdContent_ID').scrollLeft+=space
}

/*
************************************
*compare scroll
************************************
*/
function CompScr_1()
{
	if(Comp_1==0)
	{
		movelock=false; return
	}
	var num,TempSpeed=speed,TempSpace=space;
	if(Math.abs(Comp_1)<pagewidth/2)
	{
		TempSpace=Math.round(Math.abs(Comp_1/space));
		if(TempSpace<1){TempSpace=1}
	}
	if(Comp_1<0){
		if(Comp_1<-TempSpace)
		{
			Comp_1+=TempSpace;num=TempSpace
		}else{
			num=-Comp_1;Comp_1=0
		}

		GetObj('AdContent_ID').scrollLeft-=num;
		setTimeout('CompScr_1()',TempSpeed)
	}else
	{
		if(Comp_1>TempSpace)
		{
			Comp_1-=TempSpace;num=TempSpace}else{num=Comp_1;Comp_1=0
		}
		GetObj('AdContent_ID').scrollLeft+=num;
		setTimeout('CompScr_1()',TempSpeed)
	}
}

//manipulate ad image array, will be converted for self-service
var AdImgAry = new Array()
function imgObj(src,url, alt){
    this.src =src; 	this.url = url ;this.alt = alt;
}
AdImgAry[0] = new imgObj('/images/siteskin2/ad_carousel/ad_carousel_2009_medicare.gif','http://www.abetterdecision.com','Medicare Programs')
AdImgAry[1] = new imgObj('/images/siteskin2/ad_carousel/ad_carousel_green_changes.gif','/portal/member/content.do?reentry=true&region=SP&mainResourceFile=/content/general/unprotected/html/national/ad_carousel/green_changes.html','Green Changes')
AdImgAry[2] = new imgObj('/images/siteskin2/ad_carousel/ad_carousel_health_net_cup.gif','/portal/member/content.do?reentry=true&region=SP&mainResourceFile=/content/general/unprotected/html/national/ad_carousel/healthnetcup.html','Health Net Cup')
AdImgAry[3] = new imgObj('/images/siteskin2/ad_carousel/ad_carousel_metabolic_syndrome.gif','/portal/member/content.do?reentry=true&region=SP&mainResourceFile=/content/general/unprotected/html/national/ad_carousel/metabolic_syndrome.html','Metabolic Syndrome')
AdImgAry[4] = new imgObj('/images/siteskin2/ad_carousel/ad_carousel_provider_search.gif','/portal/member/prvfinder/start.do?region=XX&logoutPortal=member','Provider Search')

/*
********************************
* display Ad content
********************************
*/
function showAd(index){
    if(stop_flag==0){    	
    	clearInterval(AutoplayObjTimerID);
    	stop_flag=1;
    }
    document.location=AdImgAry[--index].url;
}

