// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
	
	title = title.replace(/\//g, '');
	
	switch(title) {
		case 'got-a-good-idea':
		  title = 'Ketchup - Got a Good Idea?'
		  break;
		case 'who-can-help':
		  title = 'Ketchup - Who Can Help?'
		  break;
		case 'why-ketchup':
		  title = 'Ketchup - Why Ketchup?'
		  break;
		case 'want-to-chip-in':
		  title = 'Ketchup - Want to Chip In?'
		  break;
		case 'need-more':
		  title = 'Ketchup - Need More?'
		  break;
		default:
		  title = 'Ketchup'
		  break;
	}
	
    return title;
}


// Custom SWFAddress and Ajax handling

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}


function copyLink() {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('Text', SWFAddress.getBaseURL() + SWFAddress.getValue());
    } else {
        alert('Unsupported browser.');
    }
}

function appear(content, value) {
    if (typeof value == 'undefined') value = 0;
    if (value > 1) return;
    var property = content.filters ? 'filter' : 'opacity';
    content.style[property] = content.filters ? 'alpha(opacity=' + value*100 + ')' : value;
    setTimeout(function () {appear(content, value + .1)}, 25);
}

function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var content = document.getElementById('content');
            content.innerHTML = xhr.responseText;
            appear(content);
			initCompanies();
        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) {
    updateContent(event.path);
}


/*
Separated from handleChange to allow Flash to call this function.
*/
function updateContent(path) {
    
    var xhr = getTransport();
    xhr.onreadystatechange = function() {
        updateChange(xhr);
    }

    xhr.open('get', 'datasource.php?swfaddress=' + path, true);
    xhr.send('');

    SWFAddress.setTitle(formatTitle(path));
	updateSelectedPot(path);
	
	if (footerVisible) {
		showFooter();
	}
}


/*
Handles updating of full/empty states for navigation pots. HTML version only.
*/
function updateSelectedPot(path) {
	
	// Set all the pots to empty.
	if (elements = document.getElementById('navigation')) {
		
		elements = elements.getElementsByTagName('img')
			
		for (var i = 0; image = elements[i]; i++) {
			if (image.className == 'pot') {
				image.style.display = 'inline';
			} else if (image.className == 'pot full') {
				image.style.display = 'none';
			}
		}
	
		// Set the selected pot to full.
		path = path.replace(/\//g, '');
	
		if ((path != '') && (path != null) && (elements = document.getElementById('nav-' + path))) {
				
			elements = elements.getElementsByTagName('img')
	
			for (var i = 0; image = elements[i]; i++) {
			
				if (image.className == 'pot') {
					image.style.display = 'none';
				} else if (image.className == 'pot full') {
					image.style.display = 'inline';
				}
			}
		}
	}
}

function initCompanies() {
	
	$('li#company-wsa').click(function() {
		showCompany('wsa');
	});
	
	$('li#company-tg').click(function() {
		showCompany('tg');
	});
	
	$('li#company-mt').click(function() {
		showCompany('mt');
	});
	
	$('li#company-qd').click(function() {
		showCompany('qd');
	});
	
	hideCompanies(false);
}

function hideCompanies(slide) {
	
	if (slide == false) {
		$('#companies p').each(function() {
			$(this).hide();
		});
	} else {
		$('#companies p').each(function() {
			$(this).slideUp();
		});
	}
	
	return false;
}

function showCompany(company) {
	
	// Hide all the texts
	hideCompanies(true);
	
	$('#company-' + company + ' p').slideDown();
	
	return false;
}


var footerVisible = false;

function showFooter() {
	
	if (!footerVisible) {
		$('div#footer').animate({
			marginTop: '-270px',
			height: '270px'
		});
		
		$('div#wrap div.container').animate({
			paddingBottom: '310px'			
		});
		
		footerVisible = true;
	} else {
		$('div#footer').animate({
			marginTop: '-28px',
			height: '28px'
		});

		$('div#wrap div.container').animate({
			paddingBottom: '68px'			
		});
		
		footerVisible = false;
	}
}


SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);