var $j = jQuery.noConflict();

/**
 * Delay function
 *
 * @author James Padolsey
 * @link http://james.padolsey.com/javascript/jquery-delay-plugin/
 */
$j.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

if($j.cookie("austinCSS")) {
	$j('link:first').attr('href',$j.cookie("austinCSS"));
}

$j('html').addClass('hasjs');

$j(document).ready( 
	function() {
	
	$j('a.external.newpage').attr('target','_blank');
	
	$j('#page-nav li a').removeAttr('title');
	
	$j('.home #site-description span').delay(4000,function() {
		$j(this).jTypeWriter().animate({opacity:1}, {duration: 100});
	});
	//$j('#site-description span').delay(8000).jTypeWriter();
	
	$j('dl#changelog dd').hide();
	$j('dl#changelog dd:first-child').show();
	$j('dl#changelog dt strong, dl#changelog dt strong:hover').css('cursor','pointer');
	$j('dl#changelog dt').append('<span style="color:#666;float:right;font-weight:normal;"><abbr title="Click here to show/hide the box below">click to toggle</abbr></span>');
	$j('dl#changelog dt').click(function() {
		$j(this).toggleClass('active');
		$j(this).next().slideToggle(250);
	});
	
	/** 
	 * PRELOADING
	 */
	 $j.preload([ 'austin-passy-welcome' ], {
	    base:'../../images/',
	    ext:'.png',
	});
	$j.preload([ 'background', 'about', 'page_bg', 'contact' ], {
	    base:'../../images/',
	    ext:'.jpg',
		onComplete: doFades(),
	});	
	

	function doFades() {
		var initialFade = 2000;
		var textFade = 1000;
		var increments = 500;
		var buttons = textFade + increments;
		$j('.entry').fadeTo(textFade,0).fadeTo(500,1);
		$j('.home .house').fadeTo(buttons,0).fadeTo(500,1); buttons += increments;
		$j('.home .about').fadeTo(buttons,0).fadeTo(500,1); buttons += increments;
		$j('.home .news').fadeTo(buttons,0).fadeTo(500,1); buttons += increments;
		$j('.home .plugins').fadeTo(buttons,0).fadeTo(500,1); buttons += increments;
		$j('.home .network').fadeTo(buttons,0).fadeTo(500,1); buttons += increments;
		$j('.home .contact').fadeTo(buttons,0).fadeTo(500,1);
		$j('#container').slideDown(textFade).fadeTo(initialFade,1);
		$j('.recent-work').delay(1000).fadeTo(initialFade,1);
		$j('.welcome').delay(2000).fadeTo(initialFade,1);
	}
	
	/**
	 *  PAGES
	 */ 
	var lastClicked = "house"; //$j('.house');
	$j('.house, .about, .news, .network, .contact').click(function() {
		var page = this.id;
		
		//change up the selected links
		$j('#'+lastClicked).removeClass('current');
		lastClicked = page;
		$j(this).addClass('current');
		
		//do the animations between pages
		$j('.content').fadeTo(500,0,function() {
			$j('#container').
			animate({height:1}, {duration: 700, easing: 'easeOutExpo'}).
			css({'background-position' : '-100px 0', 'background-image' : 'url(images/loader.jpg)' }).
			animate({backgroundPosition: $j(document).width()+100 +'px 0'}, {duration: 600, easing: 'easeInOutQuad'}).
			animate({height:1}, {duration: 400}). //wait time between going back down
			animate({height:250}, {duration: 500, easing: 'easeOutExpo'}).
			fadeTo(500,1,function(){ loadcontent(page) });
		});
		
	});

	function loadcontent(page) {
		
		$j('#container').css({'background' : 'url(wp-content/themes/austinpassy/library/css/images/ajax-loader.gif) 50% 50% no-repeat'});
		var url = "";
		switch (page) {
			case "house":
			url = "http://austinpassy.com/";
			break;
			case "about" :
			url = "http://austinpassy.com/about";
			break;
			case "news" :
			url = "http://austinpassy.com/blog";
			break;
			case "portfolio" :
			url = "http://austinpassy.com/portfolio";
			break;
			case "network" :
			url = "http://austinpassy.com/network";
			break;
			case "contact" :
			url = "http://austinpassy.com/contact";
			break;
		}
		
		var site = $j.load("house.html"+$j(this).attr('href'));
		//$j('#container').html('<div class="hfeed content"></div>').fadeTo(500,1);

		$j.ajax({
			type: "GET",
			url: url,
			success: function(html){
				$j('#container').css({'background' : 'transparent'});
				$j('#container').html('<div class="hfeed content"></div>').fadeTo(500,1);
				$j('.content').show('slow');
				$j('.content').html(html);
			}
	    });


	}
	
	/**
	 * Comment author URL hover effect
	 * @author Christopher Wallace
	 */
	$j('.comment-list .comment .comment-author a.url').mouseover(function(e){
		var url = $j(this).attr('href');
		$j(this).parent('cite').append('<span class="hover-url">'+url+'</span>');
		})
			$j('.comment-list .comment .comment-author a.url').mouseout(function(e){
			$j(this).parent('cite').find('.hover-url').fadeOut("slow",0.0).addClass('remove');
			// find .remove and remove it after 250 milliseconds
			$j(this).parent('cite').delay(250,function(e){ 
				$j(this).find('.remove').remove();
			});
	});
	$j('.comment-list .pingback .comment-text, .comment-list .trackback .comment-text').css('padding','10px');
	$j('.comment-list .pingback .comment-text p, .comment-list .trackback .comment-text p').hide();
	
	/**
	 * Austin Passy hover image
	 */
	 /*
	$j('#welcome strong.AustinPassy').mouseover(function(e){
		var img = '<img src="./wp-content/themes/austinpassy/images/austin-passy-welcome.png" alt="" />';
		$j(this).parent('.textwidget').parent('.widget-inside').append('<span id="AustinPassy">'+img+'</span>');
		$j('#AustinPassy').animate({ top:"-80px" }, 100 ).animate({ opacity:"1" }, 600 );
		$j('#AustinPassy').show();
		})
	 	
			$j('#welcome strong.AustinPassy').mouseout(function(e){
			$j(this).parent('.textwidget').parent('.widget-inside').find('#AustinPassy').fadeOut("normal",0.0).addClass('remove');
			// find .remove and remove it after 250 milliseconds
			$j(this).parent('.textwidget').parent('.widget-inside').delay(250,function(e){ 
				$j(this).find('.remove').remove();
			});
	});*/
	
	/**
	 * jQuery style switcher
	 * @ref http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/
	 */
	$j('a#white').click(function() {
		$j('html link:first').attr('href',$j(this).attr('rel'));
		$j.cookie('austinCSS',$j(this).attr('rel'), {expires: 10, path: '/'});
		return false;
	});
			
	$j('a#black').click(function() {
		$j('link:first').attr('href',$j(this).attr('rel'));
		$j.cookie('austinCSS',$j(this).attr('rel'), {expires: 10, path: '/'});
		return false;
	});
	
			
	/**
	 * .net Mag hover attachement
	 */		
	$j('div#attachment_1028').hover (
		function () {
			$j(this).find('a').append('<img id="dotNetHover" src="http://austinpassy.com/wp-content/uploads/2010/02/dotnetmag-uses-wrong-wordpress-logo-hover.png" alt="Hover" style="left:0; position:absolute; top:3px;" />');
		},
		function () {
			$j(this).find('#dotNetHover').remove();
		}
	);
			
	/**
 * Dock scroller
 *
 * @since 0.2.1
 */
window.onload = function() {
	$j( window ).scroll( function( ){ 

		var scroller_object = $j( '#dock' );
	
		if( document.documentElement.scrollTop >= 114 || window.pageYOffset >= 0 )
		{
			if( $j.browser.msie && $j.browser.version == '6.0' )
			{
				scroller_object.css( 'top', ( document.documentElement.scrollTop + 10 ) + 'px' );
			}
			else
			{
				scroller_object.css( { position: 'fixed', top: '10px' } );
			}
		}
		else if( document.documentElement.scrollTop < 114 || window.pageYOffset < 10 )
		{
			scroller_object.css( { position: 'absolute', top: '114px' } );
		}
	
	} );

}
	
	
});