var widthPorp,heightPorp,leftPos,topPos,boxWidth,boxHeight,boxTop,boxLeft,eyeRollTimeOut,hoverScroll,hoverScrollb;

var cache = [];
  // Arguments are image paths relative to the current page.
  var preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  
  preLoadImages('images/l_curtain.png','images/r_curtain.png','images/redcurtain.png','images/big_cornwheel.png','images/biographylink_over.png');

function getContent(p,c, callback){
	page = p.split('?');
	$.ajax({
		type: "GET",
		url: page[0],
		data: page[1],
		cache: false,
		success: function(html){
			$("#" + c).append(html);
		},
		complete: function(){
			if(typeof callback != 'undefined'){
				callback();	
			}
		}
	});
}

function clearContent(c){
	$("#" + c).unbind();
	$("#" + c).empty();	
}

function startEyeroll(){
	eyeRollTimeOut = setTimeout('startEyeroll()',10);
	$('#theEye').animate({rotate:'+=6deg'},10);
}

function stopEyeroll(){
	clearTimeout(eyeRollTimeOut);
	$('#theEye').stop(true,true);
	$('#theEye').animate({rotate:'0deg'});
}

function startPinwheel(){
	pinwheelTimeOut = setTimeout('startPinwheel()',10);
	$('#pinWheel').animate({rotate:'+=6deg'},10);
}

function stopPinwheel(){
	$('#pinWheel').fadeOut({queue:false,complete:function(){clearTimeout(pinwheelTimeOut);}});
}





function takeoffBalloon(){

	$('#redBalloon').animate({
  			
		path : new $.path.bezier({
	    	start: { 
	    	  x: 328, 
	    	  y: 245, 
	    	  angle: -5,
	    	  length: 2.0
	    	},  
	    	end: { 
	    	  x:$(document).width()/2,
	    	  y:200, 
	    	  angle: 20, 
	    	  length: 4.0
	    	}
	  	}),
	  	scale: 0.5,
	  	rotate: '-=380deg'
	  	
	  },800,function(){$('#redBalloon').hide();}
	  
	  
	  );
}

function inflateBalloon(){
	

	$('#redBalloon').scale('0.0').rotate('0deg');
	
	$('#redBalloon').css({
		position:'absolute',
		cursor:'pointer',
		top:'245px',
		left:'234px',	
		zindex:'50'
	});
	
	$('#redBalloon').show();
	
	$('#redBalloon').animate({
		scale: '1.0',
		left:'328px'
	}, 1200);

}



$(window).load(function(){
	
	$('#theCurtain').animate({
		top:'-650px'
	},{duration:4000,queue:false,easing:false,complete:
	function(){
		
	$('#stageCurtain').remove();

	$('#pictureFrameWelcome').click(function(){
		$('#pictureSlides').cycle(3);
		$('#pictureFrame').append('<div id="galleryListClose" onclick="$(\'#pictureSlides\').cycle(0);">back</div>');	
	});

	$('#pictureFrameWelcome').mouseenter(function(){
		$(this).children('img').stop();
		$(this).children('img').fadeTo('fast',1.0);
		});
		
	$('#pictureFrameWelcome').mouseleave(function(){
		$(this).children('img').stop();
		$(this).children('img').fadeTo('fast',0.0);
		});

}
	
	
	});


});
$(document).ready(function(){

	$('#pictureSlides').cycle({
		fx:'scrollVert',
		timeout:0,
		speed:3000,
		before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			if(currSlideElement.id === 'faceSlide' && nextSlideElement.id !== 'faceSlide'){
				takeoffBalloon();
			}
		},
		after: function(currSlideElement, nextSlideElement, options, forwardFlag){
			if(nextSlideElement.id === 'faceSlide'){
				inflateBalloon();
			}
		}
	});

	boxWidth = $('#eyeballbox').width();
	boxHeight = $('#eyeballbox').height();
	
	boxLeft = $('#eyeballbox').offset().left;
	boxTop = $('#eyeballbox').offset().top
	
	widthPorp = boxWidth/$(document).width();
	heightPorp = boxHeight/$(document).height();

$(window).mousemove(function(e){


	leftPos = (boxWidth/2) + 

				(
					(-(boxWidth/2 + boxLeft) + e.pageX) 
					* widthPorp
				);
				
    topPos = (boxHeight/2) + 

				(
					(-(boxHeight/2 + boxTop) + e.pageY)
					* heightPorp
				);
	



	$('#eyeball').css({
			top:((Math.round(topPos) > boxHeight) ? boxHeight : Math.round(topPos)),
			left:((Math.round(leftPos) > boxWidth) ? boxWidth : Math.round(leftPos))
		});
		
})

});

function closeGalleryList(){
	$('#pictureSlides').cycle(0);
	setTimeout(function(){
		
			$('#gallerySlide').unbind();
			$('#gallerySlide').children().unbind();
			$('#gallerySlide').empty();
			$('#galleryListClose').remove();
			
		
		},3000);
	
}

