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

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

//var index = Math.ceil(Math.random()*5);  //nth-child indexes start at 1
//alert($("ul:nth-child(" + index + ")").text());

/**
 * Cufon
 */
Cufon.replace('h1, h2, h3, h4, h5, h6', { 
	fontFamily: 'ChunkFive'
});
Cufon.replace('#hide a', { 
	fontFamily: 'Journal'
});
Cufon.replace('#site-title a', { 
	fontFamily: 'HollywoodHills',
	textShadow: '0 1px 0 #fff'

});

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);
}

function resizeWindow() {

	var p = ( $j(window).height()/$j(window).width() );
	//console.log(p);

	$j('#imageList').width($j(window).width() - 200);

	$j('#flickr').width( $j(window).width() ).height( $j(window).height() );
		
		$j(window).resize(function() {
			$j('#flickr').width( $j(window).width() ).height( $j(window).height() );
		});
	
	$j('#body-container').height($j(window).height());
	
	$j('#primary').height($j('#container').height());
	
}

function dropDownMenu() {
	$j('#page-nav .menu ul, #page-nav ul.menu').addClass('sfHover');
	$j('#page-nav ul.sfHover').superfish({ 
		delay:       100,								// delay on mouseout 
		animation:   {opacity:'show',height:'show'},	// fade-in and slide-down animation 
		speed:       'normal',							// animation speed 
		autoArrows:  true,								// arrow mark-up 
		dropShadows: false								// drop shadows 
	});
}

function dockPosition() {
	var dock = $j('#dock-container');
	var msie6 = $j.browser == 'msie' && version < 7;
  
  	if (dock || !msie6) {
		var top = $j('#dock').offset().top - parseFloat($j('#dock').css('marginTop').replace(/auto/, 0));
		$j(window).scroll(function (event) {
			// what the y position of the scroll is
			var y = $j(this).scrollTop();
			
			// whether that's below the form
			if (y >= top) {
				// if so, ad the fixed class
				$j('#dock').addClass('fixed');
			} else {
				// otherwise remove it
				$j('#dock').removeClass('fixed');
			}
		});
	}
}

/**
 * Scroll the site
 */
function smoothScroll() {
	$j( 'a[href*=#]' ).click( function() {
		if ( location.pathname.replace(/^\//,'' ) == this.pathname.replace( /^\//,'' )
		&& location.hostname == this.hostname) {
			var $target = $j(this.hash);
			$target = $target.length && $target || $j( '[name=' + this.hash.slice(1) +']' );
				if ( $target.length ) {
					var targetOffset = $target.offset().top;
					$j( 'html,body' ).animate( { scrollTop: targetOffset }, 1200 );
				return false;
				}
			}
	});
}

function monitterWidget() {
	window.monitter={};
	$j('.monitter').each(function(e) {
		rrp=20; //The Number of tweets
		fetch_tweets(this);
	});
	
	//Resize the widget
	var width = $j('.monitter').css('width');
	$j('a#expand').click(function() {
		$j(this).parent().parent().parent().parent().css('overflow','visible');
		$j('.monitter').animate({
			width: '300px'
		},400);
		$j('.monitter').delay(300,function() {
			$j(this).css('overflow-y','scroll');
		});
		return false;
	});
	$j('a#contract').click(function() {
		$j('.monitter').animate({
			width: width
		},400);
		$j('.monitter').delay(500,function() {
			$j(this).css('overflow-y','hidden');
		});
		$j(this).delay(500,function() {
			$j(this).parent().parent().parent().parent().css('overflow','hidden');
		});
		return false;
	});
}

function showHideBody() {
	var opacity = $j('.column').css('opacity');
	$j('#hide #fade-out').click(function() {
		$j(this).toggleClass('hide');
		$j('#fade-in').toggleClass('hide');
		$j('.column:not(#hide.column)').animate({
			opacity: '0.1'
		},400);
		return false;
	});
	$j('#hide #fade-in').click(function() {
		$j(this).toggleClass('hide');
		$j('#fade-out').toggleClass('hide');
		$j('.column').animate({
			opacity: opacity
		},400);
		return false;
	});
}

$j(document).ready(function() {

	resizeWindow();
	
	dropDownMenu();
	
	dockPosition();
	
	smoothScroll();
	
	monitterWidget();
	
	showHideBody();
	
	$j(window).bind("resize", resizeWindow);
	
});