$('html').addClass('js');
var ie_old = false;
var imagesDisabled = false;
$(document).ready(function(){			   
	
	if ($("body").hasClass("home")){
		EXP_gallery();
	}
	work_gallery();
	contact_form();
	
	//fix and initialize custom scroll bar
	$('#projects_container').removeClass('hideoverflow');
	$('#projects_container li').css("margin-left","0");
	$('#projects_container').jScrollPane();

	$('html').removeClass('js');
	
	$('.elastic').elastic();
	
	//animated scroll
	$('a[href=about.php#team]').click(function(){
		$('html,body').animate({scrollTop: $("a[name=team]").offset().top},500);
		return false;
	});
	
	if (window.devicePixelRatio){
		$('.contact form').addClass("webkitAdjust");
		if (navigator.platform.indexOf("Mac") > -1){
			$("#viewer_wrapper .project_nav, .large_star").addClass("webkitAdjust");
			$('.contact .required, #footer_container .nav, #footer_container .info a').addClass("webkitAdjustMac");	
		}
		$('#viewer_container li div.first ul').addClass("webkitAdjust");
	}
	
	if (navigator.platform.indexOf("Mac") > -1){
		$('#viewer_container li div.first, #viewer_container li div sup').addClass('macAdjust');
	}
	
});

(function($){ 
EXP_gallery = function() {
		var firstTime=8;
		var myTimes=[4,4,4,4,4];
		var timeout = null;

		$('.EXP_slideshow_container').append('<div class="EXP_links"></div>');
		
		for(i=0; i<($('.EXP_slideshow_container li').length); i++){
			$('.EXP_slideshow_container .EXP_links').append('<a href="'+$('.EXP_slideshow_container li:eq('+i+') img').attr('src')+'">'+(i+1)+'</a>');
		}
		
		showHovers();
		
		$('.EXP_slideshow_container .caption_background').css("opacity","0.71");
		$('.EXP_slideshow_container .caption_container').css("opacity","1");
		
		$('.EXP_links a:eq(0)').addClass("active");
		
		timeout=setTimeout(autoSlide,firstTime*1000);
		
		if (ie_old){
			removeHovers();
			setTimeout(showHovers,800);
		}
		
		$('.righthover, .lefthover').live("mouseover",function(){
			$(this).find('a').css('visibility','visible');
		});
		$('.righthover, .lefthover').live("mouseout", function(){
			$(this).find('a').css('visibility','hidden');	
		});
		
		
		$('.EXP_links a').live("click",function(){
			
			clearTimeout(timeout);
			
			if($(this).hasClass("active"))
				return false;
				
			removeHovers();
			
			$('.EXP_links a').removeClass("active");
			
			$(this).addClass("active");
			
			var next = $('.EXP_slideshow_container li:eq('+$(this).parent().children().index($(this))+')');									
			
			switchSlides(next);
				
			showHovers();
			
			return false;
		});
		
		$('.righthover a, .lefthover a').live("click", function(){
			
			clearTimeout(timeout);
			
			var nextSlide = null;
			if($(this).parent().hasClass("righthover")){
				nextSlide = jQuery('.EXP_slideshow_container .show').next();
				if (!nextSlide.length){
					nextSlide = jQuery('.EXP_slideshow_container li:eq(0)');
				}
			}
			if($(this).parent().hasClass("lefthover")){
				nextSlide = jQuery('.EXP_slideshow_container .show').prev();
				if (!nextSlide.length)
					nextSlide = jQuery('.EXP_slideshow_container li:last-child');
			}
			
			removeHovers();
			
			$('.EXP_links a').removeClass("active");
			
			$('.EXP_links a:eq('+nextSlide.parent().children().index(nextSlide)+')').addClass("active");
			
			var next = $('.EXP_slideshow_container li:eq('+nextSlide.parent().children().index(nextSlide)+')');									
			
			switchSlides(nextSlide);
				
			showHovers();
			
			return false;
			
		});
		
		
		function autoSlide(){
	
			var nextSlide = null;
			nextSlide = jQuery('.EXP_slideshow_container .show').next();
			if (!nextSlide.length){
				nextSlide = jQuery('.EXP_slideshow_container li:eq(0)');
			}
			
			removeHovers();
			
			$('.EXP_links a').removeClass("active");
			
			$('.EXP_links a:eq('+nextSlide.parent().children().index(nextSlide)+')').addClass("active");
			
			var next = $('.EXP_slideshow_container li:eq('+nextSlide.parent().children().index(nextSlide)+')');									
			
			switchSlides(nextSlide);
				
			showHovers();
			
			timeout=setTimeout(autoSlide,myTimes[nextSlide.parent().children().index(nextSlide)]*1000);
			
		}
		
		function switchSlides(next){
			var elementToFade = (ie_old) ? "span" : "img";
			
			$('.EXP_slideshow_container li.show').addClass("changing");
			
			$('.EXP_slideshow_container li').removeClass("show").find(elementToFade).stop().animate({
				opacity: 0
			  }, 1300, function() {
				$('.EXP_slideshow_container li').removeClass("changing");
			  });
			
			$('.EXP_slideshow_container .caption_background').stop().animate({
				opacity: 0
			  }, 1300, function() {
			  });
			$('.EXP_slideshow_container .caption_container').stop().animate({
				opacity: 0
			  }, 1300, function() {
			  });
			
			next.addClass("show").
				find(elementToFade).stop().css("opacity","0")
				.animate({
				opacity: 1
			  }, 800, function() {
			  });
				
			next.find('.caption_background').stop().css("opacity","0")
				.animate({
				opacity: 0.71
			  }, 800, function() {
			  });
			
			next.find('.caption_container').stop().css("opacity","1")	
		}
		
		function showHovers(){
			$('.EXP_slideshow_container li.show .caption_container').before('<div class="righthover"><a href=""></a></div><div class="lefthover"><a href=""></a></div>');
			
			$('.righthover, .lefthover').css("opacity","0.91");
		}
		
		function removeHovers(){
			$('.righthover, .lefthover').remove();
		}
		
	}
})(jQuery);

