function replaceAmpersands(text)
{
	var textArray = text.split("");
	for(var i = 0; i < textArray.length; i++)
		if(textArray[i] == "&")
			textArray[i] = "%26";
	
	return textArray.join("");
}

function replaceLineBreaks(text)
{
	text = escape(text); // encode textarea string's carriage returns
	//loop through string, replacing carriage return encoding with HTML break tag
	for(i = 0; i < text.length; i++)
	{
		if(text.indexOf("%0D%0A") > -1) //Windows encodes returns as \r\n hex
			text = text.replace("%0D%0A", "<br />");
		else if(text.indexOf("%0A") > -1) //Unix encodes returns as \n hex
			text = text.replace("%0A", "<br />");
		else if(text.indexOf("%0D") > -1) //Macintosh encodes returns as \r hex
			text = text.replace("%0D", "<br />");
	}

	return unescape(text); //unescape all other encoded characters
}

function validateContact(contactName, contactEmail, contactMessage)
{
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(contactName == "" || contactName == "Name")
	{
		return "Please go back and enter your name";
	}
	else if(!emailReg.test(contactEmail))
	{
		return "Please go back and enter a valid email address of the form name@address.com";
	}
	else if(contactMessage == "" || contactMessage == "Message")
	{
		return "Please enter a message";
	}
	
	return "success";
}

function validateSupport(supportUsername, supportPassword)
{
	if(supportUsername == "" || supportUsername == "Username")
	{
		return "Please enter your username";
	}
	else if(supportPassword == "" || supportPassword == "Password")
	{
		return "Please enter your password";
	}
	
	return "success";
}

function clearContactForm()
{
	// need settimeouts? ie6?
	setTimeout(function(){$('#contactName').attr('value', '').blur()}, 1);
	setTimeout(function(){$('#contactCompany').attr('value', '').blur()}, 1);
	setTimeout(function(){$('#contactEmail').attr('value', '').blur()}, 1);
	setTimeout(function(){$('#contactPhone').attr('value', '').blur()}, 1);
	setTimeout(function(){$('#contactMessage').attr('value', '').blur()}, 1);
}

function clearSupportForm()
{
	// need settimeouts? ie6?
	setTimeout(function(){$('#supportUsername').attr('value', '').blur()}, 1);
	setTimeout(function(){$('#supportPassword').attr('value', '').blur()}, 1);
}

function supportPasswordFocus(event)
{
	var evt = (window.event) ? window.event : event;
	var elem = (evt.srcElement) ? evt.srcElement : evt.target;
	
	var inputID = $(elem).attr('id');
	var inputName = $(elem).attr('name');
	var inputValue = $(elem).attr('value');
	
	if(inputValue == 'Password')
	{
		var passwordInput = document.createElement('input');
		passwordInput.type = 'password';
		passwordInput.id = inputID;
		passwordInput.name = inputName;
		passwordInput.value = '';
		passwordInput.style.color = 'black';
		passwordInput.onblur = supportPasswordBlur;
		document.getElementById(inputID).parentNode.replaceChild(passwordInput, document.getElementById(inputID));
		setTimeout(function(){passwordInput.focus();}, 1);
	}
}

function supportPasswordBlur(event)
{
	var evt = (window.event) ? window.event : event;
	var elem = (evt.srcElement) ? evt.srcElement : evt.target;
	
	var inputID = $(elem).attr('id');
	var inputName = $(elem).attr('name');
	var inputValue = $(elem).attr('value');
	
	if(inputValue == "")
	{
		var textInput = document.createElement('input');
		textInput.type = 'text';
		textInput.id = inputID;
		textInput.name = inputName;
		textInput.value = 'Password';
		textInput.style.color = '#888888';
		textInput.onfocus = supportPasswordFocus;
		document.getElementById(inputID).parentNode.replaceChild(textInput, document.getElementById(inputID));
	}
}

