var home = document.getElementById('home');
if (home != null) attachEventListener(home, 'load', preloadImages, false);

var portfolio = document.getElementById('portfolio');
if (portfolio != null) attachEventListener(portfolio, 'load', preloadImages, false);


function createDateObject(dateString)
{
	//alert(dateString);
	var splitDateArray = dateString.split('/');
	var thisMonth      = Number(splitDateArray[0]);
	var thisDay        = Number(splitDateArray[1]);
	var thisYear       = Number(splitDateArray[2]);
	
	return new Date(thisYear, thisMonth - 1, thisDay);
}

function preloadImages()
{
	var images = document.getElementsByTagName('img');
	var imageCount = images.length;
	alert(imageCount);
	
	var imageArray = new Array();
	for (i = 0; i < imageCount ; i++ )
	{
		imageArray[i] = new Image();
		imageName = images.getAttribute('src');		
		imageArray[i].src = imageName;	
	}
	
	
}

function fixDate(enteredDate)
{

	var splitDateArray = enteredDate.split('/');	// Assumes m/d/yy or m/d/yyyy
	var enteredMonth   = splitDateArray[0];
	var enteredDay     = splitDateArray[1];
	var enteredYear    = splitDateArray[2];

	if (enteredYear.length == 2)
	{	// If a two-digit year was entered, determine whether to add a '19' or '20' prefix
	
		// My rule is pretty simple: a date with a two-digit year between 90 and 99 will return a year in the 1990s, else
		// the year will start with '20'
		
		if (Number(enteredYear) >= 90 && Number(enteredYear) <= 99)
		{
			enteredYear = '19' + enteredYear;
		}
		else
		{
			enteredYear = '20' + enteredYear;
		}
	}
	
	var enteredDateFinal = enteredMonth + '/' + enteredDay + '/' + enteredYear;
	return enteredDateFinal;
}

function padText(unpaddedText, newLength, padChar, padLocation) {
	var currentPad = '';
	var charsToAdd = newLength - unpaddedText.length;
	for (var i = 0; i < charsToAdd; i++) {
		currentPad = currentPad + padChar;
	}
	if (padLocation == 'left') {
		return currentPad + unpaddedText;
	} else {
		return unpaddedText + currentPad;
	}
}

function showPopup(popupURL, popupName, popupWidth, popupHeight, popupTop, popupLeft) {
	var popupArgs  = 'toolbar=0';
		popupArgs += 'location=0,';
		popupArgs += 'directories=0,';
		popupArgs += 'status=0,';
		popupArgs += 'menubar=0,';
		popupArgs += 'scrollbars=yes,';
		popupArgs += 'resizable=yes,';
		popupArgs += 'width=' + popupWidth + ','; 
		popupArgs += 'height=' + popupHeight + ',';
		
		if (popupTop == 'center' && popupLeft == 'center')
		{
			popupArgs += 'top=' + String((screen.availHeight / 2) - (popupHeight / 2 )) + ',';
			popupArgs += 'left=' + String((screen.availWidth / 2) - (popupWidth / 2 ));
		} 
		else
		{
			popupArgs += 'top=' + popupTop + ','; 
			popupArgs += 'left=' + popupLeft;
		}
					
	var popupWin = window.open(popupURL,popupName,popupArgs);
	popupWin.focus();
}


function loadParentWindow(url)
{
	opener.location.href = url;
}

function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{ 
		target.attachEvent("on" + eventType, functionRef); 
	} 
	else 
	{ 
		eventType = "on" + eventType; 
		if (typeof target[eventType] == "function") 
		{ 
			var oldListener = target[eventType]; 
			target[eventType] = function() 
			{ 
				oldListener(); 
				return functionRef(); 
			}; 
		} 
		else 
		{ 
			target[eventType] = functionRef; 
		} 
	} 
}


