var t;

function showController() {
	clearTimeout(t);
	t = setTimeout("$('#controller').fadeTo(800, 0.3)", 1200);
}

function showOnScroll() {
	$('img.img-ondemand:visible').each(function() {
		var viewportWidth = $(window).width();
		var documentScrollLeft = $(window).scrollLeft(); // or document? and below too?
		var maxLeft = documentScrollLeft + viewportWidth;
		var $img = $(this);
		if ( ($img.offset().left + $img.width()) >= documentScrollLeft && $img.offset().left <= maxLeft )
		{
			$img.fadeTo(1, 0.01).css("border","1px solid #fff").removeClass('img-ondemand').attr('src',$img.attr('longdesc')).fadeTo(1, 1.0);
		}
	})
}

function adjustCushion() {
	var viewportWidth = $(window).width();
	var currentPix = $(".pix:visible");	
	var lastProjectWidth = $(currentPix).children(".project").last().width();
	var newCushionWidth = viewportWidth - lastProjectWidth - 195;
	$(currentPix).children(".cushion").css("width",newCushionWidth);
}

function scrollControl() {
	$('.pix:visible .project').each(function() {
		var $proj = $(this);
		var $projWidth = $proj.width();
		var documentScrollLeft = $(window).scrollLeft();
		if ( $proj.offset().left < (documentScrollLeft + 96) && $proj.offset().left > ((documentScrollLeft + 94) - $projWidth) )
		{
			$(".prominent").removeClass("prominent");
			$proj.addClass("prominent");
		}
	})
}

