google.load("jquery", "1.5.1");
 
google.setOnLoadCallback(function() {

	$.ajaxSetup({async: false});
	$.getScript('/inc/jquery.spritely.js');
	$.ajaxSetup({async: true});
	
	for(layers=1;layers<6;layers++) {
		
		$("#pageBGlayer"+layers).css({
		
			width:$(window).width(),
			height:$(window).height()
		
		}).pan({fps: 24, speed: layers, dir: 'left'});
		
	}
	
	

	var iP=$("#infoPlate");
	var firstTimeThrough=true;
	
	$(".attachment-post-thumbnail").each(function() {
		
		$this=$(this);
		
		$this.wrap("<div class='post_thumb' style='background-image: url(\""+$this.attr("src")+"\")'>").parent().html("");
		
	});
	
	$("#layout").prepend("<div id='starBurst'></div><div id='swooshy'</div>");
	
	centerEverything("bitch");

	$("#infoPlate").fadeIn("slow");
	
	roundItAll("bitches!  fo real!");
	
	$(window).resize(function() {
	
		centerEverything("bitch");
														
	});

	$("#jamesPic").hover(function(){
		
		sB=$("#starBurst");
		swooshy=$("#swooshy");
		
		if(sB.children("img").length>0) { sB.children("img").each(function(){ $(this).remove(); }); }
		if(swooshy.children("img").length>0) { swooshy.children("img").each(function(){ $(this).remove(); }); }
		
		$("#starBurst").prepend("<img src='/images/page_bg_starburst.png'>").css("display","block");
		$("#swooshy").prepend("<img src='/images/page_bg_swooshy.png'>").css("display","block");
		
		var sBimg=$("#starBurst img");
		var swooshyImg=$("#swooshy img");
		
		sBimg.css("opacity",0).animate({

			width: 400,
			height: 400,
			opacity: 1,
			marginTop: '56px',
			marginLeft: '56px'
									
		},750);
		
		swooshyImg.css("opacity",0).animate({

			marginLeft: "15%",
			opacity: 1
									
		},1250);
		
	},function(){
		
		var sBimg=$("#starBurst img");
		var swooshyImg=$("#swooshy img");
	
		sBimg.animate({

			width: 640,
			height: 640,
			opacity: 0,
			marginTop: '-64px',
			marginLeft: '-64px'
									
		},750,function(){ sBimg.remove(); });
		
		swooshyImg.animate({

			marginLeft: "30%",
			opacity: 0
									
		},1250,function(){ swooshyImg.remove(); });		
		
	}).click(function(){
		
		if(firstTimeThrough==true) {
	
			$("#infoPlate").animate({
			
				marginLeft: "-24%",
				width: "60%"
														
			},400).children(".container").animate({
		
				width: "100%"
														
			},750,function(){
			
				$("#infoPlate #jamesInfo ul li").each(function(){
	
					$(this).css("width",($("#infoPlate").width()-176)+"px");
																								 
				});
			
				$("#jamesInfo").fadeIn(1000,function(){
			
					$("#contactPlate").fadeIn(500);
					$("#infoPlate").prepend("<div id='iPclose'><a href='#'><span>&laquo;</span></a></div>");
					
					$("#iPclose a").click(function(){
	
						$("#infoPlate").animate({
		
							marginLeft: "0%",
							width: "128px"
														
						},750);
			
						$("#jamesInfo").fadeOut("100");
						$("#iPclose").fadeOut("500").remove();
						$("#contactPlate").fadeOut("400");
						$("#blogLink").fadeOut("100");
						$("#jamesPic .absRounded").fadeOut(1500,function(){ $(this).remove(); });
						$("#twitterLink").remove();
						
						firstTimeThrough=true;
			
					});
				
					$("#jamesPic").prepend("<a href='http://twitter.com/sammysunset/' target='_blank' id='twitterLink'>follow me on Twitter</a><img src='images/photo_james2.jpg' width='128' height='128' class='rounded' />");
				
					roundItAll("shit be kickin'");
				
					$("#jamesPic .absRounded").css({ display: "block", opacity: 0 }).animate({
				
						opacity: 1
				
					},750);
				
					$("#jamesPic").hover(function(){

						$(this).children(".absRounded").animate({
					
							width: "200px",
							height: "200px",
							marginTop: "-37px",
							marginLeft: "-70px"
													
						},750);
					
						$("#twitterLink").css({ opacity: 0, display: "block" }).animate({ opacity: 1 },1000);
																																																																					 					},function(){
				
						$("#twitterLink").animate({ opacity: 0 },500);
					
						$(this).children(".absRounded").animate({
					
							width: "128px",
							height: "128px",
							marginTop: "0px",
							marginLeft: "0px"
														
						},750);
																																																																										
					});
																						 
				});
				
				$("#blogLink a").html("<span>"+$("#blogLink a").html()+"</span>").parent().fadeIn(500).click(function(){
				
					$("#jamesInfo").fadeOut(100);
					$("#iPclose").fadeOut(500);
					$("#contactPlate").fadeOut(400);
					$("#blogLink").fadeOut(100);						
					$("#twitterLink").fadeOut(100);
					$("#jamesPic").fadeOut(500,function(){
								
						var iPc=$("#infoPlate > .container");
						var originaliPc=Array();
						
						originaliPc['width']=iPc.css("width");
						originaliPc['height']=iPc.css("height");
						originaliPc['top']=iPc.offset().top;
						originaliPc['left']=iPc.offset().left;
					
						iPc.animate({
						
							height: 0,
							marginTop: 64,
							marginLeft: 0
																		
						},350,function(){
							
							iPcoord=findCenter("#infoPlate");
						
							$(this).animate({
						
								width: 0,
								left: (iPcoord['left']+128)
																		
							},350,function(){	
								
								if ($.browser.mozilla) {	iPtop="-12%"; }
									else { iPtop="-60%"; }
								
								iPc.animate({
								
									width: "110%",
									marginLeft: "-58%",
									height: "200%",
									marginTop: iPtop
								
								},1000,function(){
								
									$("#layout").prepend("<div id='logo'></div><div id='blueSwooshy'></div><div id='blueSwooshy2'></div>");
								
									$("logo").css("opacity",0);
									$("blueSwooshy").css("opacity",0);
									$("blueSwooshy2").css("opacity",0);
								
									pleaseCenter("#logo");
									pleaseCenter("#blueSwooshy");
									pleaseCenter("#blueSwooshy2");
									
									$("#blogPosts").fadeIn(500,function(){
									
										$("#logo").animate({
											
											top: "10%",
											opacity: 1
											
										},500).click(function(){ window.location='http://littlejames.com/'; });
										
										$("#blueSwooshy").animate({
											
											top: 0,
											opacity: 1
											
										},750);
										
										$("#blueSwooshy2").animate({
											
											top: 0,
											opacity: 1
											
										},1000);
										
										iPc.prepend("<div id='orangeRope'></div>");
										
										oRleft=iPc.position().left;
										oRtop=(iPc.position().top+iPc.height()-80);
										
										$("#orangeRope").css({
										
											top: oRtop,
											zIndex: 5
																				 
										}).animate({
											
											top: oRtop+128
											
										},1000).sprite({fps: 12, no_of_frames: 10, play_frames: 10}).hover(function(){
											
											newTop=($(this).position().top+16);
											$(this).animate({ top: newTop },500);
											
										},function(){
											
											newTop=($(this).position().top-16);
											$(this).animate({ top: newTop },500);
											
										}).click(function(){
											
											$(this).spChangeDir('right').sprite({fps: 12, no_of_frames: 10, play_frames: 10}).animate({
											
												top: oRtop
																																																								
											},1000,function(){
										
												$("#readMoreLink").fadeOut(200,function(){
											
													$("#blogPosts").fadeOut(500,function(){
												
														iPc.css("position","absolute").animate({
																
															width: originaliPc['width'],
															height: originaliPc['height'],
															top: ($(this).position().top+256),
															opacity: 0
																
														},500,function(){
															
														$("#logo").animate({
											
															top: "25%",
															opacity: 0
											
														},500);
										
														$("#blueSwooshy").animate({
											
															top: 16,
															opacity: 0
											
														},750);
										
														$("#blueSwooshy2").animate({
											
															top: 256,
															opacity: 0
											
														},1000,function(){ document.location.reload(); });
															
														});
													
													});
												
												});
												
											}).fadeOut(150);
										
										});
																											
									});
								
								});
								
							});
							
						});
					
					});
					
					// Still in blogLink click
					
					$("#readMoreLink a").hover(function(){
					
							$(this).animate({
							
								marginTop: 8
							
							},500);
					
					},function(){
					
							$(this).animate({
							
								marginTop: 0
							
							},500);
					
					});
					
				});
				
				firstTimeThrough=false;
																																																							
			});
				
		}	
	
		$("#dropMeALineLink").click(function(){
	
			$(this).animate({ marginTop: 128, opacity: 0 });
/*
			$this=$(this);
	
			$("#infoPlate").animate({

				marginTop: "-20%"

			},750);
	
			$this.parent().prepend("<a href='#' id='dMaLcover'></a>").children("#dropMeALineLink").css("display","none");
			
			$("#dMaLcover").html("Drop me a Line").css({ position: "absolute" }).animate({ marginTop: 8 },350).animate({ marginTop: 32 },250).animate({ marginTop: 54 },150).click(function(){
				
				$("#contactFormPlate form").slideUp(500);
				$("#contactFormPlate").animate({ marginTop: 0 },500);
					
				$(this).animate({
							
					marginTop: "0px",
					marginLeft: "0px"
													
				},500,function(){
					
					$("#dropMeALineLink").css("display","block");
					$("#dMaLcover").remove();
					
				});
				
																																																	 
			});
	
			if($("#contactFormPlate").length==0) {
				
				var ranVal=Math.floor(Math.random()*5);
				var dire=Array("up","right","down","left");
				
				alert(ranVal);
				
				$("#infoPlate").prepend("<div id='contactFormPlate'><form action='em.php' method='get' target='#contactIframe'><input type='hidden' name='a' value='"+ranVal+"'><ul><li><label for='name'>Name (optional):</label><input type='text' class='inputText' name='name'></li><li><label for='email'>Email (optional):</label><input type='text' class='inputText' name='email'></li><li><label for='comment'>Comment:</label><textarea class='inputTextarea' name='comment' id='commentBox'></textarea></li><li><input type='submit' value='HIT ME' class='inputSubmit'></li><li><div id='captcha'><label for='captcha'>Click on the arrow pointing "+dire[ranVal]+":</label><ul><li><span id='captchaArrowUp'><input id='r0' type='radio' name='captcha' value='d'></span><span id='captchaArrowRight'><input id='r1' type='radio' name='captcha' value='l'></span><span id='captchaArrowDown'><input id='r2' type='radio' name='captcha' value='u'></span><span id='captchaArrowLeft'><input id='r3' type='radio' name='captcha' value='r'></span></li></ul></div></li></ul></form></div>");
			
				var $imgIndex=0;
			
				$("#captcha").find("span").each(function(){
				
				$(this).attr("rel",$imgIndex);
							
				$imgIndex++;
				
				$(this).click(function(){
				
					theRel=$(this).attr("rel");
				
					$("#captcha span").each(function() { $(this).css({ backgroundPosition: "0 0" }); });
				
					$("#r"+theRel).attr("checked","checked");
					$(this).css({ backgroundPosition: "0 -32px" });
					
					$("#captcha").attr("rel","true");
														 
				});
																			
			});
				
				$("#contactFormPlate form .inputSubmit").eq(0).click(function(){

					if($("#captcha").attr("rel")=="true" && $("textarea#commentBox").val().length>0) {
					
						return true;
					
					} else {
						
						alertMsg="NOT SO FAST!\n\nThere seems to be a problem with this, here, contact form:\n\n";
						
						if($("#captcha").attr("rel")!="true") {
							$("#captcha").css("background","#ffc600");
							alertMsg+="\t* You must choose an arrow.\n";
						}
						
						if($("textarea#commentBox").val().length==0) {
							$("#textarea").css("background","#fffdfd");
							alertMsg+="\t* You must enter a comment, at the very least.";
						}
						
						alert(alertMsg);
						return false;
						
					}
					
				});
				
			}
	
			$("#contactFormPlate").animate({
	
				marginTop: 140,
				height: 180
	
			},function(){
				
				$("#dMaLcover").animate({ marginLeft: "-32px", marginTop: 160 },1000);
				$("#contactFormPlate form").fadeIn(1500);
				
			}); */
		
		});
																
	});

});

