/**********************************************************************
 *
 * swap image functions
 *
 *********************************************************************/
	
$(document).ready(function() 
{				   
	$('#mainNav ul li a img')
	.hover(function() 
		{
			if (this.parentNode.className != 'selected') 
			{
				var swap = 'images/sitebase/' + this.id + '_over.gif';
				this.src = swap;
			}
		},
		function() 
		{
			if (this.parentNode.className != 'selected') 
			{
				var swap = 'images/sitebase/' + this.id + '_up.gif';
				this.src = swap;
			}
			else 
			{
				var swap = 'images/sitebase/' + this.id + '_down.gif';
				this.src = swap;
			}
		}
	);
});

/**********************************************************************
 *
 * format title window
 *
 *********************************************************************/

function toTitleCase(str) 
{
    val = str;
	newVal = '';
	val = val.split(' ');
	for (var i=0; i < val.length; i++) 
	{
		newVal += val[i].substring(0,1).toUpperCase() + val[i].substring(1,val[i].length) + ' ';	
	}
	str = newVal;
	return str;
}      

function formatTitle(title) 
{
	newTitle = title.replace('-', ' ');
    return 'el\u00E9 Cake Company' + (newTitle != '/' ? ' | ' + toTitleCase(newTitle.substr(1, newTitle.length - 2).replace(/\//g, ' / ')) : '');
}


/**********************************************************************
 *
 * custom swfaddress 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 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);
            var links = document.getElementById('social').getElementsByTagName('a');
            for (var i = 0, link, parts, pairs; link = links[i]; i++) 
			{
                parts = link.href.split('?');
                pairs = parts[1].split('&');
                for (var j = 0, params; j < pairs.length; j++) 
				{
                    params = pairs[j].split('=');
                    if (params[0] == 'url' || params[0] == 'u' || params[0] == 'bkmk')
                        pairs[j] = params[0] + '=' + SWFAddress.getBaseURL() + SWFAddress.getValue();
                    if (params[0] == 'title' || params[0] == 't')
                        pairs[j] = params[0] + '=' + SWFAddress.getTitle();
                }
                link.href = parts[0] + '?' + pairs.join('&');
            }
        } 
		else 
		{
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) 
{
    var index, rel, links = document.getElementsByTagName('a'), path = event.path;
	var pipeStyle = '';

    if (path.substr(path.length - 1) != '/') 
	{
    	path += '/';
    }
    for (var i = 0, l, link; link = links[i]; i++) 
	{
        index = link.rel.indexOf('?');
        rel = (index > -1) ? link.rel.substr(0, index) : link.rel;
        link.className = (rel == path) ? 'selected' : '';
		
		if (link.className == 'selected') 
		{
			
			switch (link.id) 
			{			
				case 'hm': 
				{
					pipeStyle = 'wave';
					changePipeStyle(pipeStyle);
					break
				}
				case 'mn_01': 
				{
					pipeStyle = 'star';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_02': 
				{
					pipeStyle = 'beads';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_03': 
				{
					pipeStyle = 'curl';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_04': 
				{
					pipeStyle = 'star';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_05': 
				{
					pipeStyle = 'beads';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_06': 
				{
					pipeStyle = 'curl';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_07': 
				{
					pipeStyle = 'star';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_08': 
				{
					pipeStyle = 'beads';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_09': 
				{
					pipeStyle = 'curl';
					changePipeStyle(pipeStyle);
					break;
				}
				case 'mn_10': 
				{
					pipeStyle = 'star';
					changePipeStyle(pipeStyle);
					break;
				}
			}
				
			if (link.parentNode.className == 'mn') 
			{
				var source = 'images/sitebase/' + link.firstChild.id + '_down.gif';
				link.firstChild.src = source;
			}
		}
		else 
		{	
			if (link.parentNode.className == 'mn') 
			{
				var source = 'images/sitebase/' + link.firstChild.id + '_up.gif';
				link.firstChild.src = source;
			}
		}	
    }
    var parameters = '';
    for (var p in event.parameters) {
        parameters += '&' + p + '=' + event.parameters[p];
    }
    var xhr = getTransport();
    xhr.onreadystatechange = function() 
	{
        updateChange(xhr);
    }
    xhr.open('get', 'datasource.php?swfaddress=' + event.path + parameters, true);
    xhr.send('');
    SWFAddress.setTitle(formatTitle(event.path));
	
	swapImages();
	navFix();
}

function copyLink() 
{
    if (window.clipboardData && clipboardData.setData) 
	{
        clipboardData.setData('Text', SWFAddress.getBaseURL() + SWFAddress.getValue());
    } 
	else 
	{
        alert('Unsupported browser.');
    }
}

/**********************************************************************
 *
 * custom pipe style change
 *
 *********************************************************************/
 
function changePipeStyle(pipeStyle) 
{
	var pipe = document.getElementById('piping');
	pipe.className = pipeStyle;
	appear(pipe);
}

/**********************************************************************
 *
 * remote content function
 *
 *********************************************************************/
 
function swapImages() 
{
	// swapImage CLASS
	$(".swapImage")
	.live('mouseover', function() 
	{
		var swap = 'images/sitebase/' + this.id + '_over.gif';
		this.src = swap;
	});
	
	$(".swapImage")
	.live('mouseout', function() 
	{
		var swap = 'images/sitebase/' + this.id + '_up.gif';
		this.src = swap;
	});
	
	$(".swapCakes")
	.live('mouseover', function() 
	{
		var swap = 'images/specialty_cakes/cakes/' + this.id + 'thumbLRG.jpg';
		this.src = swap;
	});
	
	$(".swapCakes")
	.live('mouseout', function() 
	{
		var swap = 'images/specialty_cakes/cakes/' + this.id + 'thumbSML.jpg';
		this.src = swap;
	});
	
	$(".weddingSwap")
	.live('mouseover', function() 
	{
		var swap = 'images/wedding_cakes/cakes/' + this.id + 'thumbLRG.jpg';
		this.src = swap;
	});
	
	$(".weddingSwap")
	.live('mouseout', function() 
	{
		var swap = 'images/wedding_cakes/cakes/' + this.id + 'thumbSML.jpg';
		this.src = swap;
	});
}

/**********************************************************************
 *
 * remote content
 *
 *********************************************************************/

function navFix() {
	$("ul#select")
	.live('mouseover', function() 
	{
		$(this).addClass('hover');
	});
	
	$("ul#select")
	.live('mouseout', function() 
	{
		$(this).removeClass('hover');										   
	});
	
	$("li#selector")
	.live('mouseover', function() 
	{
		$(this).addClass('hover');
	});
	
	$("li#selector")
	.live('mouseout', function() 
	{
		$(this).removeClass('hover');										   
	});
}

/**********************************************************************
 *
 * event listener
 *
 *********************************************************************/

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);