$(window).load(function() {
	
	// add a last class to the 3rd item from each row
	$("#latest_work li:nth-child(3n), #portfolio_items li:nth-child(3n), #work li:nth-child(3n)").addClass("last");
	
	
	
	// DESCRIPTION OVERLAY
	var lis = $("#latest_work li, #work li");
	$("#latest_work li, #work li").hover(
	  function () {
		$(this).find('.description-overlay').animate({top:'0px'}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top:'140px'}, 200, 'easeOutExpo');
	  }
	);
	
	// portfolio item animation RECENT SECTION
	var portfolio_items = $("#portfolio_items .tab_content li");
	var portfolio_items_height = $("#portfolio_items .tab_content li").css('height');

	portfolio_items.each(function(el){
		$(this).find('.description-overlay').css({'height':portfolio_items_height,'top':portfolio_items_height});
	})
	
	portfolio_items.hover(
	  function () {
		$(this).find('.description-overlay').animate({top:0}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top: portfolio_items_height}, 200, 'easeOutExpo');
	  }
	);
	

	
	var lis = $("#recent li");
	$("#recent li").hover(
	  function () {
		$(this).find('.description-overlay').animate({top:'0px'}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top:'140px'}, 200, 'easeOutExpo');
	  }
	);
	
	
	
	
	// portfolio item animation
	var portfolio_items = $("#portfolio_items .tab_content li");
	var portfolio_items_height = $("#portfolio_items .tab_content li").css('height');

	portfolio_items.each(function(el){
		$(this).find('.description-overlay').css({'height':portfolio_items_height,'top':portfolio_items_height});
	})
	
	portfolio_items.hover(
	  function () {
		$(this).find('.description-overlay').animate({top:0}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top: portfolio_items_height}, 220, 'easeOutExpo');
	  }
	);
	
	
	
	
	// add a plus to the services & clients lists
	$('#mainbottom li, #sidebar ul.menu li').prepend("<span>+</span>");
	
	// display random testimonial
	var list = $(".testimonials ul li").toArray();
	var elemlength = list.length; 
	var randomnum = Math.floor(Math.random()*elemlength);
	var randomitem = list[randomnum];
	$(randomitem).css("display", "block");

	//initialize the menu
	


	
	// init infield labels
	$("#newsletter-form label, .searchbox label, #commentform label, #contact-form label ").inFieldLabels();
	
	// FAQ Code
	$('#faqs h3').each(function() {
		var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').slideUp();
		tis.prepend("<span>+</span> ")
		tis.click(function() {
		  state = !state;
		  answer.slideToggle(state);
		  
		  tis.toggleClass('active',state);
		  if(tis.hasClass('active')) {
			  tis.find('span').text('>');
		  } else {
			  tis.find('span').text('+');
		  }
		});
	}); // end each faqs
	
	
	
	
	
	//twitter
	$("#twitter").getTwitter({
		// change here the Twitter username
		userName: "CARTEBLANCHEmtl",
		numTweets: 3,
		loaderText: "Loading tweets...",
		slideIn: true,
		showHeading: true,
		headingText: "CARTEBLANCHEMTL",
		showProfileLink: true
	});
})





// FACEBOOK FEED 



$(document).ready(function(){

	// Calling our plugin with a page id and an access token
	// that you will need to generate as explained in the tutorial:
	
	$('#wall').facebookWall({
		id:'CARTEBLANCHEMTL',
		access_token:'235150663170453|SVxYvkrGiVOCHZTg4f_smfZhZfM'
	});

});

// Creating our plugin. You can optionally
// place it in a separate file.

