/*	Plugin: Presentator v0.1
	Author: Igor Barbashin
	File: jquery.presentator.js
	Description: Not a regular scroller. Allows to create storybards 
	instead of standard boring sliders.
	
	The plugin is in alpha stage and is not documented yet. If you need a custom slider like this one, please feel free to contact me at igor.barbashin@gmail.com
*/
(function ($) {

$.fn.presentator = function(options) {
	/* set default options */
    var options = $.extend({//Default options would go here
	}, options);
	
	/* iterate over the matched elements passed to the plugin */
	$(this).each(function() {
	
		//Initalize the container
		var container = $(this),
			objects = $('<div>').addClass('objects'),
			nav = $('<div>').addClass('nav'),
			slidesNum = 0,
			i, curInd = -1, obj, elType, newElement, innerElement, name;
			
		container.css({
			position: 'relative',
			overflow: 'hidden'
		}).append(objects, nav);
	
		//Adding objects
		for (name in options.initialLayout) {
			obj = options.initialLayout[name];
			elType = (obj.url || obj.url === 0) ? 'a' : 'div';
				
			if (!obj.image) {
				console.log("Error. No image set for", name);
			}
			
			
			newElement = $('<'+elType+'>').attr('id', name).css({
				position: 'absolute',
				display: 'block'
			}).css(obj.animate);
			
			if (!$.browser.msie) {
				newElement.css({
					background: 'url("'+obj.image+'") no-repeat'
				});
			} else {
				newElement.css({
					overflow: 'hidden'
				});
				innerElement = $('<div>');
				innerElement[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + obj.image + "', sizingMethod='crop')";
				
				newElement.append(innerElement);
			}
			
			if (obj.animate.opacity === 'hide') {
				newElement.hide();
			}
			
			if (obj.url || obj.url === 0) {
				if (typeof(obj.url) === 'number'){
					newElement.attr('href', '#');
					newElement.data(obj);
					newElement.click(function(e){
						e.preventDefault();
						var obj = $(this).data();
						openSlide(obj.url);
					});
				} else if (obj.url === 'next') {
					newElement.attr('href', '#');
					newElement.click(function(e){
						e.preventDefault();
						nextSlide();
					});
				} else if (obj.url === 'prev') {
					newElement.attr('href', '#');
					newElement.click(function(e){
						e.preventDefault();
						prevSlide();
					});
				} else {
					newElement.attr('href', obj.url);
				}
			}
			
			obj.position = -1;
			detectImageDimensions (obj, name);
			objects.append(newElement);
		}
		
		
		//Building navigation and counting slides
		for (name in options.slideLayout) {
			var newNavDot = $('<a>').attr('href', '#');
			nav.append(newNavDot);
			newNavDot.click(function(e){
				e.preventDefault();
				openSlide ($(this).index());
			});
			slidesNum = slidesNum + 1;
		}
		
		nav.css('margin-left', '-'+nav.width()/2+'px');
		
		openSlide(0);
		
		function nextSlide() {
			var ind = (curInd == slidesNum - 1) ? 0 : curInd + 1;
			openSlide(ind);
			
			
		}
		
		function prevSlide() {
			var ind = (curInd == 0) ? slidesNum - 1 : curInd - 1;
			openSlide(ind);
		}
		
		
		function openSlide(ind) {
			var name;
			$('.nav a.active', container).removeClass('active');
			$('.nav a:eq('+ind+')').addClass('active');
			
			
			for (name in options.initialLayout) { //We cycle through all objects
				var obj, preTransitionDelay = 0, pos = -2;
				
				for (i = ind; i >= 0; i--) {		//Checking from current index back to slide 0
					if (options.slideLayout[i][name]) {	//If object is found, it's the one.
						obj = options.slideLayout[i][name];
						pos = i;
						break;
					}
					
					if (i == 0) {
						//Go to initial state if not already there
						obj = options.initialLayout[name];
						pos = -1;
					}
				}

				if (pos != options.initialLayout[name].position) { //If the element needs to be animated, animate it
					var objElement = $('#'+name),
						targetAnimate = (obj.animate === 'initial') ? options.initialLayout[name].animate : obj.animate;

					if (curInd >= 0 && !obj.removing) {
						preTransitionDelay = 500;
					}
					objElement.stop(true, true);
					if (obj.css) {
						objElement.css(obj.css);
					}
					objElement.delay(preTransitionDelay).delay(0 || obj.delay).animate(targetAnimate);
					
					options.initialLayout[name].position = pos;
				} //If it remains at the same position, skip animation
			}
			
			//Analytics
			if (curInd >= 0 && _gaq) {
				_gaq.push(['_trackEvent', 'presentator', 'open-slide-'+(ind + 1)]);
			};
			
			
			curInd = ind;
		}
			
    });

	//Helper functions

	function detectImageDimensions (obj, name) {
		var img = new Image();
		img.onload = function() {
			
			if (!options.initialLayout[name].animate.width) {
				$('#' + name).css({
					width: this.width + 'px'
				});
			}
			
			if (!options.initialLayout[name].animate.height) {
				$('#' + name).css({
					height: this.height + 'px'
				});
			}
			
			if ($.browser.msie) {
				$('#' + name + ' div').css({
					width: this.width + 'px',
					height: this.height + 'px'
				});
			}
			
			obj.image.width = this.width;
			obj.image.height = this.height;
		  
		};
		img.src = obj.image;
	};
  }
})(jQuery);
