$(document).ready(function() {
	
	var _main = $('body').attr('id');
	
	/* Client Login form */
	if ($('form').hasClass('login')) {
		$('form.login .type-text').each(function(index) {
			var parent = $(this);
			if ($('input', this).val() !== "") {
				$('label', parent).css({'display': 'none'});
			}
			$('input', this).focus(function() {
				$('label', parent).css({'display': 'none'});
			}).blur(function(){
				if ($(this).val() == "") {
					$('label', parent).css({'display': 'inline'});
				}
			});
		});
		$('form.login #submit').click(function() {
			//alert('click');
			//return false;
		});
	}
	
	/* Home */
	if (_main == 'home') {
		
		var totalLoaded = 0;
		$('#manifesto').css({'display': 'none'});
		
		function animatePhotos() {
			totalLoaded = totalLoaded + 1;
			if (totalLoaded == 2) {
				$('#manifesto').css({'display': 'block'});
				$('#img3').fadeIn(3000, function() {
					$(this).fadeOut(3000);
					$('#img4').fadeIn(2000);
				});
			}
		}
		
		$('#manifesto img').each(function() {
			var thisImg = $(this);
			if(this.complete) { 
				$(this).load();
				animatePhotos();
			} else {
				//$(this).load(function() { animatePhotos(); });
				$(this).ready(function() {
					animatePhotos();
				});
			};
		});
	}
	
	/* Work */
	if (_main == 'work') {
		
		var flashvars = {};
			flashvars.xml = "xml/workconfig.xml";
			
		var params = {};
			params.menu = "true";
			params.quality = "high";
			params.scale = "showall";
			params.salign = "";
			params.wmode = "transparent";
			params.bgcolor = "#ffffff";
			params.allowfullscreen = "true";
			params.allowscriptaccess = "always";
			
		var attributes = {};
			attributes.id = "flashContent";
			attributes.name = "flashContent";

		swfobject.embedSWF("swf/work.swf", "flashContent", "772", "1480", "10.0.0", "js/expressInstall.swf", flashvars, params, attributes);
	}

	/* Case Studies */
	if (_main == 'cases') {
		
		var flashvars = {};
			flashvars.xml = "xml/casesconfig.xml";
			flashvars.filter = false;
			
		var params = {};
			params.menu = "true";
			params.quality = "high";
			params.scale = "showall";
			params.salign = "";
			params.wmode = "transparent";
			params.bgcolor = "#ffffff";
			params.allowfullscreen = "true";
			params.allowscriptaccess = "always";
			
		var attributes = {};
			attributes.id = "flashContent";
			attributes.name = "flashContent";

		swfobject.embedSWF("swf/work.swf", "flashContent", "772", "1443", "10.0.0", "js/expressInstall.swf", flashvars, params, attributes);
	}

	
	/* Contact */
	if (_main == 'contact') {
		
		if ($('form').hasClass('contact')) {
			$('form.contact .type-text').each(function(index) {
				var parent = $(this);
				if ($('input', this).val() !== "") {
					$('label', parent).css({'display': 'none'});
				}
				$('input', this).focus(function() {
					$('label', parent).css({'display': 'none'});
				}).blur(function(){
					if ($(this).val() == "") {
						$('label', parent).css({'display': 'inline'});
					}
				});
			});
			$('form.contact .type-message').each(function(index) {
				var parent = $(this);
				if ($('textarea', this).val() !== "") {
					$('label', parent).css({'display': 'none'});
				}
				$('textarea', this).focus(function() {
					$('label', parent).css({'display': 'none'});
				}).blur(function(){
					if ($(this).val() == "") {
						$('label', parent).css({'display': 'inline'});
					}
				});
			});
			
			var submitOkay = true;
			function fixError() { $(this).parent().removeClass('error'); }
			function addError($input) {
				$($input).parent().addClass('error');
				submitOkay = false;
			}
			$('#email').focus(fixError);
			$('#name').focus(fixError);
			
			$('form.contact #submit').click(function() {
				submitOkay = true;
				if ($('[name=name]').val() == "") { addError($('form.contact #name')); }
				if ($('[name=email]').val() == "") { addError(('form.contact #email')); }
				else {
					var emailRegEx = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/;
					var email = $('[name=email]').val();
					if(email.match(emailRegEx)){}
					else { addError($('form.contact #email')); }
				}
				if (submitOkay) {
					dataString = $('form.contact').serialize();
					$.load("contact.php", dataString);
				}
				return false;
			})
		}
		
		$("#video").fancybox({
			'titlePosition'		: 'inside',
			'width'				: 297,
			'height'			: 505,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe',
			'autoDimensions'	: true
		});
	}
	
	/* approach */
	if (_main == 'approach') {
		
		var screenTracker = $(window).height();
		var accountForMargin = 5 + 2;
		var constantHeight = $('#access').height() + accountForMargin + $('#category').height() + $('h2').height();
		var innerHeight = $('#services .tabPages').height();
		var hashLocation = "";
		
		/*adjust div height */
		function adjustHeight() {
			
			var hashLocationName = hashLocation;
			if (hashLocationName == "") {
				hashLocationName = $('.tabNavigation li:nth-child(1) a').attr('href');
				hashLocationName = hashLocationName.replace('#', '');
			}
			
			var browserHeight = $(window).height();
			var newHeight = $(window).height() - $('#wrapper').height();
			var servicesHeight = $('#services').height();
			var newInnerHeight = $('.tabPages').height();//parseFloat($('.tabPages div[id*='+ hashLocationName +']').attr('rel'));//$('.tabPages div[id*='+ hashLocationName +']').height();
			var viewHeight = (constantHeight + newInnerHeight);
			
			if (browserHeight < screenTracker) { // window contracting
				
				var offset = screenTracker - browserHeight;
				newHeight = servicesHeight - offset;
				if (newHeight <= newInnerHeight) { newHeight = newInnerHeight; }
				
				if (servicesHeight >= newHeight) {
					$('#services').css({'height': newHeight +'px'}); 
				}
				
			} else { // window expanding
				
				if (servicesHeight < viewHeight) {
					
					newHeight = servicesHeight + newHeight;
					if (newHeight < newInnerHeight) { newHeight = newInnerHeight; }
					
					if (servicesHeight <= newHeight) {
						$('#services').css({'height': newHeight +'px'}); 
					}
				} else if (browserHeight > (constantHeight + servicesHeight)) {
					
					newHeight = (browserHeight - (constantHeight + servicesHeight)) + servicesHeight - 27;
					$('#services').css({'height': newHeight +'px'});
				}
			}
			screenTracker = $(window).height(); // reset
		}
		
		/* set visible div height */
		function setInnerHeight() {
			
			var hashLocationName = hashLocation;
			if (hashLocationName == "") {
				hashLocationName = $('.tabNavigation li:nth-child(1) a').attr('href');
				hashLocationName = hashLocationName.replace('#', '');
			}
			var newInnerHeight = parseFloat($('.tabPages div[id*='+ hashLocationName +']').attr('rel'));
			if (newInnerHeight < $('#services .tabNavigation').height()) { newInnerHeight = $('#services .tabNavigation').height(); }
			innerHeight = newInnerHeight;
			
		}
		
		/* window resize event */
		$(window).resize(function() {
			//adjustHeight();
		});
		
		/* set's display and link click */
		function formatTabPages() {
			
			$('.tabNavigation li').each(function(index) {
				
				if (index == 0) {
					$('a', this).addClass('selected');
					hashLocation = $('a', this).attr('href').replace('#', '');
					$('.tabPages div[id*='+ hashLocation +']').css({'display': 'block'});
				}
				
				$('a', this).click(function() {
					
					$('.tabNavigation li a').removeClass('selected');
					$(this).addClass('selected');
					
					var newClick = $(this).attr('href').replace('#', '');
					if (newClick == hashLocation) {
						return false;
					} else {
						// remove old stuff
						$('.tabNavigation li a').removeClass('selected');
						$('.tabPages div[id*='+ hashLocation +']').css({'display':'none'});
						
						//show new stuff
						$(this).addClass('selected');
						$('.tabPages div[id*='+ newClick +']').css({'display':'block'});
						
						setInnerHeight();
						//adjustHeight();
					}
					hashLocation = newClick;
					
					return false;
				});
			});
		}
		
		$('.tabPages').children().each(function(index) {
			
			$(this).attr('rel', $(this).height());
		});
		
		$('.tabPages').children().css({'display':'none'});
		
		setInnerHeight();
		//adjustHeight();
		
		formatTabPages();
		
		// flash
		var flashvars = {};
			
		var params = {};
			params.menu = "true";
			params.quality = "high";
			params.scale = "showall";
			params.salign = "";
			params.wmode = "transparent";
			params.bgcolor = "#ffffff";
			params.allowfullscreen = "true";
			params.allowscriptaccess = "always";
			
		var attributes = {};
			attributes.id = "flashContent";
			attributes.name = "flashContent";

		swfobject.embedSWF("swf/approach.swf", "flashContent", "777", "500", "10.0.0", "js/expressInstall.swf", flashvars, params, attributes);
	}
	
	// audio on all pages
	var flashvars = {};
		
	var params = {};
		params.menu = "true";
		params.quality = "high";
		params.scale = "showall";
		params.salign = "";
		params.wmode = "transparent";
		params.bgcolor = "#ffffff";
		params.allowfullscreen = "true";
		params.allowscriptaccess = "always";
		
	var attributes = {};
		attributes.id = "flashAudio";
		attributes.name = "flashAudio";

	swfobject.embedSWF("swf/audioPlayer.swf", "flashAudio", "119", "33", "9.0.0", "js/expressInstall.swf", flashvars, params, attributes);
	
});

function muteAudio() { thisMovie("flashAudio").playAudio(false); }

function playAudio() { thisMovie("flashAudio").playAudio(true); }

function thisMovie(movieName) { return document.getElementById(movieName); }

/* Work & Case Studies - ExternalInterface from flash */
function changeHeight(flashHeight)
{
	$('#flashContainer').animate({height: flashHeight +'px'}, 1000, 'linear');
}

function scrollToTop() {
	var numHTML = $('html').scrollTop();
	var numBODY = $('body').scrollTop();
	var xPos = 0;
	var tag = 'html,body';
	if (numHTML > numBODY) { xPos = numHTML; tag = 'html'; }
	if (numHTML < numBODY) { xPos = numBODY; tag = 'body'; }
	if (xPos !== 0){
		var speed = xPos * 1.25;
		$(tag).animate({ scrollTop: 0}, speed, 'linear');
	}
}

