window.onload = function() {
	HtinyScrolling.init(); scrollTips.init();
}

function gotoit(target){
	HtinyScrolling.scrollToIt(target);
}

var HtinyScrolling = {
	speed : 1,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 300,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedX:0,
	init: function() {
		var lnks = document.getElementsByTagName('a');   
		for(var i = 0, lnk; lnk = lnks[i]; i++) {   
			if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
			('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
			addEvent(lnk,'click',HtinyScrolling.initScroll,false);
			lnk.onclick=function(){return false;} // Safari
			}   
		}    
	},
	getTarget: function(target) {
		while(target.tagName.toLowerCase() != 'a')
			target = target.parentNode;
		return target;
	},
	getElementXpos: function(el){
		var x = 0;
		while(el.offsetParent){  
			x += el.offsetLeft;    
			el = el.offsetParent;
		}	return x;
	},		
	getScrollLeft: function(){
		if(document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		else return window.pageXOffset;   
	},	
	getWindowWidth: function(){
		if (window.innerWidth)	return window.innerWidth; 
		if(document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	},
	getDocumentWidth: function(){
		if (document.width) return document.width;
		if(document.body.offsetWidth) return document.body.offsetWidth;
	},
	initScroll: function(e){
		var targ;  
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;  
		targ = HtinyScrolling.getTarget(targ);  //a fix by Skid X
		HtinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
		HtinyScrolling.currentBlock = document.getElementById(HtinyScrolling.hash);   
		if(!HtinyScrolling.currentBlock) return;
		HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock); 
		HtinyScrolling.scroll(targ); 
		return false;
	},
	scrollToIt: function(targ){ //thanks to Michael Ionita-Ganea
		target = document.getElementById(targ);
		HtinyScrolling.currentBlock = document.getElementById(targ);
		if(!HtinyScrolling.currentBlock) return;
		HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock);
		HtinyScrolling.scroll(target);
		return false;
		
	}, 
	scroll: function(targ) {
		var left  = HtinyScrolling.getScrollLeft();
		if(HtinyScrolling.requestedX > left) { //a fix by Michael Ionita-Ganea
			var endDistance = Math.round((HtinyScrolling.getDocumentWidth() - (left + HtinyScrolling.getWindowWidth())) / HtinyScrolling.brakeK);
			endDistance = Math.min(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK), endDistance);
			var offset = Math.min(Math.abs(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK)), HtinyScrolling.maxStep);
		}else {
				var offset = - Math.min(Math.abs(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK)), HtinyScrolling.maxStep);
		} 
		window.scrollTo(left + offset, 0);
		if(Math.abs(left-HtinyScrolling.requestedX) <= 1 || HtinyScrolling.getScrollLeft() == left) {
			HtinyScrolling.hash = null;
		} else 	setTimeout(HtinyScrolling.scroll,HtinyScrolling.speed);
	}
}

 
var scrollTips = {
	dx : null,
	init : function() {	
		if (window.addEventListener) {
		window.addEventListener("DOMMouseScroll", this.mouseScroll, false);
		} else document.attachEvent("onmousewheel", this.mouseScroll); 
		var left = document.getElementById('left');
		addEvent(left,'mousedown', function() {this.dx=setInterval('scrollTips.arrowScroll(0)',25);return false;});
		addEvent(left,'mouseup', function() { clearInterval(this.dx); return false;});
		var right = document.getElementById('right');
		addEvent(right,'mousedown', function() {this.dx=setInterval('scrollTips.arrowScroll(1)',25);return false;});
		addEvent(right,'mouseup', function() { clearInterval(this.dx); return false;});
	},
	mouseScroll : function(e) {
		if (!e) var e = window.event;
		  var scroll = e.detail ? e.detail * 20 : e.wheelDelta / -20;
		if (scroll>=0 ){  
		window.scrollBy(80,0);
		} else  window.scrollBy(-80,0) ; 
	},	
	arrowScroll: function(val) {
		if(val==1) {
			window.scrollBy(20,0);
		} else {
			window.scrollBy(-20,0)
		}
	}
}


function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

//Moved init into here.
 jQuery.noConflict();
     
    //  Put all your code in your document ready area
     jQuery(document).ready(function($){
     
       $(function() {
                $(window).scroll(function(){
					var scrollLeft = $(window).scrollLeft();
					if(scrollLeft < (window.length)+1)
						$('#left').stop().animate({'opacity':'0'},200);
					else	
						$('#left').stop().animate({'opacity':'1'},200);
				});
				
				/*above makes the left arrow disappear and below makes it so the right disappear after subtracting the widnow width from wrapper width */
				
				 $(window).scroll(function(){
					var rightarrow = $('#wrapper').width();
					var widowwidth = document.documentElement.clientWidth + 8;
					
				
					
					var scrollLeft = $(window).scrollLeft();
					if(scrollLeft > rightarrow - widowwidth)
						$('#right').stop().animate({'opacity':'0'},200);
					else	
						$('#right').stop().animate({'opacity':'1'},200);
				});
	
			//finds the width of the posts and adds it up and than adds that width to the wrapper 
			
			var width = 0;
				$('.post').each(function() {
				    width += $(this).outerWidth( true );
				});
				$('#wrapper').css('width', width + 189);
			
			var width = 0;
				$('.post').each(function() {
				    width += $(this).outerWidth( true );
				});
				$('.category-case-studies #wrapper').css('width', width + 168);
			
			var width = 0;
				$('.post').each(function() {
				    width += $(this).outerWidth( true );
				});
				$('.single #wrapper').css('width', width + 189);	
				
			
			
			
			//moves the arrows and the header so that when you scroll vertically they move up
			
			var $scrollingScroller = $("#scroller");
	 
			$(window).scroll(function(){			
				$scrollingScroller
					.stop()
					.animate({"marginTop": (-$(window).scrollTop()) + 190 + "px"}, "fast" );			
			});
			
			var $scrollingHeader = $("#header");
	 
			$(window).scroll(function(){			
				$scrollingHeader
					.css("marginTop", (-$(window).scrollTop()) + "px");		
			});	
			
			
		});
			//window shade
						
			$(document).ready(function(){
				
				$(".cover", this).css('top', 0);
				$(".cover", this).stop().delay(10000).animate({top:'-281px'},{duration:400});
				
				
				$('.boxgrid.captionfull').hover(function(){
					$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:400});
				}, function() {
					$(".cover", this).stop().animate({top:'-281px'},{queue:false,duration:400});
				});
			});
			
			$(document).ready(function() {
					$('.fadein').css({ opacity:0, visibility:'visible'}).delay(800).fadeTo(400,1);
			});
			
			

     });
