/* AJAX'y Forms */
/*
jQuery.ajaxSetup( {
  'beforeSend': function( xhr ){ xhr.setRequestHeader( "Accept", "text/javascript" ) }
} )
*/

/* Parse Twitter text and convert/create hyperlinks */
String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/gi, function(url) {
		return url.link(url);
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/gi, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};

String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/gi, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

var firstY         = 900;
var secondY        = 1820;
var thirdY         = 2275;
var fourthY        = 2746;
var easingEquation = "easeOutCirc";
var easingSpeed    = 1600;

function preloadImages(){
	/*
	var site_images = new Array( "images/slides/slide1.jpg", "images/slides/slide2.jpg" );

	var img = new Image();
	for( var i = 0; i < site_images.length; i++ )
    img.src = site_images[i];
*/
		
	jQuery( '#index-banner' ).fadeIn( 1200 );
	
	// Gets Y Position
	this.checkPosition;
}

function checkPosition(){
	var posy = $( window ).scrollTop();

 	if ( posy <= firstY ){
		$( '#triangle-pattern-1' ).stop().animate( { opacity: 0.95 }, 400, "linear" );
	} else {
		$( '#triangle-pattern-1' ).stop().animate( { opacity: 0 }, 1200, "easeInOutQuad" );
	}

	if ( posy > firstY && posy <= secondY ){
   	$( '#triangle-pattern-2' ).stop().animate( { opacity: 0.95 }, 400, "linear" );
	} else {
		$( '#triangle-pattern-2' ).stop().animate( { opacity: 0 }, 1200, "easeInOutQuad" );
	} 

 	if ( posy > secondY && posy <= fourthY ){
		$( '#triangle-pattern-3' ).stop().animate( { opacity: 0.95 }, 400, "linear" );
	} else {
		$( '#triangle-pattern-3' ).stop().animate( { opacity: 0 }, 1200, "easeInOutQuad" );
	}

 	if ( posy > fourthY && posy <= 5000 ){
		$( '#triangle-pattern-4' ).stop().animate( { opacity: 0.95 }, 400, "linear" );
	} else {
		$( '#triangle-pattern-4' ).stop().animate( { opacity: 0 }, 1200, "easeInOutQuad" );
	}
}

