$(document).ready(function(){
	$("body").addClass("Active");

	PrepareLightboxCaptions();
	LightBox();
	Carousel();
	PageSlider();
	
	/* Make flash only show on first load */
	var referrer = document.referrer;
	var correctreferrer = referrer.indexOf('quatreus.com');
	if(correctreferrer == -1){
		$('.IntroPanel').css({'display': 'block'});
	}
});




function PrepareLightboxCaptions(){
	if($('.ScrollerContent').length > 0){
		$('.ScrollerContent ul li').each(function(){
			var title = "";
			if($(this).find('ul').length > 0){
				title = $(this).find('ul li').html();
				$(this).find('ul').remove();
			}
			$(this).find('a').attr('title', title);
		});
	}
}



// --- Carousel Code --- //

function Carousel(){
	if($('.ScrollerContent').length > 0){
		$('.ScrollerContent ul').wrap('<div class="ScrollerMyContent"></div>');
		
		ReadJCList();
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
		if ((version >= 5.5) && (version < 7.0) && (document.body.filters)) 
		{
		}else{
			$('.ScrollerMyContent ul').jcarousel({
				scroll: 1,
				visible: 4,
				auto: 0.01,
				animation: 2000,
				initCallback: mycarousel_initCallback,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
		}	
		
		$(window).resize(function() {
			$('.ScrollerMyContent ul').css({'left': '0px'});
		});
	}
}

var carouselpaused = false;

var mycarousel_itemList = new Array();
ReadJCList = function() {
var JCList;
if ($('.ScrollerContent ul li')!=null){
    JCList=$('.ScrollerContent ul li');
    for (var i = 0; i < JCList.length; i++) {
        mycarousel_itemList[i]=JCList[i].innerHTML;
    }
}
}

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_itemList[idx - 1]);

	if(typeof pngFix == 'function') {
		pngFix();
	}
	/* Reapplying Lightbox */
	$('.ScrollerContent a').lightBox({fixedNavigation:true});
}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
	if(carouselpaused == false){
		carousel.remove(i);
		carousel.startAuto();
	}
}

function mycarousel_initCallback(carousel)
{
    //carousel.clip.hover(function() {
	$('.ScrollerWrap').hover(function(){
		carouselpaused = true;
        carousel.stopAuto();
    }, function() {
		carouselpaused = false;
        carousel.startAuto();
    });			   
}

// --- End Carousel Code --- //



function LightBox(){
	$('.ScrollerContent a').lightBox({fixedNavigation:true});
	var images;
	$('.RightContent a').each(function(){
		if($(this).find('img').length > 0){
			$(this).lightBox({fixedNavigation:true});
		}
	});
}






/* Function called (from actionscript) when flash animation has finished */
function IntroFinish(){
	$('.IntroPanel').css({'display': 'none'});
}




// --- Page Slider Code --- //

var Slides = new Array("home", "liveevents", "eventsupport", "exhibitions", "videoandmultimedia", "experiential", "teamskills");
var CurrentSlide = "home";
var locked = false;

function PageSlider(){
	// Load Page Backgrounds
	$('.PageBackground1').addClass(Slides[0])
	for(var i = 7; i >= 1; i--){
		if(i != 1){
			$('.PageBackground1').after('<div class="PageBackground PageBackground' + i + ' ' + Slides[i-1] + '"></div>');
		}
		$('.MainContent .Enhance2ContentElement' + i).addClass('SlideContent').addClass(Slides[i-1]);
	}
	
	if($('.Home').length > 0){
		// Change slide initially
		var pageanchor = window.location.hash.substring(1);
		if(pageanchor != ""){
			$('.SlideContent.' + CurrentSlide).css({display: 'none'});
			$('.SlideContent.' + pageanchor).css({display: 'block'});
			$('.PageBackground.' + CurrentSlide).css({display: 'none'});
			$('.PageBackground.' + pageanchor).css({display: 'block'});
			CurrentSlide = pageanchor;
		}
	}
	
	// Menu click
	$('.Home a').click(function(){
		var anchor = $(this).attr('hash').substring(1);
		if(anchor != ""){
			PageSlide(anchor);
			return false;
		}
	});
	// Logo click
	$('.Home .Logo a').click(function(){
		PageSlide((Slides[0])); // Slide to first slide
		return false;
	});
}

function PageSlide(anchor){
	if($('.SlideContent.' + anchor).length > 0 && anchor != CurrentSlide && locked == false){
		SlideAnim(anchor);	
	}
}

function SlideAnim(anchor){
	locked = true;
	// Animate Content
	$('.SlideContent.' + anchor).css({opacity: 0.0, top: '200px', display: 'block'});
	$('.SlideContent.' + CurrentSlide).animate({opacity: 0.0, top: '-200px'}, 500,function(){
		$('.SlideContent.' + CurrentSlide).css({display: 'none'});												   
		$('.SlideContent.' + anchor).animate({opacity: 1.0, top: '0'}, 500, function(){
			$('.SlideContent.' + anchor).css({filter: ''}); // IE Fix														 
			// Animate Background
			$('.PageBackground.' + CurrentSlide).css({zIndex: '2'});
			$('.PageBackground.' + anchor).css({display: 'block', zIndex: '1', opacity: 1.0});
			LogoChange(anchor);
			$('.PageBackground.' + CurrentSlide).animate({opacity: 0.0}, 1000, function(){
				// Animation Complete
				CurrentSlide = anchor;
				locked = false;
			});
		});
	});
}

var altlogo = false;
function LogoChange(anchor){
	// Logo Change
	if($('.Logo .' + anchor).length > 0){
		$('.Logo p').hide();
		$('.Logo p.' + anchor).show();
		altlogo = true;
	}else if(altlogo==true){
		$('.Logo p').show();
		$('.Logo p.AltLogo').hide();
		altlogo = false;
	}
}

// --- End Page Slider Code --- //

