$(document).ready(function() {
	hideContactLines(); 
	bindContacts(); 
	bindExteriorLinks(); 
	verticalCentreContent(); 
	slideshow(); 
	centreThumbs();
	hideNewsStories() 
	bindArrows(); 
	bindServiceLinks(); 
	verticalCentrethumbs(); 
})

function hideContactLines() {
	$('#contact .txt').hide(); 
}

function bindContacts() {
	$('#contact .img').bind('mouseover', showContacts); 
	$('#contact .img').bind('mouseout', hideContacts); 
}

function showContacts(event) {
	var id = $(event.target).parent()[0].id;
	$('#' + id + ' .txt').show(); 
}

function hideContacts(event) {
	setTimeout(
		function() {
			var id = $(event.target).parent()[0].id;
			$('#' + id + ' .txt').fadeOut(1500); 
		}, 3000
	)
}

function bindExteriorLinks() {
	$('.exterior').bind('click', 
		function() {
			window.open(this.href); 
			return false; 
		}
	); // .parent().append(' (opens in a new window)'); 
}

function newWindow() {
}

// CONTACT -->
function verticalCentreContent() {
	var innerHeight = $('.contact #content #inner').height();
	$('.contact #content #inner p').css('marginBottom', ((310 - innerHeight) / 3) + 20); 
	$('.contact #content #inner p:last-child').css('marginBottom', 0); 
}
// <-- CONTACT

// INDEX -->
function slideshow() {
	// add BG/heads after first image
	var slideArea = $('.index #content ul'); 

	for (var i = 1; i <= 6; i++) {
		slideArea.append(
			'<li id="' + i + '">' + 
				'<img src="graphics/index_BG_' + i + '.jpg" alt="" class="BG"/>' + 
				'<img src="graphics/index_head_' + i + '.png" alt="" class="head transPNG"/>' + 
			'</li>'
		)
	}

	// hide all except first BG/head
	$('.index #content ul li:not(:first)').hide(); 

	slideShowStart(); 
}

function slideShowStart() {
	var currentSlide = 0; 
	var total = $('.index #content ul li').length; 

	setInterval(
		function() {
			if (currentSlide == (total - 1)) {
				var nextSlide = 0; 
			} else if (currentSlide == total) {
				currentSlide = 0; 
				var nextSlide = 1; 
			} else {
				var nextSlide = currentSlide + 1; 
			}

			$('.index #content ul li#' + currentSlide).fadeOut(1000);
			$('.index #content ul li#' + nextSlide).fadeIn(1000); 

			currentSlide++; 
		}, 4000
	)
}
// <-- INDEX

// CLIENT -->
function centreThumbs() {
	$('.clients .thumb img').each(
		function() {
			var height = this.height;
			this.style.paddingTop = ((76 - height) / 2) + 'px'; 
			this.style.paddingBottom = ((76 - height) / 2) + 'px'; 
		}
	); 
}
// <-- CLIENT

// NEWS -->
function hideNewsStories() {
	var i = 0; 

	$('.services .story p:not(h1 + p), .services .story ul').hide();	// services
	$('.news .story p:not(h2 + p), .news .story ul').hide();	// news

	$('.story').each(
		function() {
			$('.story').slice(i,(i + 1)).append('<p><a href="#">>> Read more</a></p>').children('p:last').bind('click', displayNewsStory); 
			i++; 
		}
	)
}

function displayNewsStory() {
	var story = $(this).parent().html(); 
	parseData(story); 
}
// <-- NEWS

// SERVICES -->
function bindServiceLinks() {
	$('.services .thumb a').add('.services #upperContent li a').add('.services .serviceBullet').each(
		function() {
			$(this).attr('href', '#'); 
			$(this).bind('click', displayService); 
		}
	); 

	$('#arrow_left, #arrow_right').parent('a').each(
		function() {
			$(this).attr('href', '#'); 
		}
	); 
}

function displayService() {
	var serviceId = this.id; 

	$.ajax({
		url: 'services.xml', 
		type: 'get', 
		dataType: 'xml', 
		error: function() {
			parseData('<p>There was a problem getting the data for this item!</p>'); 
		}, 
		success: function(response) {
			var serviceHTML = $(response.getElementsByTagName('service')[serviceId]).text(); 
			parseData(serviceHTML); 
		}
	});
}

function verticalCentrethumbs() {
	$('.services .thumb a').each(
		function() {
			var padding = (76 - $(this).height()) / 2; 
			$(this).css('paddingTop', padding).css('paddingBottom', padding); 
		}
	); 
}
// <-- SERVICES

// NEWS/SERVICES -->
function bindArrows() {
	$('#arrow_left, #arrow_right').bind('click', moveThumbs); 
}

function moveThumbs() {
	var arrId = this.id; 
	var pos = parseInt($('.thumb').css('left')); 

	$('.thumb').each(
		function() {
			var left = parseInt($(this).css('left')); 

			if (arrId == 'arrow_right') {
				if (pos > -180) {
					$(this).animate({left: (left - 90)}, 500); 
				}
			} else {
				if (pos < 0) {
					$(this).animate({left: (left + 90)}, 500); 
				}
			}
		}
	); 
}

function parseData(data) {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height(); 
 	var documentHeight = $(document).height(); 

	$('#wrap.news').parent().append('<div id="storyDisplay"></div><div id="storyContent">' + data + '</div>').children('#storyContent p:last').remove().add('#storyContent *').show(); 

	$('#wrap.services').parent().append('<div id="storyDisplay"></div><div id="storyContent"></div>').children('#storyContent').html(data); 

	$('#storyContent').prepend('<p id="close"><a href="#">close</a></p>').children('p:first').bind('click', 
		function() {
			$('#storyContent').fadeOut(250, 
				function() {
					$(' #storyDisplay').fadeOut(250, 
						function() {
							$('#storyContent, #storyDisplay').remove()
						}
					)
				}
			)
		}
	); 

	$('#storyDisplay, #storyContent').hide(); 
	$('#storyDisplay').css('filter', 'alpha(opacity=25)').css('-moz-opacity', .25).css('opacity', .25); 
	$('#storyContent').css('left', (windowWidth - 660) / 2); 
	$('#storyContent').css('filter', 'alpha(opacity=85)').css('-moz-opacity', .85).css('opacity', .85); 

	if (documentHeight > windowHeight) {
		$('#storyDisplay').css('height', documentHeight); 
	}

	$('#storyDisplay').fadeIn(250, 
		function() {
			$('#storyContent').fadeIn(250)
		}
	); 
}
// <-- NEWS/SERVICES
