pageCount = 0;
sectionCount = 1;
animationDelay = 400;
firstFade = 1;
bodyCount = 0;
sliderWidth = 640;
sliderCount = 0;
galleryCount = 0;
galleryWidth = 630;
var hover;

$(function() {
	/*
	var scrollPosition = [
		self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
		self.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
	];
	var html = jQuery('html');
	html.data('scroll-position', scrollPosition);
	html.data('previous-overflow', html.css('overflow'));
	html.css('overflow', 'hidden');
	window.scrollTo(scrollPosition[0], scrollPosition[1]);
*/

	$('div.home div.slider ul li:eq(0)').css('left', 0);

	$('div.body div.home div.slider ul li').each(function() {
		sliderCount++;
		$(this).css('left', sliderWidth*(sliderCount-1));
		if($(this).position().left==0)
		{
			$(this).addClass('current');
		}
		else
		{
			$(this).removeClass('current');
		}
	});
	
	setTimeout('slideFeatured()', 6000);
	
	$('div.section').each(function() {
		bodyCount++;
		$(this).addClass('section-'+bodyCount);
	});
	
	$('div.container_12').delay(500).fadeIn(500);
	
	setTimeout('lazyLoad()', 1000);
	
	$(window).scroll(function() {
		$('div.body div.section').each(function() {
			if(parseInt($(window).scrollTop())>=parseInt($(this).position().top-($(window).height()/2-$(this).height()/4)))
			{
				if(!$(this).is(':animated') && $(this).css('opacity')==.5)
				{
					$(this).fadeTo(500, 1);
				}
			}
			else
			{
				if(!$(this).is(':animated') && $(this).css('opacity')==1)
				{
					$(this).fadeTo(500, .5);
				}
			}
		});
	});
	
	$('div.body ul.services li h4').click(function() {
		$(this).parent().find('div.content').slideToggle(500);
		$(this).toggleClass('current');
	});
	
	$('div.body div.our-clients div.clients ul li:nth-child(4n+1)').css('margin-left', 0);
	
	$('div.body div.gallery ul li:nth-child(3n+1)').css('margin-left', 0);
	
	var galleryWidth = 630;
	var slideAmount = parseInt(galleryWidth*galleryCount);
	//var slideCalc = (slideWidth*(slideAmount-2))-slideWidth;
	
	$('div.body div.gallery div.prev').click(function() {
		if($('div.body div.gallery ul').css('margin-left')!=0)
		if(parseInt($('div.body div.gallery ul').css('left').replace('px',''))!=0)
		{
			if(!$('div.body div.gallery ul').is(':animated'))
			{
				$('div.body div.gallery ul').animate({
					left: '+=630'
				}, 500);
			}
		}
	});
	
	$('div.body div.gallery div.next').click(function() {
		if(parseInt($('div.body div.gallery ul:last-child').position().left)!=0)
		{
			if(!$('div.body div.gallery ul').is(':animated'))
			{
				$('div.body div.gallery ul').animate({
					left: '-=630'
				}, 500);
			}
		}
	});
	
	var offLeft;
	var offTop;
	
	$('div.body div.gallery ul li img').each(function() {
		$(this).click(function() {
			if($(this).hasClass('small'))
			{
				offLeft = parseInt($(this).parent().position().left);
				offTop = parseInt($(this).parent().position().top);
				if(parseInt($(this).parent().css('margin-left').replace('px', ''))!=0){offLeft=offLeft+parseInt($(this).parent().css('margin-left').replace('px', ''))}
				if(parseInt($(this).parent().css('margin-top').replace('px', ''))!=0){offTop=offTop+parseInt($(this).parent().css('margin-top').replace('px', ''))}
				$(this).removeClass('small');
				$(this).addClass('big');
				$(this).animate({
					left: -offLeft-3,
					top: -offTop,
					width: parseInt($(this).parent().parent().parent().width()+6),
					height: parseInt($(this).parent().parent().parent().height()+6)
				}, 500, function() {
					$(this).attr('src', $(this).parent().find('a').attr('href'));
				});
				$(this).parent().css('z-index', '200');
				
				$('div.body div.gallery div.prev, div.body div.gallery div.next').fadeOut(500);
			}
			else
			{
				$(this).parent().parent().parent().removeClass('active');
				$(this).animate({
					left: 0,
					top: 0,
					width: 200+'px',
					height: 200+'px'
				}, 500, function() {
					$(this).css('position', 'relative');
					$(this).addClass('small');
					$(this).removeClass('big');
					$(this).parent().css('z-index', '100');
				});
				
				$('div.body div.gallery div.prev, div.body div.gallery div.next').fadeIn(500);
			}
		});
	});
	
	$('div.body div.gallery div.gallery-show ul').each(function() {
		galleryCount++;
		$(this).css('left', galleryWidth*(galleryCount-1));
	});
	
	$('div.dropdown').hover(function() {
		hover = true;
	}, function() {
		hover = false;
	});
	
	$('div.dropdown').on('click', function() {
		$this = $(this);
		
		$('div.dropdown').removeClass('active');
		$this.addClass('active');
		$this.find('div.drop').fadeIn(200);
		
		if(hover==true)
			$this.find('div.dropdown').fadeIn(200);
	});
	
	$('body,html').on('click', function() {
		if(hover==false)
		{
			$('div.drop').fadeOut(200);
			$('div.dropdown').removeClass('active');
		}
	});
	
	$('div.dropdown div.drop p').on('click', function() {
		$this = $(this);
		
		$this.parent().parent().find('p').removeClass('active');
		$this.addClass('active');
		
		
		$('div.drop[id=second] > div.padding').empty();
		
		var string = $this.attr('id');
		
		$.ajax({
			url: '/wp-content/themes/excell/ajax.php',
			type: 'POST', // send data as post
			data: 'data='+string,
			success: function(data)
			{
				var info = $.parseJSON(data);
				
				if(info.length==1)
					window.location = window.location.pathname+'?id='+info[0].id;
				else
				{
					$.each(info, function(key, value) {
						$this = $(this);
						var id = value.id;
						var name = value.form_name;
						
						$('div.drop[id=second] > div.padding').append('<div class="item"><p id="'+id+'" form="false">'+name+'</p></div>');
					});
					
					$('div.dropdown:first-child').removeClass('active');
					
					$('div.dropdown:first-child div.drop').fadeOut(200, function() {
						$('div.dropdown.second, h4.second').fadeIn(200);
					});
				}
			}
		});
	});
	
	$('body').on('click', 'p[form=false]', function() {
		$('p[form=true]').attr('form', 'false').removeClass('active');
		$(this).attr('form', 'true').addClass('active');
		$(this).parent().parent().parent().fadeOut(200);
		$('a.continue').attr('href', window.location.pathname+'?id='+$(this).attr('id')).css('display', 'block');
	});
	
	$('div.qty input:text').on('click', function() {
		if($(this).val()=='QTY')
			$(this).val('');
	});
	
	$('div.qty input:text').on('blur', function() {
		if($(this).val()=='')
			$(this).val('QTY');
	});
	
	$('div.qty input:text').on('keyup', function() {
		$(this).val($(this).val().replace(/[^0-9]/, ''));
	});
	
	$('div.qty input:text').on('keydown', function() {
		$(this).val($(this).val().replace(/[^0-9]/, ''));
	});
	
	$('.whats-this').tipsy({
		gravity: 's'
	});
	
	$('div.product div.tick').on('click', function() {
		$this = $(this);
		$this.toggleClass('active').parent().toggleClass('active');
		$this.parent().find('div.qty').toggleClass('active');
		$this.parent().find('input:checkbox').click();
	});
	
	$('div.product div.qty input:text').on('keyup', function() {
		var noVat;
		if($('p.novat').html()!='')
			noVat = parseInt($('p.novat').html());
		else
			noVat = 0;
		
		priceAndQty = parseInt($(this).parent().parent().find('div.price p').attr('price')*$(this).val())
		
		$(this).attr('price', parseInt(priceAndQty));
		
		amount = 0;
		
		$('div.qty input:text').each(function() {
			amount = amount+parseInt($(this).attr('price'));
		});
		
		vat = amount*0.2;
		total = amount+vat;
		
		$('div.vat-calc').html('&pound;'+vat);
		$('div.total-calc').html('&pound;'+total);
	});
	
	$('form.validate').validate();
});

function lazyLoad() {
	$('div.body div.section').each(function() {
		$(this).delay(sectionCount*animationDelay).fadeTo(500, firstFade);
		sectionCount++;
		firstFade = .5;
	});
}

function slideFeatured() {
	if(sliderCount>1)
	{
		$('div.home div.slider ul li').each(function() {
			$(this).animate({
				left: '-=640px',
				height: $(this).find('img').height()
			}, 700, 'easeInOutCubic', function() {
				if($(this).css('left')==-640+'px')
				{
					$(this).css('left', sliderWidth*(sliderCount-1)+'px');
				}
			});
			if($(this).css('left')==640+'px')
			{
				$(this).parent().animate({
					height: $(this).find('img').height()
				}, 700, 'easeInOutCubic');
			}
		});
		setTimeout('slideFeatured()', 6000);
	}
}
