
//when page is finished loading start functions.
$(document).ready(function() {

    ShowVisuals('a#slide-visual');
    Tooltip("#timeline li");
    NumberThickbox();

    $("#timeline-thickbox a").click(function() {
        ChangeThickboxContent($(this));
    });

    //set current timeline item as active in thickbox
    $("#timeline .thickbox").click(function() {

        /*var a = $(this);
        current_date = new Date();
        var dat = current_date.getYear() + "_" + current_date.getMonth() + "_" + current_date.getDate() + "_" + current_date.getHours() + "_" + current_date.getMinutes() + "_" + current_date.getSeconds() + "_" + current_date.getMilliseconds();

        var $collection = $(this).parent().parent().parent().parent().find(".currentcollection");
        $.get('./timeline.aspx',
        { collection: $collection.val(), date: dat }, function(data) {


            $("#timeline-content").html(data);
            SetThickboxItem(a);
           
        });*/

        SetThickboxItem($(this));


    });

    $('.emergency-text').cycle({ fx: 'scrollLeft', speed: 2000, timeout: 5500 });
    //animate the visuals and center them.
    $('#fp-carousel').cycle({ fx: 'fade', speed: 2000, timeout: 4000 });
    $('#fp-carousel img').center();
    $("#timeline-content").css('display', 'block');
    $(".timeline-container").jCarouselLite({
        btnNext: "#mycarousel-next",
        btnPrev: "#mycarousel-prev",
        circular: false
    });
    $(".timeline-thickbox").jCarouselLite_thickbox({
        btnNext: "#thickbox-next",
        btnPrev: "#thickbox-prev",
        circular: false
    });

    $(".equipment-carrousel").jCarouselLite_equipmentbook({
        btnNext: "#next-equipment",
        btnPrev: "#prev-equipment",
        circular: true
    });

    $("#timeline-content").css('display', '');

    if ($("#dropdown-1").length > 0) {
        $("#dropdown-1, #dropdown-2").msDropDown();
    };

    if ($("#mycarousel").length > 0) {
        $("#mycarousel").jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    }

    if ($("#mycarousel-video").length > 0) {
        $("#mycarousel-video").jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback_video,
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    }

    if ($("#mycarousel-photo").length > 0) {
        $("#mycarousel-photo").jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback_album,
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    }


    $("input.type-equipment-search").click(function() {
        if ($(this).val().indexOf("Type keyword") > -1) {
            $(this).val("");
        }
    });



});


