/**
 * @RETARCO: RETARCO
 * @location: Wax Interactive - www.wax.be
 * @date: 03.02.2011
 * @author: Jan De Wilde
 * @description: Core logic
 */

if(typeof $.RETARCO == "undefined") {
	$.RETARCO = Object;
};

$.RETARCO.initialize = {
	construct : function(){
		$('.scroll-pane').jScrollPane({
			showArrows: true
		});
	}
};

$.RETARCO.fsgalery = {
	slideIndex : 0,
	rotateInterval : 7000,
	
	construct : function(){
		if($('#slides img').length > 0){
			var _this = this;
	
			this.resize();
	
			$(window).bind('resize',this.resize);
			$(window).bind('resize',this.scaleImgResize);
			
			this.preloadImg();
		}
	},
	
	preloadImg : function(){
		var _this = this;
		
		var loading = $('<div></div>')
		.attr('id','loader')
		.css({
			position : 'absolute',
			left : 0,
			top : 0,
			width : '100%',
			height : '100%',
			background : '#eee',
			zIndex : 100
		});
		$('body').append(loading);

		var firstImg = $('#slides img:eq(0)').attr('src');
		$('#slides img:eq(0)').remove(); // remove first image
		
		// create array with other images
		var arrImg = new Array();
		$('#slides img').each(function(zindex){
			arrImg.push($(this).attr('src'));
			$(this).remove();
		});
		
		$('<img />')
		.load(function(){
			$('#slides').show().append(this)
			_this.scaleImg(0);
			$('#loader').fadeOut('slow',function(){
				$(this).remove()
				
				/**
				 * load other images
				 */
				var arrImgCache = new Array();
				var loaded = 0;
		
				$(arrImg).each(function(index,src){
					$('<img />')
					.load(function(){
						loaded++;
						arrImgCache[index] = $(this);
						if(loaded == arrImg.length){
							var images = '';
							$(arrImgCache).each(function(){
								var image = this;
								$('#slides img:last').after(function(){
									return image;
								});
							});
	
							$('#slides img').each(function(index){
								$(this).css('z-index',$('#slides img').length-index);
							});

							_this.scaleImg(1);
							if($("#slides img").length > 1){
								setTimeout(function(){
									_this.rotate(0);
								},_this.rotateInterval);
							}
	
						}
					})
					.attr('src',src);
				});
			});
		})
		.attr('src',firstImg);
	},
	
	scaleImgResize : function(){
		var slides = $('#slides img').length;
		$.RETARCO.fsgalery.scaleImg($.RETARCO.fsgalery.slideIndex);
		var nextSlide = $.RETARCO.fsgalery.slideIndex+1 % slides;
		$.RETARCO.fsgalery.scaleImg(nextSlide);
	},
	
	scaleImg : function(slide){
		var $element = $('#slides img:eq('+slide+')');
		if($element.height() > $(window).height() || $element.width() < $(window).width()){
			$element.css({
				width : $(window).width(),
				height : 'auto'
			});
		} else {
			$element.css({
				width : 'auto',
				height : $(window).height()
			});
		}
		this.positionImg(slide);
	},
	
	positionImg : function(slide){
		$element = $('#slides img:eq('+slide+')');
		if($element.height() > $(window).height()){
			$element.css('top',(($element.height() - $(window).height()) / 2) * -1);
		} else {
			$element.css('top',0);
		}
	},
	
	rotate : function(currentSlide){
		var _this = this;
		var slides = $('#slides img').length;
		
		currentSlide = currentSlide % slides;
		nextSlide = (currentSlide+1) % slides;

		this.scaleImg(nextSlide);

		$('#slides img:eq('+currentSlide+')').fadeOut(2000,function(){
			_this.slideIndex = nextSlide;
			$('#slides img:not(:eq('+currentSlide+'))').each(function(){
				$(this).css('z-index',parseInt($(this).css('z-index'))+1);
			});
			$(this).css('z-index',1).show();
		});

		setTimeout(function(){
			_this.rotate(++currentSlide);
		},this.rotateInterval);
	},
	
	resize : function(){
		$('#slides').css({
			width : $(window).width(),
			height : $(window).height()
		});
	}
};

$(document).ready(function(){
	$.RETARCO.initialize.construct();
	$.RETARCO.fsgalery.construct();
});
