$(document).ready(function() {

	/* ----- ON LOAD EVENTS ----- */
	if(location.hash && location.hash != '#!') {

		if(location.hash == '#!/') {
			loadPage('home');
		} else {
			loadPage(location.hash);
		}
		
	} else {
	
		$('#inner-content').animate({
			opacity: 1
		});
		
		$('#wrapper, #footer').fadeIn(800);
		
	}
	
	carouselInit(true);
	
	placeholder();
	
	$('input').each(function() {
		if($(this).attr('val')) {
			$(this).fadeOut();
		}
	});
	
	carouselShuffleInit();
	
	/* ----- EVENTS ----- */
	var width = 53;
	var height = 36; 
	var maxWidth = 120;
	
	$('#work-carousel a').live('mouseover', function() {
		
		$(this).css({
			'z-index': 3
		});
		
		$(this).prev('a').css({
			'z-index': 2
		});
		
		$(this).next('a').css({
			'z-index': 2
		});
		
		$(this).prev('a').prev('a').css({
			'z-index': 1
		});
		
		$(this).next('a').next('a').css({
			'z-index': 1
		});
				
		$(this).stop().animate({
			width: maxWidth
		}, 100);
		
		$(this).next('a').stop().animate({
			width: maxWidth - maxWidth / 6
		}, 100);
		
		$(this).prev('a').stop().animate({
			width: maxWidth - maxWidth / 6
		}, 100);
		
		$(this).next('a').next('a').stop().animate({
			width: maxWidth - maxWidth / 2
		}, 100);
		
		$(this).prev('a').prev('a').stop().animate({
			width: maxWidth - maxWidth / 2
		}, 100);

	});
	
	$('#work-carousel a').live('mouseleave', function() {
		
		$(this).css({
			'z-index': 0
		});
		
		$(this).prev('a').css({
			'z-index': 0
		});
		
		$(this).next('a').css({
			'z-index': 0
		});
		
		$(this).prev('a').prev('a').css({
			'z-index': 0
		});
		
		$(this).next('a').next('a').css({
			'z-index': 0
		});
		
		$(this).stop().animate({
			width: width
		}, 100);
		
		$(this).next('a').stop().animate({
			width: width
		}, 100);
		
		$(this).prev('a').stop().animate({
			width: width
		}, 100);
		
		$(this).next('a').next('a').stop().animate({
			width: width
		}, 100);
		
		$(this).prev('a').prev('a').stop().animate({
			width: width
		}, 100);
		
	});
	
	$('#carousel-forward').live('click', function() {
		
		if($('#work-carousel-inner-wrapper').data('page') /*+ 1*/ < $(this).attr('rel')) {
			
			$('#work-carousel-inner-wrapper').data('page', $('#work-carousel-inner-wrapper').data('page') + 1);
			
			// fade the toggles accordingly
			if($('#work-carousel-inner-wrapper').data('page') == $(this).attr('rel') - 1) {
				$(this).animate({
					opacity: 0.2
				});
			}
			
			$('#carousel-back').animate({
				opacity: 1
			});
			
			$('#work-carousel-inner-wrapper').animate({
				opacity: 0	
			}, function() {
				
				$('#work-carousel-inner-wrapper').empty();
				
				$('#work-carousel-inner-wrapper').load('ajax/refresh-carousel.php', {
					page: $('#work-carousel-inner-wrapper').data('page'),
					direction: 'forward',
					//location: 'work',
					workcategory: $('#work-categories .active').attr('id')
				}, function() {
					carouselInit();
					$('#work-carousel-inner-wrapper').animate({
						opacity: 1
					});
				});
				
			});
			
		}
		
		return false;
		
	});
	
	$('#carousel-back').live('click', function() {
	
		if($('#work-carousel-inner-wrapper').data('page') > 0) {
		
			$('#work-carousel-inner-wrapper').data('page', $('#work-carousel-inner-wrapper').data('page') - 1);
			
			// fade toggles accordingly
			if($('#work-carousel-inner-wrapper').data('page') == 0) {
				$(this).animate({
					opacity: 0.2
				});
			}
			
			$('#carousel-forward').animate({
				opacity: 1
			});
			
			$('#work-carousel-inner-wrapper').animate({
				opacity: 0
			}, function() {
				
				$('#work-carousel-inner-wrapper').empty();
				
				$('#work-carousel-inner-wrapper').load('ajax/refresh-carousel.php', {
					page: $('#work-carousel-inner-wrapper').data('page'),
					direction: 'back',
					//location: 'work',
					workcategory: $('#work-categories .active').attr('id')
				}, function() {
					carouselInit();
					$('#work-carousel-inner-wrapper').animate({
						opacity: 1
					});
				});
				
			});
		
		}
		
		return false;
		
	});
	
	$('#work-carousel a').live('click', function() {
		
		var workCarouselLargePosition = $('#work-carousel-large').position();
		var workCarouselPosition = $('#work-carousel').position();
		var clientName = $(this).find('img').attr('alt');
		var workName = $(this).find('img').attr('alt');
		
		if($(this).attr('rel') == 'clients') {
			var itemId = $(this).attr('id').replace('itemid', '');
			var itemType = 'client';
			_gaq.push(['_trackEvent', 'Clients', 'View', clientName]);
		}
		
		if($(this).attr('rel') == 'work') {
			var itemId = $(this).attr('id').replace('itemid', '');
			var itemType = 'work';
			_gaq.push(['_trackEvent', 'Work', 'View', workName]);
		}
		
		if($('#profile').data('visible') == true) {
			$('#profile-button').trigger('click');
		}

		$('#work-carousel-large-content').fadeOut();
		
		if($('#work-categories').data('fade')) {
			/*$('#work-categories').animate({
				opacity: 0
			});*/
			$('#work-categories').fadeOut();
		}
		
		$('#work-categories').data('fade', true);
		
		$('#work-carousel-large').slideUp(function() {
		
			$('body').css({
				background: '#515151 url(images/loading.gif) no-repeat center center'
			});
			
			$('#work-carousel-large').empty();
			
			$('#work-carousel-large').load('ajax/update-carousel.php?'+itemType+'id='+itemId, function() {
				
				$(this).fadeIn();
				
				$('#work-carousel-large img:first').load(function() {
				
					$('body').css({
						background: '#515151'
					});
					
					/*$('#work-categories').animate({
						opacity: 1
					});*/
					
					$('#work-categories').fadeIn();
					
					$('#work-carousel-large-content').slideDown(function() {
						slideShowInit();
					});
					
				});
			
			});
			
		});
		
		$('#work-carousel-wrapper').animate({
			marginTop: '0px'
		});
	
		return false;
	
	});
	
	$('#profile-button').toggle(function() {
		
		$('#work-carousel-large:visible').addClass('wasVisible').animate({
			opacity: 0
		});
		
		showProfile();
		
		return false;
		
	}, function() {
	
		hideProfile();
		$('.wasVisible').animate({
			opacity: 1
		}).removeClass('wasVisible');
		return false;
		
	});
	
	$('#carousel-work-next').live('click', function() {
		scrollWorkNext();
		return false;
	});
	
	$('#carousel-work-previous').live('click', function() {
		scrollWorkPrevious();
		return false;
	});
	
	$('a.navigation').click(function() {
		
		if($(this).attr('href').indexOf('#!') != -1) {
			loadPage($(this).attr('href'));
		} else {
			loadPage('home');
		}
		
		return false;
		
	});
	
	$('#work-categories a').live('click', function() {
	
		loadWorkCategory($(this).attr('id'));
		return false;
		
	});
	
	$('#subscribe').live('submit', function() {
		
		$.post('ajax/add-mailchimp-subscriber.php', $(this).serialize(), function(data) {
		
			if(data == 0) {
				$('#status').empty().append('<em>Please enter a valid email address</em>');
			}
			
			if(data == 2) {
				$('#status').empty().append('<em>There was a problem with your subscription,<br />please email <a href="mailto:studio@strohacker.net">studio@strohacker.net</a> for help</em>');
			}
			
			if(data == 1) {
			
				$('#status').empty().append('<em>Thank you for subscribing!</em>');
				
				setTimeout(function() {
					loadPage('home');
				}, 900);
				
			}
			
		});
		
		return false;
		
	});
	
	/* ----- FUNCTIONS ----- */
	function carouselInit(first) {
	
		var workCarouselItems = $('#work-carousel a').size();
		
		if(workCarouselItems == 18) {
			$('#carousel-forward').css({
				opacity: 1
			});
		} else {
			$('#carousel-forward').animate({
				opacity: 0.2
			});
		}
		
		if(first == true) {
			$('#work-carousel-wrapper').animate({
				marginTop: '20%'
			}, function() {
				$('#work-categories').fadeIn();
			});
			$('#carousel-back').css({
				opacity: 0.2
			});
		}
		
		$('#work-carousel a').each(function(i) {
		
			$(this).css({
				left: ($(this).width() * i)+'px'
			});
		
		});
		
	}

	function showProfile() {
	
		$('#profile-button').css({
			backgroundImage: 'url(http://www.strohacker.net/images/hide-icon.png)',
			color: '#F78F1E'
		});
		
		$('#main-content').css({
			height: $('#wrapper').height()
		});
		
		$('#inner-content').fadeOut();
		$('#profile').fadeIn();
		$('#profile').data('visible', true);
		
	}
	
	function hideProfile() {
	
		$('#profile-button').css({
			backgroundImage: 'url(http://www.strohacker.net/images/expand-icon.png)',
			color: '#ffffff'
		});
		
		$('#inner-content').fadeIn();
		$('#profile').fadeOut();
		$('#profile').removeData('visible');
		
	}
	
	function slideShowInit() {
		
		// negative 1 because later on we're using a loop that starts on 0
		// so slide 0 is the first and 1 the second
		slides = $('#slideshow-bar div').size() - 1;
		slideWidth = $('#work-carousel-large').width();
		
		$('#slideshow-bar').width((slides + 1) * slideWidth);
		
		$('#carousel-navigation').animate({
			height: $('#work-carousel-large img').height()
		});
		
		$('#carousel-work-previous').animate({
			opacity: 0.4
		});
		
		$('#carousel-work-next').animate({
			opacity: 1
		});
		
		$('#slideshow div').each(function(i) {
			$(this).data('slide', i);
		});
		
		currentSlide = 0;
		
	}
	
	function scrollWorkNext() {
		
		// if the next slide is the last
		if(currentSlide == slides - 1) {
			$('#carousel-work-next').animate({
				opacity: 0.4
			});
		}
		
		$('#carousel-work-previous').animate({
			opacity: 1
		});
		
		// if the next slide isn't the last slide
		if(currentSlide < slides) {
			
			currentSlide = currentSlide + 1;
			marginLeft = -slideWidth * currentSlide;
						
			$('#slideshow-bar').stop().animate({
				marginLeft: marginLeft
			});
			
		}
		
		// if the next slide needs to load
		var src = $('.not-preloaded:first img').attr('rel');
		$('.not-preloaded:first img').attr('src', src);
		$('.not-preloaded:first').removeClass('not-preloaded');
		
	}
	
	function scrollWorkPrevious() {
		
		// if the next slide is the first
		if(currentSlide == 1) {
			$('#carousel-work-previous').animate({
				opacity: 0.4
			});
		}
		
		// if the previous slide isn't the first slide
		if(currentSlide > 0) {
			
			currentSlide = currentSlide - 1;
			
			$('#slideshow-bar').stop().animate({
				marginLeft: parseInt($('#slideshow-bar').css('marginLeft')) + slideWidth
			});
			
			$('#carousel-work-next').animate({
				opacity: 1
			});
			
		}
		
	}
	
	function loadPage(page) {
		
		page = page.replace('#!', '');
		
		_gaq.push(['_trackPageview', '/#!'+page]);
		
		$('a.navigation.active').removeClass('active');
		
		if(page != 'home') {
			$('a.navigation[href*='+page+']').addClass('active');
		} else {
			$('a.navigation:first').addClass('active');
		}
		
		hideProfile();
		
		$('#inner-content').fadeOut(function() {
		
			$(this).remove();
			
			//var workCategory = $('#work-categories a:first').attr('href').replace('#!', '');
			var workCategory = 'advertising';
			
			$.get('ajax/content.php', {
				page: page,
				workcategory: workCategory
			}, function(data) {
			
				var data = jQuery.parseJSON(data);

				document.title = data.pageTitle;
				$('meta[name=description]').attr('content', data.pageMetaDescription);
			
				if(page != 'home') {
					
					location.hash = '#!'+page;
					
					if(page != 'work') {
						$('#main-content').append(data.pageContent);
					} else {
						$('#main-content').append(data.workCarousel);
					}
					
				} else {
					location.hash = '#!/';
					$('#main-content').append(data.workCarousel);
				}
				
				if(page == 'contact') {
					mapInit();
				}

				$('#wrapper, #footer').fadeIn(800, function() {
					carouselInit(true);
				});
				
				placeholder();

				$('#inner-content').animate({
					opacity: 1
				});
				
				carouselShuffleInit();
											
			});
			
		});
		
	}
	
	function loadWorkCategory(category) {
		
		_gaq.push(['_trackEvent', 'Work category', 'View', category]);
	
		//var category = category.replace('#!', '');

		$('#work-categories a').removeClass('active');
		//$('a[href=#!'+category+']').addClass('active');
		$('#'+category).addClass('active');
		
		$('#work-carousel-inner-wrapper').animate({
			opacity: 0
		}, function() {
					
			$('#work-carousel').remove();
			
			$.get('ajax/refresh-carousel-category.php', {
			
				workcategory: category
				
			}, function(data) {
			
				$('#work-carousel-inner-wrapper').append(data);
				
				carouselInit();
				carouselShuffleInit();
				
				$('#work-carousel-inner-wrapper').animate({
					opacity: 1
				}, function() {
					$('#work-carousel a:first').trigger('click');
				});
				
			});
			
		});

	}

	function mapInit() {
	
		var latlng = new google.maps.LatLng(50.834919037596755, -0.7774114608764648);
		
		var myOptions = {
			zoom: 13,
			center: new google.maps.LatLng(50.834919037596755, -0.7774114608764648),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(document.getElementById('map'), myOptions);
		
		var marker = new google.maps.Marker({
			position: latlng,
			map: map, 
			title:'Strohacker Creative'
		});
		
		var infoWindow = new google.maps.InfoWindow({
			content: '<p style="color:#111111;"><strong>Strohacker Creative</strong><br />Hollybrook House<br />4 East Pallant<br />Chichester<br />PO19 7UX<br /><br />T: +44 (0)1243 774599</p>'
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infoWindow.open(map, marker);
		});
		
	}
	
	function placeholder() {
		$('input[type!=submit]:not(.placeholder-off)').each(function() {
			if($(this).val() != '') {
				$(this).addClass('placeholder');
				$(this).data('initialValue', $(this).val());	
			}
		});
		
		$('input[type!=submit]:not(.placeholder-off)').focus(function() {
			if($(this).val() == $(this).data('initialValue')) {
				$(this).removeClass('placeholder');
				$(this).val('');
			}
		});
		
		$('input[type!=submit]:not(.placeholder-off)').blur(function() {
			if($(this).val() == '') {
				$(this).addClass('placeholder');
				$(this).val($(this).data('initialValue'));
			}
		});
	}

	function carouselShuffleInit() {
		$('#work-carousel-inner-wrapper').data('slides', $('#work-carousel a').size());
		$('#work-carousel-inner-wrapper').data('page', 0);
	}

});
