jQuery(window).ready(function() {

//
//
// !••• Initialize •••

// !• Load Functions •

	loadingStart();
	
	setTimeout(function() {
		
		boxSeqOpen();
		boxTrix();
		bixSeqOpen();
		bitSeqOpen();
		bixTrix();
		loadPageClick();
		loadCatClick();
		loadPostClick();
		loadPostPageClick();
		loadNavClick();
//		loadDeliciousClick();
//		loadTweetClick();
		pagination();
		
	}, 500);

// !• onLoad Hover Failsafe •
	jQuery('img').hover(function() {
    	jQuery(this).animate({ opacity: 1 }, 'slow' );
	});
	jQuery('.bix').hover(function() {
    	jQuery(this).animate({ opacity: 1 }, 'slow' );
	});
	
	jQuery('.thumb').hover(function() {
    	jQuery(this).find('img').animate({ opacity: 1 }, 'slow' );
	});

// !• Loading •
	function loadingDone() {
		jQuery('.page_item').removeClass('loading_page_item');
		jQuery('#page').removeClass('page_loading');
	}
	function loadingStart() {
		jQuery('.current_page_item').addClass('loading_page_item');
		jQuery('#page').addClass('page_loading');
	}
	
// !• Color Change •
//	jQuery('#box-885').click(function() {
//		jQuery('body').animate({ backgroundColor: '#ff7e7e' }, 'slow');
//	});

	jQuery('#box-351').click(function() {
		jQuery('body').animate({ backgroundColor: '#000' }, 'slow');
	});

//
//	
// !••• Sequences •••

//
// !• Cascom Box Sequences •

// Open Boxes
	
	function boxSeqOpen() {
     	
     	var xseqo = jQuery('.box');
     	
    	function xseqingo(i) {
    		//if(!xseqo[i]) {return; }
    		
	     	if(i===6) {
		 		jQuery(xseqo[i]).fadeIn(200);
		 		jQuery(xseqo[i]).find('.excerpt').find('img').animate({ 
		 			width: '150px',
		 			top: '0px',
		 			left: '0px',
		 			opacity: 1 
		 			}, 200);
		 		jQuery(xseqo[i]).find('.meta').animate({ 
		 			opacity: 1 
		 			}, 400);
		 		jQuery(xseqo[i]).find('a').animate({ 
		 			opacity: 1 
		 			}, 400);
		 		jQuery('#current-cat').fadeIn('slow');
		 		loadingDone();
	     	} else {
	     		jQuery(xseqo[i]).fadeIn(200);
		 		jQuery(xseqo[i]).find('.excerpt').find('img').animate({ 
		 			width: '150px',
		 			top: '0px',
		 			left: '0px',
		 			opacity: 1
		 			}, 200);
		 		jQuery(xseqo[i]).find('.meta').animate({ 
		 			opacity: 1
		 			}, 400);
		 		jQuery(xseqo[i]).find('a').animate({ 
		 			opacity: 1 
		 			}, 400);
	     	
	     		setTimeout(function() {
	     			xseqingo(i+1);
	     		}, 50);
	     	}
	   	}
	   	
     	xseqingo(0);
		
	}
	
// Close Boxes	
	function boxSeqClose() {
	     
	    var xseqc = jQuery('.box');

	    function xseqingc(j) {
	    	//if(!xseqc[j]) {return; }
	     	
	     	if(j===0) {
	     		jQuery(xseqc[j]).fadeOut(100);
	     		jQuery(xseqc[j]).find('img').animate({ 
		 			width: '170px',
		 			top: '-10px',
		 			left: '-10px',
		 			opacity: 0.25
		 		}, 100);
		 		jQuery(xseqc[j]).find('.meta').animate({ opacity: 0 }, 50);
		 		jQuery('#contents').empty();
	     	} else {
	     		jQuery(xseqc[j]).fadeOut(100);
		 		jQuery(xseqc[j]).find('img').animate({ 
		 			width: '170px',
		 			top: '-10px',
		 			left: '-10px',
		 			opacity: 0.25
		 			}, 100);
		 		jQuery(xseqc[j]).find('.meta').animate({ opacity: 0 }, 50);
	     		
	     		setTimeout(function() {
	     			xseqingc(j-1);
	     		}, 50);
	     	}
	     }
	
	     xseqingc(6);
	        
	}
	
// Hover Boxes
	function boxTrix() {
		jQuery('.excerpt').hover(function() {
	    	jQuery(this).animate({ opacity: 0 }, 100 );
	    },
		function() {
			jQuery(this).animate({ opacity: 1 }, 200 );
	    });
	    
	    jQuery('.cat-item a').hover(function() {
	    	jQuery(this).animate({ color: '#000000', backgroundColor: '#eeeeee' }, 'fast' );
	    },
		function() {
			jQuery(this).animate({ color: '#c2c2c2', backgroundColor: '#ffffff' }, 'slow' );
	    });
	    
	    jQuery('.widget.box').hover(function() {
	    	jQuery(this).animate({ backgroundColor: '#eeeeee' }, 'fast' );
	    	jQuery(this).find('.widgettitle a').animate({ color: '#000000' }, 'fast' );
	    	jQuery(this).find('.widgettitle').animate({ color: '#000000' }, 'fast' );
	    },
		function() {
			jQuery(this).animate({ backgroundColor: '#ffffff' }, 'slow' );
			jQuery(this).find('.widgettitle a').animate({ color: '#777' }, 'slow' );
			jQuery(this).find('.widgettitle').animate({ color: '#777' }, 'slow' );
	    });
	    
	}

//
// !• Post Bix Sequences •

	function bixSeqInit() {
		jQuery('.bix').find('img').animate({ 
			width: '110px',
			top: '20px',
			left: '20px',
			opacity: 0.01 
		}, 1);
	}

// Open Sequence of Content Bixs 
	function bixSeqOpen() {
     	
     	var bseqo = jQuery('.bix');
     	
    	function bseqingo(i) {
    		if(!bseqo[i]) {return; }
    		
	     	if(i===23) {
		 		jQuery(bseqo[i]).find('img').animate({ 
		 			width: '150px',
		 			top: '0px',
		 			left: '0px',
		 			opacity: 1 
		 		}, 300);
		 		loadingDone();
		 		showMeta();
	     	} else {
	     		jQuery(bseqo[i]).find('img').animate({ 
		 			width: '150px',
		 			top: '0px',
		 			left: '0px',
		 			opacity: 1 
		 		}, 300);
	     	
	     		setTimeout(function() {
	     			bseqingo(i+1);
	     		}, 30);
	     	}
	   	}
     	
     	bseqingo(0);
		bixTrix();
		
	}
	
// Close Sequence of Content Bixs 
	function bixSeqClose() {
	     
	     var bseqc = jQuery('.bix');
	     
	     function bseqingc(j) {
	     	if(!bseqc[j]) {return; }
	     	
	     	if(j===0) {
	     		jQuery(bseqc[j]).animate({ 
		 			width: '0px',
		 			top: '75px',
		 			left: '75px',
		 			opacity: 0.01 
		 		}, 100);
		 		jQuery('.bix').fadeOut('slow');
		 		jQuery('#content').empty();
	     	} else {
	     		jQuery(bseqc[j]).find('img').animate({ 
		 			width: '0px',
		 			top: '75px',
		 			left: '75px',
		 			opacity: 0.01 
		 		}, 100);
	     		
	     		setTimeout(function() {
	     			bseqingc(j-1);
	     		}, 10);
	     	}
	     }
	
	     bseqingc(23);
	     hideMeta();
	     	     
	}

// Bix Hover & Click Functions
	function bixTrix() {
	// Bix See-Through
		jQuery('.bix').hover(function() {
	  	   	jQuery(this).animate({ opacity: 0.01 }, 200);
	  	},
	  	function() {
	  		jQuery(this).animate({ opacity: 1 }, 400);
	 	});
	 	
	 // Info Box Hover
	 	jQuery('.info').hover(function() {
	  	   	jQuery(this).animate({ opacity: 1 }, 200);
	  	},
	  	function() {
	  		jQuery(this).animate({ opacity: 0 }, 400);
	 	});
	 	
	 // Next/Prev Box Hover
	 	jQuery('a .postpaginationnexprev').hover(function() {
	  	   	jQuery(this).animate({ opacity: 1 }, 200);
	  	},
	  	function() {
	  		jQuery(this).animate({ opacity: 0 }, 400);
	 	});
	 	
	 // Web Link Box Hover
	 	jQuery('.web-link').hover(function() {
	  	   	jQuery(this).animate({ opacity: 1 }, 200);
	  	},
	  	function() {
	  		jQuery(this).animate({ opacity: 0 }, 400);
	 	});
	 	
	 // Close Bix Seq
	 	jQuery('.bix').click(function() {
	 		bixSeqClose();
	 		setTimeout(function() {
		 		jQuery('body').animate({ backgroundColor: '#c2c2c2' }, 'slow');
	 		}, 1000);
	 	});
	 
	 // Close Bit Seq
	 	jQuery('.bit').click(function() {
	 		bitSeqClose();
	 	});
	 	
	}
	
//
// !• Cascom Bits •

// Open Sequence of Legacy Bits 
	function bitSeqOpen() {
		jQuery('.bit').fadeIn(200);	
		showMeta();
		bixTrix();
	}	
	
// Close Sequence of Legacy Bits 
	function bitSeqClose() {
		jQuery('.bit').fadeOut(200);
		hideMeta();
		loadingDone();
	}
	
//
// !• Nav Animations •

	function pagination() {
		// Left Hover
		jQuery('#left img').hover(function() {
			 jQuery(this).animate({
			 	marginLeft: '-25px',
			 	marginTop: '-10px',
		     	height: '320px'
		     	}, 'fast' );
		     jQuery('.box').animate({
		     	left: '10px'
		     	}, 'fast' );
			},
			function() {
			 jQuery(this).animate({
			 	marginLeft: '0px',
			 	marginTop: '0px',
		     	height: '300px'
		     	}, 'fast' );
		     jQuery('.box').animate({
		     	left: '0px'
		    	}, 'fast' );
		    });
		
		// Right Hover
		jQuery('#right img').hover(function() {
			 jQuery('.box').animate({
		    	left: '-10px'
		     	}, 'fast' );
			 jQuery(this).animate({
				marginRight: '-25px',
		     	marginTop: '-10px',
		     	height: '320px'
		     	}, 'fast' );
		    },
			function() {
			 jQuery('.box').animate({
		    	left: '0px'
		     	}, 'fast' );
			 jQuery(this).animate({
			 	marginRight: '0px',
		     	marginTop: '0px',
		     	height: '300px'
		     	}, 'fast' );
		    });
		    
		// Next Slide Animation
		jQuery('#right a').click(function() {
		     jQuery('.box').animate({
		     	left: '-500px',
		     	opacity: 0.01
		     	}, 500 );
		     jQuery('#right').animate({
		     	marginRight: '100px',
		     	opacity: 0.01
		     	}, 500 );
		     jQuery('#left').animate({
		     	marginLeft: '-400px',
		     	opacity: 0.01
		     	}, 500 );
		    });
		
		// Previous Slide Animation
		jQuery('#left a').click(function() {
		     jQuery('.box').animate({
		     	left: '500px',
		     	opacity: 0.01
		     	}, 500 );
		     jQuery('#left').animate({
		     	marginLeft: '100px',
		     	opacity: 0.01
		     	}, 500 );
		     jQuery('#right').animate({
		     	marginRight: '-400px',
		     	opacity: 0.01
		     	}, 500 );
		    });
		    
		}
		

//
//	
// !••• AJAX Loaders •••

//	
// !• Checkers •
	function boxChecker() {
			
		var boxLength = jQuery('.box').length;
		var boxTotal = 7;
		
		if(boxLength===boxTotal) {
			boxSeqOpen();
			boxTrix();
			jQuery('#page').append('<section id="content"></section>');

			loadPostClick();
			loadPostPageClick();
			loadCatClick();
			loadNavClick();
//			loadDeliciousClick();
//			loadTweetClick();
			pagination();
		} else {
			setTimeout(function() {
				boxChecker();
			}, 50);
		}
		
	}
	
	function bixChecker() {
		
		var bixLength = jQuery('.bix').length;
		var bixTotal = 24;
		
		if(bixLength===bixTotal) {
			bixSeqInit();
			bixSeqOpen();
			loadPostPageClick();
			bixTrix();
		} else {
			setTimeout(function() {
				bixChecker();
			}, 200);
		}

	}
	
	function extCheck() {
		setTimeout(function() {
			//jQuery('#photo').fadeIn('slow');
		}, 2000);
	}
	
//
// !• Pages •
	function loadPageClick() {
	
		jQuery('.page_item a').click(function(event) {
		
			event.preventDefault();
			
			jQuery('.page_item').removeClass('current_page_item');
			jQuery(this).parent().addClass('current_page_item');
			
			loadingStart();
			boxSeqClose();
			bixSeqClose();
			
			var toLoadP = jQuery(this).attr('href')+' #contents';
			
			setTimeout(function() {
				jQuery('#page').load(toLoadP,'', boxChecker());
			}, 1000);
			
			pageTracker._trackPageview(toLoadP);
		
		});
		
	}

//	
// !• Categories •
	function loadCatClick() {

		jQuery('.cat-item a').click(function(event) {
			
			event.preventDefault();
						
			loadingStart();
			boxSeqClose();
			
			var toLoadC = jQuery(this).attr('href')+' #contents';
	
			setTimeout(function() {
				jQuery('#page').load(toLoadC,'', boxChecker());
			}, 1000);
			
			pageTracker._trackPageview(toLoadC);
			
		});
			
	}

//
// !• Posts •
	function loadPostClick() {
	
		jQuery('.excerpt').click(function(event){
			
			event.preventDefault();
			
			loadingStart();
						
			var toLoad = jQuery(this).attr('href')+' #post';
			
			//var toPreLoad = jQuery(this).attr('href');
			//jQuery.preloadContent(toPreLoad+2+'/#post');
			//alert(toPreLoad+2+'/#post');

			
			jQuery('#content').load(toLoad,'', bixChecker());
			
			setTimeout(function() {
				bitSeqOpen();
			}, 1000);
			
			pageTracker._trackPageview(toLoad);
			
		});
			
	}
	
	
//
// !• Post Page •
	function loadPostPageClick() {
	
		jQuery('#post .postpagination a').click(function(event){
			
			event.preventDefault();
			
			loadingStart();
			bixSeqClose();
			
			var toLoad2 = jQuery(this).attr('href')+' #post';
						
			setTimeout(function() {
				jQuery('#content').load(toLoad2,'', bixChecker());
			}, 2000);
			
			setTimeout(function() {
				bitSeqOpen();
			}, 2000);
			
			pageTracker._trackPageview(toLoad2);
			
		});
			
	}

			
//
// !• Pagination •
	function loadNavClick() {
	
		jQuery('nav a').click(function(event) {

			event.preventDefault();
			
			loadingStart();
			boxSeqClose();
			jQuery('#current-cat').fadeOut('slow');
			
			var toLoadN = jQuery(this).attr('href')+' #contents';
			
			setTimeout(function() {
				jQuery('#page').load(toLoadN,'', boxChecker());
			}, 2000);			
			
			pageTracker._trackPageview(toLoadN);
			
		});
		
	}
	
//
// !• Delicious •
	function loadDeliciousClick() {
	
		jQuery('.delicious-link').click(function(event) {

			event.preventDefault();
			
			loadingStart();
			boxSeqClose();
			
			var toLoadD = jQuery(this).attr('href');
			
			setTimeout(function() {
				jQuery('#page').load(toLoadT,'', extCheck());
			}, 1000);			
			
			pageTracker._trackPageview(toLoadD);
			
		});
		
	}
	
//
// !• Tweet •
	function loadTweetClick() {
	
		jQuery('.twitter-link').click(function(event) {

			event.preventDefault();
			
			loadingStart();
			boxSeqClose();
			
			var toLoadT = jQuery(this).attr('href')+' #photo';
			
			setTimeout(function() {
				alert(toLoadT);
				jQuery('#page').load(toLoadT,'', extCheck());
			}, 1000);			
			
			pageTracker._trackPageview(toLoadT);
			
		});
		
	}

	
//
//	
// !••• Meta •••

//
// !• Show & Hide Meta •
    function showMeta() {
    	jQuery('.post-meta').fadeIn('slow');
    	jQuery('#post footer').fadeIn('slow');
    	jQuery('.extended').slideDown('slow');
	}
	
	function hideMeta() {
		jQuery('.post-meta').fadeOut('slow');
		jQuery('#post footer').fadeOut('slow');
		jQuery('.extended').slideUp('slow');
	}

	
//
//
// !••• Menu •••
	jQuery('#menu').fadeIn('slow');

// Title Hover
//    jQuery('.page_item a').hover(function() {
//		jQuery(this).animate({ opacity: 1 }, 'fast');
//    },
//	function() {
//		jQuery(this).animate({ opacity: 0.25 }, 'slow');
//    });
    	    
// Search… Hover
	jQuery('#search-4').find('input#s').hover(function() {	 
		jQuery(this).animate({ opacity: 1 }, 'fast' );
    },
	function() {
		jQuery(this).animate({ opacity: 0.25 }, 'slow' );
    });

});