// Variables
var tabCurrent 	= 1;
var csCurrent	= 1;
var t = "";

$(document).ready(function(){
	// Case Studies Landing Page
	$(".cs-link").hover(
		function(){
			id = $(this).attr("id").split("-");
			$("#cs-thumb-"+id[1]).parent("li").addClass("selected");
		},
		function(){
			$(".cs-thumb").parent("li").removeClass("selected");
		}
	);
	
	$(".cs-thumb").hover(
		function(){
			id = $(this).attr("id").split("-");
			$("#cs-"+id[2]).parent("li").addClass("selected");
		},
		function(){
			$(".cs-link").parent("li").removeClass("selected");
		}
	);
	
	// Case Studies Entry Page
	sliderCount = $(".slider li").length;
	
	$(".cs-pag-next").click(function(){
		if (csCurrent == sliderCount){
			csCurrent = 1;
		} else {
			csCurrent++;
		}
		$(".slider li").removeClass("on");
		$("#cs-slider-"+csCurrent).addClass("on");
		$("#viewer-nav > ul > li").removeClass("selected");
		$("#cs-sliderBtn-"+(csCurrent-1)).addClass("selected");
	});
	
	$(".cs-pag-prev").click(function(){
		if (csCurrent == 1){
			csCurrent = sliderCount;
		} else {
			csCurrent--;
		}
		$(".slider li").removeClass("on");
		$("#cs-slider-"+csCurrent).addClass("on");
		$("#viewer-nav > ul > li").removeClass("selected");
		$("#cs-sliderBtn-"+(csCurrent-1)).addClass("selected");
	});
	
	$("#viewer-nav > ul > li > a").click(function(){
		$("#viewer-nav > ul > li").removeClass("selected");
		id = $(this).parent("li").addClass("selected").attr("id").split("-");
		csCurrent = (id[2]*1)+1;
		$(".slider li").removeClass("on");
		$("#cs-slider-"+csCurrent).addClass("on");
	});
	
	// Our Work Landing Page	
	$(".work-grid .to-do li:nth-child(3n+1)").css("margin-left","0");
	
	
	// Team Member Select
	$(".team-select").change(function(){
		$(location).attr('href',$(this).val());
	});
	
	$(".team-name").hover(
		function(){
			id = $(this).attr("id").split("-");
			$("#team-thumb-"+id[2]).parent("li").addClass("selected");
		},
		function(){
			$(".team-thumb").parent("li").removeClass("selected");
		}
	);
	
	$(".team-thumb").hover(
		function(){
			id = $(this).attr("id").split("-");
			$("#team-name-"+id[2]).parent("dd").addClass("selected");
		},
		function(){
			$(".team-name").parent("dd").removeClass("selected");
		}
	);
	
	// Blog
	
	$(".blog-pag-next").click(function(){
		id 			= $(this).parent("li").parent(".controls").siblings(".slider").children(".on").removeClass("on").attr("id").split("-"); 
		galSize		= $(this).parent("li").parent(".controls").siblings(".slider").children("li").length;
		galId		= id[1];
		entryId		= id[2];
		
		if (galSize == entryId){
			entryId = 1;
		} else {
			entryId++;
		}
		
		$("#gal-"+galId+"-"+entryId).addClass("on");
		$(".galBtn-"+galId).removeClass("on");
		$("#galBtn-"+galId+"-"+entryId).parent("li").addClass("on");
	
	});
	
	$(".blog-pag-prev").click(function(){
		id 			= $(this).parent("li").parent(".controls").siblings(".slider").children(".on").removeClass("on").attr("id").split("-"); 
		galSize		= $(this).parent("li").parent(".controls").siblings(".slider").children("li").length;
		galId		= id[1];
		entryId		= id[2];
		
		if (entryId == 1){
			entryId = galSize;
		} else {
			entryId--;
		}
		
		$("#gal-"+galId+"-"+entryId).addClass("on");
		$(".galBtn-"+galId).removeClass("on");
		$("#galBtn-"+galId+"-"+entryId).parent("li").addClass("on");
	
	});
	
	$(".galBtn").click(function(){
		id 			= $(this).attr("id").split("-"); 
		galId		= id[1];
		entryId		= id[2];
		
		$(this).parent("li").siblings("li").removeClass("on");
		$(this).parent("li").parent("ol").siblings(".slider").children("li").removeClass("on");
		$(this).parent("li").addClass("on");
		$("#gal-"+galId+"-"+entryId).addClass("on");
	});
	
	// Methodology
	tabCount = $("#process section").length;
	
	$(".tab").click(function(){
		$(".tabbed").removeClass("on");
		$(this).parent("h2").parent("section").addClass("on");	
		tabCurrent = $(this).attr("id");
	});
	
	$(".pag-next").click(function(){
		$(".tabbed").removeClass("on");
		if (tabCurrent == tabCount){
			tabCurrent = 1;
		} else {
			tabCurrent++;
		}
		$(".tab"+tabCurrent).addClass("on");
	});
	
	$(".pag-prev").click(function(){
		$(".tabbed").removeClass("on");
		if (tabCurrent == 1){
			tabCurrent = tabCount;
		} else {
			tabCurrent--;
		}
		$(".tab"+tabCurrent).addClass("on");
	});
	
	// korq board	
	$("#korq-board section h3 a").click(function(){
		$("#korq-board section").removeClass("on");
		$(this).parent("h3").parent("section").addClass("on");
		
		oScrollbar = $('#scrollbar1').tinyscrollbar({ sizethumb: 24 });
		oScrollbar.update(); 
		
		oScrollbar = $('#scrollbar2').tinyscrollbar({ sizethumb: 24 });
		oScrollbar.update(); 
		
		oScrollbar = $('#scrollbar3').tinyscrollbar({ sizethumb: 24 });
		oScrollbar.update(); 
	});
	
	// Form Validation
	
	$("#contact").submit(function() {
		formErrors = false;
		validEmail = true;
		errors = "";
		
		$("label, input, textarea").removeClass("error");
		
  		$(this).find("input").each(function(index) {
  			if ($(this).siblings("label").hasClass("required") && ($(this).val().length==0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  			
  			if ($(this).attr("name")=="email" && ($(this).val().length>0)){
  				if (!(isValidEmailAddress($(this).val()))){
  					formErrors = true;
  					validEmail = false;
  					if (!($(this).siblings("label").hasClass("error"))){
  						$(this).siblings("label").addClass("error");
  					}
  					
  					if (!($(this).hasClass("error"))){
  						$(this).addClass("error");
  					}
  					
  					errors = "You have entered an invalid email address.";
  				}
  			}
  		});
  		
  		$(this).find("select").each(function(index) {
  			if ($(this).siblings("label").hasClass("required") && ($(this).val() == 0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  		});
  		
  		$(this).find("textarea").each(function(index) {
  			if ($(this).siblings("label").hasClass("required") && ($(this).val().length==0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  		});
  		
  		if (formErrors){
  			if (errors.length > 0){
  				errors += "<br />";
  			}
  			
  			errors += "Some required info is missing from your submission";
  			
  			$(".errors").html(errors).slideDown(100);
  			var destination = $(".errors").offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
  			return false;
  		} else {
  			return true;
  		}
	});
	
	//Main Navigation
	$('#big-drop-down ul li:last-child').addClass('last');
	$('nav#get-around #drop').hover(
		function(){
			$('#big-drop-down').slideDown(333);
			$('nav#get-around #drop').addClass('selected');
		},
		function(){
			t = setTimeout(function() {
				$('#big-drop-down').slideUp(333);
				$('nav#get-around #drop').removeClass('selected');
			},100);
		}
	);
	
	$('#big-drop-down').hover(
		function(){
			clearTimeout(t);
		},
		function(){
			$('#big-drop-down').slideUp(333);
			$('nav#get-around #drop').removeClass('selected');
		}
	);
	
});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
