/*
	slideris.js
*/
/*
var arg = {
	frame: 'title_news',
	content: 'title_news_cont',
	step: 600,
	speed: 300,
	navleft: 'titleSliderBack',
	navright: 'titleSliderNext',
	removeempty: false,
	loop: flase,
	autolength: null
}
*/
function horizontalSlider(args){
	this.frame = args.frame;
	this.content = args.content;
	this.navleft = args.navleft;
	this.navright = args.navright;
	this.step = args.step;
	this.speed = args.speed;
	this.totalwidth = 0;
	this.currentpos = 0;
	this.currentindex = 0;
	this.totalslides = 0;
	this.unlock = true;
	this.removeempty = (args.removeempty);
	this.loop = (args.loop);
	this.autolength = args.autolength;
	this.timmer = null;
	this.navcontainer = args.navcontainer;
	this.currentclass = args.currentclass;
	var self = this;
	
	this.init = function(){
		if(document.getElementById(this.content) && document.getElementById(this.frame)){
			if(!this.step || this.step<1){
				this.step = $('#'+this.frame).innerWidth();
			}
			if(!this.speed || this.speed<1){
				this.speed = 300;
			}
			this.totalwidth = $('#'+this.content).innerWidth();
			if(!this.autolength || this.autolength<1000){
				this.autolength = 0;
			}
			this.totalslides = Math.round(this.totalwidth/this.step);
			if(this.navcontainer && document.getElementById(this.navcontainer)){
				if(this.totalwidth > $('#'+this.frame).innerWidth() && this.totalwidth>this.step){
					for(var i=0; i<Math.round(this.totalwidth/this.step); i++){
						var a = document.createElement('a');
						document.getElementById(this.navcontainer).appendChild(a);
						a.href="javascript:void(0);";
						$(a).data('pos', (-1*i*self.step));
						$(a).data('it', i);
						$(a).html(i+1);
						$(a).click(function(){
							$(this).blur();
							if(self.unlock){
								var auto = (arguments[1]);
								if(auto){
									self.pauseAutoSlide();
								}
								self.unlock = false;
								$('#'+self.navcontainer+' .'+self.currentclass).removeClass(self.currentclass);
								var it = $(this).data('it');
								
								/**
								 * TODO: turi buti padaryta per papildoma parametra kaip callback
								 */
								$('#'+self.content).animate({'left': $(this).data('pos')}, self.speed, function(){
									changeTopBg($('#'+self.content+' td:eq('+it+')').css('background-image'));
									self.unlock = true;
									self.currentindex = it;
									if(auto){
										self.autoSlide();
									}
									
								});
								$(this).addClass(self.currentclass);
							}	
						});
						if(i<1){
							$(a).addClass(self.currentclass);
						}
					}
					changeTopBg($('#'+self.content+' td:eq(0)').css('background-image'));
				}
			}
			
			if(document.getElementById(this.navleft) && document.getElementById(this.navright)){
				if(this.totalwidth > $('#'+this.frame).innerWidth() && this.totalwidth>this.step){
				$('#'+this.navright).click(function(){
					$(this).blur();
					if(self.unlock){
						self.pauseAutoSlide();
						var np = self.currentpos + (-1*self.step);
						if(Math.abs(np) < self.totalwidth){
							self.unlock = false;
							$('#'+self.content).animate({'left': np}, self.speed, function(){
								self.unlock = true;
								self.currentpos = np;
								self.currentindex--;
							});
						}
						else if(self.loop){
							self.toStart();
						}
						if(self.autolength>0 && arguments[1]){
							self.autoSlide();
						}
					}
				});
				$('#'+this.navleft).click(function(){
					$(this).blur();
					if(self.unlock){
						self.pauseAutoSlide();
						var np = self.currentpos + (1*self.step);
						if(np < 1){
							self.unlock = false;
							$('#'+self.content).animate({'left': np}, self.speed, function(){
								self.unlock = true;
								self.currentpos = np;
								self.currentindex++;
							});
						}
						else if(self.loop){
							self.toEnd();
						}
					}
				});
				
				
				}
				else{
					if(this.removeempty){
						document.getElementById(this.navright).style.display = 'none';
						document.getElementById(this.navleft).style.display = 'none';
					}
				}
			}
			if(this.autolength>0 && ($('#'+this.navright).length > 0 || $('#'+this.navcontainer).length > 0)){
				$('#'+this.frame).mouseover(function(){
					self.pauseAutoSlide();
				});
				$('#'+this.frame).mouseout(function(){
					self.autoSlide();
				});
				if($('#'+this.navcontainer).length > 0){
					$('#'+this.navcontainer).mouseover(function(){
						self.pauseAutoSlide();
					});
					$('#'+this.navcontainer).mouseout(function(){
						self.autoSlide();
					});
				}
				this.autoSlide();
			}
		}
	};
	
	this.toStart = function(){
		self.unlock = false;
		$('#'+self.content).animate({'left': 0}, self.speed, function(){
			self.unlock = true;
			self.currentpos = 0;
		});
	};
	this.toEnd = function(){
		self.unlock = false;
		var np = -1 * (Math.floor(self.totalwidth / self.step)-1) * self.step;
		$('#'+self.content).animate({'left': np}, self.speed, function(){
			self.unlock = true;
			self.currentpos = np;
		});
	};
	
	this.autoSlide = function(){
		if($('#'+self.navright).length > 0){
			this.timmer = setTimeout(function(){$('#'+self.navright).trigger('click', true);}, self.autolength);
		}
		else if($('#'+self.navcontainer).length > 0){
			
			this.timmer = setTimeout(function(){
				var gotoi = -1;
				if(self.currentindex+1 < self.totalslides){
					gotoi = (self.currentindex+1);
				}
				else if(self.loop){
					gotoi = 0;
				}
				if(gotoi>-1){
					$('#'+self.navcontainer+' a:eq('+gotoi+')').trigger('click', true);
				}
				
			}, self.autolength);
		}
	};
	
	this.pauseAutoSlide = function(){
		if(this.timmer){
			clearTimeout(this.timmer);
		}
	};
	
	this.init();
}

function changeTopBg(img_url){
	var div = document.createElement('div');
	$(div).addClass('top_image').css({'display' : 'none', 'background-image': img_url}).html('&nbsp;');
	$('#top_holder').append(div);
	$('#top_image').fadeOut(1000);
	//$('#'+holder_id).css('visibility', 'hidden');
	$(div).fadeTo(1000, 0.5, function() {$('#top_image').remove();});
	$(div).attr("id", "top_image");
}



