if(navigator.userAgent.match(/msie/i)) window.location = 'http://www.erobertwald.com/ncomp.html';

window.onload = init;

var locArr = new Array();
var qryObj = new Object();

var contactFormListener;

var xhr_sndEml;

function applyEvent(trg, evt, fnc, cap)
{
	if(trg.addEventListener)
	{
		//alert('applying ' + evt + ' event to ' + trg.id + ' as addEventListener');
		trg.addEventListener(evt, fnc, cap);
	}
	else if(trg.attachEvent)
	{
		//alert('applying ' + evt + ' event to ' + trg.id + ' as attachEvent');
		evt = 'on' + evt;
		trg.attachEvent(evt, fnc);
	}
	else
	{
		//alert('applying on' + evt + ' event to ' + trg.id);
		evt = 'on' + evt;
		trg.evt = fnc;
	}
}

function deleteEvent(trg, evt, fnc, cap)
{
	if(document.removeEventListener)
	{
		trg.removeEventListener(evt, fnc, cap);
	}
	else if(document.detachEvent)
	{
		evt = 'on' + evt;
		trg.detachEvent(evt, fnc);
	}
	else
	{
		evt = 'on' + evt;
		trg.evt = null;
	}
}

function init()
{
	getLoc();
	ranBan();
	if(document.getElementById('ego'))
	{
		ranEgo();
	}
	if(locArr[0] == 'Home')
	{
		init_contact();
		showContactFormBtns();
	}
	if(locArr[0] != 'Home'){
		var bnrLbl = document.getElementById('bnrLbl');
		bnrLbl.style.backgroundImage = 'url(/img/bnrLbl_' + locArr[0].toLowerCase() + '.png)';
		var portfolioThumbnails = document.getElementById('portfolioThumbnails_0');
		for(var i = 0; i < portfolioThumbnails.childNodes.length; i++)
		{
			var child = portfolioThumbnails.childNodes[i];
			if(child.id != qryObj[locArr[0]])
			{
				child.onclick = portfolioThumbnailOnclick;
				child.style.cursor = 'pointer';
			}
		}
		if(document.getElementById('portfolioThumbnails_1'))
		{
			var portfolioThumbnails = document.getElementById('portfolioThumbnails_1');
			for(var i = 0; i < portfolioThumbnails.childNodes.length; i++)
			{
				var child = portfolioThumbnails.childNodes[i];
				if(child.id != qryObj[locArr[0]])
				{
					child.onclick = portfolioThumbnailOnclick;
					child.style.cursor = 'pointer';
				}
			}
		}
	}
}

function init_contact()
{

	nameField = document.getElementById('nameField');
	emailField = document.getElementById('emailField');
	messageField = document.getElementById('messageField');
	sendBtn = document.getElementById('sendBtn');
	clearBtn = document.getElementById('clearBtn');
	
	if(navigator && navigator.appName.match(/Microsoft Internet Explorer/))
	{
		var av = navigator.appVersion.split(";");
		var msie = 0;
		for(var i = 0; i < av.length; i++)
		{
			if(av[i].match(/MSIE/))
			{
				msie = av[i].replace(/MSIE/, '');
				msie = msie.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			}
		}
		if(msie < 9.0)
		{
			nameField.parentNode.style.width = (nameField.parentNode.offsetWidth - 8) + 'px';
			emailField.parentNode.style.width = (emailField.parentNode.offsetWidth - 8) + 'px';
			messageField.parentNode.style.width = (messageField.parentNode.offsetWidth - 8) + 'px';
		}
		if(msie == 9.0) messageField.style.width = (messageField.offsetWidth - 16) + 'px';
	}
	
	applyEvent(nameField, 'focus', function() { togglePlaceholder('nameField', 'focus'); }, false);
	applyEvent(emailField, 'focus', function() { togglePlaceholder('emailField', 'focus'); }, false);
	applyEvent(messageField, 'focus', function() { togglePlaceholder('messageField', 'focus'); }, false);
	
	applyEvent(nameField, 'blur', function() { togglePlaceholder('nameField', 'blur'); }, false);
	applyEvent(emailField, 'blur', function() { togglePlaceholder('emailField', 'blur'); }, false);
	applyEvent(messageField, 'blur', function() { togglePlaceholder('messageField', 'blur'); }, false);

	if(nameField.value != '') togglePlaceholder('nameField', 'focus');
	if(emailField.value != '') togglePlaceholder('emailField', 'focus');
	if(messageField.value != '') togglePlaceholder('messageField', 'focus');
	
	applyEvent(sendBtn, 'click', function() { sendContactForm(nameField.value, emailField.value, messageField.value); }, false);
	applyEvent(clearBtn, 'click', clearContactForm, false);

	showContactFormBtns();
}

