//importJavaScript('livesArchiveAPI','javascript/animation/prototype.js');
//importJavaScript('livesArchiveAPI','javascript/animation/scriptaculous.js');
//importJavaScript('livesArchiveAPI','http://regisandkelly.go.com/features.js');

var objArchive = { id: '', currentItem: 1, currentPage: 1, length: 0, pageLength: 0,  offsetX: 0, isPagingActive: false };
var objArchiveHiglight = { id: '', activeItem: 1, offsetX: 0};
var strActiveURL = '';
function writeArchiveItems(){
	_pageLength = Math.floor(features.length/4);
	if( features.length%4 != 0 ){
		_pageLength++;
	}
	objArchive = { id: 'archiveItems', currentItem: 1, currentPage: 1, length: features.length, pageLength: _pageLength,  offsetX: 588, isPagingActive: false };
	objArchiveHiglight = { id: 'archiveHighlight', activeItem: 1, offsetX: 147 };
	var objFeature = { url: '', image: '', title: '', text: ''};	
	for( i = 1; i <= features.length; i++ ){
		
		highlightItem = (i%4 == 0) ? 4 : (i%4);
				
		objFeature.image = features[i-1].image;			
				
		document.writeln('<td valign="top" align="left">');
		document.writeln('\
			<div onclick="highlightArchiveItem('+highlightItem+')" style="position: relative; width: 146px; height: 114px;">\
				<div style="z-index: 405; position: absolute; top: 0; left: 0px; width: 146px; height: 114px; background: url(./images/archive/RK_archive_itemInactiveFrame_146x114.png) top left no-repeat; *background-image: none; *filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'./images/archive/RK_archive_itemInactiveFrame_146x114.png\', sizingMethod=\'scale\');"><!-- --></div>\
				<div style="z-index: 404; position: absolute; top: 0; left: 0px; width: 146px; height: 114px;">\
					<img src="'+objFeature.image+'" width="145" height="110" alt="" border="0" />\
				</div>\
			</div>');
		document.writeln('</td>');		
	}
	inner('archiveItem_'+objArchiveHiglight.activeItem.toString(),'<a class="archiveCaption" href="'+features[objArchive.currentItem-1].url+'"><div style="opacity: 0.0; filter:alpha(opacity=0); background-color: #ffffff; width: 146px; height: 114px"></div></a>');
}

function scrollArchiveTL(){
	if( objArchive.currentPage == objArchive.pageLength ){
		new Effect.Move( objArchive.id, { beforeStart: function(){	
				objArchive.currentPage = 1;
				objArchive.currentItem = 1;
			}, afterFinish: function(){
				highlightArchiveItem(1,true);
			}, duration: 0.1, x: (objArchive.offsetX*(objArchive.pageLength-1)), y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
	}
	else{
		new Effect.Move( objArchive.id, { beforeStart: function(){
				objArchive.currentPage++;
				objArchive.currentItem += 4;
			}, afterFinish: function(){				
				if((objArchive.currentItem) > objArchive.length ){
					highlightArchiveItem(1);
					objArchive.currentItem = ((objArchive.currentPage - 1) * 4) + 1;
				}
				writeArchiveInfo();
			}, duration: 0.1, x: -(objArchive.offsetX), y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
	}
	inner('archiveItem_'+objArchiveHiglight.activeItem.toString(),'<a class="archiveCaption" href="'+features[objArchive.currentItem-1].url+'"><div style="opacity: 0.0; filter:alpha(opacity=0); background-color: #ffffff; width: 146px; height: 114px"></div></a>');
	return true;
}

function scrollArchiveTR(){
	if( objArchive.currentPage == 1){
		new Effect.Move( objArchive.id, { beforeStart: function(){
				objArchive.currentPage = objArchive.pageLength;
				objArchive.currentItem = ((objArchive.currentPage - 1) * 4 ) + 1;
			}, afterFinish: function(){
				highlightArchiveItem(1,true);
			}, duration: 0.1, x: -(objArchive.offsetX*(objArchive.pageLength-1)), y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
	}
	else{
		new Effect.Move( objArchive.id, { beforeStart: function(){
				objArchive.currentPage--;
				objArchive.currentItem -= 4;
			}, afterFinish: function(){				
				writeArchiveInfo();
			}, duration: 0.1, x: objArchive.offsetX, y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
	}
	inner('archiveItem_'+objArchiveHiglight.activeItem.toString(),'<a class="archiveCaption" href="'+features[objArchive.currentItem-1].url+'"><div style="opacity: 0.0; filter:alpha(opacity=0); background-color: #ffffff; width: 146px; height: 114px"></div></a>');
	return true;	
}

function highlightArchiveItem(item,reset){
	reset = (reset) ? true : false;
	inner('archiveItem_'+objArchiveHiglight.activeItem.toString(),'');
	if( item != objArchiveHiglight.activeItem ){		
		if( item < objArchiveHiglight.activeItem ){			
			new Effect.Move( objArchiveHiglight.id, { beforeStart: function(){
				if(!reset){
					objArchive.currentItem -= (objArchiveHiglight.activeItem-item);
				}
			}, afterFinish: function(){				
				writeArchiveInfo();
			}, duration: 0.1, x: -(objArchiveHiglight.offsetX*(objArchiveHiglight.activeItem-item)), y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
		}
		else{
			if((objArchive.currentItem + (item-objArchiveHiglight.activeItem)) > (objArchive.length)){
				return false;
			}
			new Effect.Move( objArchiveHiglight.id, { beforeStart: function(){
				if(!reset){
					objArchive.currentItem += (item-objArchiveHiglight.activeItem);
				}
			}, afterFinish: function(){				
				writeArchiveInfo();
			}, duration: 0.1, x: (objArchiveHiglight.offsetX*(item-objArchiveHiglight.activeItem)), y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal });
		}
		objArchiveHiglight.activeItem = item;		
	}
	else{
		writeArchiveInfo();
	}
	inner('archiveItem_'+objArchiveHiglight.activeItem.toString(),'<a class="archiveCaption" href="'+features[objArchive.currentItem-1].url+'"><div style="opacity: 0.0; filter:alpha(opacity=0); background-color: #ffffff; width: 146px; height: 114px"></div></a>');
	return true;
}

function writeArchiveInfo(){
	try{
		inner('archiveCaption','<a class="archiveCaption" href="'+features[objArchive.currentItem-1].url+'"><span class="archiveCaption_title">'+features[objArchive.currentItem-1].title+' </span>'+features[objArchive.currentItem-1].short_text+'</a>');
	}
	catch(e){
		//console.log(e.message)
		return false;
	}	
	return true;
}

//importJavaScript('PrototypeAPI','javascript/animation/prototype.js');
//importJavaScript('ScriptaculousAPI','javascript/animation/scriptaculous.js');
//importJavaScript('livesArchiveAPI','http://regisandkelly.go.com/features.js');