/**
 * Constants used for fading and slide timing
 */

// for small pics
var fadeOpacity = 0.50;
var slideSpeed = 600;

// for the div with the name on that slides up
var slideUpSpeed = 300;

// for the left to right slide on main image
var slideLRSpeed = 800;

function fadeInSelection(currentSelection) {

	// fade out all except this one
	$('div.scrollable div.items div').each(function() {

		if ($(currentSelection).find('a').attr('href') != $(this).find('a').attr('href')) {

			$(this).filter(':not(div.slide)').fadeTo(slideSpeed, fadeOpacity);
			$("div.slide", this).stop().animate({top:'0px'},{queue:false,duration:slideUpSpeed});
		}

	});
	
}

function fadeOutSelection(currentSelection) {

	// check if not currently selected and fade out if so
	if ($($(currentSelection).find('a').attr('href')).is(':not(:visible)')) {

		$(currentSelection).filter(':not(div.slide)').fadeTo(slideSpeed, fadeOpacity);
		$("div.scrollable div.slide", currentSelection).stop().animate({top:'0px'},{queue:false,duration:slideUpSpeed});
	}
	
}

function slideToProject(currentSelection, widthOfViewer) {

	// find position of required div
	var positionRequired = 0;

	$('div.items > div').each(function() {

		if ($(currentSelection).attr('href') == $(this).find('a').attr('href')) {

			return false;

		}

		positionRequired++;

	});

	$('#slidingDivs div.slideWrapper').stop().animate({left:'-' + (positionRequired * widthOfViewer) + 'px'},{queue:false,duration:slideLRSpeed});

	return false;
	
}

$(document).ready(function() {

	// if swf page insert swf into projects
	if ($('#project17').length) {
		$('<div class="project"><div class="photo" id="giffordVideo"><iframe src ="/fileadmin/assets/gifford/movie/hungerford/" width="519" height="303" scrolling="no" frameborder="no"></iframe></div></div>').insertAfter('div.slideWrapper div.project:nth(0)');
	}

	// change sliding divs style (so displays properly if js not enabled)
	$('#slidingDivs div.slideWrapper').css('width', '2000em');

	// code for page listing multiple projects
	var menuImages = '';
	var imageCount = 0;
	$('.projectListWrapper #slidingDivs div.slideWrapper').children().each(function () {
		menuImages += '<div><a href="' + $(this).find('div.info a').attr('href') + '"><img src="' + $(this).find('div.photo > img').attr('src') + '" width="138" height="81" alt="" /></a>';
		menuImages += '<a href="' + $(this).find('div.info a').attr('href') + '"><div class="slide"><p>' + $(this).find('div.infoWrapper > h2').text() + '</p></div></a></div>';
		imageCount++;
	});

	for (var i = imageCount; i < 6; i++) {
		menuImages += '<div>&nbsp;</div>';
	}

	$('.projectListWrapper').append('<div class="wrap"><div class="scrollable"><div class="items">' + menuImages + '</div></div><div id="grey">&nbsp;</div><span class="next" /><span class="prev" /></div>');

	$(".projectListWrapper div.scrollable").scrollable({

		vertical:false,
		size: 5,
		loop: true,
		clickable: false,
		next: 'span.next',
		prev: 'span.prev'

	});

	// unhide divs
	$('div.tx-llcatalog-pi div.project').css('display', 'block');

	// fade all except first
	$('.projectListWrapper div.items div').filter(':not(:first)').each(function() {

		$(this).filter(':not(div.slide)').fadeTo(1, fadeOpacity);

	});

	// slide up info on first
	$(".projectListWrapper div.slide:first").stop().animate({top:'-4.5em'},{queue:false,duration:0});

	// mouseovers
	$('.projectListWrapper div.items div').hover(function() {
		// ignore mousovers on slide div
		if ($(this).attr('class') != 'slide') {
			// ignore divs without img
			if ($(this).find('img').attr('src') != null) {
				$(this).fadeTo(slideSpeed, 1);
				$("div.slide", this).stop().animate({top:'-4.5em'},{queue:false,duration:slideUpSpeed});
				var currentSelection = this;
				fadeInSelection(currentSelection);
			}
		}
	}, function () {
		fadeOutSelection(this);
	});

	// clicks
	$('.projectListWrapper div.items div a').click(function() {

		return slideToProject(this, 753);

	});

	// code for single page
	menuImages = '';
	imageCount = 0;

	$('.mainHeaderPic #slidingDivs div.slideWrapper').children().each(function () {
		if (imageCount == 1 && $('#project17').length) {
			menuImages += '<div><a href="icon_pdf_buildingstructure.gif"><img src="/fileadmin/assets/gifford/movie/hungerford/hungerford-thumbnail.jpg" width="75" height="52" alt="" /></a></div>';
		} else {
			menuImages += '<div><a href="' + $(this).find('div.photo img').attr('src') + '"><img src="' + $(this).find('div.photo > img').attr('src') + '" width="75" height="52" alt="" /></a></div>';
		}
		imageCount++;

	});


	for (var i = imageCount; i < 6; i++) {
		menuImages += '<div>&nbsp;</div>';
	}

	$('.mainHeaderPic').append('<div class="wrap"><div class="scrollable"><div class="items">' + menuImages + '</div></div><div id="grey">&nbsp;</div><span class="next" /><span class="prev" /></div>');

	$(".mainHeaderPic div.scrollable").scrollable({
		vertical:false,
		size: 6,
		loop: true,
		clickable: false,
		next: 'span.next',
		prev: 'span.prev'

	});

	// fade all except first
	$('.mainHeaderPic div.items div').filter(':not(:first)').each(function() {
		$(this).filter(':not(div.slide)').fadeTo(1, fadeOpacity);
	});

	// mouseovers
	$('.mainHeaderPic div.items div').hover(function() {
		// ignore divs without img
		if ($(this).find('img').attr('src') != null) {
			$(this).fadeTo(slideSpeed, 1);
			var currentSelection = this;
			fadeInSelection(currentSelection);
		}
	}, function () {
		fadeOutSelection(this);
	});

	// clicks
	$('.mainHeaderPic div.items div a').click(function() {
		return slideToProject(this, 521);
	});

});