function setupSupportForm()
{
	$('#supportUsername').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Username')
		{
			$(el).css('color', 'black');
			el.value = '';
		}
		
		return false;
	});
	
	$('#supportUsername').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			el.value = 'Username';
		}
		
		return false;
	});
	
	$('#supportPassword').unbind('focus').focus(function(e){supportPasswordFocus(e);});
	$('#supportPassword').unbind('blur').blur(function(e){supportPasswordBlur(e);});
	
	$('#loginButton').mouseover(function()
	{
		$(this).attr('src', 'images/form/loginButtonMouseOver.png');
	});
	
	$('#loginButton').mouseout(function()
	{
		$(this).attr('src', 'images/form/loginButton.png');
	});
	
	$('#supportForm').submit(function()
	{
		$('#supportResult').fadeOut(function()
		{
			$('#supportResult').empty();
			$('#supportResult').css('border', 'none');
			$('#supportResult').css('background', 'transparent');
			$('#supportResult').html('<img src="images/form/loading.gif" alt="Loading..." />');
			$('#supportResult').fadeIn(function()
			{
				var supportUsername = document.supportForm.supportUsername.value;
				if(!supportUsername)
					supportUsername = document.forms['supportForm'].elements['supportUsername'].value;
					
				var supportPassword = document.supportForm.supportPassword.value;
				if(!supportPassword)
					supportPassword = document.forms['supportForm'].elements['supportPassword'].value;

				supportUsername = replaceAmpersands(supportUsername);
				supportPassword = replaceAmpersands(supportPassword);

				var validationResult = validateSupport(supportUsername, supportPassword);
				if(validationResult == "success")
				{
					$.ajaxSetup({cache: false});
					$.ajax(
					{
						type: "GET",
						url: 'supportLogin.php',
						dataType: 'html',
						data:
						{
							username: supportUsername,
							password: supportPassword
						},
						success: function(data, textStatus, xhrObject)
						{
							$('#supportResult').fadeOut(function()
							{
								$('#supportResult').empty();
								$('#supportResult').css('border', '1px solid #19547c');
								$('#supportResult').css('background', '#b1c7d6');
								$('#supportResult').html(data);
								$('#supportResult').fadeIn();
								clearSupportForm();
							});
						},
						error: function(xhrObject, textStatus, errorThrown)
						{
							$('#supportResult').fadeOut(function()
							{
								$('#supportResult').empty();
								$('#supportResult').css('border', '1px solid #19547c');
								$('#supportResult').css('background', '#b1c7d6');
								$('#supportResult').html(xhrObject);
								$('#supportResult').fadeIn();
							});
						}
					});
				}
				else
				{
					$('#supportResult').fadeOut(function()
					{
						$('#supportResult').empty();
						$('#supportResult').css('border', '1px solid #19547c');
						$('#supportResult').css('background', '#b1c7d6');
						$('#supportResult').html(validationResult);
						$('#supportResult').fadeIn();
					});
				}
			});
		});
		
		return false;
	});
}

