﻿$(function() {

	// hide carousel while loading 
	setTimeout("$('#gm-carousel').css('visibility','visible')", 1000);
	setTimeout("$('.gm-carousel-container .loader').css('display','none')", 1000);

	// do calculations for prev/next images 
	containerWidth = $('#gm-carousel').width();
	displayImgWidth = $('#gm-carousel .image').width();
	prevWidth = containerWidth - displayImgWidth - 1;
	nextWidth = prevWidth - (prevWidth * 2);


	function carousel_initCallback(carousel) {
		$('#gm-carousel-control .control-button').bind('click', function() {
			var jcItem = parseInt(jQuery(this).attr('carousel-item'));
			carousel.scroll(jQuery.jcarousel.intval(jcItem));
			return false;
		});

	};
	function carousel_firstInCallback(carousel, el, idx, state) {

		var jcPage = parseInt(idx - 1);
		jQuery('#gm-carousel-control .control-button')
					.removeClass()
					.addClass('control-button')
					.eq(jcPage)
					.toggleClass('control-button-on');


		var url = "/glance?sectionid=" + JS_SECTIONID;
		$('.gm-carousel-container .jcarousel-next-disabled').click(function() {
			window.location.href = url;
		});

	};

	function carousel_nextCallback(carousel, el, enabled) {

	};

	$('#slideshow-list').children('div').wrapAll('<ul id="jcarousel-ul"></ul>').wrap('<li></li>');

	$('#jcarousel-ul').jcarousel({
		scroll: 1,
		start: 1,
		initCallback: carousel_initCallback,
		itemFirstInCallback: carousel_firstInCallback,
		buttonNextCallback: carousel_nextCallback,
		itemLoadCallback: { onBeforeAnimation: loadCallback }
	});

	function loadCallback(carousel, state) {
		
		//animate side panel images
		$('.gm-carousel-right-panel').animate({ opacity: 0 }, 0).animate({ opacity: 0.8 }, 1000);
		$('.gm-carousel-left-panel').animate({ opacity: 0 }, 0).animate({ opacity: 0.8 }, 1000);

		// title bar
		$('#gm-carousel .title').fadeIn(1000);

		// animate prev/next images	
		var current = carousel.get(carousel.first);
		var speed = 0;
		var currentImg = current.find(".image");
		var nextImg = current.next().find(".image");
		var prevImg = current.prev().find(".image");

		if ($.browser.msie) {
			currentImg.css("left", "0");
			nextImg.css("left", nextWidth + "px");
			prevImg.css("left", prevWidth + "px");
		}

		else {
			currentImg.animate({ left: 0 }, speed);
			nextImg.animate({ left: nextWidth + "px" }, speed);
			prevImg.animate({ left: prevWidth + "px" }, speed);
		}

		currentImg.animate({ opacity: 1 }, speed);
		nextImg.animate({ opacity: 0.4 }, speed);
		prevImg.animate({ opacity: 0.4 }, speed);

		currentImg.css("opacity", 1);
		nextImg.css("opacity", 0.4);
		prevImg.css("opacity", 0.4);

	}

	$(window).unbind('resize');

	// hide/display next & prev buttons

	$('.gm-carousel-container .jcarousel-next').stop().animate({ 'opacity': 0 }, 1);
	$('.gm-carousel-container .jcarousel-next').hover(function() {

		$(this).stop().animate({ 'opacity': 1 }, 200);

	}, function() {

		$(this).stop().animate({ 'opacity': 0 }, 200);
	});

	$('.gm-carousel-container .jcarousel-prev').stop().animate({ 'opacity': 0 }, 1);
	$('.gm-carousel-container .jcarousel-prev').hover(function() {

		$(this).stop().animate({ 'opacity': 1 }, 200);
	}, function() {

		$(this).stop().animate({ 'opacity': 0 }, 200);
	});

});
