$(function(){
	
	$(window).scroll(function(e) {
		sl = $(this).scrollLeft();
		if(sl > 0) {
			$("#page-header, #page-footer").css({left: -sl+'px'});
		} else {
			$("#page-header, #page-footer").css({left: 'auto'});
		}
	});
	
	$("#main .is-leaving").append( $("<img>")
		.addClass("flicker")
		.attr({
			src: "workspace/assets/img/flicker.png"
		})
	);
	$("#now-showing-nav .is-leaving").append( $("<img>")
		.addClass("flicker")
		.attr({
			src: "workspace/assets/img/flicker-tn.png"
		})
	);
	!function flicker(elm,minOp) {
		$(elm).each(function() {
			var $this = $(this);
			$this.animate({opacity: Math.random() - minOp},80,function(){ flicker($this,minOp); });
		});
	}(".flicker", 0.25);
	
	$('#now-showing-nav').localScroll();
	
	var max_time = 1439,
		min_time = max_time;
	$(".schedule").each(function(){
		var $this = $(this),
			$st = $this.find(".showtime"),
			$tst = $this.find(".today .showtime"),
			dur = Number($this.closest(".movie").find(".duration").attr("data-duration"));
		$st.each(function() {
			var $this = $(this),
				ta = $this.find(".time").text().split(":");
			ta[0] *= 1 
			ta[0] += 12;
			if(ta[1].match(/am$/gi)) {
				ta[0] -= 12;
				ta[1].replace(/am$/gi,"")
			}
			ta[1] *= 1;
			$this.data("time_minutes", (ta[0]*60) + ta[1]);
			$this.data("dur", dur);
			min_time = Math.min(min_time, $this.data("time_minutes"));
		});
		$tst.each(function() {
			var $this = $(this),
				offset_dur = Math.round((dur / (max_time-min_time))*254);
			$this.css({width: offset_dur+"px"});
		});
		$st.each(function() {
			var $this = $(this),
				offset_time = $this.data("time_minutes") - min_time,
				offset_left = Math.round((offset_time / (max_time - min_time))*254);
			//$this.text($this.text());
			$this.css({position: "absolute", left: offset_left+"px"});
		});
	});
	
	(function time_progress() {
		var d = new Date();
			//d.setHours(d.getHours()-4);
		var	time_minutes = (d.getHours() * 60) + d.getMinutes(),
			morning = Math.min(time_minutes, min_time),
			morning = (morning / min_time) * 166,
			offset_now = Math.max(0, time_minutes - min_time),
			offset_left = offset_now / (max_time - min_time),
			r = morning+Math.round(offset_left*254);
		
		$(".time-bar").animate({width: r+"px"});
		$(".today .showtime").each(function() {
			var $this = $(this);
			if(!$this.is(".past") && time_minutes > $this.data("time_minutes")) {
				$this.addClass("playing");
			}
			if(!$this.is(".past") && time_minutes > $this.data("time_minutes")+$this.data("dur")) {
				$this.removeClass("playing").addClass("past");
			}
		});
		
		window.setTimeout(time_progress, 60 * 1000);
		
	})();
	
	$(".showtime").mouseover(function(e) {
		var $this = $(this),
			pid = $this.attr("data-pricing"),
			$time = $this.find(".time"),
			co = $("#container").offset();
			l = ($time.offset().left + ($time.width()/2)) - co.left,
			t = $this.offset().top - co.top;
		
		$("#pricing-bubble").css({left: l+"px", top:t+"px"}).show().find("table[id="+pid+"]").show();
	}).mouseout(function(e) {
		$("#pricing-bubble").css({left: l+"px", top:t+"px"}).hide().find("table").hide();
	});
	
	$("#ie-warning .close").click(function() {
		$("#ie-warning").hide();
	});
	
	$("#now-showing-nav .posters .screen .poster:nth-child(2)").each(function(){
		$s = $(this).closest(".screen");
		$s.data("cur", 0);
		var f = function(_$s) {
			return function() {
				var cur = _$s.data("cur");
				var $p = _$s.find(".poster");
				$p.eq(cur).fadeOut(200);
				cur = (cur == $p.length-1) ? 0 : cur+1;
				$p.eq(cur).fadeIn(200);
				_$s.data("cur", cur)
			};
		};
		window.setInterval(f($s), 2500);
	});

});

