// Credits: Robert Penners easing equations (http://www.robertpenner.com/easing/).
$.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

function mycarousel_initCallback(carousel){
	galeria = carousel;
}

function andacarousel(){
	var hash = getHashFromString(location.hash) < 0 ? 0 : getHashFromString(location.hash);
	var inicio = (parseInt($("#mycarousel").css("left")) / 56) * -1;
	var fim = inicio+11;
	if(hash < inicio || hash >= fim){
		galeria.scroll(getHashFromString(location.hash));
	}
}

function exibeIntructions(){
	if(instruction){
		$("#instruction").fadeOut("slow");
	}else{
		if($.cookie("galeria") == null){
			$("#uacsen")
			.append($("<div id=\"instruction\" class=\"png_bg\"></div>")
			.append("<a href=\"javascript:void(0)\"><span>fechar</span></a>")
			.click(function(){
				$.cookie("galeria", "jasei", { expires: 300 });
				$("#instruction").fadeOut("slow");
			}));
			var timer = setInterval(function(){
				$("#instruction").fadeOut("slow");
				clearInterval(timer);
			},
			10000);
		}
	}
	instruction = true;
}
function getHashFromString(hash) {
	if (!hash) return -1;
	hash = hash.replace(/^.*#/, '');
	if (isNaN(hash)) return -1;
	return (+hash);
}

$(document).ready(function() {
	var gallery = $('#mycarousel').jcarousel({
		animation: 800,
		easing: 'BounceEaseOut',
		initCallback:mycarousel_initCallback,
		start: getHashFromString(location.hash),
		scroll:11
	});
	
	
	$(".jcarousel-item").hover(
		function () {
			var objA = $("a", this);
			var obj = $("img", this);
			$("#dv_centraliza")
				.append($("<div></div>").css({
					backgroundColor:"#FFFFFF",
					padding:"5px",
					position:"absolute",
					top:$(this).offset().top + 60,
					left:$(this).offset().left,
					zIndex:99})
					.append($("<img src='" + obj.attr("rel") + "' height='90' />"))
					.append($("<p>" + obj.attr("alt") + "</p>").css({
						fontFamily:"arial",
						fontSize:"0.6em",
						textAlign:"left"})));
		}, 
		function () {
			$("#dv_centraliza").find("div:last").remove();
		});
		
	// Initialize Advanced Galleriffic Gallery
	var galleryAdv = $('#gallery').galleriffic('#thumbs', {
		delay:                  2000,
		numThumbs:              11,
		preloadAhead:           11,
		enableTopPager:         true,
		enableBottomPager:      true,
		imageContainerSel:      '#slideshow',
		controlsContainerSel:   '#controls',
		captionContainerSel:    '',
		loadingContainerSel:    '#loading',
		renderSSControls:       true,
		renderNavControls:      true,
		imgPrev:				imgPrev,
		imgNext:				imgNext,
		cor:					cor,
		playLinkText:           'Slide Show',
		pauseLinkText:          'Parar Slide Show',
		prevLinkText:           '&lsaquo; Previous Photo',
		nextLinkText:           'Next Photo &rsaquo;',
		nextPageLinkText:       'Next &rsaquo;',
		prevPageLinkText:       '&lsaquo; Prev',
		enableHistory:          true,
		autoStart:              false,
		onChange:               function(prevIndex, nextIndex, cor) {
			$('#thumbs ul.thumbs a').children()
				.eq(prevIndex).css({borderColor:"#FFFFFF"}).end()
				.eq(nextIndex).css({borderColor:cor});
			var atual = nextIndex+1;
			$(".status span").html(atual);
			andacarousel();
			exibeIntructions();
		},
		onTransitionOut:        function(callback) {
			$('#caption').fadeOut('fast');
			$('#slideshow').fadeOut('fast', callback);
		},
		onTransitionIn:         function() {
			$('#slideshow, #caption').fadeIn('fast');
		},
		onPageTransitionOut:    function(callback) {},
		onPageTransitionIn:     function() {}
	});
});