(function($){
	
	$.fn.facebookWall = function(options){
		
		options = options || {};
		
		if(!options.id){
			throw new Error('You need to provide an user/page id!');
		}
		
		if(!options.access_token){
			throw new Error('You need to provide an access token!');
		}
		
		// Default options of the plugin:
		
		options = $.extend({
			limit:2	// You can also pass a custom limit as a parameter.
		},options);

		// Putting together the Facebook Graph API URLs:

		var graphUSER = 'https://graph.facebook.com/'+options.id+'/?fields=name,picture&access_token='+options.access_token+'&callback=?',
			graphPOSTS = 'https://graph.facebook.com/'+options.id+'/feed/?access_token='+options.access_token+'&callback=?&date_format=U&limit='+options.limit;
		
		var wall = this;
		
		$.when($.getJSON(graphUSER),$.getJSON(graphPOSTS)).done(function(user,posts){
			
			// user[0] contains information about the user (name and picture);
			// posts[0].data is an array with wall posts;
			
			var fb = {
				user : user[0],
				posts : []
			};

			$.each(posts[0].data,function(){
				
				// We only show links and statuses from the posts feed:  
				//EDIT: ADDED PHOTOS AND VIDEO (july 29,2011)
				
				if((this.type != 'link' && this.type!='status' && this.type!='video' && this.type!='photo' ) || !this.message){
					return true;
				}
				
			

				// Copying the user avatar to each post, so it is
				// easier to generate the templates:
				this.from.picture = fb.user.picture;
				
				// Converting the created_time (a UNIX timestamp) to
				// a relative time offset (e.g. 5 minutes ago):
				this.created_time = relativeTime(this.created_time*1000);
				
				// Converting URL strings to actual hyperlinks:
				this.message = urlHyperlinks(this.message);

				fb.posts.push(this);
			});

			// Rendering the templates:
			$('#headingTpl').tmpl(fb.user).appendTo(wall);
			
			// Creating an unordered list for the posts:
			var ul = $('<ul>').appendTo(wall);
			
			// Generating the feed template and appending:
			$('#feedTpl').tmpl(fb.posts).appendTo(ul);
		});
		
		return this;

	};

	// Helper functions:

	function urlHyperlinks(str){
		return str.replace(/\b((http|https):\/\/\S+)/g,'<a href="$1" target="_blank">$1</a>');
	}

	function relativeTime(time){
		
		// Adapted from James Herdman's http://bit.ly/e5Jnxe
		
		var period = new Date(time);
		var delta = new Date() - period;

		if (delta <= 10000) {	// Less than 10 seconds ago
			return 'Just now';
		}
		
		var units = null;
		
		var conversions = {
			millisecond: 1,		// ms -> ms
			second: 1000,		// ms -> sec
			minute: 60,			// sec -> min
			hour: 60,			// min -> hour
			day: 24,			// hour -> day
			month: 30,			// day -> month (roughly)
			year: 12			// month -> year
		};
		
		for (var key in conversions) {
			if (delta < conversions[key]) {
				break;
			}
			else {
				units = key;
				delta = delta / conversions[key];
			}
		}
		
		// Pluralize if necessary:
		
		delta = Math.floor(delta);
		if (delta !== 1) { units += 's'; }
		return [delta, units, "ago"].join(' ');
		
	}
	
})(jQuery);





// FOOTER


jQuery(function($) {
		var slide = false;
		var height = $('#footer_content').height();
		$('#footer_button').click(function() {
			var docHeight = $(document).height();
			var windowHeight = $(window).height();
			var scrollPos = docHeight - windowHeight + height;
			$('#footer_content').animate({ height: "toggle"}, 400);
			if(slide == true) {d
				if($.browser.opera) {
					$('html').animate({scrollTop: scrollPos+'px'}, 1000);
				} else {
					$('html, body').animate({scrollTop: scrollPos+'px'}, 1000);
				
				}
				slide = true;
			} else {
				slide = false;
			}
		});
	});


	
	
	
	

// NAVIGATION HOVER

$(document).ready(function() {
	$("ul#nav li a").addClass("js");
	$("ul#nav li a").hover(
      function () {
        $(this).stop(true,true).animate({backgroundPosition:"(0px 0px)"},100);
        $(this).animate({backgroundPosition:"(0px 0px)"}, 100);
      }, 
      function () {
        $(this).animate({backgroundPosition:"(0 -5px)"}, 300);

      }
    );

});


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]]; 
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery); 


// Switch buttons on footer on click

$(document).ready(function() {
	
	
	
	
	$("#toggle a").click(function (e) {
		e.preventDefault();
	$("#toggle a").toggle();
	}); 
		
});



