$(document).ready(function(){ 

	var video_type = 'flash';
	var browser = navigator.userAgent;
	if (browser.match('iPhone') || browser.match('iPad')) {
		video_type = 'html5';
	}	

	var current_page = 'home';
	var current_film = 'main';
	$('#main_menu #'+current_page).addClass('active');
	$("#content_wrap #inner #films #list_"+current_film).fadeIn();

	var homepics = 16;
	var current_homepic = 1;
	for (a = current_homepic; a <= homepics; a++) {
		$("#content_wrap #inner #home").append('<div class="homepic" ID="'+a+'"><img src="images/home/'+a+'.jpg" /></div>');
	}
	$("#content_wrap #inner #home #"+current_homepic+".homepic").fadeIn(1);
	setTimeout(function(){homepic_fade(current_homepic, homepics)}, 4000);


	$(".filmlink").click(function(){
		$("#content_wrap #inner #films #list_"+current_film).fadeOut();
		current_film = (this).id;
		$("#content_wrap #inner #films #list_"+current_film).fadeIn();
	});

	$(".back_to_films").click(function(){
		$("#content_wrap #inner #films #list_"+current_film).fadeOut();
		current_film = 'main';
		$("#content_wrap #inner #films #list_"+current_film).fadeIn();
	});


	$("#main_menu a").click(function(){
		$('#main_menu #'+current_page).removeClass('active');
		if (current_page == 'home' && $(this).parent().get(0).id != 'home') {
			$(".gold_banner").fadeOut('fast');
		} else if (current_page != 'home' && $(this).parent().get(0).id == 'home') {
			$(".gold_banner").fadeIn('fast');
		}
		current_page = $(this).parent().get(0).id;
		$('#main_menu #'+current_page).addClass('active');
		$('#reel_video_player').html('<img src="/images/reel_placeholder.jpg" alt="reel" id="reel_placeholder" />');
		if (current_film != 'main') {
			$("#content_wrap #inner #films #list_"+current_film).fadeOut();
			current_film = 'main';
			$("#content_wrap #inner #films #list_"+current_film).fadeIn();
		}
		var leftPosition = -(930*(this).id);
		$('#content_wrap #inner').animate(
			{ left: (leftPosition) },
			{ duration: 1200, easing: 'easeInOutCirc' }
		);	
	});

	$("a.filmlink").hover(
		function() {$('#title_'+(this).id).fadeIn('fast');},
		function() {$('#title_'+(this).id).fadeOut('fast');}
	);


	var a = '';
	$("#contact_form #email_address").focus(function(){
		$(this).val(a);
    });
	$("#contact_form #subject").focus(function(){
		$(this).val(a);
    });
	$("#contact_form #message").focus(function(){
		$(this).val(a);
    });
	$("#contact_form #submit_button").click(function() {
		var email = $('input[id=email_address]');
		if (email.val().length == 0 || email.val() == 'your email address') {
			alert('Please enter your email address.');
	        return false;
		}
		var subject = $('input[id=subject]');
		if (subject.val().length == 0 || subject.val() == 'subject') {
			alert('Please enter a subject for your email.');
	        return false;
		}
		var data = $("#contact_form").serialize();
		$.ajax({
			type: "POST",
			url: '/functions/contact_send.php',
	        dataType: "text",
	        data: data,
			error: function(){ alert('Sorry, an error occurred.  Please try again.'); },
			success: function(a) {
				$('#contact_form').html(a);
			}
		});
		return false;
    });


	$("a.trailer").click(function(){
		$('#main_menu #'+current_page).removeClass('active');
		current_page = 4;
		$('#main_menu #'+current_page).addClass('active');
		var leftPosition = -(930*4);
		$('#content_wrap #inner').animate(
			{ left: (leftPosition) },
			{ duration: 1200, easing: 'easeInOutCirc' }
		);	
		show_video((this).id);
	});

	$("#reel_titles a").click(function(){
		show_video((this).id);
	});

	function show_video(this_id) {
		if (video_type == 'none') {
		} else if (video_type == 'html5') {
			$('#reel_video').html('<video width="720" height="400" id="video1" controls><source id="mp4" src="/images/trailers/'+this_id+'.mp4" type="video/mp4" codecs="avc1, mp4a"></video>');
		    document._video = document.getElementById("video1");
			document._video.play();
		} else {
		    $('#reel_video_player').jwPlayer({
		        //debug: 'console',
		        swf: '/js/player.swf',
		        file: '/images/trailers/'+this_id+'.mp4',
		        id: 'jwPlayer_'+this_id,
		        height: 400,
		        width: 720,
		        autostart:true
		    });
		}
	}



	
	function homepic_fade(current_homepic, homepics) {
		$("#content_wrap #inner #home #"+current_homepic+".homepic").fadeOut(2000);
		if (current_homepic == homepics) {
			current_homepic = 1;
		} else {
			current_homepic++;
		}
		$("#content_wrap #inner #home #"+current_homepic+".homepic").fadeIn(2000);
		setTimeout(function(){homepic_fade(current_homepic, homepics)}, 4000);
	}





});



