var image_width = 400,image_height = 333;
var max_zindex = 10;
var default_opacity = 1,offset_base = 0.25;
var flip_reference,flip_auto_reference,image_global_href,image_stop_nr = -1;
var caption_text = [];
var fixHeight = 20;
var captionAnimationTime = 750;
var captionMaxHeight = 166;
var firstMove = true;
var moveTime = 750;
var moveCaptionUpCursor = false;

function hightLightCurrent(current){
	$(".it_carousel_controls_block .it_carousel_control").fadeTo(0,1);
	$("#it_carousel_control_" + current).fadeTo(0,0.3);
}


$(function() {
	$('.it_carousel_controls_block .it_carousel_control').each(function(){
		$(this).click(function(){
			clearInterval(flip_auto_reference);
			flip_reference.jcoverflip('current',$(this).attr('goto'));
		});
	});
	$.preload( '.flip-wrap-all img', {
		onFinish: function(){
			flip_reference = $('#flip');
			$(".flip-wrap-preload").remove();
			$(".flip-wrap").show();
			$("#gallery-caption").show().fadeTo(0,0.65).css('top','-'+captionMaxHeight+'px');
			$(".it_carousel_controls_block").show();
			flip_reference.jcoverflip({
				time:moveTime,
				fastAnimation:1,
				beforeCss: function( el, container, offset ){
					var new_width = Math.round(image_width/(offset + 2));
					var new_height = Math.round(image_height/(offset + 2));
					var new_left = Math.round(container.width( )*(0.5 - 2*offset_base*(1-(Math.pow(0.5,(offset+1)))))-new_width/2);
					var new_bottom = Math.round((image_height - new_height)/2);
					var new_opacity = default_opacity - (2 * offset + 2)/10;
					return [
					$.jcoverflip.animationElement( el, { left: new_left + 'px', bottom: new_bottom+'px'}, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { opacity: new_opacity, width: new_width + 'px', height:new_height + 'px' }, {} )
					];
				},
				afterCss: function( el, container, offset ){
					var new_width = Math.round(image_width/(offset + 2));
					var new_height = Math.round(image_height/(offset + 2));
					var new_left = Math.round(container.width( )*(0.5 + 2*offset_base*(1-(Math.pow(0.5,(offset+1)))))-new_width/2);
					var new_bottom = Math.round((image_height - new_height)/2);
					var new_opacity = default_opacity - (2 * offset + 2)/10;
					return [
					$.jcoverflip.animationElement( el, { left: new_left+'px', bottom: new_bottom+'px'}, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { opacity: new_opacity, width: new_width+'px', height:new_height + 'px' }, {} )
					];
				},
				currentCss: function( el, container ){
					var new_left = Math.round((container.width() - image_width)/2);
					return [
					$.jcoverflip.animationElement( el, { left: new_left+'px', bottom: 0}, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { opacity: default_opacity, width: image_width + 'px', height: image_height + 'px' }, { } )
					];
				},
				beforeMove : function(nmtr) {
					clearTimeout(moveCaptionUpCursor);
					var __current = parseInt(flip_reference.jcoverflip('current'));
					hightLightCurrent(__current);
					if(firstMove == true){
						firstMove = false;
						$("li.ui-jcoverflip--item").each(function() {
							var position = parseInt($(this).attr('ordercoverflip'),10);
							var offset = __current > position ? __current - position : position - __current;
							var new_zindex = max_zindex - offset;
							$(this).css({'z-index':new_zindex,cursor: offset == 0 ? 'pointer' : 'default'});
						});
						return {el:false,animation:false,time:false};
					}
					var caption = $('#gallery-caption');
					return {
						el: caption,
						animation: {top:"-"+captionMaxHeight+"px"},
						time:captionAnimationTime,
						callback: function() {
							$("li.ui-jcoverflip--item").each(function() {
								var position = parseInt($(this).attr('ordercoverflip'),10);
								var offset = __current > position ? __current - position : position - __current;
								var new_zindex = max_zindex - offset;
								$(this).css({'z-index':new_zindex,cursor: offset == 0 && $(this).attr('top-location') ? 'pointer' : 'default'});
							});
						}
					};
				},
				afterMove : function(nmtr) {
					$('#gallery-caption .gallery-caption-inner').html(caption_text[flip_reference.jcoverflip('current')]);					
					var caption = $('#gallery-caption');
					if(caption_text[flip_reference.jcoverflip('current')] != "") {
						moveCaptionUpCursor = setTimeout(function(){caption.animate({top:"0px"},captionAnimationTime);},1200);
					}
				}
			}
			);
			flip_auto_reference = setInterval(function() {
				flip_reference.jcoverflip('next');
				if(image_stop_nr == flip_reference.jcoverflip('current')) {
					clearInterval(flip_auto_reference);
				}
			}, 5500);
			$('.ui-jcoverflip--item').click(function() {
				clearInterval(flip_auto_reference);
				if($(this).attr('top-location') && $(this).css('cursor') == "pointer") {
					window.location = $(this).attr('top-location');
				}
			});
		}
	});

});
