(function($) {
	
	if( $("body.home").length > 0 && typeof ContentFlow != "undefined" ){
		//homepage
			var myNewFlow = new ContentFlow('coverflow', {				
				reflectionHeight: 0, 
				circularFlow: false, 
				endOpacity: 1, 
				scrollWheelSpeed: 0, 
				scaleFactorLandscape: 2.7, 
				scaleFactor: 1, 
				maxItemHeight: 300,
				visibleItems: 6, 
				scrollInFrom: 0, 
				flowSpeedFactor: 0.3
			});
			$("#coverflow").show();
	}
	
	//Projects
	if($("#carousel").length > 0){
		$.getScript('http://thephuse.com/cms/wp-content/themes/thephuse/lib/js/libs/jquery.jCarouselLite.js', function() {
		
			$("#projectnav a.dot").click(function(e){
				$("#projectnav a.active").removeClass("active");  
				$(this).addClass("active"); 
				e.preventDefault();
			});
	
			//handles startposition
			var start = 0
			if (window.location.hash) { 
				start = parseInt(window.location.hash.substring(2));
				$("#a" + (start)).trigger("click");
			}
	
			var nav = $("#projectnav a.dot").each(function(){
				return "#" + $(this).attr('id');
			});
	    
	
			$("#carousel").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				btnGo: nav,
				scroll: 1,
				start: start,
				afterEnd: function(a) {
					$("#a" + a.get(0).id.substr(1)).trigger("click");
				}
			});
			
			$("#carousel li").height('auto');
		});
	}
	
	if( $('a.lightbox').length > 0 ){
		$.getScript('http://thephuse.com/cms/wp-content/themes/thephuse/lib/js/libs/jquery.fancybox.js', function() {
			$('head').append('<link rel="stylesheet" type="text/css" href="/cms/wp-content/themes/thephuse/lib/css/jquery.fancybox.css" />');
			
			$('article').each( function( i ) {
				$('a.lightbox', this).attr('rel', 'post-'+i);
			});
			
			$('a.lightbox').fancybox({
				'padding': 0
			});
		});
	}
	
	 /*
	 * stickyfloat - jQuery plugin for verticaly floating anything in a constrained area
	 * 
	 * Example: jQuery('#menu').stickyfloat({duration: 400});
	 * parameters:
	 * 		duration 	- the duration of the animation
	 *		startOffset - the amount of scroll offset after it the animations kicks in
	 *		offsetY		- the offset from the top when the object is animated
	 *		lockBottom	- 'true' by default, set to false if you don't want your floating box to stop at parent's bottom
	 * $Version: 05.16.2009 r1
	 * Copyright (c) 2009 Yair Even-Or
	 * vsync.design@gmail.com
	 
	 altered by thephuse
	 
	 */

	$.fn.stickyfloat = function(options, lockBottom) {
		var $obj 				= this;
		var parentPaddingTop 	= parseInt($obj.parent().css('padding-top'));
		var startOffset 		= $obj.parent().offset().top;
		var opts 				= $.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom:true }, options);
		
		if(($obj.height()+startOffset) > $(window).height()) {
			$obj.css({ position: 'absolute' });
		} else {
			$obj.css({ position: 'fixed' });
		}
		
		if(opts.lockBottom){
			var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop; //get the maximum scrollTop value
			if( bottomPos < 0 )
				bottomPos = 0;
		}
		
		$(window).scroll(function () { 
			$obj.stop(); // stop all calculations on scroll event
	
			var pastStartOffset			= $(document).scrollTop() > opts.startOffset;	// check if the window was scrolled down more than the start offset declared.
			var objFartherThanTopPos	= $obj.offset().top > startOffset;	// check if the object is at it's top position (starting point)
			var objSmallerThanWindow 	= ($obj.outerHeight()+startOffset) < $(window).height();	// if the window size is smaller than the Obj size, then do not animate.
			var windowTooThin			= $obj.parent().width() > $(window).width();
	
			// if window scrolled down more than startOffset OR obj position is greater than
			// the top position possible (+ offsetY) AND window size must be bigger than Obj size
			if( (pastStartOffset || objFartherThanTopPos)  && objSmallerThanWindow ){ 
				var newpos = ($(document).scrollTop() -startOffset + opts.offsetY );
				
				if ( newpos > bottomPos ) {
					newpos = bottomPos;
				}
					
				if ( $(document).scrollTop() < opts.startOffset ) { // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
					newpos = parentPaddingTop;
				}
	
				if ( $(document).scrollTop() > bottomPos && !windowTooThin ) {
					$obj.css({ position: 'absolute', top: bottomPos });
				}
				
				if ( $(document).scrollTop() < bottomPos && !windowTooThin ) { 
					$obj.css({ position: 'fixed', top: opts.startOffset });
				}
				
				if (windowTooThin) {
					$obj.css({ position: 'absolute'});
				}
			} else if(!objSmallerThanWindow) {
				$obj.css({ position: 'absolute', top: 0 });
			}
		});
	};
	
	//single post page sidebar
	$('.single-post aside').stickyfloat({ duration: 400 });
	
})(this.jQuery);

window.log = function(){
  log.history = log.history || [];   
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);
