function ajaxExecute(args) {
	var append = "";
	if(args.append)
		append = args.append;
	$.ajax({
		url: args.link,
		dataType: 'xml',
		data: 'noLayout=true&'+append,
		success: function (data, textStatus) {
			if(textStatus == 'success') {
				loadedData = data;
				dataLoaded = true;
			} else {
				loadedData = 'We were unable to load the requested page.';
				dataLoaded = true;
			}
			args.onComplete();
		},
		error: function () {
			loadedData = 'We were unable to load the requested page.';
			dataLoaded = false;
			args.onComplete();
		}
	});
}
function submitContactForm(id) {
	var data = "";
	var mod = false;
	$('input, textarea', $('#'+id)).each(function() {
		if(mod)
			data += "&";
		data += $(this).attr('name')+"="+$(this).val();
		mod = true;
	}); 
	data += "&Submit=true";
	$.ajax({
		url: baseUrl+"/emailProcess.php",
		data: data,
		dataType: 'xml',
		type: 'POST',
		success: function (data, textStatus) {
			var result = "";
			$(data).find("result").each(function() {
				result = $(this).text();
			});
			var message = "";
			$(data).find("message").each(function() {
				message = $(this).text();
			});
			if(textStatus == 'success' && result == 'success') {
				$('#messageContainer').html(message);
				$('#'+id).hide();
			} else {
				$('#messageContainer').html(message);
			}
		},
		error: function () {
			$('#messageContainer').html("There was an error submitting your comment.<br /><br />");
		}
	});
}
function replaceTextWithImageNT(element, color, bgColor, fontSize) {
	if(!$('img', element).length) {
		$(element).html('<img src = "'+baseUrl+'/textImageLoad/ne_'+$(element).attr('alt')+'_'+color+'_'+bgColor+'_0_0_'+fontSize+'.png" />');
	}	
}
function replaceTextWithImageWT(element, color, bgColor, fontSize, width, height) {
	if(!$('img', element).length) {
		$(element).html('<img src = "'+baseUrl+'/textImageLoad/tiw_'+$(element).attr('alt')+'_'+color+'_'+bgColor+'_0_'+fontSize+'_'+width+'_'+height+'.png" height = "'+height+'" width = "'+width+'" class = "ieFix" />');
	}	
}
function URLEncode(url) {var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()"; var HEX = "0123456789ABCDEF";var plaintext = url;var encoded = "";for (var i = 0; i < plaintext.length; i++ ) {var ch = plaintext.charAt(i);if (ch == " ") {encoded += "+"; } else if (SAFECHARS.indexOf(ch) != -1) {encoded += ch;} else {var charCode = ch.charCodeAt(0);if (charCode > 255) {alert( "Unicode Character '"+ ch+ "' cannot be encoded using standard URL encoding.\n" +"(URL encoding only supports 8-bit characters.)\n" +"A space (+) will be substituted." );encoded += "+";} else {encoded += "%";encoded += HEX.charAt((charCode >> 4) & 0xF);encoded += HEX.charAt(charCode & 0xF);}}}return encoded;}
function URLDecode(url) {var HEXCHARS = "0123456789ABCDEFabcdef";var encoded = url;var plaintext = "";var i = 0;while (i < encoded.length) {var ch = encoded.charAt(i);if (ch == "+") {plaintext += " ";i++;} else if (ch == "%") {if (i < (encoded.length-2)&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {plaintext += unescape( encoded.substr(i,3) );i += 3;} else {alert( 'Bad escape combination near ...' + encoded.substr(i) );plaintext += "%[ERROR]";i++;}} else {plaintext += ch;i++;}} return plaintext;}
jQuery.fn.log = function (msg) {console.log("%s: %o", msg, this);return this;};
function getFlashObject(container) {
	var flashObj = $('embed', container);
	if(flashObj.length > 0)
		return flashObj[0];
	else {
		flashObj = $('object', container);
		if(flashObj.length > 0)
			return flashObj[0];
		else
			return false;
	}
}
function setFragmentAndTitle() {
	if(windowFragment)
		window.location.href = "#"+windowFragment;
	if(pageTitle) {
		window.document.title = pageTitle;
		setTimeout(function() {
			window.document.title = pageTitle;
		}, 100);
	}
}
function hidePortfolio(showPortfolios, continueShowing) {
	$('#seePhotosInAction').fadeOut(500);
	$('.photosNav').fadeOut(500);
	$("#portfolioTitle").fadeOut(500, function() {
		$(this).empty();
		$(this).show();
	});
	$('#portfolioImages').animate({top: '675px'}, 1000, 'easeInCubic', function() {
		$('#portfolioTitle').empty();
		$('#seePhotosInAction').empty();
		$('#portfolioImages').empty();
		$('#portfolioLargeImage').empty();
		if(showPortfolios)
			showAllPortfolios(continueShowing);
		else if($.isFunction(continueShowing))
			continueShowing();
	});
}
function hidePage(continueShowing) {
	$('#pageContent').animate({top: '700px'}, 500, 'easeInCubic', function() {
		$(this).empty();
		$('#pageContentContainer').hide();
		showAllPortfolios(continueShowing);
	});
}
function showAllPortfolios(continueShowing) {
	setupMorePortfolios();
	$('#allPortfolios').fadeIn(500);
	$('#portfolio').fadeOut(500);
	$('#backToPortfolios').fadeOut(500);
	$('#portfolioNavigations').animate({top: '0px'}, 1000, 'easeInOutCubic', function() {
		animating = false;
		$('#portfolioContainer').hide();
		activeLink = false;
		currentPosition = 'allPortfolios';
		if(continueShowing) 
			showNewestAdditions();
	});
}
function prepShowAllPortfolios(continueShowing) {
	if(!animating) {
		animating = true;
		if(currentPosition == 'index') {
			$("#newestAdditions").animate({top: 430}, 1000, 'easeInCubic', function() {
				animating = false;
				$(this).parent().hide();
			});
			$('#newestPortfolios').fadeOut(300);
			slideAllPortfoliosLeft();
			currentPosition = "allPortfolios";
		} else if(currentPosition == 'portfolio') 
			hidePortfolio(true, continueShowing);
		else if(currentPosition == 'page') 
			hidePage(continueShowing);
	}
}
function morePortfolios() {
	var portfolioNavigations = $("#portfolioNavigations");
	var currentTop = parseInt($(portfolioNavigations).css('top'));
	var newTop = (currentTop - 3 * portfolioNavItemHeight);
	if(Math.abs(currentTop) < ((portfoliosCount - 6) * portfolioNavItemHeight)) {
		if(!$(portfolioNavigations).hasClass("animating")) {
			$(portfolioNavigations).addClass("animating").animate({top: newTop}, 500, 'easeOutCubic', function() {
				$("#previousPortfolios").show();
				$(this).removeClass("animating").attr("rememberMe", $(this).css('top'));
				if(Math.abs(currentTop) >= ((portfoliosCount - 9) * portfolioNavItemHeight)) {
					$("#morePortfolios").hide();
				}
			});
		}
	}
}
function previousPortfolios() {
	var portfolioNavigations = $("#portfolioNavigations");
	var currentTop = parseInt($(portfolioNavigations).css('top'));
	if(currentTop <= 0) {
		var newTop = (currentTop + 3 * portfolioNavItemHeight);
		if(!$(portfolioNavigations).hasClass("animating")) {
			$(portfolioNavigations).addClass('animating').animate({top: newTop}, 500, 'easeOutCubic', function() {
				$("#morePortfolios").show();
				$(this).removeClass("animating").attr("rememberMe", $(this).css('top'));
				if(newTop == 0) 
					$("#previousPortfolios").hide();
			});
		}
	}
}
function morePhotos() {
	var portfolioImages = $("#portfolioImages");
	var currentTop = parseInt($(portfolioImages).css('top'));
	var newTop = (currentTop - 244);
	if(Math.abs(currentTop) < (Math.ceil(photosCount / 3) * 244)) {
		if(!$(portfolioImages).hasClass("animating")) {
			$(portfolioImages).addClass("animating").animate({top: newTop}, 500, 'easeOutCubic', function() {
				if(Math.abs(currentTop) == (Math.ceil(photosCount / 3) * 244) - 488) {
					$("#morePhotos").hide();
				}
				$(this).removeClass('animating');
				$("#previousPhotos").show();
			});
		}
	}
}
function previousPhotos() {
	var portfolioImages = $("#portfolioImages");
	var currentTop = parseInt($(portfolioImages).css('top'));
	if(currentTop <= 0) {
		var newTop = (currentTop + 244);
		if(!$(portfolioImages).hasClass("animating")) {
			$(portfolioImages).addClass('animating').animate({top: newTop}, 500, 'easeOutCubic', function() {
				$(this).removeClass('animating');
				$("#morePhotos").show();
				if(newTop == 0) 
					$("#previousPhotos").hide();
			});
		}
	}
}
function showNewestAdditions() {
	if(currentPosition == 'portfolio')
		prepShowAllPortfolios(true);
	else if(currentPosition == 'photo') {
		$('#portfolioLargeImage').animate({top: '700px'}, 500, 'easeInCubic', function() {
			$('#portfolioLargeImage').hide().empty();
			$("#largeImageContainer").hide();
			$('#loadingLargeImage').show();
			$('#photoNavigationContainer').fadeOut(500);
			currentPosition = 'portfolio';
			prepShowAllPortfolios(true);
		});
	} else if(currentPosition == 'allPortfolios') {
		if(!animating) {
			currentPosition = 'index';
			animating = true;
			slideAllPortfoliosRight();
			$("#newestAdditions").animate({top: 15}, 1000, 'easeOutCubic', function() {
				setupMorePortfolios();
				animating = false;
			}).parent().show();
			$("#portfolioNavigations").animate({top: '2px'}, 1000, 'easeOutCubic');
		}
	} else if(currentPosition == 'page') 
		hidePage(true);
}
function queuePage(link) {
	if(!animating) {
		animating = true;
		loadPageFunction = function() {
			//stopLoad = false;
			var pageContent = "";
			$(loadedData).find("pageContent").each(function() {
				pageContent = $(this).text();
			});
			$("#progressBarContainer").attr("currentInterval", "0").attr('finalWidth', "721");
			$('#pageContent').css('top', '700px');
			$('#pageContentContainer').show();
			$('#progressBarContainer').animate({'width':'721px'}, 100, 'linear', function() {
				$(this).fadeOut(500, function() {
					$('#pageContent').html(pageContent).animate({top: 0}, 500, 'easeInCubic', function() {
						animating = false;
					});
				});
			});
			setFragmentAndTitle();
		};
		if(currentPosition == 'portfolio' || currentPosition== 'photo') {
			loadReset = true;
			if(currentPosition == 'photo') {
				$('#portfolioLargeImage').animate({top: '700px'}, 500, 'easeInCubic', function() {
					$('#portfolioLargeImage').hide().empty();
					$("#largeImageContainer").hide();
					$('#loadingLargeImage').show();
					$('#photoNavigationContainer').fadeOut(500);
				});
			}
			hidePortfolio(false, function() {
				$("#progressBarContainer").animate({width: '100px'}, 200, 'linear', function() {});
				currentPosition = "page";
				var args = new Object();
				args.link = $(link).attr('href');
				args.onComplete = loadPageFunction;
				ajaxExecute(args);
			});
		} else {
			if(currentPosition == 'index') {
				$("#newestAdditions").animate({top: 430}, 1000, 'easeInCubic', function() {
					$(this).parent().hide();
				});
				$('#newestPortfolios').fadeOut(300);
				slideAllPortfoliosLeft();
			}
			$('#portfolioNavigations').animate({top: '-'+farOutPortfolioAnimation+'px'}, 1000, 'easeInCubic', function() {
				$("#progressBarContainer").animate({width: '100px'}, 200, 'linear', function() {});
				currentPosition = "page";
				var args = new Object();
				args.link = $(link).attr('href');
				args.onComplete = loadPageFunction;
				ajaxExecute(args);
			});
		}
	}
}
function queuePortfolio(link) {
	if(!animating) {
		animating = true;
		$('#portfolioHeaderContainer').css('top', '0px');
		var id = $(link).parent().attr('alt');
		var thisElement = null;
		var num = 0;
		var img = $('img', link);
		navScrollOut(img);
		$('.portfolioNavItem').each(function(i, val) {
			if($(val).attr('alt') == id) {
				thisElement = val;
				num = i;
			}
		});
		if(currentPosition == 'index') {
			$("#newestAdditions").animate({top: 430}, 1000, 'easeInCubic', function() {
				$(this).parent().hide();
			});
			$('#newestPortfolios').fadeOut(300);
			slideAllPortfoliosLeft();
		} 
		var firstAnimate = 0;
		if(num > 0)
			firstAnimate = (num * portfolioNavItemHeight);
		$('#portfolioNavigations').animate({top: '-'+firstAnimate+'px'}, 1000, 'easeInOutCubic', function() {
			$('#portfolioContainer').show();
			$("#portfolioTitle").html($('a', thisElement).html());
			var img = $('img', $('#portfolioTitle'));
			navScrollOut(img);
			$(this).animate({top: '-'+farOutPortfolioAnimation+'px'}, 1000, 'easeInCubic', function() {
				$('#allPortfolios').fadeOut(500);
				$(".portfoliosNav").fadeOut(500);
				$('#portfolio').fadeIn(500);
				$('#backToPortfolios').fadeIn(500);
				navFadeOut(thisElement);
				currentPosition = "portfolio";
				var args = new Object();
				args.link = $(link).attr('href');
				$("#progressBarContainer").animate({width: '100px'}, 200, 'linear', function() {});
				args.onComplete = function() {
					//stopLoad = false;
					var pageContent = "";
					$(loadedData).find("pageContent").each(function() {
						pageContent = $(this).text();
					});
					var seePhotosInAction = "";
					$(loadedData).find("seePhotosInAction").each(function() {
						seePhotosInAction = $(this).text();
					});
					$('#seePhotosInAction').css('display', 'none').html(seePhotosInAction);
					$('#portfolioImages').css('top', '675px').html(pageContent);
					photosCount = $(".portfolioImage").length;
					$(".smallImage").click(function() {
						showLargeImage(this);
					});
					$("#progressBarContainer").attr("currentInterval", "0").attr('finalWidth', "721");
					progressBar($("#progressBarContainer"), $("#portfolioImages"));
					setFragmentAndTitle();
				}
				ajaxExecute(args);
			});
	
		});
	}
}
function slideAllPortfoliosLeft() {
	$('#allPortfolios').animate({left: 0}, 1000, 'easeOutCubic');
	$('#allPortfolios > img').fadeOut(500);
}
function slideAllPortfoliosRight() {
	$('#allPortfolios > img').fadeIn(500);
	$('#allPortfolios').animate({left: '450.5px'}, 1000, 'easeOutCubic', function() {
		$('#newestPortfolios').fadeIn(300);
	});
}
function showPortfolio() {
	$("#progressBarContainer").fadeOut(500, function() {
		$(this).css('width', '0px');
		if($('#seePhotosInAction').html() != "")
			$('#seePhotosInAction').fadeIn(500);
		if(photosCount > 6) 
			$('#morePhotos').fadeIn(500);
		$('#portfolioImages').animate({top: 0}, 1000, 'easeOutCubic', function() {
			animating = false;
		});
	});
}
function navFadeOut(link) {$(link).stop().fadeTo(300, .12);var img = $('img', link);navScrollOut(img);}
function navFadeIn(link) {$(link).stop().fadeTo(300, .30);var img = $('img', link);navScrollIn(img)}
function navScrollIn(img) {if($(img).width() > 550) {var difference = $(img).width() - 500;$(img).stop().animate({left: -difference}, difference * 8);}
}
function navScrollOut(img) {if($(img).width() > 550) {var current = parseInt($(img).css('left'));current = current * -1;$(img).stop().animate({left: 0}, current * 8);}}
function preloadImages(imageArray) {for(var i = 0; i < imageArray.length; i++) {preload(imageArray[i]);}}
function preload(imageSource) {var img = new Image();img.src = imageSource;}
function progressBar(progressBarContainer, imagesContainer) {
	var allImages = $('img', imagesContainer);
	if(allImages.length > 0) {
		//doing it this slow way instead of doing onLoad flags allows for the bar to progress...
		var finalWidth = parseFloat($(progressBarContainer).attr('finalWidth'));
		var increment = parseFloat($(progressBarContainer).attr('finalWidth')) / allImages.length;
		var currentInterval = parseFloat($(progressBarContainer).attr('currentInterval'));
		if(currentInterval < allImages.length) {
			if(allImages[currentInterval].complete) {
				var newWidth = $(progressBarContainer).width() + increment;
				if(newWidth > finalWidth)
					newWidth = finalWidth;
				currentInterval++;
				$(progressBarContainer).attr('currentInterval', currentInterval);
				$(progressBarContainer).animate({'width': newWidth+'px'}, 100, 'linear', function() { progressBar(this, imagesContainer); });
			} else 
				setTimeout(function() { progressBar(progressBarContainer, imagesContainer); }, 25);
		} else
			showPortfolio();
	} else 
		showPortfolio();
}
function setupMorePortfolios() {
	if(portfoliosCount > 10) 
		$('#morePortfolios').show();
	$("#previousPortfolios").hide();
}
function showLargeImage(img) {
	loadingPhoto = true;
	currentPosition = "photo";
	$('#portfolioImages').animate({top: '675px'}, 1000, 'easeInCubic', function() {
		$('#backToPortfolios, #portfoliosNavigationLinks').fadeOut(500);
		$('#photoNavigationContainer').fadeIn(500);
		$('#largeImageContainer').fadeIn(500, function() {
			$(this).attr('alt', $(img).attr('count'));	
			var largeImage = new Image();
			largeImage.src = $(img).attr('alt');
			loadReset = false;
			var loadFunction = function() {
				if(!loadReset) {
					$('#loadingLargeImage').fadeOut(300, function() {
						if(largeImage.width > largeImage.height)
							$('#portfolioLargeImage').html("<div class = \"photoHorizontalContainer\"><img src = \""+largeImage.src+"\" /></div>");
						else
							$('#portfolioLargeImage').html("<div class = \"photoVerticalContainer\"><img src = \""+largeImage.src+"\" /></div>");
						$('#portfolioLargeImage').css('top', '700px').show().animate({top: '0px'}, 500, 'easeOutCubic', function() {
							loadingPhoto = false;
						});
					});
				} 
				loadReset =false;
			};
			if(largeImage.complete == true)
				loadFunction();
			else
				largeImage.onload = loadFunction;
		});
	});
	$('#portfolioHeaderContainer').animate({top:'-100px'}, 500, 'easeInCubic', function() {});
}
function photoClose() {
	loadReset = true;
	$('#portfolioLargeImage').animate({top: '700px'}, 500, 'easeInCubic', function() {
		$("#largeImageContainer").hide();
		$('#loadingLargeImage').show();
		$('#photoNavigationContainer').fadeOut(500);
		$('#backToPortfolios, #portfoliosNavigationLinks').fadeIn(500);
		$('#portfolioImages').animate({top: '0px'}, 1000, 'easeInCubic', function() {
			currentPosition = "portfolio";
		});
		$('#portfolioLargeImage').hide().empty();
		$('#portfolioHeaderContainer').animate({top:'0px'}, 500, 'easeInCubic', function() {});
	});
}
function photoNext() {photoChange(1);}
function photoBack() {photoChange(-1);}
function photoChange(direction) {
	if(!loadingPhoto) {
		loadingPhoto = true;
		$('#portfolioLargeImage').animate({top: '700px'}, 500, 'easeInCubic', function() {
			$('#loadingLargeImage').show();
			var next = parseFloat($(this).parent().attr('alt')) + direction;
			if($('.smallImage[count="'+next+'"]').length > 0) 
				showLargeImage($('.smallImage[count="'+next+'"]'));
			else if(direction > 0)
				showLargeImage($('.smallImage[count="0"]'));
			else
				showLargeImage($('.smallImage[count="'+(photosCount -1)+'"]'));
		});
	}
}
function displayFeaturedPortfolio(element) {var link = $(".portfolioNavItem[alt='"+$(element).attr('alt')+"'] > a");queuePortfolio(link);}
function siteInit() {
	$('.portfolioNavItem > a').each(function() {$(this).html('<img border = "0" src = "'+baseUrl+'/textImageLoad/ne_'+$(this).html()+'_FFFFFF_000000_0_0_50_0_0_44.png" />');});
	$('.portfolioNavItem').bind('mouseenter', function() {if(!activeLink) {	navFadeIn(this);}});
	$('.portfolioNavItem').bind('mouseleave', function() {if(!activeLink) {	navFadeOut(this);}});
	$("#portfolioHeaderContainer").bind('mouseenter', function() {var img = $('img', this);navScrollIn(img);});
	$("#portfolioHeaderContainer").bind('mouseleave', function() {var img = $('img', this);navScrollOut(img);});
	$('#allPortfolios, #backToPortfolios').click(function() {prepShowAllPortfolios(false);});
	$('#morePortfolios').click(function() {	morePortfolios();});
	$('#previousPortfolios').click(function() {previousPortfolios();});
	$('#morePhotos').click(function() {morePhotos();});
	$('#previousPhotos').click(function() {previousPhotos();});
	$('#logoContainer').click(function() {showNewestAdditions();});
	$('.portfolioNavItem > a').click(function() {activeLink = true;queuePortfolio(this);return false;});
	$('.featuredImage > a').click(function() {displayFeaturedPortfolio(this);return false;});
	$('#photoNext').click(function() {photoNext();});
	$('#photoBack').click(function() {photoBack();});
	$('#photoClose').click(function() {photoClose();});
	$('#contactUs > a').click(function() {queuePage(this);return false;});
	$('.topLink, .footerItem').hide();
	farOutPortfolioAnimation = portfoliosCount * portfolioNavItemHeight + 100;
	$('#portfolioNavigations').css('top', '-'+farOutPortfolioAnimation+'px');
	//siteInAnimations();
}
function fragmentInit() {
	var fragmentIndex = (window.location.href).indexOf("#");
	if(fragmentIndex > 0) {
		fragment = (window.location.href).substr((fragmentIndex + 1));
		if(fragment && fragment != 'notFound') {
			windowFragment = fragment;      
			//stopLoad = true;                
			activeDynamic = "";     
			var anchor = document.createElement("a");
			anchor.href = baseUrl+"/"+fragment+".html";
			$('a[href="'+anchor.href+'"]', '#portfolioNavigations').click();
		}               
	}    
}
function siteInAnimations() {
	//swfobject.removeSWF('flash');
	$('#flashContainer').css('z-index', '-1');//width', '0px').css('height', '0px').hide();
	animating = true;
	//$('#flashContainer').remove();
	$('#navigationTopLinksContainer').animate({top: '5px'}, 500, 'easeOutCubic', function() {
		if(currentPosition == 'page') { 
			$('#pageContent').css('top', '700px');
			$('#pageContentContainer').show();
			$('#allPortfolios, .footerItem').fadeIn(500);
			$('#pageContent').animate({top: 0}, 500, 'easeInCubic', function() {animating = false;});
		} else {
			$('#portfolioNavigations').animate({top: '0px'}, 1000, 'easeOutCubic', function() {
				$("#newestAdditions").animate({top: 15}, 1000, 'easeOutCubic', function() {
					$('#newestPortfolios, #allPortfolios, .footerItem').fadeIn(500);
					animating = false;
					setupMorePortfolios();
					var url = window.location.href.split('/').pop();
					if(url != 'index.html' && url.indexOf('#') == -1) {
						$('a[href="'+window.location.href+'"]', '#portfolioNavigations').click();
					} else {
						fragmentInit();
					}
				});
			});
		}
	});
	$('#footer').animate({bottom: '5px'}, 500, 'easeOutCubic');
}
