$(document).ready(function(){ 

	var page = jQuery.url.segment(0);
	if (page == 'ppsuccess') {
		$.cookie('rso_ok', 1, {path: '/',domain:'.rsothemovie.com',expires: 365});
		$('#home').fadeOut(1000);
		$('#view_page').fadeIn(1000);
		$('#darkened').fadeIn(1000);
		$('#callouts').fadeOut(1000);
		show_movie('part2');
	} else {
		callout_animation();
		show_trailer('false');
	}

	$("#watch_button").hover(
		function() { $('#watch_button').attr({src:'images/watch_on.png'}); },
		function() { $('#watch_button').attr({src:'images/watch_off.png'}); }
	);
	$("#order_button").hover(
		function() { $('#order_button').attr({src:'images/order_on.png'}); },
		function() { $('#order_button').attr({src:'images/order_off.png'}); }
	);

	$("#watch_button").click(function(){
		$('#home').fadeOut(1000);
		$('#view_page').fadeIn(1000);
		$('#darkened').fadeIn(1000);
		$('#callouts').fadeOut(1000);
		show_movie('part1');
	});

	function show_trailer(auto) {
		$('#movie_box').html('');
		var browser = navigator.userAgent;
		if (browser.match('iPhone') || browser.match('iPad')) {
//			$('#trailer_box').html('<video src="http://rsothemovie.s3.amazonaws.com/RSOTrailer_trimmed.mp4" height="370" width="676" controls autoplay poster="/images/RSOTrailer.jpg" type="video/mp4"></video>');
			$('#trailer_box').html('<video src="/images/RSOTrailer_trimmed.mp4" height="370" width="676" controls autoplay poster="/images/RSOTrailer.jpg" type="video/mp4"></video>');
		} else {
			$('#trailer_box').flash({
				swf: '/lib/player.swf',
				width: '676',
				height: '388',
				flashvars: {
					file: '/images/RSOTrailer_trimmed.mp4',
					image: '/images/RSOTrailer.jpg',
					autostart: auto
				}
			});
		}
	}

	function show_movie(part) {
		$('#chapters_list').html('');
		$('#chapters_list').append('<div><IMG SRC="/movies/part1.jpg" class="ill"><div><A class="chapter" ID="part1">Chapter 1</A></div></div>');
		if ($.cookie('rso_ok') == 1) {
			for (a = 2; a < 11; a++) {
				$('#chapters_list').append('<div><IMG SRC="/movies/part'+a+'.jpg" class="ill"><div><A class="chapter" ID="part'+a+'">Chapter '+a+'</A></div></div>');
			}

			var donate = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">';
			donate += '<input type="hidden" name="cmd" value="_s-xclick">';
			donate += '<input type="hidden" name="hosted_button_id" value="RGRDSLW3CQ6CS">';
			donate += '<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">';
			donate += '<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">';
			donate += '</form>';
			$('#chapters_list').append('<div><P class="thanks">Thank you for your donation!  If you really like the film, you can donate more...</P></div>');
			$('#chapters_list').append(donate);

//			if ($('#movie_info_box .thanks').length == 0) {
//				$('#movie_info_box').prepend('<P class="thanks">Thank you for your donation!</P>');
//			}
			$(".chapter").click(function(){
				load_a_chapter(this.id);
			});
		} else {
			var donate = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">';
			donate += '<input type="hidden" name="cmd" value="_s-xclick">';
			donate += '<input type="hidden" name="hosted_button_id" value="RGRDSLW3CQ6CS">';
			donate += '<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">';
			donate += '<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">';
			donate += '</form>';
			$('#chapters_list').append('<div><P>You can watch the first chapter of the film for free.  If you want to watch the rest, please use the Donate button below to donate something.  Any amount you choose is acceptable.</P></div>');
			$('#chapters_list').append(donate);
		}
		load_a_chapter(part);
	}

	function load_a_chapter(part) {
		$('#movie_box').html('');
		$('.chapter').removeClass("current");
		$('#'+part).addClass("current");
		var browser = navigator.userAgent;
		if (browser.match('iPhone') || browser.match('iPad')) {
			$('#movie_box').html('<video src="/movies/'+part+'.mp4" height="420" width="720" controls poster="/images/RSOTrailer.jpg" type="video/mp4"></video>');
		} else {
			$('#trailer_box').html('');
			$('#movie_box').flash({
				swf: '/lib/player.swf',
				width: '720',
				height: '426',
				allowFullScreen:'true',
				flashvars: {
					file: '/movies/'+part+'.mp4',
					image: '/images/RSOTrailer.jpg',
					autostart: 'true'
				}
			});
		}
	}


	$("#close_button").click(function(){
		$('#home').fadeIn(1000);
		$('#view_page').fadeOut(1000);
		$('#darkened').fadeOut(1000);
		$('#callouts').fadeIn(1000);
		$('#callout1').fadeOut(10);
		$('#callout2').fadeOut(10);
		$('#callout3').fadeOut(10);
		show_trailer('false');
	});

	function callout_animation() {
		$('#callout1').fadeIn(3000,function(){
			$('#callout1').fadeOut(3000);
			$('#callout2').fadeIn(3000,function(){
				$('#callout2').fadeOut(3000);
				$('#callout3').fadeIn(3000,function(){
					$('#callout3').fadeOut(3000);
					setTimeout(function(){callout_animation();}, 3000); 
				}); 
			}); 
		}); 
	}


});