function ranBan(){
	if(locArr[0] == 'Home')
	{
		var ban = document.getElementById('bnr_home');
	}
	else
	{
		var ban = document.getElementById('bnr');
	}
	var bg = new Array('bnrBg_00.jpg', 'bnrBg_01.jpg', 'bnrBg_02.jpg', 'bnrBg_03.jpg');
	var ran = Math.floor(Math.random() * bg.length);
	ban.style.backgroundImage = 'url(/img/' + bg[ran] + ')';
}

function ranEgo(){
	var ego = document.getElementById('ego');
	var bg = new Array('ego_00.png', 'ego_01.png', 'ego_02.png', 'ego_03.png');
	var ran = Math.floor(Math.random() * bg.length);
	ego.style.backgroundImage = 'url(/img/' + bg[ran] + ')';
}

function getLoc()
{
	var sLoc = window.location.toString().split('?');
	var loc = sLoc[0];
	var qry = sLoc[1];
	sLoc = loc.toString();
	if(sLoc.match(/8888/))
	{
		sLoc = sLoc.split('8888');
	}
	else
	{
		sLoc = sLoc.split('.com');
	}
	if(sLoc.length > 1)
	{
		var locVars = sLoc[1].split('/');
		for(var i = 1; i < locVars.length; i++)
		{
			locArr[i - 1] = locVars[i];
		}
	}
	if(locArr[0] == '')
	{
		locArr[0] = 'Home';
	}
	if(qry)
	{
		var qryVars = qry.toString().split('&');
		for(var prop in qryVars)
		{
			var qv = qryVars[prop].split('=');
			qryObj[qv[0]] = qv[1];
		}
	}
}

function togglePlaceholder(trgt, state)
{
	if(document.getElementById(trgt + '_placeholder'))
	{
		switch(state)
		{
			case 'focus':
				document.getElementById(trgt + '_placeholder').style.visibility = 'hidden';
				break;
			case 'blur':
				if(document.getElementById(trgt).value == '') document.getElementById(trgt + '_placeholder').style.visibility = 'visible';
				break;
			default:
				if(document.getElementById('nameField').value != '') document.getElementById('nameField_placeholder').style.visibility = 'hidden';
				if(document.getElementById('emailField').value != '') document.getElementById('emailField_placeholder').style.visibility = 'hidden';
				if(document.getElementById('messageField').value != '') document.getElementById('messageField_placeholder').style.visibility = 'hidden';
				break;
		}
	}
}

function showContactFormBtns()
{
	var nameField = document.getElementById('nameField');
	var emailField = document.getElementById('emailField');
	var messageField = document.getElementById('messageField');
	var clearBtn = document.getElementById('clearBtn');
	var sendBtn = document.getElementById('sendBtn');
	if(nameField)
	{
		if(nameField.value != '' || emailField.value != '' || messageField.value != '')
		{
			clearBtn.style.display = 'block';
		}
		else
		{
			clearBtn.style.display = 'none';
		}
		if(nameField.value == '' || emailField.value == '' || messageField.value == '')
		{
			sendBtn.style.display = 'none';
			nameField = '';
			emailField = '';
			messageField = '';
		}
		else
		{
			if(validateContactForm())
			{
				sendBtn.style.display = 'block';
			}
			else
			{
				sendBtn.style.display = 'none';
			}
		}
		if(sendBtn.style.display == 'none' && clearBtn.style.display == 'none')
		{
			document.getElementById('formButton_container').style.height = '0px';
		}
		contactFormListener = setTimeout('showContactFormBtns()', 0);
	}
}