/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });

    $('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = $.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};


function mycarousel_initCallback_video(carousel) {
    $('.video-album-wrap .jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });

    $('.video-album-wrap .backwards-gallery').bind('click', function() {
        carousel.next();
        return false;
    });

    $('.video-album-wrap .forward-gallery').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function mycarousel_initCallback_album(carousel) {
    $('.photo-album-wrap .jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });

    /*    $('li.next-album').bind('click', function() {
    carousel.next();
    return false;
    });

    $('li.next-album').bind('click', function() {
    carousel.prev();
    return false;
    });*/
};


/*
- slides content up and down on the homepage, 
- change the show/hide button
- hide the caption block
*/
function ShowVisuals (elem) {
	
	$(elem).click(function() {
	
		//check if class exists
		if($(this).is('.hide-visual')) {
			ScaleFooter();
			$(".caption").css("display","none"); 
			$('#fp-carousel').cycle('pause'); //pause slideshow

			$('#header-image').animate({
				height: '250px'
			}, 1000, function() {
				$(elem).removeClass('hide-visual')
					.addClass('show-visual')
					.find('img')
					.attr({'src':'img/show-visuals.gif', 'alt':'show visuals'});
			});
		} else {
			 $('#header-image').animate({
				height: '546px'
			 }, 1000, function() {
				$(elem).removeClass('show-visual')
						.addClass('hide-visual')
						.find('img')
						.attr({'src':'img/hide-visuals.gif', 'alt':'hide visuals'});
				$(".caption").css("display","");
				$('#fp-carousel').cycle('resume'); //resume slideshow
			 });
			 $('#footer').animate({
			 	height: '134px'
			  }, 1000);
		} //end else
	  
	}) //end click
	
}
/*
 Show tooltip on the timeline and calculate the location 
 where the tooltip needs to appear based on the leftspace of the current item.
*/
function Tooltip(elem) {
	
	var $nav = $("#nav-wrap"); 
	
	//give unique attribute to each li item
	$('#timeline li').each(function(index) {
		var count = index + 1;
		$(this).attr('jcarouselindex', count);
	});

	//give unique class to each tooltip item
	$('.tooltip').each(function(index) {
		var count = index + 1;
		$(this).addClass("tooltip-item-" + count);
	});
		
	$(elem).mouseenter(function() {
		var timelineIndex = $(this).attr('jcarouselindex'); //get index number from carousel list
		var ulSpace = $("#timeline").css("left");
		var leftSpace = $(this).position();
		
		if(parseInt(ulSpace) == 0 ) {
			var ToolPos = 35;	
		} else {
			var ToolPos = 35 - parseInt(ulSpace);		
		}
		
		$nav.css('z-index','1');
		
		var cssStyle = {'left' : + leftSpace.left - ToolPos, 'display' : 'block'}	
		
		$('.tooltip-item-' + timelineIndex).css(cssStyle);
		

		
	}).mouseleave(function() {
		var timelineIndex = $(this).attr('jcarouselindex'); //get index number from carousel list
	   	$('.tooltip-item-' + timelineIndex).css("display","");
		$nav.css('z-index','101'); //set z-index so that the navigation falles behin the thickbox
	});
	
}

jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var elem = jQuery(this);

		elem.css({
			//position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%',
			marginLeft:	'-' + (elem.outerWidth() / 2) + 'px'
		})

		if (absolute) {
			elem.css({marginLeft:	parseInt(elem.css('marginLeft'), 10) + jQuery(window).scrollLeft()});
		}
	});
};

/*
 Show the image and text of timeline item when user clicks on link
*/
function ChangeThickboxContent(elem) {
											 
	var $parentLi = $(elem).parent('li');									 
	var $dateContent =  $parentLi.find('.date-content');
	var $dateContentImg =  $parentLi.find('.date-content img');
	
	//create img with new attributes,
	$activeImg = $('<img />').attr({
			src: $dateContentImg.attr('src'),
			width: $dateContentImg.attr('width'),
			height: $dateContentImg.attr('height')
		});
	
	//get the text that describes the image.
	$activeText =  $parentLi.find('.date-text').html();
	
	//set active state on active p
	$("#timeline-thickbox p").removeClass('active-event')
	 $parentLi.find('p').addClass('active-event');
	
	//set new img and text in active timeline div
	$("#active-timeline").html($activeImg).append($activeText);
	
}


//give each li item in the tickbox a unique nr 
function NumberThickbox() {
	
	$('#timeline-thickbox li').each(function(index) {
		var count = index + 1;
		$(this).attr('thickboxindex', count);
	});
	
}

/*
set active item in the thickbox li that corresponds 
with the clicked item on the maintimeline.
*/
function SetThickboxItem(elem) {
	
	var activeNumber = $(elem).parent('li').attr('jcarouselindex'); //clicked item in the timeline
	var $thickbox = $("#timeline-thickbox");
	
	//loop through items untill match is found with jcarouselindex
	$("#timeline-thickbox li").each(function(index) {
		if($(this).attr('thickboxindex') == activeNumber) {
			$(this).find('p').addClass('active-event');
		}
	});
	
	//set clicked timeline content as active content in the thickbox
	$currentEvent = $("#timeline-thickbox p.active-event").parent('li').find('a');
	ChangeThickboxContent($currentEvent)

	//remove active-event class from class when popup is closed again
	$("#TB_overlay").click(function() {
		$thickbox.find('p').removeClass('active-event');							
	});
	
}

/*
change the height of the footer is necessary to fill the screen
*/
function ScaleFooter() {
	
	var winHeight = $(window).height();
	var docHeight = $(document).height();
	var headerHeight = $("#header-image").height();
	var contentHeight = $("#content-wrap").height();
	var footerHeight = $("#footer").height();
	var totalHeight = headerHeight + contentHeight + footerHeight + 2; //calcutated height always seems to be 2 pixels short?
	var newTotalHeight = totalHeight - 296; //296 = difference in px between #header-image full size or collapsed.
	
	/*
	see if the total height will fit into the window, 
	if not change the height of the footer to make up for the missing space.
	*/
	if(newTotalHeight < winHeight) {		
		var footerGap = winHeight - newTotalHeight + 2;		
		var newFooterHeight = footerHeight + footerGap;
		$("#footer").css('height',newFooterHeight);
	}
		
}
