// Unit Interactive JS

var text = null, first = null;

var classes = null, country = null, city = null, cHeight = new Array(), cWidth = new Array();

$(document).ready(function(){
						   
	if ($('.cdot, .cmap').length > 0) $('.cdot, .cmap').hide();
	
	$(window).load(initUnitJS);

});

initUnitJS = function () {
	// port slides
	$('div.pictureShow').height( $('div.pictureShow img').height() );
	$('div.galleryNav a').bind('click', function () { gallSwitch(this); });
	
	// clients map
	$('.clients ul li').hover(function () { 
		
		classes = $(this).attr('class');
		classes = classes.split(' ');
		country = classes[0];
		
		country = country.split('-');
		
		if (classes[1] && classes[1] != 'leftEdge') {
			city = classes[1];
			city = city.split('-');
		}
		
		cGrow();
		
	}, cShrink);
	
	// chatty form
	if ($('.cForm').length > 0) {
		$('.cForm *').bind('change', function () { yakky(this); });
		
		$('.cForm input').bind('focus', function () { $(this).addClass('focus'); });
		$('.cForm textarea').bind('focus', function () { $(this).addClass('focus'); });
		$('.cForm input').bind('blur', function () { $(this).removeClass('focus'); });
		$('.cForm textarea').bind('blur', function () { $(this).removeClass('focus'); });
		
		$('.cForm input.formSubmit').bind('click', formSubmit);
	}
}

cGrow = function () {
	var i;
	for (i in country) {
		
		cHeight[i] = $('#' + country[i]).height();
		cWidth[i] = $('#' + country[i]).width();
		
		if ($.browser.msie) {
			$('#' + country[i]).show().animate({
				marginTop: '-3px',
				opacity: 1.0
			}, 200, function () {			
				if (city) {
					var j;
					for (j in city) $('#' + city[j]).show();
				}
			});
		}
		else {
			$('#' + country[i]).show().animate({
				height: $('#' + country[i]).height()/0.9,
				width: $('#' + country[i]).width()/0.9,
				marginTop: '-' + ((($('#' + country[i]).height() * .1)/2) + 5) + 'px',
				marginLeft: '-' + (($('#' + country[i]).width() * .1)/2) + 'px',
				opacity: 1.0
			}, 300, function () {			
				if (city) {
					var j;
					for (j in city) $('#' + city[j]).show();
				}
			});
		}
	}
}

cShrink = function () {	
	city = null;
	$('.cdot, .cmap').stop().hide();

	var i;
	for (i in country) {
		if ($.browser.msie) {
			$('#' + country[i]).css({
				marginTop: '0',
				opacity: 0.0
			});
		}
		else {
			$('#' + country[i]).css({
				height: cHeight[i],
				width: cWidth[i],
				marginTop: '0',
				marginLeft: '0',
				opacity: 0.0
			});
		}
	}
	
	country = null;
}

gallSwitch = function (obj) {
	
	if (obj) {
		
		var left = 724 * ($(obj).html() - 1);
		if (left == 0 ) left = left + 'px';
		else left = '-' + left + 'px';
		
		$('div.pictureShow div').animate({
			marginLeft: left
		}, 1000, 'easeInQuint' );
		
		$('div.galleryNav a').removeClass('current');
		$(obj).attr('class', 'current');
		
	}

}

