var archiveWidth = 0;
function adjustWrapperWidth(){
	archiveWidth = 1;
	$('archive-tabs').getElements('.archive-tab').each(function(aTabs){
		archiveWidth += aTabs.offsetWidth;
	});
	$('archive-wrapper').style.width = archiveWidth + 'px';
	$('archive-tabs').style.width = archiveWidth + 'px';
	adjustBar('archive');
}

function createColDrags(){
	$$('.archive-tab').each(function(colTab){
		colTab.makeResizable({
			// Limit resize to horizontal only
			modifiers: {x: 'width', y: false},
			// Sets the resize limits
			limit: {x: [50, 250]},
			// The element that must be dragged to resize the component
			handle: colTab.getElement('.archive-tab-handle'),
			// How many pixels you must drag the resize element before it actually begins resizing
			snap: 5, 
			onSnap: function(){
				colTab.parentNode.style.width = colTab.parentNode.offsetWidth + 200 + 'px';
			},
			onComplete: function(){
				adjustColWidth(colTab.className.substr(colTab.className.indexOf(' ')+1),colTab.offsetWidth-11);
			}
		})
	});
}

function adjustColWidth(colClass,colWid){
	$$('.'+colClass).each(function(vCols){
		vCols.style.width = colWid + 'px';
	});
	adjustWrapperWidth();
}

var currentSelectedVideo, curLigDar;
function selectArchivedVideo(selVid){
	if(currentSelectedVideo != selVid)
	{
		if(currentSelectedVideo)
		{
			if(viewStats)
				compactElement(currentSelectedVideo,20);
			$(currentSelectedVideo).className = curLigDar;
			$(currentSelectedVideo).style.margin = '0';
		}
		currentSelectedVideo = selVid;
		curLigDar = $(currentSelectedVideo).className;
			$(currentSelectedVideo).style.margin = '4px 0';
		$(currentSelectedVideo).className = 'archived-video archived-selected';
		if(viewStats)
			expandElement(currentSelectedVideo,'auto');
	}
}

function createVidSelect(selVidId){
	$(selVidId).addEvent('click',function(){
		selectArchivedVideo(selVidId);
	});
}

var numSelVids = 0;
function createStartingVidSelects(){
	$$('.archived-video').each(function(arcVid){
		numSelVids++;
		arcVid.id = "archived-video"+numSelVids;
		createVidSelect(arcVid.id);
	});
}

var viewStats = false;
function viewStatsClick(){
	if(viewStats)
	{
		if(currentSelectedVideo)
			compactElement(currentSelectedVideo,20);
		$('archive-view-stats').className = "archive-stats-off Tips";
		viewStats = false;
	}
	else
	{
		if(currentSelectedVideo)
			expandElement(currentSelectedVideo,'auto');
		$('archive-view-stats').className = "archive-stats-on Tips";
		viewStats = true;
	}
}

var colWidth = new Array();
function archiveHideCol(secCol){
	if($('archive-check-'+secCol).checked)
	{
		$$('.archive-tab-'+secCol).each(function(vCols){
			vCols.style.width = colWidth[secCol] + 'px';
			vCols.style.padding = '0 5px';
			vCols.style.borderWidth = '1px 0 1px 1px';
		});
	}
	else
	{
		colWidth[secCol] = $('archive-tabs').getElement('.archive-tab-'+secCol).offsetWidth - 11;
		$$('.archive-tab-'+secCol).each(function(vCols){
			vCols.style.width = 0;
			vCols.style.padding = 0;
			vCols.style.borderWidth = '1px 0 1px 0';
		});
	}
	adjustWrapperWidth();
}

function createStartingHides(){
	$('archive-tabs').getElements('.archive-tab').each(function(vCol){
		if(vCol.className.replace('archive-tab archive-tab-','') != 'dot')
		{
			colWidth[vCol.className.replace('archive-tab archive-tab-','')] = vCol.offsetWidth - 11;
			archiveHideCol(vCol.className.replace('archive-tab archive-tab-',''));
		}
	});
}

