/*
* jQuery UI carousel
* 
* Depends:
*   ui.core.js
* 
*/

(function($) {
	
	$.widget("ui.carousel", {
				
		_init: function() {	
			var self = this;
			self.initdata();
			self.hookLinks();
			self.autoplay();
			self.setStartView();
		},
		
		initdata: function(){
			var self = this;
			var widthsplit = self.options.finalWidth;
			self.options.pxorem = "em";
			self.options.curposi = self.getCurPosi();
			self.options.maxwidth = Number(widthsplit);
			if(self.options.singlewidth === false){
			self.options.singlewidth = Number(widthsplit) / Number(self.options.sliders);
			}
			self.element.css("width", (self.options.finalWidth)+self.options.pxorem);
		},
		
		setStartView: function(){			
			var self = this;
			
			if(self.options.itemtemplate == null){
				alert("no item template given");
				return;
			}
			try{
				if(self.options.slidemode == 1){ //atm aviable for mode 1 TODO: mode 2
								
					var self = this;
					var cdiv = self.element;
					var c = 0;
					var cnd = null;
					var cpd = null;
					var ts = (self.options.to - 1);
					var ti = 0;
					var to = null;
	
					//dynamic width for slider div
					self.element.css("width", ((3*self.options.vieweditems) * self.options.singlewidth)+self.options.pxorem);
					
					if(self.options.roundabout === true){
					for(var i = (self.options.to - 1); i < ((self.options.to - 1) + self.options.vieweditems); i++){
						
						if(i > (self.options.data.length -1)){
							cnd = self.options.data[ti];
							to = ti;
							ti++;
							if(ti > (self.options.data.length -1)){
								ti = 0;
							}
						}
						else{
							cnd = self.options.data[i];
							to = i;
						}
						$(self.generateTemplate(cnd)).appendTo(cdiv);					
					}	
					
					self.options.toposi = self.options.vieweditems + 1;
					ti = 0;
					for(var i2 = to+1; i2 <= (to + self.options.vieweditems); i2++){
						
						if(i2 > (self.options.data.length -1)){
							cnd = self.options.data[ti];
							self.options.lastindex = ti;
							ti++;
							if(ti > (self.options.data.length -1)){
								ti = 0;
							}
						}
						else{
							cnd = self.options.data[i2];
							self.options.lastindex = i2;
						}
						$(self.generateTemplate(cnd)).appendTo(cdiv);					
					}
					
					for(var i = (self.options.to - 1); i < ((self.options.to - 1) + self.options.vieweditems); i++){
												
						if(((ts - 1)-c)<0){
							ts = self.options.data.length;
							c = 0;
						}
						cpd = self.options.data[(ts - 1)-c];
						if(i < ((self.options.to - 1) + self.options.vieweditems)){
							$(self.generateTemplate(cpd)).prependTo(cdiv);
							self.options.firstindex = (ts - 1)-c;
							c++;
						}
					}	
						self.slide(self.options.toposi);
					}
					else{
						
						if(self.options.to > ((self.options.data.length) - (3 * self.options.vieweditems))){
							self.options.to = (self.options.data.length) - (3 * self.options.vieweditems);
						}
						
						//self.element.css("left", Number("-"+((self.options.to-1) * self.options.singlewidth))+"px");
						
						self.options.firstindex = self.options.to;
						for (var i = self.options.to; i < (self.options.to + self.options.vieweditems); i++){
							$(self.generateTemplate(self.options.data[i])).appendTo(self.element);
							self.options.lastindex = i;
						}
					}	
				}else{
					$(self.generateTemplate(self.options.data[0])).appendTo(self.element);
				}
			}catch(err){
			}
		},
		
		buildNextItems: function(newto){
			var self = this;
			var cnd = null;
			var cdiv = self.element;
			var ti = 0;

			self.options.firstindex = self.options.firstindex+self.options.vieweditems;
			if(self.options.firstindex > self.options.data.length){
				self.options.firstindex = (self.options.firstindex-(self.options.data.length));
				if(self.options.firstindex > self.options.data.length){
					self.options.firstindex = self.options.firstindex - self.options.data.length;
				}
			}
			try{
				for(var i = newto; i < (newto + self.options.vieweditems); i++){
	
					if(i > (self.options.data.length -1) && self.options.roundabout === true){
						cnd = self.options.data[ti];
						self.options.lastindex = ti;
						ti++;
						if(ti > (self.options.data.length -1)){
							ti = 0;
						}
					}
					else{
						if(i < self.options.data.length){
						cnd = self.options.data[i];
						self.options.lastindex = i;
						}
					}	
					if(self.options.roundabout === false && i <= (self.options.data.length -1)){
						$(self.generateTemplate(cnd)).appendTo(cdiv);
						self.options.lastindex = i;
					}
					else if(self.options.roundabout === true){
						$(self.generateTemplate(cnd)).appendTo(cdiv);
					}
				}
			}catch(err){
			}
		},
		
		buildPrevItems: function(prevto){
			
			var self = this;
			var ts = prevto;
			var cdiv = self.element;
			var c = 0;
			var cpd = null;
			
			try{
				var precalc = self.options.vieweditems - self.options.data.length;
				self.options.lastindex = self.options.lastindex - precalc;
				if(self.options.lastindex<0){
					self.options.lastindex = self.options.data.length + self.options.lastindex;
				}
				
				for(var i = prevto; i < (prevto + self.options.vieweditems); i++){
	
					if(((ts)-c)<0 && self.options.roundabout === true){
						ts = self.options.data.length-1;
						c = 0;
					}
					cpd = self.options.data[ts-c];
					if(((ts)-c)>=0 && self.options.roundabout === false){
						cdiv.prepend(self.generateTemplate(cpd));
						self.options.firstindex = ts-c;
					}else if(self.options.roundabout === true){
						cdiv.prepend(self.generateTemplate(cpd));
						self.options.firstindex = ts-c;
					}
					c++;
				}
			}catch(err){
			}
		},
		
		generateTemplate: function(data){
			var self = this;
			var template =  self.options.itemtemplate; 
			if(data != undefined){
				$.each(data, function(i, val){
					template = self.str_replace("{"+i+"}", val, template);
				});
			}
			template = $(template).bind("mouseenter mouseleave", function(e){
				if(e.type == "mouseenter"){
					self.options.hook_over(data, template, e);
				}else{
					self.options.hook_out(data, template, e);
				}
			});

			return template;
		},
		
		str_replace: function(search, replace, subject) {
		    return subject.split(search).join(replace);
		},
		
		getCurPosi: function(){
			var self = this;
			var posi = self.element.css("left").split(self.options.pxorem);
			posi = posi[0];	
			return Number(posi);
		},
		
		slide: function(to){

			var self = this;
			try{
			if(to == "prev"){
				if(self.options.toposi > 1){
					to = (self.options.toposi - self.options.slidesteps)-1;
					if(to<1){
						to = 1;
					}
				}
				else if(self.options.roundabout === true){
					self.element.css("left", "-"+self.options.maxwidth+self.options.pxorem);
					to = (self.options.vieweditems*3);
				}
				else{
					to = 1;
				}
			}else if(to == "next"){
				if(self.options.toposi < self.options.sliders){
					to = (self.options.toposi + self.options.slidesteps);
					if(to > (self.options.vieweditems*2) && self.options.slidemode == 1){
						to = ((self.options.vieweditems*3)+1)-self.options.vieweditems;
					}
				}
				else if(self.options.roundabout === true){
					to = (self.options.vieweditems*3)+1;
				}
				else{
					to = self.options.toposi;
				}
			}
			
			if(self.options.slidemode == 1){

				var to_ =  "-"+(Math.round(((to-1) * self.options.singlewidth)*100)/100);
				var animto = to_+self.options.pxorem;
				if(self.options.roundabout === true){				
				self.element.stop().animate({left: animto}, self.options.slidespeed, null , function(){

					if(to > ((self.options.vieweditems * 3)-self.options.vieweditems) && self.options.roundabout === true){
						
						to = self.options.vieweditems + 1;
						
						self.element.css("left", Number("-"+((to-1) * self.options.singlewidth))+self.options.pxorem);
						
						var newto = self.options.lastindex+1;
						
						self.buildNextItems(newto);
						
						var firstelements = self.element.children(":lt("+self.options.vieweditems+")");
						
						$.each(firstelements, function(i, val){
							$(val).remove();
						}); 
					}
					
					if(to == 1 && self.options.roundabout === true){
						to = self.options.vieweditems + 1;
						self.element.css("left", Number("-"+((to-1) * self.options.singlewidth))+self.options.pxorem);
						prevto = self.options.firstindex-1;
						var firstelements = self.element.children(":gt("+((self.options.vieweditems*2)-1)+")");
						$.each(firstelements, function(i, val){
							$(val).remove();
						});
						self.buildPrevItems(prevto);
					}	
										
					if(to>self.options.sliders && self.options.roundabout === true){
						self.element.css("left", "0"+self.options.pxorem);
						to = 1;
					}
					self.options.toposi = to;
				});
				}
				else{

					if(to == 1){
						
						var lastindex_old = self.options.firstindex;					
						if(lastindex_old-1>=0){
							self.buildPrevItems(lastindex_old-1);
							var lastindex_new = self.options.firstindex;						
							self.element.css("left", Number("-"+((lastindex_old-lastindex_new) * self.options.singlewidth))+self.options.pxorem);								
						}
						
					}else{

						var lastindex_old = self.options.lastindex;
						var newto = self.options.lastindex+1;
						self.buildNextItems(newto);
						var lastindex_new = self.options.lastindex;
						
						if(newto <= self.options.data.length-1){
							to_ = "-"+Number((((lastindex_new-lastindex_old)) * self.options.singlewidth));	
						}	
						else{
							to_ = 0;
						}
					}
					
					self.element.stop().animate({left: to_}, self.options.slidespeed, null , function(){
						
						if(to == 1){
									
							var cutlast= self.element.children(":gt("+(self.options.vieweditems-1)+")");
							$.each(cutlast, function(i, val){;
								$(val).remove();
							});
							
						}else{
							
							if((lastindex_new-lastindex_old)>0){
								var firstelements = self.element.children(":lt("+(lastindex_new-lastindex_old)+")");
								$.each(firstelements, function(i, val){
									$(val).remove();
								}); 
								
								self.element.css("left", "0"+self.options.pxorem);
							}
							
						}
						
					});
				}
			}
			else if(self.options.slidemode == 2){	
				var to_ = (self.options.singlewidth*100)/100;
				var animto = to_+self.options.pxorem;

				//customanimation
				
				self.element.find("#customanimation").fadeOut(100, function(){
					self.element.stop().animate({left: animto}, self.options.slidespeed, null , function(){
						var toindex = to-1;
						if(to >= self.options.sliders){
							to = 0;
						}
						var teaser = self.generateTemplate(self.options.data[toindex]);
						teaser.css("display", "none");
						teaser.find("#customanimation").css("display", "none");
						self.element.html(teaser);
						self.element.css("left", "0"+self.options.pxorem);	
						teaser.fadeIn(100, function(){
							teaser.find("#customanimation").fadeIn("slow");	
						});
						self.options.toposi = to;
					});	
				});			
			}
			}catch(err){
			}		
			
			
		},
		
		hookLinks: function(){
			var self = this;
			self.options.hook_links(self);
		},
		
		stopplay: function(){
			var self = this;
			self.options.auto = false;
		},
		
		startplay: function(){
			var self = this;
			self.options.auto = true;
		},
		
		clearautoplay: function(){
			var self = this;
			clearInterval(self.options.interval);
			self.autoplay();
		},
		
		autoplay: function(){
			var self = this;
						
				self.options.interval = setInterval(function(){
					if(self.options.auto === true){
						var count = self.options.to;
					if(count > Number(self.options.sliders)){
						self.options.to = 2;					
						self.slide(1);
					}else{
						self.slide(count);
						self.options.to = count + 1;
					}
					}
					
				}, self.options.playtimer);
		}
	
	});
	
	$.extend($.ui.carousel,{
		defaults: {
			sliders: 3,
			vieweditems: 1,
			hook_links: function(self){
			return false;
			},
			hook_over: function(data, element, event){
				return false;
			},
			hook_out: function(data, element, event){
				return false;
			},
			auto: true,
			playtimer: 5000,
			slidespeed: 1000,
			finalWidth: false,
			to: 1, // index of element where start to build
			toposi: 1, // jumps to the current position in the slider
			slidesteps: 0,
			roundabout: true,
			slidemode: 1,
			itemtemplate: null,
			singlewidth: false
		}
	});
})(jQuery);

