var flagMenu = [false,false,false];

// remap jQuery to $
(function($){

	/*
	 * Slider
	 */
	/*$('#slider').nivoSlider({
        effect:'slideInLeft',
        slices:15,
        animSpeed:500,
        pauseTime:6000,
        directionNav:true,
        directionNavHide:false,
        controlNav:false,
        keyboardNav:true,
        pauseOnHover:true,
        manualAdvance:false
    });*/
	
	
	$('#slider').cycle({ 
	    fx: 'scrollHorz',
	    speed:    1500, 
	    timeout:  8000,
	    pause: true,
	    next: ".nivo-nextNav",
	    prev: ".nivo-prevNav",
	    easing: "easeInOutCubic"
	});
	
	/*
	 * Nav Animations
	 */	 
	
	$('#navHeaderProjectos').mouseenter( function() {
		flagMenu[0] = true;
		$('#navEntriesProjectos').slideDown();		
	});
	$('#navProjectos').mouseleave( function() {
		flagMenu[0] = false;
		setInterval("if(!flagMenu[0]){$('#navEntriesProjectos').slideUp();}",500);	
	});
	
	$('#navHeaderAtelier').mouseenter( function() {
		flagMenu[1] = true;
		$('#navEntriesAtelier').slideDown();		
	});
	$('#navAtelier').mouseleave( function() {
		flagMenu[1] = false;
		setInterval("if(!flagMenu[1]){$('#navEntriesAtelier').slideUp();}",500);
	});
	
	$('#navHeaderImprensa').mouseenter( function() {
		flagMenu[2] = true;	
		$('#navEntriesImprensa').slideDown();		
	});
	$('#navImprensa').mouseleave( function() {
		flagMenu[2] = false;
		setInterval("if(!flagMenu[2]){$('#navEntriesImprensa').slideUp();}",500);
	});
	
	/*
	 * Mouse Move Scrolling @ Projectos
	 */
	{
		var maxWidth = $('#footerEntriesProjectos').width()-250;
		var marginLeft = 0;
		var breath = 10;
		var numProjectos = $('#footerProjectos .footerEntryProjectos').length;
		
		var flagMovement = false;
		
		function getBlockPercentage(obj,e) {
			var objPosition = obj.position();
			var xStart = objPosition.left;
			var xEnd = xStart + obj.width();
			
			var percentage = 0;
			//console.log("X = "+objPosition.left);
			percentage = ((e.pageX-xStart)/obj.width());
			//percentage *= 100;
			//console.log((percentage*100)+"%");
			
			marginLeft = parseInt( $('#footerEntriesProjectos').css( 'margin-left' ) );
			marginLeftMovement = Math.round(maxWidth*percentage);
			return -(marginLeftMovement)+breath;
		}
		
		if(numProjectos > 10) {
			if(breath > 0)
				maxWidth += breath;
			$('#footerProjectos').mouseenter( function(e) {
				flagMovement = true;
				$('#footerEntriesProjectos').animate({ marginLeft: getBlockPercentage($(this),e) }, 250, function(){ flagMovement = false; });
			});
			$('#footerProjectos').mousemove( function(e) {
				if(!flagMovement)
					$('#footerEntriesProjectos').css( 'margin-left', getBlockPercentage($(this),e) );
			});
		}
	}
	
	/*
	 * Close and Open Animation @ Projectos
	 */
	{
		$('#articleWrapper').css( 'right', -$('#articleWrapper').width() );
		$('#buttonCloseHome').click( function() {
			$('#articleWrapper').animate(
				{ top: '+=220' },
				750,
				function()
				{
				}
			);
		});
		$('#buttonClose').click( function() {
			$('#articleWrapper').animate(
				{ right: '+=15' },
				250,
				function()
				{
					$('#arrowLeftWrapper').fadeOut();
					$('#arrowRightWrapper').fadeOut();
				}
			);
			$('#articleWrapper').animate(
				{ right: -$('#articleWrapper').width() },
				1000,
				function()
				{
					$('#articleWrapper').hide();
					$('#arrowLeftWrapper').fadeIn();
					$('#arrowRightWrapper').fadeIn();
				}
			);
		});
		
		$('#buttonInfo').click( function() {
			if( $('#articleWrapper').is(":visible") )
				return;
			
			$('#articleWrapper').show();
			$('#arrowLeftWrapper').fadeOut();
			$('#arrowRightWrapper').fadeOut();
			$('#articleWrapper').animate(
				{ right: 50 },
				750,
				function()
				{
					$('#arrowLeftWrapper').fadeIn();
					$('#arrowRightWrapper').fadeIn();
				}
			);
			$('#articleWrapper').animate(
				{ right: '-=15' },
				250,
				function() {}
			);
		});
	}
	
	$("a.lightbox").fancybox();
	
	$(".isReadble").readble();
	
	$(".blog-list-header").css("cursor","pointer").click(function(){
		var parentID = $(this).parent().attr("id"); 
		$(".blog").each(function(){
			if($(this).attr("id") != parentID) {
				
				$(this).children(".blog-list").slideUp();
			}
		});
		$(this).parent().children(".blog-list").slideToggle();
	});
	
	/*$(".blog-list-header").hover(
		function(){
			$(this).animate({
				textIndent: "5px"
			},100);
		},
		function(){
			$(this).animate({
				textIndent: "0px"
			},100);
		}
	);*/
	
})(this.jQuery);


// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};



// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