var totalArchived, currentSort, sortAsc;
function sortingFunction(a,b){
	if(a.substr(a.indexOf(' -- ')+4) == b.substr(b.indexOf(' -- ')+4))
		return 0;
	return (a.substr(a.indexOf(' -- ')+4) < b.substr(b.indexOf(' -- ')+4)) ? -1 : 1;
}
function sortingTime(a,b){
	if(a.match('pm'))
	{
		if(a.match('12:'))
			a = parseInt(a.substr(a.indexOf(' -- ')+4).replace(':','').replace('pm',''));
		else
			a = parseInt(a.substr(a.indexOf(' -- ')+4).replace(':','').replace('pm','')) + 1200;
	}
	else
	{
		if(a.match('12:'))
			a = parseInt(a.substr(a.indexOf(' -- ')+4).replace(':','').replace('am','')) - 1200;
		else
			a = parseInt(a.substr(a.indexOf(' -- ')+4).replace(':','').replace('am',''));
	}
	if(b.match('pm'))
	{
		if(b.match('12:'))
			b = parseInt(b.substr(b.indexOf(' -- ')+4).replace(':','').replace('pm',''));
		else
			b = parseInt(b.substr(b.indexOf(' -- ')+4).replace(':','').replace('pm','')) + 1200;
	}
	else
	{
		if(b.match('12:'))
			b = parseInt(b.substr(b.indexOf(' -- ')+4).replace(':','').replace('am','')) - 1200;
		else
			b = parseInt(b.substr(b.indexOf(' -- ')+4).replace(':','').replace('am',''));
	}
	return a - b;
}
function sortArchive(sortCol){
	totalArchived = 0;
	var colArray = new Array();
	$$('.archive-tab-'+sortCol).each(function(vCols){
		if(vCols.parentNode.id != 'archive-tabs')
		{
			colArray[totalArchived] = vCols.parentNode.id.replace('archived-video','') + ' -- ' + vCols.innerHTML;
			totalArchived++;
		}
	});
	if(sortCol == 'time')
		colArray.sort(sortingTime);
	else
		colArray.sort(sortingFunction);
	if(currentSort == sortCol && sortAsc)
	{
		sortAsc = false;
		colArray.reverse();
	}
	else
		sortAsc = true;
	if(currentSort)
		$('archive-tabs').getElement('.archive-tab-'+currentSort).style.background = '#1a1a1a';
	currentSort = sortCol;
	$('archive-tabs').getElement('.archive-tab-'+currentSort).style.background = '#666666';
	for(x=0;x<totalArchived;x++)
		$('archived-video'+colArray[x].substring(0,colArray[x].indexOf(' -- '))).inject($('archive-wrapper'));
	recolorArchiveRows();
}

function recolorArchiveRows(){
	var darkRow = true;
	$$('.archived-video').each(function(vRows){
		if(darkRow)
		{
			if(vRows.className == 'archived-video archived-selected')
				curLigDar = 'archived-video archived-dark';
			else
				vRows.className = 'archived-video archived-dark';
			darkRow = false;
		}
		else
		{
			if(vRows.className == 'archived-video archived-selected')
				curLigDar = 'archived-video archived-light';
			else
				vRows.className = 'archived-video archived-light';
			darkRow = true;
		}
	});
}

function removeArchivedVideo(){
	if(currentSelectedVideo)
	{
		if(confirm("Are you sure you want to remove the selected video permanently?"))
		{
			$(currentSelectedVideo).dispose();
			recolorArchiveRows();
			currentSelectedVideo = false;
		}
	}
	else
		alert("You have no video selected.");
}

var archivePlaying;
function archivePlayVideo(){
	if(archivePlaying)
	{
		$(archivePlaying).getElement('.archive-tab-dot').style.background = "url(images/gray_dot.gif) no-repeat center center";
		$('archive-play-video').className = "archive-play Tips";
		archivePlaying = false;
	}
	else if(currentSelectedVideo)
	{
		$(currentSelectedVideo).getElement('.archive-tab-dot').style.background = "url(images/gray_dot_hover.gif) no-repeat center center";
		$('archive-play-video').className = "archive-stop Tips";
		archivePlaying = currentSelectedVideo;
	}
	else
		alert("You have no video selected.");
}

// Fires the functions when the page finishes loading
window.addEvent('domready', function(){
	createDrag('archive');
	setComponentLimit('archive', 26);
	createScroll('archive');
	createScroll('archive-horizontal','horizontal');
	changeResize('archive','centermod');
	adjustWrapperWidth();
	createColDrags();
	createStartingVidSelects();
	createStartingHides();
	sortArchive('video');
});