$(document).ready(function(){

	$('#pix-home div:not(#footer)').hover(
		function () {
			$(this).fadeTo(30, 0.6).fadeTo(120, 1.0).find('p').fadeIn(150);
		}, 
		function () {
			$(this).find('p').fadeOut(300);
		}
	);
	
	$("#fix ul li").click(function() {
		var $clicked = this
		var $horizontal = $(window).scrollLeft();
		$(window).unbind('scroll resize');
		$(".prominent").removeClass("prominent");
		if ($(this).is('.active-nav')) {
			$('html, body').animate({scrollLeft: 0}, $horizontal/9, function() {
				$(window).bind('scroll resize', function() {
					showOnScroll();
					scrollControl();
				}).trigger('scroll');
			});
			$(".pix:visible:not(#pix-home, #pix-contact)").children().first().addClass("prominent");
		}
		else {
			$('html, body').animate({scrollLeft: 0}, $horizontal/9, function() {
				var $fetch = $($clicked).attr('id');
				var $pass = '#pix-' + $fetch;
				$('.active-nav').toggleClass('active-nav');
				$($clicked).toggleClass('active-nav');
				$('.pix').fadeOut(200), setTimeout( function() {
					$($pass).fadeIn(800).filter("#pix-art, #pix-theater").children().first().addClass("prominent");
					adjustCushion();
					$(window).bind('scroll resize', function() {
						showOnScroll();
						scrollControl();
					}).trigger('scroll');
					if ($($clicked).is('#art, #theater')) {
						showController();
					}
				}, 300);
			});
			if ($($clicked).is('#home, #contact')) {
				clearTimeout(t);
				$("#controller").fadeOut(300, 0);
			}
		}
	});

	$('#pix-home .project').click(function() {
		var $tgt = '.target_' + ($('.project').index(this) + 1);
		$(window).unbind('scroll resize');
		$(".caption_home").hide();
		$(".active-nav").toggleClass('active-nav');
		if ($(this).is('.art')) {
			$('#art').toggleClass('active-nav');
		}
		else {
			$('#theater').toggleClass('active-nav');
		}
		$('#pix-home').fadeOut(100, function(){
			$($tgt).addClass("prominent").parent().fadeIn(450, function(){
				adjustCushion();
				var $distance = $($tgt).offset();
				$('html, body').animate({scrollLeft: $distance.left-95}, $distance.left/4, function() {
					showOnScroll();
					$(window).bind('scroll resize', function() {
						showOnScroll();
						scrollControl();
					});
					showController();
				});
			});
		});
	});
	
	$('.begin').click(function() {
		$(window).unbind('scroll resize');
		var $horizontal = $(window).scrollLeft();
		$(".prominent").removeClass("prominent").siblings(".project").first().addClass("prominent"); //new
		$('html, body').animate({scrollLeft: 0}, $horizontal/9, function() {
			showOnScroll();
			$(window).bind('scroll resize', function() {
				showOnScroll();
				scrollControl();
			});
		});
	});

	$('.back').click(function() {
		var $origin = $(".prominent").offset();
		var documentScrollLeft = $(window).scrollLeft();
		var $ifduration =  (documentScrollLeft + 95) - $origin.left;
		if ( $origin.left < (documentScrollLeft + 95) || $(".prominent").is('.first')) {
			$(window).unbind('scroll resize');
			$('html, body').animate({scrollLeft: $origin.left-95}, $ifduration/2, function() {
				showOnScroll();
				$(window).bind('scroll resize', function() {
					showOnScroll();
					scrollControl();
				});
			});
		}
		else {
			var $distance = $(".prominent").prev().offset();
			var $duration = $origin.left - $distance.left;
			$(".prominent").removeClass("prominent").prev().addClass("prominent");
			$(window).unbind('scroll resize');
			$('html, body').animate({scrollLeft: $distance.left-95}, $duration/2, function() {
				showOnScroll();
				$(window).bind('scroll resize', function() {
					showOnScroll();
					scrollControl();
				});
			});
		}
	});
		
	$('.forward').click(function() {
		var $origin = $(".prominent").offset();
		var $distance = $(".prominent").next().offset();
		var $duration = $distance.left - $origin.left
		if ($(".prominent").is('.last')) {
			return false;
		}
		else {
			$(".prominent").removeClass("prominent").next().addClass("prominent");
			
			$(window).unbind('scroll resize');
			$('html, body').animate({scrollLeft: $distance.left-95}, $duration/2, function() {
				showOnScroll();
				$(window).bind('scroll resize', function() {
					showOnScroll();
					scrollControl();
				});
			});
		}
	});
	
	$('.end').click(function() {
		var $origin = $(".prominent").offset();
		var $distance = $(".prominent").siblings(".project").last().offset();
		var $duration = $distance.left - $origin.left
		if ($(".prominent").is('.last')) {
			return false;
		}
		else {
			$(".prominent").removeClass("prominent").siblings(".project").last().addClass("prominent"); //new
			
			$(window).unbind('scroll resize');
			$('html, body').animate({scrollLeft: $distance.left-95}, $duration/9, function() {
				showOnScroll();
				$(window).bind('scroll resize', function() {
					showOnScroll();
					scrollControl();
				});
			});
		}
	});

	$(window).unload(function() {
		$('html, body').scrollLeft(0);
	});	
	
	//if ( $('#art, #theater').is(':visible')){
		$("#deck").hover(
			function () {
				clearTimeout(t);
				$(this).parent().stop().fadeTo(150, 1.0);
			}, 
			function () {
				//$(this).parent().delay(800).fadeTo(1300, 0.3);
				$(this).parent().fadeTo(800, 0.3);
			}
		);
	//}
	
	//$(window).bind('scroll', function() {
	$(window).scroll(function() {
		scrollControl();
	})
	
	/* $('#submit').click(function() {
		alert($('form').serialize());
		return false;
	}); */
	
	$("#submit").click(function() {
		var yourname = $("#yourname").val();
		var yourmail = $("#yourmail").val();
		var yourmessage = $("#yourmessage").val();
		var dataString = $('form').serialize();
		
		/* if(yourname=='' || yourmail=='' || yourmessage=='' ) {
			alert('Problem');
		} */
		
		$('input:text').each(function() {
			if ($(this).is("#yourname")) {
				if ( (/^[a-z\s\-\.äàáâçèéêëìíîïôœùöóüùúûÿẞß]{2,30}$/i).test(this.value) ) {
					$(this).css({'background-color':'#BABBBA'}).removeClass("not").parent().prev().css({'color':'#000'});
				}
				else {
					$(this).css({'background-color':'#FF0068'}).addClass("not").parent().prev().css({'color':'#FF0068'});
				}
			}
			if ($(this).is("#yourmail")) {
				if ( (/^(("[\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).test(this.value) ) {
					$(this).css({'background-color':'#BABBBA'}).removeClass("not").parent().prev().css({'color':'#000'});
				}
				else {
					$(this).css({'background-color':'#FF0068'}).addClass("not").parent().prev().css({'color':'#FF0068'});
				}
			}
		});
		
		var pass = $("form .not").index();
		if (pass != -1) {
			return false;
		}
		
		$.ajax({
			type: "POST",
			url: "rhy_form_process.php",
			data: dataString,
			success: function(){
				//$("#pix-contact").hide().html("<span id='message_sent'><br />Dear " + yourname + ",<br />thank you for your message; it has been sent to Thomas.<br /><br />You wrote:<br />&ldquo;" + yourmessage + "&rdquo;&nbsp;&nbsp;&nbsp;</span>").fadeIn(1200);
				$("#pix-contact").hide().html("<span id='message_sent'><p>Dear " + yourname + ",<br />thank you for your message; it has been sent to Thomas.</p><p>You wrote:<br />&ldquo;" + yourmessage + "&rdquo;</p></span>").fadeIn(1200);
			}
		});
		return false; //prevents page reload
	});	
	
	
	
});


/* 

re $('#pix-home div').hover - all of these work, none of them fix IE7:
- $('p', this).show();
- $(this).find('p').show();
- $(this).children('p').show();

to do:
x next/previous/back to start navigation?
- activity monitor?
x $distance: stop propagation?
- re-implement imgOndemand?
- fix/footer on top (z-index), white background
- fix footer position (right, Safari 3.x only?)
- read http://blog.freelancer-id.com/index.php/2009/03/26/scroll-window-smoothly-in-jquery
- remove click (to scroll to 0) on active navigation?
- rhy.js: 'Resource interpreted as other but transferred with MIME type text/javascript.' ???
- (not js related) new navigation style not working in IE7 (missing underlines)
- Opera?
- text spacing in pix div
x wide window: pix-back shows at right
x recognize scroll position after manual scroll (not just when using controller)

NEW:
x controller not showing up in small window, no scrollbars appearing
- recalculate cushion size on window resize
- create clean working CSS, then target IE with modified versions thereof

*/
