/**
 * jq_filmStrip Jan 2011 Version 1.0
 * Maritta Sturt
 * Copyright (c) M. Sturt 
 */

//  images to be loaded
p = 0;
ldImgs = new Array;
ldTles = new Array;
ldImgs[p] = 'images/hm_film15.jpg';
ldTles[p] = 'Live @ The Troubadour, London';
p++;
ldImgs[p] = 'images/hm_film12.jpg';
ldTles[p] = 'Live @ The Troubadour, London';
p++;
ldImgs[p] = 'images/hm_film13.jpg';
ldTles[p] = 'Live @ The Troubadour, London';
p++;
ldImgs[p] = 'images/hm_film14.jpg';
ldTles[p] = 'Live @ The Troubadour, London';
p++;
ldImgs[p] = 'images/hm_film1.jpg';
ldTles[p] = 'Live @ HALO, London';
p++;
ldImgs[p] = 'images/hm_film2.jpg';
ldTles[p] = 'Live @ HALO, London';
p++;
ldImgs[p] = 'images/hm_film3.jpg';
ldTles[p] = 'Live @ HALO, London';
p++;
ldImgs[p] = 'images/hm_film4.jpg';
ldTles[p] = 'Live @ HALO, London';
p++;
ldImgs[p] = 'images/hm_film5.jpg';
ldTles[p] = 'Live @ HALO, London';
p++;
ldImgs[p] = 'images/hm_film6.jpg';
ldTles[p] = 'Live @ Storm, Leicester Square, London';
p++;
ldImgs[p] = 'images/hm_film7.jpg';
ldTles[p] = 'Live @ Storm, Leicester Square, London';
p++;
ldImgs[p] = 'images/hm_film8.jpg';
ldTles[p] = 'Live. Opening for Candi Staton';
p++;
ldImgs[p] = 'images/hm_film9.jpg';
ldTles[p] = 'Live. Opening for Candi Staton';
p++;
ldImgs[p] = 'images/hm_film10.jpg';
ldTles[p] = 'Live. Opening for Candi Staton';
p++;
ldImgs[p] = 'images/hm_film11.jpg';
ldTles[p] = 'Live. Opening for Candi Staton';
p++;


if (playFilm == undefined) var newObj = true;
else var newObj = false;

// start images
var imgNo = 0;

var sldPlay = true;
var motion = true;
var playFilm = {};

var flmTt;

$(function() {

	playFilm = {

		initFilmAnim: function() {
			
			// get image Height for movement amount and add borders and padding
			//var imgHeight = $('#flm_ph1 img').height();
				
			$('#flm_ph1').css('z-index', 1);
			$('#flm_ph2').css('z-index', 2);
		
			playFilm.moveUpPh2();
			
			sldPlay = true;
			motion = true;
			imgNo = 0;		
		
		},// initFilmAnim()
		moveUpPh2: function() {		
	
			$('#flm_ph2').animate({top: 0}, {
				duration: 2000, 
				specialEasing: {
				  top: 'linear'
				}, 
				complete: function() {
					playFilm.loadPh1();
				}
			});	
		},// moveUpPh2
		loadPh1:function() {
		
			// get next image name
			imgNo++;
			if (imgNo == ldImgs.length) imgNo = 0;
			var src;
			src = ldImgs[imgNo];
					
			// load new image
			var img = new Image();
			$(img)
				.load(function () {
					
					var imageHeight = this.height;
				
					// close ph1
					$('#flm_ph1').animate({height: 1}, {
						duration: 100, 
						specialEasing: {
						  top: 'linear'
						}, 
						complete: function() {
						
							// move down ph2
							$('#flm_ph2').animate({top: 17}, {
								duration: 1000, 
								specialEasing: {
								  top: 'linear'
								}, 
								complete: function() {
									playFilm.openPh1(src, imageHeight);
								}
							});
						}
					});
				
				})// load
				.attr('src', src);		
		
		},// loadPh1
		openPh1: function(src, newHeight) {
			
			$('#flm_ph1 img').attr('src', src);
			$('#flm_ph1 img').attr('alt', ldTles[imgNo]);
			
			// open ph1
			var openAmt = newHeight + 8;
			$('#flm_ph1').animate({height: newHeight}, {
				duration: 3000, 
				specialEasing: {
				  top: 'linear'
				}, 
				complete: function() {
				}
			}); 
			
			// close ph2 and move down
			var moveAmt = newHeight + 17;
			$('#flm_ph2').animate({top: moveAmt, height: 1}, {
				duration: 3000, 
				specialEasing: {
				  top: 'linear'
				}, 
				complete: function() {
					flmTt = setTimeout(function(){playFilm.loadPh2(src, newHeight);}, 100);
				}
			});
			
		},// loadPh1
		loadPh2: function(src, newHeight) {
		
			$('#flm_ph2 img').attr('src', src);
			
			// open ph2
			var openAmt = newHeight + 8;
			$('#flm_ph2').animate({height: openAmt}, {
				duration: 3000, 
				specialEasing: {
				  top: 'linear'
				}, 
				complete: function() {
					motion = false;
					if (!sldPlay) $('#sld_play').css('display','block');
					flmTt = setTimeout(function(){playFilm.startFilm();}, 3000);
				}
			}); 
		
		},// loadPh2	
		startFilm: function() {
			clearTimeout(flmTt);
			if (sldPlay) {
				motion = true;
				playFilm.moveUpPh2();
			}
		}// startFilm
	
	};//playFilm


	$('#film_strip').unbind();
	$('#sld_pause a').unbind();
	$('#sld_play a').unbind();
	
	$('#film_strip').hover(function() {
		$('#controls').slideDown(700);
	}, function() {
		$('#controls').slideUp(400);
	});
	
	$('#sld_pause a').click(function() {
		if (sldPlay) {
			sldPlay = false; 
			$('#sld_pause').css('display','none');
			if (!motion) {$('#sld_play').css('display','block'); }		
		}
		return false;
	});
	$('#sld_play a').click(function() {
		if (!sldPlay && !motion) {
			sldPlay = true;
			$('#sld_play').css('display','none'); 
			$('#sld_pause').css('display','block');
			playFilm.startFilm();
		}
		return false;
	});
	
	function filmLoad() {

		var img = new Image();
		
		for(i=0;i<ldImgs.length;i++) {
			$(img)
				.load(function () {
				  //$(this).hide();
				})
				// if there was an error loading the image, react accordingly
				//.error(function () {
				  // notify the user that the image could not be loaded
				//})
				// *finally*, set the src attribute of the new image to our image
				.attr('src', ldImgs[i]);
		 } // for
			
	}// filmLoad
	
	setTimeout(function(){playFilm.initFilmAnim();}, 3000);
	
	if (newObj) filmLoad();
	
});//function
