/*******

	***	Anchor Slider by Cedric Dugas   ***
	*** Http://www.position-absolute.com ***
	
	Never have an anchor jumping your content, slide it.

	Don't forget to put an id to your anchor !
	You can use and modify this script for any project you want, but please leave this comment as credit.
	
*****/
		
var oldArrayItem;

$(document).ready(function() {
	$("a.anchorLink").anchorAnimate()
});

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			//var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			//if(elementClick == "about ")var destination = $(elementClick).offset().top - 55;
			//else var destination = $(elementClick).offset().top - 40;
			var destination = $(elementClick).offset().top;
			
			//console.log("destination " + destination );
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = "p2-" + elementClick.replace('#', '');
			});
		  	return false;
		})
	})
}
function gotoAnchor(hash){
	//console.log("hash " +hash);
	//hash = hash.split("?")[0];
	hash0 = hash.split("#p2-")[1];
	//console.log("hash0 " +hash0);
	if(hash0 == undefined){	
		hash0 = hash.split("#")[1];
	}
	
	var destination = $("#"+hash0).offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100);
//	window.location = "#"+hash0;
//	window.location.replace("#p2-" + hash0);
//	updateLocationHash($(window).scrollTop());
}
function updateLocationHash(scrolltop)
{
	//find closest anchor to 0 pos
	var sct = Math.abs(scrolltop);

	var anchors = new Array(
		$("#about-plus-two"),
		$("#our-work"),
		$("#our-clients"),
		$("#our-team"),
		$("#our-blog"),
		$("#contact-us")
	);
	
	var num;
	var closestNum;
	var closestArrayItem;
	var tempNum;
	
	for(var i = 0;i<anchors.length;i++){
		
		num = anchors[i].offset().top;
		tempNum = Math.abs(num - sct);
	
		if(tempNum < closestNum || i == 0){
			closestNum = tempNum;
			closestArrayItem = i;
		}
	
	}
	if(closestArrayItem!=oldArrayItem){
		window.location.replace("#p2-" + anchors[closestArrayItem].attr("id")) 
		if(closestArrayItem == 0)$("#nav").css("background-position",  "54px 17px");
		else if(closestArrayItem == 1)$("#nav").css("background-position",  "117px 17px");
		else if(closestArrayItem == 2)$("#nav").css("background-position",  "179px 17px");
		else if(closestArrayItem == 3)$("#nav").css("background-position",  "252px 17px");
		else if(closestArrayItem == 4)$("#nav").css("background-position",  "307px 17px");
		else if(closestArrayItem == 5)$("#nav").css("background-position",  "362px 17px");		
	}
	oldArrayItem = closestArrayItem;
	
}

