var CAROUSEL =
{
	speed: -3500,
	radius: 600,
	height: 120,
	min_size: 0.4,
	min_opa: -0.1,
	opacity: 0,
	mouse_x: null,
	yposes_carousel: [],
	yposes_detail: [],
	sizes_zoom: [],

	hover_img: false,
	hover_start: 0,

	in_range: false,

	start_time: new Date().getTime(),
	sizes: {},

	yofs: 0,

	firsttick: 1,

	init: function()
	{
		$('#carousel_loading').fadeOut();

		if (IDEVICE) {
			$('body').mousemove(function(event)
			{
				if (CAROUSEL.in_range) {
					var abspos = gallery_absolutepos(document.getElementById('carousel'));
					CAROUSEL.mouse_x = event.pageX-abspos.x;
				}
			});

			var mr = document.getElementById('carousel_mouse_range');

			mr.onmouseover = function() { CAROUSEL.in_range = true; }
			mr.onmouseout = function() { CAROUSEL.in_range = false; }

			var car = document.getElementById('carousel');

			car.onmouseover = function() { CAROUSEL.in_range = true; }

			var imgs = car.getElementsByTagName('img');
			for (var a=0; a<imgs.length; a++) {
				CAROUSEL.sizes[imgs[a].src] = [imgs[a].width,imgs[a].height];
				imgs[a].onmouseover = function() {
					CAROUSEL.hover_img = this.src;
					CAROUSEL.hover_start = new Date().getTime();
				};
				imgs[a].onmouseout = function() {
					CAROUSEL.hover_img = false;
				};
			}

			window.setInterval(CAROUSEL.tick, 20);
		}
		else {
			var car = document.getElementById('carousel');
			var icms_imgs = '';
			var icms_yposes = CAROUSEL.yposes_carousel.join(':');

			var imgs = car.getElementsByTagName('img');
			for (var a=0; a<imgs.length; a++) {
				icms_imgs += (icms_imgs ? ':' : '') + imgs[a].src.substr(imgs[a].src.lastIndexOf('/')+1);
			}

			var car_flash = document.getElementById('carousel_flash');

			car_flash.style.position = 'relative';
			car_flash.style.top = (-200 + CAROUSEL.yofs) + 'px';
			car_flash.innerHTML = '\
				<object style="outline-style: none" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="750" height="450" id="carouselswf" align="middle">\
				<param name="allowScriptAccess" value="sameDomain" />\
				<param name="allowFullScreen" value="false" />\
				<param name="wmode" value="transparent" />\
				<param name="movie" value="/carousel.swf?icms_height='+CAROUSEL.height+'&amp;icms_radius='+CAROUSEL.radius+'&amp;icms_speedyo='+h_urlencode(CAROUSEL.speed)+'&amp;icms_imgs='+h_urlencode(icms_imgs)+'&amp;icms_yposes='+h_urlencode(icms_yposes)+'" /><param name="quality" value="best" /><param name="bgcolor" value="#000000" /><embed style="outline-style: none"  src="/carousel.swf?icms_height='+CAROUSEL.height+'&amp;icms_speedyo='+h_urlencode(CAROUSEL.speed)+'&amp;icms_radius='+CAROUSEL.radius+'&amp;icms_imgs='+h_urlencode(icms_imgs)+'&amp;icms_yposes='+h_urlencode(icms_yposes)+'" quality="best" bgcolor="#000000" width="750" height="450" wmode="transparent" name="carouselswf" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
				</object>\
			';
		}
	},

	tick: function()
	{
		if (typeof GALLERY_STOPPED == 'undefined' || (GALLERY_STOPPED)) return;

		var t = new Date().getTime() - CAROUSEL.start_time;

// Speed relative to mouse position
//		if (CAROUSEL.mouse_x !== null) CAROUSEL.start_time += Math.ceil(((CAROUSEL.mouse_x/CAROUSEL.radius)-0.5)*250);

		var imgs = document.getElementById('carousel').getElementsByTagName('img');

		CAROUSEL.opacity += 0.03;
		if (CAROUSEL.opacity > 1) CAROUSEL.opacity = 1;

		for (var a=0; a<imgs.length; a++) {
			var phase = (a / imgs.length) * 3.14159 * 2;

			var sizefac = ((Math.cos(phase + t/CAROUSEL.speed)+1)/2) * (1-CAROUSEL.min_size) + CAROUSEL.min_size;
			var opafac = ((Math.cos(phase + t/CAROUSEL.speed)+1)/2) * (1-CAROUSEL.min_opa) + CAROUSEL.min_opa;

			var opa = opafac * CAROUSEL.opacity;
			if (opa < 0) opa = 0;

			imgs[a].style.zIndex = Math.ceil(sizefac * 100);
			if (!IE8) $(imgs[a]).css('opacity', opa);

			if (imgs[a].src != CAROUSEL.hover_img) {
				sizefac *= 0.85;
			}
			else {
				var bigger = (new Date().getTime()-CAROUSEL.hover_start)/3000;
				if (bigger > 0.15) bigger = 0.15;
				sizefac *= 0.85 + bigger;
			}

			imgs[a].width = Math.ceil(CAROUSEL.sizes[imgs[a].src][0] * sizefac);
			imgs[a].height = Math.ceil(CAROUSEL.sizes[imgs[a].src][1] * sizefac);

			var x = (((Math.sin(phase + t/CAROUSEL.speed)+1)/2)*CAROUSEL.radius) + (CAROUSEL.sizes[imgs[a].src][0]-imgs[a].width) / 2 + (600-CAROUSEL.radius) / 2;

			var yofs_custom = 0;
			if (parseInt(CAROUSEL.yposes_carousel[a])) yofs_custom = parseInt(CAROUSEL.yposes_carousel[a]);

			imgs[a].style.left = Math.ceil(x) + 'px';
			imgs[a].style.top = Math.ceil(CAROUSEL.yofs+3+yofs_custom+(((Math.cos(phase + t/CAROUSEL.speed)+1)/2)*CAROUSEL.height) + (CAROUSEL.sizes[imgs[a].src][0]-imgs[a].height) / 2 - (x/3)) + 'px';

			if (CAROUSEL.firsttick) {
				$(imgs[a]).css('visibility', 'visible');
			}

		}

		CAROUSEL.firsttick = 0;
	}
}