yakky = function (inpt) {
	
	var name = null, emailAdd = null, num = null, comment = null;
	
	var conv = $('#conversation');
	
	if ($(inpt).attr('id') == 'name') {
		name = $(inpt).val();
		name = name.replace(/^(\s*)/, '');
		nameCheck = name.split(' ');
		if (24 > nameCheck[0].length > 0) first = nameCheck[0];
		else if (nameCheck[0].length >= 24) first = 'Partner';
		else first = null;
		
		conv.css({ color: '#77F4FF' });
		$(inpt).removeClass('errorFocus');
		
		if (first && !emailAdd) text = 'Thanks ' + first + '! Now enter the best email address where we can reach you.';
		else if (first && !comment) text = 'Thanks ' + first + '! So, what&rsquo;s on your mind?';
		else if (!first) {
			text = 'Not sure I&rsquo;ve heard such a name. How do you pronounce that?';
			conv.css({ color: '#FFAEAE' });
			$(inpt).addClass('errorFocus');
			name = null;
		}
		else text = 'Thanks, ' + first + '! Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'email') {
		emailAdd = $(inpt).val();
		
		conv.css({ color: '#77F4FF' });
		$(inpt).removeClass('errorFocus');
		
		var ereg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		if (ereg.test(emailAdd) == false) {
			text = 'Not sure that&rsquo;s a valid email. We do need a valid email to get back to you.';
			conv.css({ color: '#FFAEAE' });
			$(inpt).addClass('errorFocus');
		}
		else if (first && !num) text = 'Uploading to spam server... Ha! Just kidding. How about a phone number? (It&rsquo;s not required.)';
		else if (first && !comment) text = 'Uploading to spam server... Ha! Just kidding. So, ' + first + ', what&rsquo;s on your mind?';
		else if (!first) text = 'Uploading to spam server... Ha! Just kidding. You got a name, mystery person?';
		else text = 'Uploading to spam server... Ha! Just kidding. Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'phone') {
		num = $(inpt).val();
		var numAdd = 0;
		
		conv.css({ color: '#77F4FF' });
		
		for (i=0; i<num.length; i++) {
			var digit = parseInt(num.charAt(i));
			if (digit) numAdd += digit;
		}
		
		if (first && !emailAdd) text = 'Great! Now enter the best email address where we can reach you.';
		else if (first && !comment) text = 'Great! Here&rsquo;s another number: ' + numAdd + '. It&rsquo;s your lucky number. So, ' + first + ', what&rsquo;s on your mind?';
		else if (!first) text = 'Great! Here&rsquo;s another number: ' + numAdd + '. Its your lucky number. So, what should we call you?';
		else text = 'Great! Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'comment') {		
		comment = $(inpt).val();
		
		conv.css({ color: '#77F4FF' });
		$(inpt).removeClass('errorFocus');
		
		var linkreg = comment.match(/http:\/\//gi);
		linkreg = (linkreg) ? linkreg.length : 0;
		
		if (linkreg.length > 3) {
			text = 'Um&hellip; I think you&rsquo;ve got some spam here. We only accept 3 URLs or less. Please try again.';
			conv.css({ color: '#FFAEAE' });
			$(inpt).addClass('errorFocus');
		}
		else if (name && emailAdd && name.length < 24) text = 'Well, ' + name + ', it&rsquo;s been fun. Hit submit, and a Unitard will get back to you shortly.';
		else if (!name && !emailAdd) text = 'Hmmm... interesting. Still not sure I got your name or email.';
		else if (name && !emailAdd) text = 'Still not sure I got your email. We&rsquo;ll need it to get back to you.';
		else if (!name && emailAdd) text = 'Hmmm... interesting. Still not sure I got your name. We do require a name.';
		else text = 'Well, it&rsquo;s been fun. Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}

}

formSubmit = function () {
	
	var name = ( $('#name').attr('class').match('errorFocus') ) ? false : $('#name').val(), 
	emailAdd = ( $('#email').attr('class').match('errorFocus') ) ? false : $('#email').val(), 
	num = $('#phone').val(),
	comment = ( $('#comment').attr('class').match('errorFocus') ) ? false : $('#comment').val();
	
	var linkreg = comment.match(/http:\/\//gi);
	linkreg = (linkreg) ? linkreg.length : 0;
	
	var conv = $('#conversation');
	
	if ( name && emailAdd != false && comment && linkreg < 4 ) {
		
		var iFrameID = 'formSubmitIframe';
		
		var iframe = document.createElement('iframe');
		$(iframe).attr({ id: iFrameID, name: iFrameID, action: "#", src: 'about:blank' });
		$('body').append(iframe);
		if (self.frames[iFrameID].name != iFrameID) { self.frames[iFrameID].name = iFrameID; }
		
		document.cForm.submit();
		
		$('#cForm input#name, #cForm input#email, #cForm input#phone, #cForm textarea#comment').val('');
		
		text = 'Your email was succesfully sent. We&rsquo;ll get back to you soon for a longer chat.';
		conv.html(text);

	}
	else {
		text = 'Whoops! Looks as though you&rsquo;re missing ';
		conv.css({ color: '#FFAEAE' });
		
		if (!name) {
			text += 'a name.';
			$('#name').addClass('errorFocus');
		}
		else if (!emailAdd) {
			text += 'a valid email address.';
			$('#email').addClass('errorFocus');
		}
		else if (!comment) {
			text += 'a comment.';
			$('#comment').addClass('errorFocus');
		}
		else if (linkreg > 3) {
			text = 'Um&hellip; still think you&rsquo;ve got some spam here. Remember: We only accept 3 URLs or less.';
			$('#comment').addClass('errorFocus');
		}
		
		conv.html(text);
	}
	
}
	
// sIFR _________________________________________

if(typeof sIFR == "function"){
	sIFR.replaceElement(".phone h4", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#666666", sBgColor:"#EBEBEB", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".dualLong h4 strong, .dualShort h4 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#999999", sBgColor:"#EBEBEB", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".ourWork dt strong, .ourWork h4 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#000000", sBgColor:"#EBEBEB", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".ourWork h3", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#999999", sBgColor:"#FFFFFF", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
//	sIFR.replaceElement(".mainFeature h2.contrast", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#FFFFFF", sBgColor:"#001111", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
//	sIFR.replaceElement(".mainFeature h2", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#D3CAB4", sBgColor:"#001111", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".contact h5", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#555555", sBgColor:"#FFFFFF", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".learnMore h4 strong, .contact h4 strong, .clients h3 strong, .fullSection h4 strong, .values h4 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#FFFFFF", sBgColor:"#7FAA2D", sWmode: "transparent", sCase: "upper", sFlashVars: "offsetTop=5&letterSpacing=-1"}));
	sIFR.replaceElement(".team1 h5, .team2 h5, .team3 h5, .team4 h5", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#000000", sBgColor:"#FFFFFF", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	sIFR.replaceElement(".innerTitle h2", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#D3CAB4", sBgColor:"#001111", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
	//sIFR.replaceElement(".portPage .logoNav li#work", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#FFFFFF", sBgColor:"#001111", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	//sIFR.replaceElement(".logoNav li", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#7A7A7A", sHoverColor: "#7FAA2D", sBgColor:"#001111", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	sIFR.replaceElement(".caseTitle h3", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#000000", sBgColor:"#BCBCBC", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	sIFR.replaceElement(".caseInfo h4 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#FFFFFF", sBgColor:"#000000", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	sIFR.replaceElement(".moreCases h4 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#000000", sBgColor:"#E2E2E2", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	sIFR.replaceElement(".contactTitle h4", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#FFFFFF", sBgColor:"#000000", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1" }));
	sIFR.replaceElement(".contactCopy h5 strong", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#000000", sBgColor:"#EBEBEB", sWmode: "transparent", sCase: "upper", sFlashVars: "textAlign=center&letterSpacing=-1" }));
	sIFR.replaceElement(".dualLong h4#white", named({sFlashSrc: "fonts/tradegothicboldcond20.swf", sColor: "#ffffff", sBgColor:"#7faa2d", sWmode: "transparent", sCase: "upper", sFlashVars: "letterSpacing=-1"}));
};