$(document).ready(function(){	

		if (history.pushState) {
		  	var pushState = true;
		}else{
			var pushState = false;
		}

		var pagePos = [
		    {
		        left: '0',
		        url: '',
		        title: 'Copywriters | Website Copywriting | Article Writing Services | Pure Ink Sheffield'
		    },
		    {
		        left: '-888',
		        url: 'what_we_offer',
		        title: 'Website Copywriting Services | What We Offer | Pure Ink'
		    },
		    {
		        left: '-1776',
		        url: 'clients',
		        title: 'Copywriter | Copywriting | Our Clients | Pure Ink'
		    },
		    {
		        left: '-2664',
		        url: 'contact_us',
		        title: 'Copywriter | Copywriting | Contact Us | Pure Ink'
		    }
		];

		$('#slideStrip').empty();

		$.post('../ajaxRetrieve.php',
				function(data){
					$('#slideStrip').html(data);
					$('#slideStrip').css('left', pagePos[thisPage].left + 'px');
				}
			,'html');

		url = document.URL;

		if(url.indexOf('#') !== -1){
			pageName = url.split('#').slice(-1)[0];
			pageName = pageName.replace(/-/g,'');
			pageNo = getPageNo(pagePos, pageName);
			if(pageNo !== false){
				slideToPage(pageNo);
				thisPage = pageNo;
			}
		}

		function slideToPage(pageNo){
			$('#slideStrip').animate({left: pagePos[pageNo].left},{easing:"easeInOutExpo"});
		}

		function getPageNo(pagePos, name){
		    var returnKey = false;
		    $.each(pagePos, function(key, info){
		        if (info.url == name){
		           returnKey = key;
		        };   
		    });
		    return returnKey;
		}

		toggle_arrow_display();
		highlightMenu(thisPage);

		function toggle_arrow_display(){
			switch(thisPage){
				case 0:
					$('.arrow.back').hide();
					$('.arrow.forward').show();
					break;
				case 1:
				case 2:
					$('.arrow.back, .arrow.forward').show();
					break;
				case 3:
					$('.arrow.forward').hide();
					$('.arrow.back').show();
					break;															
			}
		}

		function highlightMenu(page){
			$('#strapline a').css('color','#5e5e5e');
			$('#strapline a[rel="'+page+'"]').css('color','#000000');
		}

		function push_history(){
			if(pushState == true){
				history.pushState(thisPage, '', '/' + pagePos[thisPage].url);
			}
		}

		window.onpopstate = function(event) {  
			if(event.state !== null){			// FOR GOOGLE CHROME
				thisPage = event.state;
				reconcile_display();
			}
		}; 

		if(pushState == true){
			history.replaceState(thisPage, '', '/' + pagePos[thisPage].url);
			document.title = pagePos[thisPage].title;
		}

		function reconcile_display(){
			toggle_arrow_display();
			slideToPage(thisPage);
			highlightMenu(thisPage);
			if(pushState == true){
				history.replaceState(thisPage, '', '/' + pagePos[thisPage].url);
				document.title = pagePos[thisPage].title;
			}else{
				window.location = "#-" + pagePos[thisPage].url;
				document.title = pagePos[thisPage].title;
			}
		}

		$('li.arrow').click(function(){
			dir = $(this).attr('class').split(' ').slice(-1)[0];
			switch(dir){
				case 'forward':
					push_history();
					thisPage++;
					reconcile_display();
					break;
				case 'back':
					push_history();
					thisPage--;
					reconcile_display();
					break;
			}
		});

		$('#strapline a').click(function(){
			push_history();
			thisPage = $(this).attr('rel');
			thisPage = parseInt(thisPage);
			reconcile_display();
			return false;
		});

});