function clearContactForm()
{
	var nameField = document.getElementById('nameField');
	var emailField = document.getElementById('emailField');
	var messageField = document.getElementById('messageField');
	nameField.value = '';
	emailField.value = '';
	messageField.value = '';
	document.getElementById('nameField_placeholder').style.visibility = 'visible';
	document.getElementById('emailField_placeholder').style.visibility = 'visible';
	document.getElementById('messageField_placeholder').style.visibility = 'visible';
}

function validateContactForm()
{
	var valid = false;
	var success;
	var nameField = document.getElementById('nameField');
	var nameField_val = nameField.value;
	var emailField = document.getElementById('emailField');
	var emailField_val = emailField.value;
	var messageField = document.getElementById('messageField');
	var messageField_val = messageField.value;
	if(nameField_val != '' && emailField_val != '' && messageField_val != '')
	{
		var ef = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (ef.test(emailField_val)) valid = true;
	}
	return valid;
}

function sendContactForm(nam, eml, msg)
{
	xhr_sndEml = constructAjaxRequest();
	if(xhr_sndEml)
	{
		var sndPrms = 'nam=' + nam + '&eml=' + eml + '&msg=' + msg;
		xhr_sndEml.onreadystatechange = receiveContactFormResponse;
		xhr_sndEml.open('GET', '/inc/eml.php?' + sndPrms, true);
		//xhr_sndEml.open('POST', '/inc/eml.php', true);
		//xhr_sndEml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		//xhr_sndEml.setRequestHeader('Content-length', sndPrms.length);
		//xhr_sndEml.setRequestHeader('Connection', 'close');
		//xhr_sndEml.send(sndPrms);
		xhr_sndEml.send(null);
	}
	else
	{
		alert('Unable to send the request.');
	}
}

function receiveContactFormResponse()
{
	var xhr = xhr_sndEml;
	var xhrRsp = 'failed to initialize';
	var con = document.getElementById('contactForm');
	if(xhr.readyState == 4)
	{
		if(xhr.status == 200)
		{
			xhrRsp = xhr.responseText;
			if(xhrRsp == "success")
			{
				clearTimeout(contactFormListener);
				document.getElementById('formHeader').innerHTML = 'Thanks for getting in touch with me!<br /><br />';
				var str = 'The following information was successfully sent:<br /><br />';
					str += '&nbsp;&nbsp;&nbsp;Name: <i>' + nameField.value + '</i><br />';
					str += '&nbsp;&nbsp;&nbsp;Email: <i>' + emailField.value + '</i><br />';
					str += '&nbsp;&nbsp;&nbsp;Name: <i>' + messageField.value + '</i>';
				con.innerHTML = str;
				con.style.padding = '4px';
			}
			else
			{
				var cfa = document.getElementById('contactForm_alert')
				cfa.innerHTML = 'Oops!<br /><br />There was an error while sending your message. Please check your information and try again. If the problem persists, email us at <a href="mailto:contact@erobertwald.com?subject=Message from ' + nameField.value + ' (' + emailField.value + ') via fifthvenue.com&body=' +  messageField.value + '">contact@erobertwald.com</a>.';
				cfa.style.display = 'block';
			}
		}
	}
	else
	{
		xhrRsp = 'Loading...';
	}
}

function constructAjaxRequest()
{
	var xhr;
	if(window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else
	{
		if(window.ActiveXObject)
		{
			try
			{
				xhr = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)
			{
			}
		}
	}
	return xhr;
}

function portfolioThumbnailOnclick(e) {
	var trgt;
	if (!e)
	{
		var e = window.event;
	}
	if (e.target)
	{
		trgt = e.target;
	}
	else if (e.srcElement)
	{
		trgt = e.srcElement;
	}
	if (trgt.nodeType == 3)
	{
		// defeat Safari bug
		trgt = trgt.parentNode;
	}
	//alert(trgt.parentNode.id);
	window.location = './?' + locArr[0] + '=' + trgt.parentNode.id;
}