function setupContactForm()
{
	$('#contactName').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Name')
		{
			$(el).css('color', 'black');
			el.value = '';
		}
	});
	
	$('#contactName').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			el.value = 'Name';
		}
	});
	
	$('#contactCompany').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Company')
		{
			$(el).css('color', 'black');
			el.value = '';
		}
	});
	
	$('#contactCompany').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			el.value = 'Company';
		}
	});
	
	$('#contactEmail').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Email')
		{
			$(el).css('color', 'black');
			el.value = '';
		}
	});
	
	$('#contactEmail').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			el.value = 'Email';
		}
	});
	
	$('#contactPhone').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Phone')
		{
			$(el).css('color', 'black');
			el.value = '';
		}
	});
	
	$('#contactPhone').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			el.value = 'Phone';
		}
	});
	
	$('#contactMessage').unbind('focus').focus(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == 'Message')
		{
			$(el).css('color', 'black');
			$(el).css('text-align', 'left');
			el.value = '';
		}
	});
	
	$('#contactMessage').unbind('blur').blur(function(e)
	{
		var evt = (window.event) ? window.event : e;
		var el = (evt.srcElement) ? evt.srcElement : evt.target;
		
		if(el.value == '')
		{
			$(el).css('color', '#888888');
			$(el).css('text-align', 'center');
			el.value = 'Message';
		}
	});
	
	$('#nextButton').mouseover(function()
	{
		$(this).attr('src', 'images/form/nextButtonMouseOver.png');
	});
	
	$('#nextButton').mouseout(function()
	{
		$(this).attr('src', 'images/form/nextButton.png');
	});
	
	$('#backButton').mouseover(function()
	{
		$(this).attr('src', 'images/form/backButtonMouseOver.png');
	});
	
	$('#backButton').mouseout(function()
	{
		$(this).attr('src', 'images/form/backButton.png');
	});
	
	$('#sendButton').mouseover(function()
	{
		$(this).attr('src', 'images/form/sendButtonMouseOver.png');
	});
	
	$('#sendButton').mouseout(function()
	{
		$(this).attr('src', 'images/form/sendButton.png');
	});
	
	$('#nextButton').click(function()
	{
		setTimeout(function(){$('#contactName').animate({opacity: 0}, 200);}, 100);
		setTimeout(function(){$('#contactCompany').animate({opacity:0}, 200);}, 200);
		setTimeout(function(){$('#contactEmail').animate({opacity:0}, 200);}, 300);
		setTimeout(function(){$('#contactPhone').animate({opacity:0}, 200);}, 400);
		setTimeout(function(){$('#nextButton').animate({opacity:0}, 200);}, 500);
		
		setTimeout(function()
		{
			$('#firstLayer').css('display', 'none');
			$('#secondLayer').css('display', 'block');
		}, 800);
		
		setTimeout(function()
		{
			$('#backButton').animate({opacity:1}, 200);
			$('#contactMessage').animate({opacity:1}, 200);
			$('#sendButton').animate({opacity:1}, 200);
		}, 900);
		
		return false;
	});
	
	//fade message out from bottom to top when going back
	
	$('#backButton').click(function()
	{
		$('#sendButton').animate({opacity: 0}, 200);
		$('#backButton').animate({opacity: 0}, 200);
		setTimeout(function(){$('#contactMessage').animate({opacity: 0}, 200);}, 100);
	
		setTimeout(function()
		{
			$('#firstLayer').css('display', 'block');
			$('#secondLayer').css('display', 'none');
		}, 500);
		
		setTimeout(function()
		{
			$('#nextButton').animate({opacity:1}, 200);
			setTimeout(function(){$('#contactPhone').animate({opacity:1}, 200);}, 100);
			setTimeout(function(){$('#contactEmail').animate({opacity:1}, 200);}, 200);
			setTimeout(function(){$('#contactCompany').animate({opacity:1}, 200);}, 300);
			setTimeout(function(){$('#contactName').animate({opacity:1}, 200);}, 400);
		}, 600);
		
		return false;
	});
	
	$('#contactForm').submit(function()
	{
		$('#contactResult').fadeOut(function()
		{
			$('#contactResult').empty();
			$('#contactResult').css('border', 'none');
			$('#contactResult').css('background', 'transparent');
			$('#contactResult').html('<img src="images/form/loading.gif" alt="Loading..." />');
			$('#contactResult').fadeIn(function()
			{
				var contactName = document.contactForm.contactName.value;
				if(!contactName)
					contactName = document.forms['contactForm'].elements['contactName'].value;
					
				var contactCompany = document.contactForm.contactCompany.value;
				if(!contactCompany)
					contactCompany = document.forms['contactForm'].elements['contactCompany'].value;
				
				var contactEmail = document.contactForm.contactEmail.value;
				if(!contactEmail)
					contactEmail = document.forms['contactForm'].elements['contactEmail'].value;
					
				var contactPhone = document.contactForm.contactPhone.value;
				if(!contactPhone)
					contactPhone = document.forms['contactForm'].elements['contactPhone'].value;
					
				var contactMessage = document.contactForm.contactMessage.value;
				if(!contactMessage)
					contactMessage = document.forms['contactForm'].elements['contactMessage'].value;
					
				contactName = replaceAmpersands(contactName);
				contactCompany = replaceAmpersands(contactCompany);
				contactEmail = replaceAmpersands(contactEmail);
				contactPhone = replaceAmpersands(contactPhone);
				contactMessage = replaceAmpersands(contactMessage);
				contactMessage = replaceLineBreaks(contactMessage);

				var validationResult = validateContact(contactName, contactEmail, contactMessage);
				if(validationResult == "success")
				{
					if(contactCompany == "Company") contactCompany = "N/A";
					if(contactPhone == "Phone") contactPhone = "N/A";
				
					$.ajaxSetup({cache: false});
					$.ajax(
					{
						type: "GET",
						url: 'email.php',
						dataType: 'html',
						data:
						{
							name: contactName,
							company: contactCompany,
							email: contactEmail,
							phone: contactPhone,
							message: contactMessage
						},
						success: function(data, textStatus, xhrObject)
						{
							$('#contactResult').fadeOut(function()
							{
								$('#contactResult').empty();
								$('#contactResult').css('border', '1px solid #19547c');
								$('#contactResult').css('background', '#b1c7d6');
								$('#contactResult').html(data);
								$('#contactResult').fadeIn();
								clearContactForm();
							});
						},
						error: function(xhrObject, textStatus, errorThrown)
						{
							$('#contactResult').fadeOut(function()
							{
								$('#contactResult').empty();
								$('#contactResult').css('border', '1px solid #19547c');
								$('#contactResult').css('background', '#b1c7d6');
								$('#contactResult').html(xhrObject);
								$('#contactResult').fadeIn();
							});
						}
					});
				}
				else
				{
					$('#contactResult').fadeOut(function()
					{
						$('#contactResult').empty();
						$('#contactResult').css('border', '1px solid #19547c');
						$('#contactResult').css('background', '#b1c7d6');
						$('#contactResult').html(validationResult);
						$('#contactResult').fadeIn();
					});
				}
			});
		});
		
		return false;
	});
}

