var homecount = 1;

$(document).ready(function() {

	if ($('.home').length > 0){
		$('<img width="100%" />')
			.attr('src', '/assets/img/backgrounds/home/hero1.jpg')
			.load(function(){
				$('#heroimage').append( $(this) );
				$('#heroimage img').fadeIn('medium');
				setHeroHeight();
		});
		function runIt(){
		 $('#imgholder p').each(function(i, elem) {
		   $('#heroimage').delay(5000).fadeOut(1000, function() {
			 $(this).html($(elem).html());
			 setHeroHeight();
		   }).fadeIn(1000, runIt);
		 });
	   };
	   runIt();
	}
	
	if ($('.services').length > 0){
		var theimg = $(".services").attr("title");
		$('<img width="100%" />')
			.attr('src', theimg)
			.load(function(){
				$('#heroimage').append( $(this) );
				$('#heroimage img').fadeIn('medium');
				setHeroHeight();
		});
		function runIt(){
		 $('#imgholder p').each(function(i, elem) {
		   $('#heroimage').delay(5000).fadeOut(1000, function() {
			 $(this).html($(elem).html());
			 setHeroHeight();
		   }).fadeIn(1000, runIt);
		 });
	   };
	   runIt();
	}

	// Add google map
	if ($('#map_canvas').length > 0){
		var myLatlng = new google.maps.LatLng(53.648555,-1.775923);
		var myOptions = {
		  zoom: 15,
		  center: myLatlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		var contentString = '<div id="content">'+
			'<h4>Atlas Display Ltd</h4>'+
			'<p>Atlas Display UK Ltd<br>17 Old Leeds Rd<br>Huddersfield<br>HD1 1SG</p>'+
			'<p>Tel: 01484 429429<br>Fax: 01484 429237 </p>';
			
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});

		var marker = new google.maps.Marker({
			position: myLatlng, 
			map: map,
			title:"Atlas Display Ltd"
		});
		
		google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(map,marker);
		});
	}
	
	
	// Preload large hero images and run maths functions to work out dynamic height
	if ($('.nothome').length > 0){
		var image = $('.hero').attr('id');
	
		$('<img width="100%" />')
			.attr('src', image)
			.load(function(){
				$('.hero').append( $(this) );
				$('.hero img').fadeIn('medium');
				setHeroHeight();
		});
	}
	
	
	// Run gallery script and add swipe for iphone/ipad and mouse
	if ($('.gallery').length > 0){
		slideShow();
		var down_x = 0;
		var up_x = 0;
		
		$("#slideInner > div").mousedown(function(e){
			e.preventDefault();
			down_x = e.pageX;
		});

		$("#slideInner > div").mouseup(function(e){
			up_x = e.pageX;
			if (up_x <= down_x){
				slideForward();
			}
			else{
				slideBack();
			}
		});
		
		$("#slideInner > div").bind('touchstart', function(e){
			down_x = e.originalEvent.touches[0].pageX;
		});
		
		$("#slideInner > div").bind('touchmove', function(e){
			e.preventDefault();
			up_x = e.originalEvent.touches[0].pageX;
		});
		
		$("#slideInner > div").bind('touchend', function(e){
			if (up_x <= down_x){
				slideForward();
			}
			else{
				slideBack();
			}
		});
	}
	
	// Run function to check screen size and set hero image height on document load
	setHeroHeight();
	
	 
});


// Gallery Slider //

var currentPosition = 0;
var numberOfSlides = 0;
var slideWidth = 706;

function slideShow(){
	var slides = $('.slide');
	numberOfSlides = slides.length;
	
	slides.wrapAll('<div id="slideInner"></div>') // Wrap all .slides with #slideInner div
	
	$('#slideInner').css('width', (slideWidth * numberOfSlides)+(3 * numberOfSlides)); // Set #slideInner width equal to total width of all slides
}

function slideForward(){
	if(currentPosition < numberOfSlides-1){
		currentPosition = currentPosition+1; // Determine new position
		$('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)},700); // Move slideInner using margin-left
	}else{
		currentPosition = 0;
		$('#slideInner').animate({'marginLeft' : 0},700);
	}
}

function slideBack(){
	if(currentPosition > 0){
		//alert(currentPosition);
		currentPosition = currentPosition-1; // Determine new position
		$('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)},700); // Move slideInner using margin-left
	}else{
		currentPosition = numberOfSlides-1;
		$('#slideInner').animate({'marginLeft' : slideWidth-(slideWidth * numberOfSlides)},700);
		//alert(currentPosition);
	}
}

// Check screen size and set hero image height
function setHeroHeight(){
	var screenheight = 0, screenwidth = 0;
  	if( typeof( window.innerWidth ) == 'number' ) {
		screenheight = window.innerHeight;
		screenwidth = window.innerWidth;
	}else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		screenheight = document.documentElement.clientHeight;
		screenwidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		screenheight = document.body.clientHeight;
		screenwidth = document.body.clientWidth;
	}
	
	var screenheight = Math.round(screenwidth/4.55);
	if(screenheight > 434){
		screenheight = 434;	
	}
	if(screenwidth < 1150){
		$('.content').css('background','none');
	}
	if(screenwidth >= 1150){
		$('.content').css('background','url("/assets/img/lower-logo.jpg") no-repeat right top');
	}
	//$('.hero').css('height',screenheight);
	$('.heromap').css('height',screenheight);
	
	if($('.hero img').length > 0){
		var heroheight = $('.hero img').height();
			if(heroheight > 434){
			heroheight = 434;	
		}
		$('.hero').css('height',heroheight);
		//alert(screenheight+' / '+heroheight);
	}
	
	if($('#heroimage img').length > 0){
		var heroheight = $('#heroimage img').height();
		if(heroheight > 434){
			heroheight = 434;	
		}
		$('.hero').css('height',heroheight);
	}
};

// Run function above everytime screen is resized
$(function(){
	$(window).resize(function(){
		setHeroHeight();
	});
});