(function($){ 
work_gallery = function() {	
		
		//flag for ie6, 7
		var isIEOldFirst = false;
		//store json
		var imgSrcs = [];
		//store if already loaded
		var loaded = [];
		
		//put json in array
		var json_file = ($('body').hasClass('interactive_design') ? 'interactive' : 'print');
		$.getJSON('includes/' + json_file + '.json', function(data) {
			$.each(data, function(i) {
				imgSrcs[parseInt(i)] = [];				  
				$.each(this, function(k, v) {								
					imgSrcs[i][k] = v;
				});
			});
			//store "loaded" for the active project
			$("#viewer_container .active").each(function(){
				loaded[$(this).parent().children().index($(this))+1] = "loaded";
			});
		});
		
		//load nav/hovers
		showProjectNav();
		showHovers();
		
		//ie6,7 fix--need to add,remove
		if (ie_old){
			isIEOldFirst = true;
			$(window).load(function(){
				removeHovers();
				removePrevProjectNav();
				showHovers();
				showProjectNav();
				isIEOldFirst = false;
			});
		}
		
		//set up aleve flash
		$('.interactive_design #viewer_container .viewer:eq(0) span:eq(1) img').wrap('<a href="#" class="aleve_flash_link" />');
		
		//set up alka flash
		$('.interactive_design #viewer_container .viewer:eq(2) span:eq(0) img').wrap('<a href="#" id="alka_flash_link" />');
		
		//set up bdc flash
		$('.interactive_design #viewer_container .viewer:eq(6) span:eq(0) img').wrap('<a href="#" id="bdc_flash_link" />');
		
		//load preloader
		var img = new Image();
		$(img).addClass("no_display")
			  .load(function () {
				$(this).remove();
			  })
			  .attr('src', 'img/work/preloader.gif');
		
		/*
		 * listener/handler for mouseenter
		 */ 
		$('.righthover, .lefthover').live("mouseenter",function(){
			$(this).find('a').css('visibility','visible');
		});
		$('.righthover, .lefthover').live("mouseleave", function(){
			$(this).find('a').css('visibility','hidden');	
		});
		
		/*
		 * listener/handler for project chooser
		 */
		$("#chooser_container a").click(function(){
			if($(this).hasClass("active"))
				return false;
			
			var elementToFade = (ie_old) ? "" : " img";
				
			removePrevProjectNav();
			removeHovers();
				
			$('#chooser_container a').removeClass("active");
			
			$(this).addClass("active");	
			
			var parentItem = $(this).parent();
			var nextNum = parentItem.parent().children().index(parentItem);
			var next = $('#viewer_container .viewer:eq('+nextNum+')');
			var currentHeight = $('#viewer_container .active').height()+"px";
			
			$('#viewer_container .active').addClass("changing");
			
			$('#viewer_container .viewer .holder span' + elementToFade).stop().animate({
				opacity: 0
			  }, 1000, function() {
				$('#viewer_container .viewer').removeClass("changing");
			  });
			
			if ((loaded[nextNum+1] != "loaded") && (!imagesDisabled)){
				$("#viewer_container").addClass("loading");
				
				next.find(".holder img").each(function(i){
					var thisImg = $(this);
					img = new Image();
					$(img).load(function () {
						thisImg.attr("src",imgSrcs[nextNum+1][i]);
						var parentToCompare = (thisImg).closest("span");
						if (parentToCompare.parent().children().index(parentToCompare) == (parentToCompare.parent().children().length - 1)){//check last
							$("#viewer_container").removeClass("loading");
							nextProject(elementToFade, next, currentHeight);
							loaded[nextNum+1] = "loaded";
						}
					}).attr('src', imgSrcs[nextNum+1][i]);
				});
				
			}
			else {
				nextProject(elementToFade, next, currentHeight);
			}
			
			return false;
				
		});
		
		
		/*
		 * listener/handler for prev/next chooser
		 */
		$('.project_nav a, .righthover a, .lefthover a').live("click",function(){
			
			var nextSlide = null;
			if($(this).hasClass("nextBtn") || $(this).parent().hasClass("righthover")){
				nextSlide = jQuery('#viewer_container .active .show').next();
				if (!nextSlide.length)
					nextSlide = jQuery('#viewer_container .active span:eq(0)');
			}
			
			if($(this).hasClass("prevBtn") || $(this).parent().hasClass("lefthover")){
				nextSlide = jQuery('#viewer_container .active .show').prev();
				if (!nextSlide.length)
					nextSlide = jQuery('#viewer_container .active span:last-child');
			}
			
			removeHovers();
			
			$('#viewer_container .active .show').addClass("changing");
			
			var elementToFade = (ie_old) ? "" : " img";
			
			$('#viewer_container .active .holder').children().removeClass("show");
			$('#viewer_container .active .holder span ' + elementToFade).stop().animate({
				opacity: 0
			  }, 1800, function() {
				$(this).removeClass("changing");
			  });
			
			
			nextSlide.addClass("show");
			$("#viewer_container .active .holder .show" + elementToFade)
				.css("opacity","0.1")
				.stop()
				.animate({
				opacity: 1
			  }, 1000, function() {
			  });	
			
			$('.project_nav .shownNum').html(nextSlide.parent().children().index(nextSlide)+1);
			
			showHovers();
					
			return false;
		});
		
		/*
		 * listener/handler for aleve flash
		 */
		if(navigator.userAgent.match(/iPad/i)) {
			$('.aleve_flash_link').attr("href","mobile/aleve/animation_ipad.mp4");
		}
		else if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			$('.aleve_flash_link').attr("href","mobile/aleve/animation.mp4");
		}
		else if(navigator.userAgent.match(/Android/i)) {
			$('.aleve_flash_link').attr("href","mobile/aleve/animation_android.mp4");
		}
		else{
			$('.aleve_flash_link').click(function(){
				showFlashModal(870, 462, "flash/aleve/Aleve_Bottle.swf", "flash/aleve/alternative.jpg");
				return false;
			});
		}
		
		/*
		 * listener/handler for alka flash
		 */
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i))) {
			$('#alka_flash_link').attr("href","mobile/alka/animation.mp4");
		}
		else{
			$('#alka_flash_link').click(function(){
				showFlashModal(518, 541, "flash/alka/preloader.swf", "flash/alka/alternative.jpg");
				return false;
			});
		}
		
		/*
		 * listener/handler for bdc flash
		 */
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i))) {
			$('#bdc_flash_link').attr("href","mobile/bdc/animation.mp4");
		}
		else{
			$('#bdc_flash_link').click(function(){
				showFlashModal(636, 215, "flash/bdc/main3.swf", "flash/bdc/alternative.jpg");
				return false;
			});
		}
		
		function nextProject(elementToFade, next, currentHeight){
			next = $(next);
			$('#viewer_container .viewer').removeClass("active");
			next.addClass("active");
			showProjectNav();
			showHovers();
			var nextHeight = next.height()+"px";
			if (currentHeight != nextHeight)
				next.css("height",currentHeight).stop().animate({"height": nextHeight}, 300, function(){next.css("height","auto");});
			$('#viewer_container .active .show' + elementToFade).stop().css("opacity",0.05).animate({"opacity":1},800, function(){
					
			});
			$('#viewer_container .viewer .third a').css("color","#FFF");
			$('#viewer_container .viewer .first, #viewer_container .viewer .second').css("color","#FFF");
			$('#viewer_container .viewer .first, #viewer_container .viewer .second').stop().animate({color: "#383D40"},1000);
			$('#viewer_container .viewer .first a, #viewer_container .viewer .second a').css("color","#FFF");
			$('#viewer_container .viewer .first a, #viewer_container .viewer .second a').stop().animate({color: "#383D40"},1000, function(){
				$('#viewer_container .viewer .first a, #viewer_container .viewer .second a').attr("style","");
				$('#viewer_container .viewer, #viewer_container .viewer .first, #viewer_container .viewer .second').stop(true);
			});
			
			
			if (ie_old){
				$('#viewer_container').animate({"height": nextHeight}, 300);
			}
		}
		
		function showFlashModal(flash_width, flash_height, flash_src, flash_alt_src){
			$.modal('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+flash_width+'" height="'+flash_height+'" title="">'+
					'<param name="movie" value="'+flash_src+'" />'+
					'<param name="quality" value="high" />'+
					'<param name="wmode" value="transparent" />'+
					'<param name="scale" value="noscale" />'+
					'<param name="swfversion" value="6.0.65.0" />'+
					'<!--[if !IE]>-->'+
					'<object type="application/x-shockwave-flash" data="'+flash_src+'" width="'+flash_width+'" height="'+flash_height+'">'+
					  '<!--<![endif]-->'+
					  '<param name="quality" value="high" />'+
					  '<param name="wmode" value="transparent" />'+
					  '<param name="scale" value="noscale" />'+
					  '<param name="swfversion" value="6.0.65.0" />'+
						'<div style="background-image:url('+flash_alt_src+');height:'+flash_height+'px;">'+
						'<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>'+
						'<p><a href="http://www.adobe.com/go/getflashplayer" target="_blank"><img id="get_flash" src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>'+
					  '</div>'+
					  '<!--[if !IE]>-->'+
					'</object>'+
					'<!--<![endif]-->'+
				  '</object>', {
				onOpen: function (dialog) {
					dialog.overlay.fadeIn(function(){
						dialog.container.fadeIn();
						dialog.data.fadeIn();
					});
				},
				containerCss: {
					height: flash_height,
					width: flash_width
				},				
				closeHTML:"<a href='#' class='close_link'><img src='img/modal_close.gif' alt='Close' /></href>",
				overlayClose:true
			});
		}
		
		function showProjectNav(){
			$('#viewer_container .active .hide').removeClass("hide");
			
			if (!isIEOldFirst){
				$('#viewer_container .active .holder .show').removeClass("show");
				$('#viewer_container .active .holder span:first-child').addClass("show");
			}
			
			if ($('#viewer_container .active .holder').children().length > 1) {
				$('#viewer_wrapper').append('<div class="project_nav"></div>');
				
				if (window.devicePixelRatio && (navigator.platform.indexOf("Mac") > -1)){
					$("#viewer_wrapper .project_nav").addClass("webkitAdjust");
				}
				
				$('.project_nav').append('<span class="shownNum">1</span>/' + 
										 $('#viewer_container .active .holder').children().length +
										 '<a href="#" class="nextBtn">&gt;</a>'+
										 '<a href="#" class="prevBtn">&lt;</a>');
				
				if (ie_old){
					if ($('#viewer_container .active .holder').children().length > 9)
						$('.project_nav').css("width","74px");
					else
						$('.project_nav').css("width","65px");
				}
				
			}
			
		}
		
		function removePrevProjectNav(){
			$('#viewer_container .active .first, #viewer_container .active .second').addClass("hide");
			$('.project_nav').remove();
		}
		
		function showHovers(){
			if ($('#viewer_container .active .holder').children().length > 1) {
				$('#viewer_wrapper').append('<div class="righthover"><a href=""></a></div><div class="lefthover"><a href=""></a></div>');
				$('.righthover, .lefthover').css("opacity","0.91");
			}
		}
		
		function removeHovers(){
			$('.righthover, .lefthover').remove();
		}
		
	}
})(jQuery);
(function($){
	contact_form = function() {	

		//for proper phone formatting
		$("#contact_phone").live("keyup",(function (e)
		{
		  this.value = this.value.replace(/[^0-9\-\.]/g,'');
		  var len = this.value.length;
					if (len == 3) {
						this.value = this.value + "-";
					}
					if (len == 7) {
						this.value = this.value + "-";
					}
					if (len >= 12) {
						this.value = this.value.substring(0, 12);
					}
		}));

		
		//submit comment form
		$(".contact form").submit(function() {
			
			//if present
			$(".error").removeClass("error");
			$(".error_star").removeClass("error_star");
			$("#error_message").remove();
			
			//if only whitespace submitted
			if($("#contact_name").val().match(/[\S]+/) == null)
				$("#contact_name").val("");
			if($("#contact_email").val().match(/[\S]+/) == null)
				$("#contact_email").val("");
			if($("#contact_message").val().match(/[\S]+/) == null)
				$("#contact_message").val("");
				
			//store missing, invalid state
			var missingName = ($("#contact_name").val() == "") ? true: false;
			var missingMessage = ($("#contact_message").val() == "") ? true: false;
			var missingEmail = ($("#contact_email").val() == "") ? true: false;
			var invalidmail = !isValidEmail($("#contact_email").val());
			var missing = missingName || missingMessage || missingEmail;
			
			var errorClass = (missing && invalidmail) ? "double" : "";
			var error_string = "<div id='error_message' class='"+errorClass+"'>";
			  if (missing)
				error_string += "<p><span class='large_star'>*</span> Please fill in the required fields.</p>";
			  if (invalidmail){
				error_string += "<p";
				  if (errorClass == "double")
					error_string += " class='push_up'";
				  error_string += "><span class='large_star'>*</span> Please supply a valid email address.</p>";
			  }
			  error_string += "</div>";
			
			//animate outlines
			if (missingName){
				$("#contact_name").switchClass("","error",500);
				$("#label_name").switchClass("","error",500);
				$("#label_name .large_star").switchClass("","error_star",500);
			}
			if (missingEmail || invalidmail){
				$("#contact_email").switchClass("","error", 500);
				$("#label_email").switchClass("","error", 500);
				$("#label_email .large_star").switchClass("","error_star", 500);
			}
			if (missingMessage){
				$("#contact_message").switchClass("","error", 500);
				$("#label_message").switchClass("","error", 500);
				$("#label_message .large_star").switchClass("","error_star", 500);
			}
			
			//listener for focus (remove red)
			$("#contact_name").focus(function(){
				$("#contact_name").removeClass("error");
				$("#label_name").removeClass("error");
				$("#label_name .large_star").removeClass("error_star");
			});
			$("#contact_email").focus(function(){
				$("#contact_email").removeClass("error");
				$("#label_email").removeClass("error");
				$("#label_email .large_star").removeClass("error_star");
			});
			$("#contact_message").focus(function(){
				$("#contact_message").removeClass("error");
				$("#label_message").removeClass("error");
				$("#label_message .large_star").removeClass("error_star");
			});
			
			//append error message and end function
			if (missing || invalidmail){
				$('.contact form').append(error_string);
				return false;
			}

			//submit if ok
			$.ajax({
			  url: "send.php",
			  type: "POST",
			  cache: false,
			  data: "name="+encodeURIComponent($("#contact_name").val())+"&email="+encodeURIComponent($("#contact_email").val())+"&message="+encodeURIComponent($("#contact_message").val())+"&phone="+encodeURIComponent($("#contact_phone").val())+"&company="+encodeURIComponent($("#contact_company").val())+"&noinput="+encodeURIComponent($("#contact_noinput").val()),
			  success: function(data){
				  
				  if (data=="Success")
				  	window.location = "contact_thankyou.php";
				  },
			  error: function(xhr, text, e){alert(e);}
		   });
			
			return false;
		});
		
	}
})(jQuery);

/*email format checker*/
function isValidEmail(elementValue){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(elementValue);  
}

/*get param from query string*/
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

