$(function($){

$himg = $("#homepageImages"), $cnt = $("#container");
if($himg.length){
	$cnt.hide().delay(800).fadeIn(300,function(){
		$map.gMap({ // re-init map to avoid fadeIn clashes
		markers: [{	latitude:51.527642,
							longitude:-0.079997,
							address:"Echo Studios, London, SW19 1UW",
							html:"<img src='http://www.echostudios.co.uk/images/map-detail.png' style='float:right'>Echo Studios, <br />The Pavilions,<br />East Road,<br />London,<br />SW19 1UW",
							icon: { image: "http://www.echostudios.co.uk/images/map-marker-yellow.png",
                                      iconsize: [20, 34],
                                      iconanchor: [21,40],
                                      infowindowanchor: [22, 2] }
						}],
			//controls: false,
			scrollwheel: false,
			zoom: 15
			
		});
	});
}


	var mapVisible = false;
	$map = $('#map');
	$mH  = $('#mapH');
	
	
	function showMap() {
		//Some cleanup re: google maps default extras
		$ts = $('.terms-of-use-link').parent();
		$ts.hide();
			if(!mapVisible){
				$mH.animate({"height": 500}, 300,"easeOutQuad");
				mapVisible = true;
			} else{
				$mH.animate({"height": -500}, 600,"easeOutQuad");
				mapVisible = false;
			}
	}

	
	$("#mapClose, #mapLink").click(function(e){
		e.preventDefault();
		if(!($("#mapLink").hasClass("active"))){
			$("#mapLink").addClass("active");
		} else{
			$("#mapLink").removeClass("active");
		}
		showMap();
	});

//51.527642,-0.079997&sspn=0.011067,0.027874
	$map.gMap({			
		markers: [{	latitude:51.527642,
							longitude:-0.079997,
							address:"Echo Studios, London, SW19 1UW",
							html:"<img src='http://www.echostudios.co.uk/images/map-detail.png' style='float:right'>Echo Studios, <br />The Pavilions,<br />East Road,<br />London,<br />SW19 1UW",
							icon: { image: "http://www.echostudios.co.uk/images/map-marker-yellow.png",
                                      iconsize: [20, 34],
                                      iconanchor: [21,40],
                                      infowindowanchor: [22, 2] }
						}],
		//controls: false,
		scrollwheel: false,
		zoom: 15
		
	});

			
	var portfolio;  if (!portfolio) portfolio = {}; 
	portfolio = {
	
		checkPages : function()
		{
			var ulCount = $("#portfolio-entries li").length;
			return ulCount;
		},
		getLinks : function(pages)
		{
			var linkArr = [];
			for (i=0;i<=pages;i++){
				if(i%10 == 0){
					linkArr.push(i);
				}
			}
			return linkArr;
		},
		makeLinks : function (linkArray)
		{
			var htmlChunk = '';
			//Only create links if there's more than 1
			if(linkArray.length > 1){
				for (i=0;i<linkArray.length;i++){
					htmlChunk += '<a href="#'+i+'">'+i+'</a>';
				}
			}
			return htmlChunk;
		},
		currentPage : function()
		{
			//Check the current page by the number on the end of the url
			//What? I know.
			curlarr = location.href.split("/"),
			//page = (typeof(curlarr[4]) == "string") ? parseInt(curlarr[4].substr(-1)) : 0;
			page = (typeof(curlarr[4]) == "string") ? parseInt(curlarr[4].substring((curlarr[4].length-1), curlarr[4].length)) : 0;
			
			
			//If there's no number we get an empty string, we're on the first page so set to zero in this instance
			if (isNaN(page)){
				page = 0;
			}
			return page;
		},
		checkDirection : function(urlString)
		{
			var direction = null,
			currentPage = this.currentPage();
			pageLink = parseInt(urlString.substring(1));
			
			//console.log("currentPage " + currentPage +" pageLink " +pageLink);
			
			
			if(currentPage < pageLink){
				direction = "-="+""+((pageLink-currentPage)*768);
			} else if (currentPage > pageLink){				
				direction = "+="+""+((currentPage-pageLink)*768);
			} else if (currentPage == pageLink){
				direction = "+=0";
			}
			//console.log(direction);
			return direction;

		}
	}
	
	function portfolioInit()
	{
		$("#portfolio-entries ul").hide().delay(800).fadeIn("slow");
		//CSS fix for nested uls within the portfolio entries
		$("#portfolio-entries ul").each(function(index){
			//console.log((index+1)*768);
			$(this).css({
				//"margin-left":((index)*768)+"px",
			});
		});
		$("#portfolio-entries ul li.reflect").find("img").reflect({height:72,opacity:1/3});
		

		ls = portfolio.checkPages(), lks = portfolio.getLinks(ls), hls = portfolio.makeLinks(lks);
		$("#topBar .page-controlNav").append(hls);
		//check if we need to animate the page, seeing as we may have arrived at e.g. /#9
		animAmount = (portfolio.currentPage()); 
		//console.log(animAmount);
		if (animAmount > 0){
			
			//$("#topBar .page-controlNav a").removeClass("active");
			$("#topBar .page-controlNav a").eq(animAmount).addClass("active");
						
			aProp = (768*animAmount);
			//console.log(aProp);
			$("#portfolio-entries li").animate({
				left: '-='+aProp
			}, 700, "easeInCubic" );
		} else {
			$("#topBar .page-controlNav a:first").addClass("active");
		}
		
		$("#topBar .page-controlNav a").bind("click",function(){
			if($(this).hasClass("active")){
				return false;
			}
			// on click of paginate link do some whizzies with the pagination
			$("#topBar .page-controlNav a").removeClass("active");
			$(this).addClass("active");
			var href = $(this).attr("href"), direction = portfolio.checkDirection(href);
			//var href = $(this).attr("href");
			//portfolio.checkDirection(href);
			
			//console.log(direction);
			$("#portfolio-entries li").animate({
				left: direction
			}, 700, "easeInCubic" );
			//return false;
			
		});
		
	}
	
	if($("#portfolio-entries").length)
	{
		portfolioInit();
	}

	if( $('#testimonials').length > 0){
		$('#testimonials').codaSlider({
			autoSlide: true,
			autoSlideInterval: 10000,
			dynamicArrows : false,
			dynamicTabs: top,
			slideEaseDuration : 600,
			autoSlideStopWhenClicked: true
		});
	}
	
	if( $('#projectImages ul li').length > 0){
		$('#projectImages ul li img').reflect({height:72,opacity:1/3});
		$('#projectImages ul ').codaSlider({
			autoSlide: true,
			autoSlideInterval: 4000,
			dynamicArrows : false,
			slideEaseDuration : 600,
			autoSlideStopWhenClicked: true
		});		
		
	}
	
});


