// ---------------------------------------- jQuery extentions ----------------------------------------
/*	jQuery: Exists Function
	Checks to see if the a selector exists before processing with a function.
	Why this itself doesn't exist, I have no idea.
*/
jQuery.fn.exists = function(){return jQuery(this).length>0;}

/* jQuery: Outer HTML Function
	Converts DOM element to string with html
*/
jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
}

// ---------------------------------------- Twitter Linkify ----------------------------------------
function Linkify(text) {
    text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
        return '<a target="_blank" href="' + s + '">' + s + '</a>';
    });

    text = text.replace(/(^|)@(\w+)/gi, function (s) {
        return '<a target="_blank" href="http://twitter.com/' + s + '">' + s + '</a>';
    });

    text = text.replace(/(^|)#(\w+)/gi, function (s) {
        return '<a target="_blank" href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
     });
    return text;
}

// ---------------------------------------- Adjust Wrapper ----------------------------------------
function resizePages(timing) {

	var width = $(window).width();
	var height = $(window).height();
	var paddingH = (width-1024)/2;
	var paddingV = (height-768)/2;
	
	if (paddingH < 0){
		paddingH = 0;	
	}
	
	if (paddingV < 0){
		paddingV = 0;
	}
	
	var pagePosition = window.currentPage * 1024;
	var bookWidth = 1024 * $('.pageContainer').length;
	$('.pageGroup').css({'width':bookWidth});
	$('.pageGroup').stop().animate({'margin-left':paddingH-pagePosition},timing,'easeInOutExpo');
	$('header').css({'padding-top':paddingV});
	$('.siteLightbox').css({'width':width,'min-height':height});
}

function scrollAbout(timing) {
	var pagePosition = window.aboutPageScroll * 512;
	var pageHeight = 512 * $('#about_page .contentContainer').length;
	$('#about_page').css({'height':pageHeight});
	$('#about_page').stop().animate({'margin-top':pagePosition},timing,'easeOutExpo');
}

function scrollPress(timing) {
	var pagePosition = window.pressPageScroll * 512;
	var pageHeight = 512 * $('#press_page .contentContainer').length;
	$('#press_page').css({'height':pageHeight});
	$('#press_page').stop().animate({'margin-top':pagePosition},timing,'easeOutExpo');
}

function scrollMusic(timing) {
	var pagePosition = window.musicPageScroll * 512;
	var pageHeight = 512 * $('#music_page .contentContainer').length;
	$('#music_page').css({'height':pageHeight});
	$('#music_page').stop().animate({'margin-top':pagePosition},timing,'easeOutExpo');
}

// ---------------------------------------- JavaScript ----------------------------------------
$(document).ready(function(){
	// Top Secret CODES
	if ( window.addEventListener ) {
		// Konami Komando
		var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
		window.addEventListener("keydown", function(e){
			kkeys.push( e.keyCode );
			if ( kkeys.toString().indexOf( konami ) >= 0 )
				window.location = "http://onenyne.com/";
		}, true);
		
		// Make Me Beats
		
		// Blackout
		var bkeys = [], blackout = "66,76,65,67,75,79,85,84";
		window.addEventListener("keydown", function(e){
			bkeys.push( e.keyCode );
			if ( bkeys.toString().indexOf( blackout ) >= 0 ){
				while(bkeys.length > 0){
					bkeys.pop();						
				}
				// Custom Blackout Function
			}
		}, true);
		
		// I Make Beats
	}
	
	// Resize Handler
	$(window).resize(function () {
		resizePages(0);
	});
	
	// Global Variables
	window.currentPage = 0;
	window.musicPageScroll = -1;
	window.aboutPageScroll = 0;
	window.pressPageScroll = 0;
	window.albumsLoaded = false;
	window.cache = [];
		
	// Run Default	
	resizePages(0);
	scrollMusic(0);
	scrollAbout(0);
	scrollPress(0);
	makeAudioPlayer();
	
});

$(window).load(function() {
	$('.siteContainer').removeClass('loading');
});