function loadGalleryList(){ 
getContent('galleries.php','gallerySlide',function(){              
	$('#pictureSlides').cycle(2);
	setTimeout(function(){	
		
		$('#pictureFrame').append('<div id="galleryListClose" onclick="closeGalleryList()">back</div>');		
		hoverScroll = $('#galleryList').hoverscroll({
			vertical: true,    // Display the list vertically or horizontally
     
			width:    213,      // Width of the list container
			height:   320,       // Height of the list container
  
			arrows:   false,      // Display direction indicator arrows or not
			
			rtl:      false,     // Print images from right to left

			debug:    false     // Debug output in the firebug console								
		});
		
		hoverScroll[0].startMoving(1, 2);
		
		$('.galleryListOverlay').hide();
		
		$('#galleryList').mouseout(function(){
			
			setTimeout(function(){hoverScroll[0].startMoving(1, 2)},100);
			
			});
		
		$('.galleryListThumb').click(function(){
			
			showGallery(this.id);
		
		});
		
		$('.galleryListThumb').mouseenter(function(){
			
			$(this).children('.galleryListOverlay').fadeTo('fast',0.7);
		
		});
		
		$('.galleryListThumb').mouseleave(function(){
			
			$(this).children('.galleryListOverlay').fadeTo('fast',0.0);
		});

		
	},3000)
});
}

function showGallery(idstr){
	clearContent('bigGalleryWindow');
	$('#bigGalleryWindow').append('<div id="bigGalleryScroller"></div>');
	
	
	getContent('gallery.php?idstr='+idstr, 'bigGalleryScroller', function(){
		$('#bigGalleryWindow').animate({height:'650px'},function(){
		
			$('#bigGalleryScroller').children('.galleryThumb').mouseenter(function(){
				$(this).children('.galleryOverlay').animate({top:'0px',left:'0px',width:'112px',height:'112px'},{queue:false});
			});
			
			$('#bigGalleryScroller').children('.galleryThumb').mouseleave(function(){
				$(this).children('.galleryOverlay').animate({top:'110px',left:'110px',width:'0px',height:'0px'},{queue:false});
			});
			
			$('#bigGalleryWindow').append('<img id="galleryCloseLink" src="images/close.png" width="37" height="31" border="0">');
			$('#galleryCloseLink').click(function(){
					$('#bigGalleryWindow').animate({height:'0px'},function(){$('#bigGalleryWindow').empty();});
				});	
				
			$(this).css({'overflow':'visible'});
				
			console.log($('#'+idstr).next())	
				
			if($('#'+idstr).next().length != 0){
				$('#bigGalleryWindow').append(
										$('<img>') 
											.attr({'class':'nextGallery','src':'images/nextGal.png'})
											.click(function(){
												showGallery($('#'+idstr).next().attr('id'));
											})
										)	
			
			}
			
			
			if($('#'+idstr).prev().length != 0){
				$('#bigGalleryWindow').append(
										$('<img>') 
											.attr({'class':'prevGallery','src':'images/prevGal.png'})
											.click(function(){
												showGallery($('#'+idstr).prev().attr('id'));
											})
										)	
			
			}	
				
				
		});
	});
}

function showImage(fname,repath,w,h){
	var args = arguments;
	
	if($('#imageOverlay').length == 0){
	
		$('body').append('<div id="imageOverlay"><div id="imageBg">&nbsp;</div><img src="images/big_cornwheel.png" id="pinWheel" class="imgPinwheel" /><img src="image.php/'+ 
														fname+
														'?width='+
														($(window).width() > w ? w : $(window).width())+
														'&height='+
														($(window).height() > h ? h : $(window).height())+
														'&image='+
														repath+
														'" border="0" id="overlayedImage" '+
														'style="margin-top:-'+(($(window).height() > h ? h : $(window).height())/2)+'px;"'+
														' /></div>');
														startPinwheel();
														$('#overlayedImage').hide();
														$('#imageOverlay').hide().fadeIn();
														
	}else{
		
		$('#overlayedImage,.prevImage,.nextImage').remove();
		startPinwheel();
		$('#pinWheel').fadeIn({queue:false});
		$('#imageOverlay').append('<img src="image.php/'+ 
											fname+
											'?width='+
											($(window).width() > w ? w : $(window).width())+
											'&height='+
											($(window).height() > h ? h : $(window).height())+
											'&image='+
											repath+
											'" border="0" id="overlayedImage" '+
											'style="margin-top:-'+(($(window).height() > h ? h : $(window).height())/2)+'px;"'+
											' />'
		);
		$('#overlayedImage').hide();
		
	}
													
													
	$('#overlayedImage').bind('load',function(){
		stopPinwheel();
		$('#overlayedImage').fadeIn();
		
		
		
		
		if(args[4].length != 0){
			if(args[4].next().length != 0){
				$('#imageOverlay').append(
										$('<img>') 
											.attr({'class':'nextImage','src':'images/nextImg.png'})
											.click(function(){
												args[4].next().click();
											})
										)	
			
			}
			
			
			if(args[4].prev().length != 0){
				$('#imageOverlay').append(
										$('<img>') 
											.attr({'class':'prevImage','src':'images/prevImg.png'})
											.click(function(){
												args[4].prev().click();
											})
										)	
			
			}
		}
			
	}).click(function(){
		$('#imageOverlay').fadeOut(function(){$(this).remove();});	
	});
}