function pleaseCenter(elementID,direction) {
	
	switch(direction) {
		
		case "h":
			eCenter=findCenter(elementID);	
			$(elementID).css({
				position: "absolute",
				left: eCenter['left']
			});			
			break;
		case "v":
			eCenter=findCenter(elementID);	
			$(elementID).css({
				position: "absolute",
				top: eCenter['top']
			});			
			break;
		default:
			eCenter=findCenter(elementID);	
			$(elementID).css({
				position: "absolute",
				left: eCenter['left'],
				top: eCenter['top']
			});			
		break;
	
	}
	
}

function findCenter(elementID) {
	
	posInfo = Array();
	
	posInfo['left']=($(window).width()/2) - ($(elementID).width()/2);
	posInfo['top']=($(window).height()/2) - ($(elementID).height()/2);
	
	return posInfo;
	
}

function centerEverything() {
	
	pleaseCenter("#infoPlate");
	pleaseCenter("#starBurst");	
	pleaseCenter("#swooshie");
	
}

function roundItAll($string) {
	
	var iindex=0;
	
	$("img.rounded").each(function(){
																
		var theImg = $(this);
															
		roundImgStr="<div class='roundedImg";
		
		if($string.indexOf("kickin'")>0) {
			
			roundImgStr+=" absRounded";
			
		}
		
		roundImgStr+="'>";
															
		$(this).wrap(roundImgStr).parent().css("background-image", "url("+theImg.attr("src")+")").css({

			width: theImg.attr("width"),
			height: theImg.attr("height"),
			float: "left",
			margin: "0 16px 16px 0"
			
		}).children("img").remove();
																 
	});

}

function formFail($why) {
	
		alert($why);
	
}

function fullSize(element) {
	
	winWidth=$(window).width();
	winHeight=$(window).height();
	
	element.width(winWidth).height(winHeight);
	
	return true;
	
}
