function rotate_promos(){
	var current_promo = $("#promo_rotate .images .current");
	var next_promo = current_promo.next();
	if (next_promo.size() == 0) next_promo = $("#promo_rotate .images .image:first");
	
	current_promo.fadeOut(1000, function(){
		current_promo.removeClass("current");
		next_promo.fadeIn(1000, function(){
			next_promo.addClass("current");
		});
	});
}

var promos_rotate;

function start_promos(){
	promos_rotate = setInterval(rotate_promos, 7500);
}

function stop_promos(){
	clearInterval(promos_rotate);
}

$(function(){
	start_promos();
	
	//recent work spacing
	$("#home #recent-work .work:nth-child(2n+1)").css({"margin-right":"43px"});
	$("#what-weve-done #recent-work .work.nomargin").css({"margin-right":"0"});
	$("#tuesletters #recent-work .work:nth-child(4n)").css({"margin-right":"0"});
	$("#other-work #recent-work .work:nth-child(4n)").css({"margin-right":"0"});
	$("#who-we-are .bio:nth-child(5n)").css({"margin-right":"0"});
	
	//recent work detail
	$(".whatwevedone_gallery .whatwevedone_gallery_image:first-child").addClass("active").show();
	$(".whatwevedone_gallery .dot:first-child").addClass("active").show();
	
	$(".whatwevedone_gallery").each(function(){
		var gallery = $(this);
		
		gallery.find(".dot").click(function(){
			gallery.find(".dot").removeClass("active");
			$(this).addClass("active");
			var image_id = $(this).attr("id").split("_")[1]; //dot_IMAGEID
			gallery.find(".whatwevedone_gallery_image").removeClass("active").hide();
			$("#image_"+ image_id).addClass("active").show();
		});
	});
	
	//newsletter field
	var default_email_field_text = 'Email Address';
	$("#tuesletter_email_address").focus(function(){
		if ($(this).val() == default_email_field_text) $(this).val("");
	});
	
	$("#tuesletter_submit_post").click(function(){
		var signup_field = $("#tuesletter_email_address").val();
		//email is blank or invalid
		if (signup_field == '' || signup_field == default_email_field_text || !is_valid_email(signup_field)) alert('Please enter a valid email address');
		//email is valid
		else {
			$.colorbox({href:tuesday_template_url+'/tuesletter/signup.php?email='+encodeURIComponent(signup_field), iframe:true, innerWidth:400, innerHeight:300, opacity:0.5, scrolling: false, open:true});
		}
	});
	$("#tuesletter_subscribe_submit_button").click(function(){
		var signup_field = $("#tuesletter_email").val();
		//email is blank or invalid
		if (signup_field == '' || signup_field == default_email_field_text || !is_valid_email(signup_field)) alert('Please enter a valid email address');
		//email is valid
		else $("#tuesletter_subscribe_form").submit();
	});
	
	//rotating client list
	$("#clients_more_link").click(function(){
		var current_page = $("#clients-slider .current");
		var next_page = current_page.next();
		
		if (next_page.length == 0){
			next_page = $("#clients-slider div.page:first");
		}
		
		//slide current page out
		current_page.animate({ left: '-=495' }, 1000, function(){ $(this).removeClass("current").removeClass("first"); });
		//slide next page in
		next_page.addClass("current").css({"left":"495px"}).show().animate({ left: '-=495' }, 1000, function(){  });
	});
	
	/*
	$("#tuesletter_submit_signup").click(function(){
		var signup_field = $("#tuesletter_email_address").val();
		//email is blank or invalid
		if (signup_field == '' || signup_field == default_email_field_text || !is_valid_email(signup_field)) alert('Please enter a valid email address');
		//email is valid
		else {
			$.ajax({
				type: "POST",  
				//url: 'http://tuesday.createsend.com/t/y/s/pjhuu/', 
				url: '/tuesletter/_process/tuesletter_signup.php',
				data: "e="+ encodeURIComponent(signup_field),
				success: function(data, textStatus, XMLHttpRequest){
					if (data.substring(0, 2) != '0|') $("#tuesletter_signup_text").html(data);
					else alert("There was an error subscribing with \""+ data.substring(2) +"\".\n\nPlease check that it is valid and try again.");
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert('There was an error... please try again.');
				}
			});
		}
	});
	*/
	
	//blogroll categories
	$(".blogrollcat").toggle(
		function(){
			$("#links_"+ $(this).attr("id").split("_")[1]).slideDown();
		},
		function(){
			$("#links_"+ $(this).attr("id").split("_")[1]).slideUp();
		}
	);
	
	//sorting subnav
	$(".content-width .redbargroup .copy ul li:nth-child(6n)").not("li.wide").css({"margin-right":"0px"});
	$(".content-width .redbargroup .copy ul li:nth-child(6n+1)").not("li.wide").css({"margin-left":"12px"});
	$(".content-width .redbargroup .copy ul li.wide:nth-child(5n)").css({"margin-right":"0px"});
	$(".content-width .redbargroup .copy ul li.wide:nth-child(5n+1)").css({"margin-left":"12px"});
	var content_showing = false;
	var currently_showing = '';
	$(".redbargroup ul.sort a.list").click(function(){
		var id = $(this).attr("id");
		if (currently_showing != id){
			currently_showing = id;
			var content_id = id +"_content";
			$(".redbargroup .copy div").not(".redbargroup .copy #"+ content_id).hide();
			$(".redbargroup .copy #"+ content_id).show();
			if (!content_showing) $(".redbargroup .content").slideDown("fast", function(){ content_showing = true; });
		}
		else{
			currently_showing = '';
			$(".redbargroup .content").slideUp("fast", function(){ content_showing = false; });
		}
	});
	
	//submit button on comments
	$("a#submit_button").click(function(){
		$(this).closest("form").submit();
		return false;
	});
	
	$(".close_btn").click(function(){ parent.$.fn.colorbox.close(); return false; });
});

//validate email address
function is_valid_email(email){
	//var reg = /^[A-z0-9-+.]+@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var reg = /^.+@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (reg.test(email)) return true;
	else return false;
}
