/*
UI - Slideshow

*/

(function($) {

var slideId = "03AS4",
	lastId = 0;
	

function getNextListId() {
	return ++listId;
}

$.widget("ui.slideshow", {
	
	options: {
		settings: {
			type: 'slide', dir:'h', groupName:'slideshow',
			spaceBetwenItems: 0, itemsPerSlide:1, randomFill: false, firstItem:'random',
			autoplay:false, showtime: 3000, pauseOnHover: false, delay: false,
			tracking:{done:{}, update:[]}
		},
		slide: { disable: null,hide: null,show: null,numOfItemsToFill: null,max: null },
		timer: null, buttons: null,
		animations: {
			_default:{
				show: {speed: 100, speedOnAutoSlide: 2000, ease:'linear'},
				hide: {speed: 100, speedOnAutoSlide: 2000, ease:'linear'}						
			}
		},
		onShow: function(item, self) {},
		onChange: function(item, self) {},
		onClick: function(item, self) {},
		onClose: function(item, self) {},
		onLowSlideLength: function(btns, len) {}
	},
	_create: function() {
		this.ele = this.element;
		this.slides = [];
		this.items = this.ele.find(".ui-slideshow-item");
		
		if(this.items.length == 0) return false;
		this.getSettings();
		
		this.items.hide();
		this.createSlides();
		this.addEvents();
		
	},
	_init: function() {
		var self = this;
		
		if(this.items.length == 0) return false;
		this.o.settings.slideshowOpt = this.getSlideSettings(this, this.o.animations[this.o.settings.animation]);
		this.o.settings.defaultOpt = this.getSlideSettings(this, this.o.animations["_default"]);
		
		this.o.slide.show = this.o.settings.firstItem == "random" ? this.getRandomItem(this.slides.length):(this.o.settings.firstItem>this.slides.length ? this.slides.length:this.o.settings.firstItem);
		
		this.switchSlide(this.o.settings.defaultOpt);
		
		if(this.o.settings.delay && this.o.settings.delay != 'false') {
			setTimeout(function(){self.init();}, this.o.settings.delay);
		}
		else {
			self.init();	
		}
	},
	init: function() {
		
		
		if(this.o.settings.autoplay == true && this.slides.length > 1) {
			this.startAutoPlay();
			this.autoPlayEvents(this.ele);
			
		} 
		
		
	},
	autoPlay: function() {
		this.action.nextSlide(this, this);
		this.switchSlide(this.o.settings.slideshowOpt);
		this.o.onChange(this.slides[this.o.slide.show], this);
		
	},
	autoPlayEvents: function(ele) {
		var _this = this;
		if(!_this.o.settings.pauseOnHover) return false;
		ele.mouseenter(function() {
			//trace("jaja je not prsu");
		  	_this.stopAutoPlay()
		}).mouseleave(function() {
			//trace("pa je su tud vn")
		  _this.startAutoPlay()
		});
	},
	startAutoPlay: function() {
		var self = this;
		this.o.timer = setInterval(function() {self.autoPlay()}, this.o.settings.showtime);
		//this.o.timer = this.autoPlay.periodical(this.o.settings.showtime, this);
	},
	stopAutoPlay: function() {
		clearInterval(this.o.timer);
	},
	createSlides: function() {
		var self = this,
			mask = this.createMask();
		this.o.slide.numOfItemsToFill = this.items.length % this.o.settings.itemsPerSlide == 0 ? 0:(this.o.settings.itemsPerSlide - (this.items.length % this.o.settings.itemsPerSlide));		
		
		var si = 0;
		this.items.each(function(i, ele) {
		//	CREATE SLIDE GROUP - n items per this.o.slide
			var _this = $(this);
			self.setStyles(_this, self);
			
			if(i % self.o.settings.itemsPerSlide ==0) {
				slides = self.slides[si] = $("<div class='ui-slideshow-slide' name='slide-"+si+"'/>").appendTo(mask)
				var decW = slides.outerWidth(true)-(slides.width()),
					decH = slides.outerHeight(true)-(slides.height());

					slides.css({
						'width':self.o.mask.w-decW,
						'height':self.o.mask.h-decH,
						'position': 'absolute',
						'left':'0px', 'top':'0px',
						'display': 'block',
						'visibility': 'visible'
					});
				++si;
				_this.css({"margin-left": 0, "margin-top":0});
				//if(self.o.settings.dir == 'v') _this.css("margin-top", 0);			else _this.css("margin-left", 0);
			};
			
			slides.append(ele).hide();
		//	numOfItemsToFill EMPTY items
			if(self.o.slide.numOfItemsToFill != 0 && i == self.items.length-1) {
				for(fi = 0; fi<self.o.slide.numOfItemsToFill; fi++) {
					if(self.o.settings.randomFill == true)
						nn = self.getRandomItem(items.length.length);
					else nn = i;
					tmpi = self.items.length+fi;
					itm = $(self.items[nn]).clone();
					self.setStyles(itm, self);
					slides.append(itm);
				}
			}
			
		});
		
		this.o.slide.max = this.slides.length-1		
		
		return;
	},
	setStyles: function(_this, self) {
		var decW = _this.outerWidth(true)-(_this.width()),
			decH = _this.outerHeight(true)-(_this.height());
		
		if(self.o.settings.dir == 'v') {	//navpičen
			_this.css({
				'width': self.o.item.w-decW,
				'height': self.o.item.h-decH,
				'margin-top':self.o.settings.spaceBetwenItems+'px',
				'left':'0px',
				'display': 'block', 'position': 'relative', 'visibility': 'visible'
			});
		} else {//vodoraven
			_this.css({
				'float':'left',
				'width': self.o.item.w-decW,
				'height': self.o.item.h-decH,
				'margin-left':self.o.settings.spaceBetwenItems+'px',
				'top':'0px',
				'display': 'block', 'position': 'relative', 'visibility': 'visible'
			});
		}
	},
	getSettings: function() {
		this.o = this.options;
		if(this.ele.attr('data-options')) $.extend(true, this.o.settings, this.ele.getMeta('data-options'));
		else if(this.ele.attr('options')) $.extend(true, this.o.settings, this.ele.getMeta('options'));
		
		//console.log(this.ele);
		var itm = this.ele.find('.ui-slideshow-item');
		$.extend(true, this.o, {
			mask:{
				w: itm.width(),
				h: itm.height()
			}
		});
		
		$.extend(true, this.o, {
			item: { w: null, h: null },
			pos:{
				t: {left:'0px',top:-this.o.mask.h+'px'},
				r: {left:this.o.mask.w+'px',top:'0px'},
				b: {left:'0px',top:this.o.mask.h+'px'},
				l: {left:-this.o.mask.w+'px',top:'0px'},
				tr: {left:this.o.mask.w+'px',top:-this.o.mask.h+'px'},
				tl: {left:-this.o.mask.w+'px',top:-this.o.mask.h+'px'},
				br: {left:this.o.mask.w+'px',top:this.o.mask.h+'px'},
				bl: {left:-this.o.mask.w+'px',top:this.o.mask.h+'px'}
			}
		});

		if(this.o.settings.dir == 'v') {
			this.o.item.w = this.o.mask.w;
			this.o.item.h = Math.floor((this.o.mask.h-this.o.settings.spaceBetwenItems)/this.o.settings.itemsPerSlide);				
		} else {
			this.o.item.w = Math.floor((this.o.mask.w-this.o.settings.spaceBetwenItems)/this.o.settings.itemsPerSlide);
			this.o.item.h = this.o.mask.h;
		}
	},
	createMask: function() {
		this.items.wrapAll("<div class='ui-slideshow-mask '/>");
		return this.ele.find(".ui-slideshow-mask").width(this.o.mask.w).height(this.o.mask.h).css({
			'display': 'block', 'position': 'relative', 'overflow': 'hidden'//, 'background':'#0f0'
		}); 		
	},
	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Bind button events and animations
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
	action: {
		prevSlide: function(self, root) {
			var prev = root.getPreviousItem(root.o.slide.show);
			root.setSlides(prev, root.getPreviousItem(prev));
		},
		nextSlide: function(self, root) {
			var next = root.getPreviousItem(root.o.slide.show);
			root.setSlides(next, root.getPreviousItem(next));
		},
		showSlideByItem: function(self, root) {
			var item = $(self).attr("name");
			root.setSlides(item);
			return;
			
		}

	},
	addEvents: function() {
		var self = this;
		
		if(this.slides.length > 1) {
			$.each(this.o.buttons, function(selector, o) {
				var _this = this;
				if(self.o.settings.autoplay == true)
				self.autoPlayEvents($(selector+"[target="+self.o.settings.groupName+"]"));
				$(selector+"[target="+self.o.settings.groupName+"]").data('opt', self.getSlideSettings(self, self.o.animations[o.animation])).bind(o.event+".slideshow", function(e) {
					
					if(self.o.slide.show == $(this).attr("name")) return false;
					self.action[o.action](this, self);
					
					self.switchSlide($(this).data('opt'));
					self.o.onClick(self.slides[self.o.slide.show], this);
				}).css({'cursor':'pointer', 'visibility': 'visible'});
			});
		} else {
			$.each(this.o.buttons, function(selector, o) {
				self.o.onLowSlideLength($(selector+"[target="+self.o.settings.groupName+"]"), self.slides.length);
				//self.autoPlayEvents($(selector+"[target="+self.o.settings.groupName+"]"));
			});
		}
	},
	getSlideSettings: function(self, opt) {
		var set = {
			show: self.getShowSettings(opt),
			hide: self.getHideSettings(opt)
		}
		return set;
	},
	getShowSettings: function(opt) {
		var from = {}, to = {}, spcEase = {}, show = opt.show;
		
		
		if(show.slide != false && show.slide != null) {
			$.extend(true, from, this.o.pos[show.slide.dir]);
			$.extend(true, spcEase, {left:show.slide.ease,top:show.slide.ease});
			//$.extend(true, to, {left:'0px',top:'0px'});
		}
		if(show.fade != false && show.fade != null) {
			$.extend(true, from, {opacity:0});
			$.extend(true, spcEase, {opacity:show.fade.ease});
			//$.extend(true, to, {opacity:1});
		}
		
		if(typeof opt.show.slide == 'object' || typeof opt.hide.slide == 'object')
			$.extend(true, to, {left:'0px',top:'0px'});
		if(typeof opt.show.fade == 'object' || typeof opt.hide.fade == 'object')
			$.extend(true, to, {opacity:1});
			
			
		//$.extend(true, to, {left:'0px',top:'0px',opacity:1});
		
		//console.log(to);
		var set = {
			speed: show.speed,
			ease: show.ease,
			from: from,
			to: to,
			spcEase: spcEase
		};
		
		return  set;
	},
	getHideSettings: function(opt) {
		var from = {}, to = {}, spcEase = {}, hide = opt.hide;
		
		if(hide.slide != false && hide.slide != null) {
			$.extend(true, to, this.o.pos[hide.slide.dir]);
			$.extend(true, spcEase, {left:hide.slide.ease,top:hide.slide.ease});
			
		}
		if(hide.fade != false && hide.fade != null) {
			$.extend(true, to, {opacity:0});
			$.extend(true, spcEase, {opacity:hide.fade.ease});
			
		}
		if(typeof opt.hide.slide == 'object' || typeof opt.show.slide == 'object')
			$.extend(true, from, {left:'0px',top:'0px'});
		if(typeof opt.hide.fade == 'object' || typeof opt.show.fade == 'object')
			$.extend(true, from, {opacity:1});
			
		//$.extend(true, from, {left:'0px',top:'0px',opacity:1});
		var set = {
			speed: hide.speed,
			ease: hide.ease,
			from: from,
			to: to,
			spcEase: spcEase
		};
		
		return  set;
	},
	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Animation manipulation functions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
	switchSlide: function(opt) {
		// Show slide
		
		//console.log(opt)
		//console.log("pokaže "+this.slides[this.o.slide.show].attr('name'));
		this.slides[this.o.slide.show].show().dequeue().css({'z-index': 10}).css(opt.show.from);
		this.slides[this.o.slide.show].animate(opt.show.to, { queue:true, duration:opt.show.speed, ease:opt.show.ease, specialEasing: opt.show.spcEase});
		this.o.onShow(this.slides[this.o.slide.show], this);
		
		// Hide slide
		if(this.o.slide.hide != null) {
			this.slides[this.o.slide.hide].css({'z-index': 9}).css(opt.hide.from);
			this.slides[this.o.slide.hide].stop(true,false).animate(opt.hide.to, { queue:true, duration:opt.hide.speed, ease:opt.hide.ease, specialEasing: opt.hide.spcEase,
				complete: function() {
					$(this).css(opt.hide.to);
				}
			});
			this.o.onClose(this.slides[this.o.slide.hide], this);
		};
		
		// Disable slide
		if(this.o.slide.disable != null) {
			this.slides[this.o.slide.disable].dequeue().css(opt.show.from).css({'z-index': 0}).hide();
		}
		
	},
	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Slide manipulation functions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
	setSlides: function(next, prepare) {
		this.o.slide.disable = this.o.slide.hide == next ? null:this.o.slide.hide;
		this.o.slide.hide = this.o.slide.show == next ? null:this.o.slide.show;
		this.o.slide.show = next;
		this.o.slide.prepare = prepare;
		
		return next;
	},
	getNextItem: function(cur) {
		++cur;
		if(cur == this.slides.length) cur = 0;
		return cur;
	},
	getPreviousItem: function(cur) {
		--cur;
		if(cur == -1) cur = this.o.slide.max;
		return cur;
	},
	getRandomItem: function(max) {
		return Math.floor(Math.random()*max);;
	},
	


	destroy: function() {
		var o = this.options;

		return this;
	},

	createFilmstrip: function(items, i) {
	return;
		n = this.items.wrapAll("<div class='ui-slideshow-mask'><div class='ui-slideshow-strip'/></div>");
		trace("Filmstrip");
		return;
	}

});

$.extend($.ui.slideshow, {
	version: '1.8.2'
});

$.extend($.ui.slideshow.prototype, {
	rotation: null,
	rotate: function(ms, continuing) {


		return this;
	}
});

})(jQuery);