function setupPortfolio()
{
	$('#portfolioCarousel').waterwheelCarousel('horizontal',
	{
		startingItem: 2,
		flankingItems: 2,
		animationEasing: 'easeOutQuad',
		itemSeparationFactor: 0.25,
		movedToCenter: function(element)
		{
			var id = element.attr('id');
			if(id == "cmw")
			{
				$('#portfolioDescription').load('cmw.html', function()
				{
					if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6")
					{
						$('#portfolioImage').css('margin-top', '-77px');
					}
				});
			}
			else if(id == "rautech")
			{
				$('#portfolioDescription').load('rautech.html', function()
				{
					if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6")
					{
						$('#portfolioImage').css('margin-top', '41px');
					}
				});
			}
			else if(id == "newsCube")
			{
				$('#portfolioDescription').load('newsCube.html', function()
				{
					if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6")
					{
						$('#portfolioImage').css('margin-top', '137px');
					}
				});
			}
			else if(id == "acsConference")
			{
				$('#portfolioDescription').load('acsConference.html', function()
				{
					if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6")
					{
						$('#portfolioImage').css('margin-top', '141px');
					}
				});
			}
		},
		clickedCenter: function(element)
		{
			var imgarray = new Array();
			var newsCubePreview =
			{
				player: 'html',
				content: "<center><img alt=\"News Cube Preview\" src=\"images/portfolio/newsCubeBig.png\" /><br/><a class=\"shadowboxTitle\" href=\"http://www.alphadesigns.com.au/NewsCube\">News Cube</a></center>",
				width: 1036,
				height: 700
			};
			
			var rautechPreview =
			{
				player: 'html',
				content: "<center><img alt=\"Rautech Pty Ltd Preview\" src=\"images/portfolio/rautechBig.png\" /><br/><a class=\"shadowboxTitle\" href=\"http://www.rautech.com.au\">Rautech Pty Ltd</a></center>",
				width: 1036,
				height: 700
			};
			
			var cmwPreview =
			{
				player: 'html',
				content: "<center><img alt=\"Canberra Music Workshop Preview\" src=\"images/portfolio/cmwBig.png\" /><br/><a class=\"shadowboxTitle\" href=\"http://www.canberramusicworkshop.com.au\">Canberra Music Workshop</a></center>",
				width: 1036,
				height: 700
			};
			
			var acsConferencePreview =
			{
				player: 'html',
				content: "<center><img alt=\"ACS Conference Preview\" src=\"images/portfolio/acsConferenceBig.png\" /><br/><a class=\"shadowboxTitle\" href=\"http://www.acsconference.org.au\">ACS Conference</a></center>",
				width: 1036,
				height: 700
			};
			
			if(element.attr('id') == "cmw")
			{
				imgarray[0] = cmwPreview;
				imgarray[1] = rautechPreview;
				imgarray[2] = newsCubePreview;
				imgarray[3] = acsConferencePreview;
			}
			else if(element.attr('id') == "rautech")
			{
				imgarray[0] = rautechPreview;
				imgarray[1] = newsCubePreview;
				imgarray[2] = acsConferencePreview;
				imgarray[3] = cmwPreview;
			}
			else if(element.attr('id') == "newsCube")
			{
				imgarray[0] = newsCubePreview;
				imgarray[2] = acsConferencePreview;
				imgarray[3] = cmwPreview;
				imgarray[4] = rautechPreview;
			}
			else if(element.attr('id') == "acsConference")
			{
				imgarray[0] = acsConferencePreview;
				imgarray[1] = newsCubePreview;
				imgarray[2] = cmwPreview;
				imgarray[3] = rautechPreview;
			}
		
			Shadowbox.open(imgarray);
		}
	});
	
	$('#portfolioDescription').load('cmw.html', function()
	{
		if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6")
		{
			$('#portfolioImage').css('margin-top', '-77px');
		}
	});
}


