function getVideoIncludeBase(){
	return '/.element/ssi/auto/1.0/aps/video';
}

function getPrevId() {
	return 'yesterday';
}

function getPrevText() {
	return 'Yesterday';
}

function getCurrId() {
	return 'today';
}

function getCurrText() {
	return 'Today';
}

function getAscFileName() {
	return 'recent_games1.html';
}

function getDescFileName() {
	return 'recent_games_desc1.html';
}

function padNumber(intNumber,intDigits) {
	intNumber = intNumber.toString();
	var strPadding = '';
	if (intDigits > intNumber.length) {
		for (i=0;i<(intDigits-intNumber.length);i++) {
			strPadding += '0';
		}
	}
	return strPadding + intNumber.toString();
}

function getDatePath(strDay) {

	if (isNaN(strDay)) {

		var dateNow = new Date();
		var dateToday = new Date(dateNow - (3*60*60*1000));
		
		switch (strDay) {
			case 'yesterday':
				var dateYesterday = new Date(dateToday - (24*60*60*1000));
				var strYesterdayYear = dateYesterday.getFullYear();
				var strYesterdayMonth = padNumber((dateYesterday.getMonth()+1),2);
				var strYesterdayDay = padNumber(dateYesterday.getDate(),2);
	
				return '/'+strYesterdayYear+'/'+strYesterdayMonth+'/'+strYesterdayDay+'/';
			break;
	
			case 'today':
				var strTodayYear = dateToday.getFullYear();
				var strTodayMonth = padNumber((dateToday.getMonth()+1),2);
				var strTodayDay = padNumber(dateToday.getDate(),2);
				
				return '/'+strTodayYear+'/'+strTodayMonth+'/'+strTodayDay+'/';
			break;
		
			case 'tomorrow':
				var dateTomorrow = new Date(dateToday * 1 + (24*60*60*1000));
				var strTomorrowYear = dateTomorrow.getFullYear();
				var strTomorrowMonth = padNumber((dateTomorrow.getMonth()+1),2);
				var strTomorrowDay = padNumber(dateTomorrow.getDate(),2);
	
				return '/'+strTomorrowYear+'/'+strTomorrowMonth+'/'+strTomorrowDay+'/';
			break;
		}
	} else {
		//Convert the number to a string
		strDay = strDay + '';
		//Extract the Substring
		return '/'+strDay.substr(0,4)+'/'+strDay.substr(4,2)+'/'+strDay.substr(6,2)+'/';
	}
}

function createLink(strFileName,strId,strDisplayText) {

	//Set base path
	var strBaseFolder = getVideoIncludeBase();	

	//Build the full path
	var strFileUrl = strBaseFolder+getDatePath(strId)+strFileName;

    new Ajax.Request(strFileUrl, {
        method:'get',
        onSuccess: function(transport){   
            var response = transport.status || "no response text";   
            if(parseInt(response) == 200){
            	var strLink="javascript:replaceGridContent('"+strFileName+"','nbaVideoGrid','"+strId+"')";
            	$(strId).innerHTML='<a href="'+strLink+'">'+strDisplayText+'</a>';
            }
        },     
        onFailure: function(){
        	$(strId).innerHTML=strDisplayText;
        }
    });	
}

function toggleDropDown(strLoadId) {
	$$("#nbaDD_MPctv_list option")[0].value = "javascript:replaceGridContent('" + getAscFileName() + "','nbaVideoGrid','"+strLoadId+"')";
	$$("#nbaDD_MPctv_list option")[1].value = "javascript:replaceGridContent('" + getDescFileName() + "','nbaVideoGrid','"+strLoadId+"')";
}

function toggleLink(strFileName,strLoadId) {

	var strPrevId = getPrevId();
	var strPrevText = getPrevText();
	var strCurrId = getCurrId();
	var strCurrText = getCurrText();

	//If a date was passed...
	if (!isNaN(strLoadId)) {

		//Convert to a string
		strLoadId = strLoadId + '';

		//Temporarily reset the current page id
		strLoadId = '/'+strLoadId.substr(0,4)+'/'+strLoadId.substr(4,2)+'/'+strLoadId.substr(6,2)+'/';

		//Set to a text value if it matches
		switch (strLoadId) {
			case getDatePath(strPrevId):
				strLoadId = strPrevId;
			break;

			case getDatePath(strCurrId):
				strLoadId = strCurrId;
			break;
		}
	}

	//Set the previous link
	if (strLoadId == strPrevId) {
		$(strPrevId).innerHTML=strPrevText;
	} else {
		createLink(strFileName,strPrevId,strPrevText);
	}

	//Set the current link
	if (strLoadId == strCurrId) {
		$(strCurrId).innerHTML=strCurrText;
	} else {
		createLink(strFileName,strCurrId,strCurrText);
	}
}

//Check the time of day to see if the yesterday link should be the default
function toggleGridContent() {

	var dateNow = new Date();

	var intHours = dateNow.getHours();

	if (intHours > 2 && intHours < 13) {
		replaceGridContent(getAscFileName(),'nbaVideoGrid','yesterday');
	} else {
		replaceGridContent(getAscFileName(),'nbaVideoGrid','today');
	}
}

//Very simple AJAX function for updating the video grid content for all video pages.
function replaceGridContent(strFileName,strId,strLoadId) {

	//Set base path
	var strBaseFolder = getVideoIncludeBase();	

	//Build the full path
	var strFileUrl = strBaseFolder+getDatePath(strLoadId)+strFileName;

	//alert(strFileUrl);

    var myAjax = new Ajax.Request(strFileUrl, {
        method:'get',
        onSuccess: function(transport) {

            var response = transport.status || "no response text";

            if(parseInt(response) == 200){

            	//Kill the link that correlates to the page that was just selected
            	//Based on the day only, the link always stays locked on the first page of the current day.
            	//Also update the dropdown link
            	if (strLoadId) {

            		//Check what the current value is for the dropdown and base the link off of that
            		if ($('nbaDD_MPctv_list').selectedIndex == 1) {
            			toggleLink(getDescFileName(),strLoadId);
            		} else {
            			toggleLink(getAscFileName(),strLoadId);       				
            		}
        			toggleDropDown(strLoadId);
            	}

            	//Remove the old footer
            	$('nbaVideoGridFoot').remove();

				//Remove the old body
				//(for reasons I cannot explain, have to invoke remove instead of calling it directly on the body element)
            	$('nbaVideoGridBody').remove();

				//Add in the new tbody and tfoot tags
				$('nbaVideoGridHead').insert({after: transport.responseText});
            }
        },     
        onFailure: function(){

        	//Kill the link that correlates to the page that was just selected
        	if (strLoadId) {

        		//Check what the current value is for the dropdown and base the link off of that
        		if ($('nbaDD_MPctv_list').selectedIndex == 1) {
        			toggleLink(getDescFileName(),strLoadId);
        		} else {
        			toggleLink(getAscFileName(),strLoadId);       				
        		}
    			toggleDropDown(strLoadId);
        	}
        }
    });
}