jQuery( document ).ready( function(){

	setTimeout( function(){ jQuery( '#logo' ).fadeIn( 800 ) }, 1000 );
	setTimeout( function(){ jQuery( 'nav' ).fadeIn( 600 ) }, 1500 );

	/* Modal Windows */
	$( ".colorbox" ).colorbox( { width:"80%", height:"80%", iframe:true } );
	
	/* AJAX Form Submit */
	var form = jQuery( "form" );
  if ( form.length ){		
		jQuery( "form[class='ajax'] input[type='submit']" ).click( function( e ){
			var f = jQuery( this ).parents( 'form:first' );
			f.submit( function(){
			  jQuery.post( f.attr( 'action' ), f.serialize() );
				return false;
			} );
		} );
  }

  /* Twittttttttttttttttttttttter */
  if ( jQuery( "#twitter" ).length ){
    jQuery( "#twitter" ).hide();
    result = jQuery.get( "/twitter/recent", {}, 
      function( response ){
        if ( response.length > 1 ){
          jQuery( "#twitter" ).append( response );
          jQuery( ".twitter-message .twitter-status .raw" ).each( function( index, msg ){
            var status = jQuery( msg ).html().parseURL().parseUsername().parseHashtag();
            jQuery( msg ).html( status + "<a class=\"follow-us\" href=\"http://twitter.com/carbonmedia_ca\">Follow Us on Twitter</a>" );
          } );

          jQuery( "#loading" ).hide();
          jQuery( "#twitter" ).animate( { opacity: 1 }, 2000 ).fadeIn();
        }
      }
    );
  };

	/* Fancy Transitions for #index-banner */
	var banner = jQuery( '#index-banner' )
	if ( banner.length ){
		jQuery( "#slideshow" ).jqFancyTransitions( { width: 750, height: 440, effect: 'curtain', delay: 8000, links: true } );		
  }

  /* Email Obfuscation */
  if ( $( "span.addy" ).length ){
    $( "span.addy" ).each( function( index, span ){
      addy = span.innerHTML.replace( " -at- ", "@" );
      addy = addy.replace( " -dot- ", "." );
      addy = addy.replace( "[\(]", "" );
      addy = addy.replace( "[\)]", "" );
      span.innerHTML = '<a href="mailto:' + addy + '">' + addy + '</a>';
    } );
  };
  
	if ( $( "#work" ).length ){
		
		if ( navigator.platform == "iPad" ) return;		
		$( '.project img' ).lazyload( {
 			placeholder : "/images/lazyload.gif",
      effect      : "fadeIn",

		}	);
		
		$( '.photos a' ).each( function( idx ){
			var link  = $( this );
			var ul    = link.parent().parent();
			var image = ul.prev();
			
			link.click( function(){				
				// Even or odd
				var out = ( idx % 2 ) ? 0 : 1;
				var act = ( idx % 2 ) ? 1 : 0;
				
				$( image.children()[out] ).fadeOut( 'slow', function(){
					$( image.children()[act] ).fadeIn();
				} );
											
				ul.find( 'li' ).toggleClass( "selected" );
				return false;
			} );
		} );
	}

  if ( $( "#project-grid" ).length ){		
		$( window ).load( preloadImages );
		
		/* Only run the scroll animations on modern browsers */
  	if ( Modernizr.canvas ){
			$( window ).scroll( checkPosition );
		};
	
		/* Tooltips */
		$( "#project-grid img[title]" ).tooltip( {
			effect: 'slide',
			position: "center right",
			offset: [ 10, -30 ]
		} ).dynamic( { bottom: { direction: 'down', bounce: true } } );

		/* Nav Links */
		$( '.what-you-get a' ).click( function(){
			$( "html, body" ).animate( { scrollTop: ( firstY + 5 ) }, easingSpeed, easingEquation );
			return false;
		} );
	
		$( '.what-we-do a' ).click( function(){
			$( "html, body" ).animate( { scrollTop: ( secondY + 5 ) }, easingSpeed, easingEquation );
			return false;
		} );
	
		$( '.who-we-are a' ).click( function(){
			$( "html, body" ).animate( { scrollTop: ( thirdY + 5 ) }, easingSpeed, easingEquation );
			return false;
		} );
	
		$( '.we-should-talk a' ).click( function(){
			$( "html, body" ).animate( { scrollTop: ( fourthY + 5 ) }, easingSpeed, easingEquation );
			return false;
		} );
		
		/* Flyout Links */
		$( "a.flyout-link, div#link-list a" ).click( function(){
			// Get the chosen flyout
			var selected = jQuery( this ).attr( 'id' ).match( /^[a-z]*/ )[0];
			
			$( "html, body" ).animate( { scrollTop: ( firstY + 105 ) }, easingSpeed, easingEquation );
			$( "div#flyouts" ).animate( { left: -350 }, 800, "easeOutBack", function(){
				$( "div#" + selected ).slideDown( 1200 );
			} );
			
			return false;
		} );
				
		/* Controls */
		$( "div#flyout-controls a.x-button" ).click( function(){
			$( "div.flyout" ).slideUp( 1200, function(){
				$( "div#flyouts" ).animate( { left: -1500 }, 800, "easeInBack" );
			} );
			return false;
		} );
		
		$( "div#flyout-controls div#tabs a" ).click( function(){
			// Get the chosen flyout
			var selected = jQuery( this ).attr( 'id' ).match( /^[a-z]*/ )[0];
			
			// The .css call was necessary as slideUp/slideDown will sometimes cut the content
			// if things are clicked too quickly
			$( "div.flyout" ).stop().slideUp( 800 );
				$( "div#" + selected ).css( "height", "780px" ).animate( { opacity: 1 }, 800 ).slideDown( 1500 );	
			return false;
		} );


	}

} );