var NavigareContent = Class.create();

/* options:
**
** nav_buttons_ar - array (default 0 - dezactivat)
** [keys]	=>			value
** [1_left_act] =>		'calea spre primul buton stang de navigare - activ'
** [1_left_inact] =>	'calea spre primul buton stang de navigare - inactiv'
** [2_left_act] =>		'calea spre al doilea buton stang de navigare - activ'
** [2_left_inact] =>	'calea spre al doilea buton stang de navigare - inactiv'
** [1_right_act] =>		'calea spre primul buton drept de navigare - activ'
** [1_right_inact] =>	'calea spre primul buton drept de navigare - inactiv'
** [2_right_act] =>		'calea spre al doilea buton drept de navigare - activ'
** [2_right_inact] =>	'calea spre al doilea buton drept de navigare - inactiv'
** [bull_inact] =>		'calea spre imaginile de paginare - inactiv'
** [bull_act] =>		'calea spre imaginile de paginare - activ'
**
** nav_buttons - integer (default: 1)
** 1 - un rand de butoane stanga-dreapta (sus-jos) de navigare
** 2 - doua randuri de butoane stanga-dreapta (sus-jos) de navigare
**
** page_buttons - integer (default:0)
** 1 - activeaza existenta imaginilor pentru butoanele intermediare de navigare
** 0 - dezactivat
** 
** page_buttons_links - integer (default:0)
** 1 - activeaza existenta linkurilor pentru butoanele intermediare de navigare
** 0 - dezactivat
** 
** auto_nav - integer (default:0)
** 1 - activeaza navigarea automata a continutului
** 0 - dezactivat
**
** max_width - integer (default:0)
** - lungimea maxima de navigare (dimensiunea continutului in pixeli)
**
** item_width - integer (default:0)
** - dimensiunea unui element pentru navigare
**
** circular - integer (default:0)
** - slide circular
**
** direction - char
** h - navigare pe orizontala
** v - navigare pe verticala
**
** myclass - varchar
** - prefixul clasei
**
** total - item
** - numarului total de elemente
**
**
** autor Iulian Pobleanu
*/

NavigareContent.prototype = {
	initialize: function(options)
	{
		this.curent = 1;
		this.options = options;
		this.options.nav_buttons = this.options.nav_buttons!=undefined?this.options.nav_buttons:1;
		this.options.animation = this.options.animation!=undefined?this.options.animation:1;
		this.options.nav_buttons_ar = this.options.nav_buttons!=undefined?this.options.nav_buttons_ar:undefined;
		this.options.page_buttons = this.options.page_buttons!=undefined?this.options.page_buttons:0;
		this.options.page_buttons_links = this.options.page_buttons_links!=undefined?this.options.page_buttons_links:0;
		this.options.cb_function = this.options.cb_function!=undefined?this.options.cb_function:0;
		this.options.slide_time = this.options.slide_time!=undefined?this.options.slide_time:5000;
		this.options.circular = this.options.circular!=undefined?this.options.circular:0;
		if(this.options.myclass+'_item_1'==undefined) this.options.circular=0;
		if(this.options.nav_buttons)
			this.make_events();
		this.auto_nav = this.options.auto_nav?this.options.auto_nav:0;
		this.max_width = this.options.max_width?this.options.max_width:0;
		this.timeout = 0;
		this.correct_first_position_circular = 0;
		if(this.options.total==undefined)
		{
			this.options.total = $(this.options.myclass+'_cnt').scrollHeight/this.options.item_width;
			//alert(this.options.total)
			if(this.options.total<1)
			{
				$(this.options.myclass+'_left').style.display = 'none';
				$(this.options.myclass+'_right').style.display = 'none';
			}
			this.options.total = this.options.total.ceil();
		}
		if(this.auto_nav==1) this.go_auto_nav();
		this.width_correction = 0;
		if(this.options.circular)
		{
			children_items = $$('#'+this.options.myclass+'_cnt li');
			var new_item = children_items[0].cloneNode(true);
			$(this.options.myclass+'_cnt').appendChild(new_item);	
			//var dummy_container = document.createElement('div')
			//dummy_container.appendChild(children_items[0]);
			
			//item_to_copy = dummy_container.innerHTML;
			
			//alert(item_to_copy);
		}
		
	},
	go_nav: function()
	{
		if(this.options.cb_function!=0) this.options.cb_function(this.curent);
		//alert(this.max_width);
		if(this.max_width>0)
		{
			if(this.curent*this.options.item_width>this.max_width)
			{
				this.vnew = this.vnew - this.max_width+this.curent*this.options.item_width;
				this.width_correction = this.curent*this.options.item_width - this.max_width;
			}
		}
		if(this.options.animation==1)
		{
			if(this.options.direction=="v")
			{
				new Effect.Move($(this.options.myclass+'_cnt'), {y: this.vnew,queue: { position: 'end', scope: 'slider'},afterFinish:function(){
					if(this.correct_first_position_circular == 1)
					{
						$(this.options.myclass+'_cnt').style.top = '0px';
						this.correct_first_position_circular = 0;
					}
				}.bind(this)});
			}
			else if(this.options.direction=="h")
			{
				new Effect.Move($(this.options.myclass+'_cnt'), {x: this.vnew,queue: { position: 'end', scope: 'slider'},afterFinish:function(){
					if(this.correct_first_position_circular == 1)
					{
						$(this.options.myclass+'_cnt').style.left = '0px';
						this.correct_first_position_circular = 0;
					}
				}.bind(this)});
			}
		}
		else
		{
			if(this.options.direction=="v")
			{
				new Effect.Move($(this.options.myclass+'_cnt'), {duration:0, y: this.vnew,queue: { position: 'end', scope: 'slider'}});
			}
			else if(this.options.direction=="h")
			{
				new Effect.Move($(this.options.myclass+'_cnt'), {duration:0, x: this.vnew,queue: { position: 'end', scope: 'slider'}});
			}
		}
	},
	make_events: function()
	{
		if($(this.options.myclass+'_main_cnt')!=undefined)
		{
			Event.observe($(this.options.myclass+'_main_cnt'),'mouseover',function(){
				this.auto_nav = 0;
			}.bind(this));
			Event.observe($(this.options.myclass+'_main_cnt'),'mouseout',function(){
				clearTimeout(this.timeout);
				this.timeout = 0;
				this.auto_nav = 1;
				this.go_auto_nav();
			}.bind(this));
		}
		if($(this.options.myclass+'_left')!=undefined)
		{
			Event.observe($(this.options.myclass+'_left'),'click',function(){
				this.auto_nav = 0;
				this.calc_nav('down');
			}.bind(this));
			if(this.options.nav_buttons_ar!=undefined && this.options.nav_buttons_ar['1_left_over']!=undefined)
			{
				Event.observe($(this.options.myclass+'_left'),'mouseover',function(){
					if($(this.options.myclass+'_left').src.indexOf(this.options.nav_buttons_ar['1_left_inact'])==-1)
						$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_over'];
				}.bind(this));
				Event.observe($(this.options.myclass+'_left'),'mouseout',function(){
					if($(this.options.myclass+'_left').src.indexOf(this.options.nav_buttons_ar['1_left_inact'])==-1)
						$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_act'];
				}.bind(this));
			}
			if(this.options.nav_buttons_ar!=undefined && this.options.nav_buttons_ar['1_left_down']!=undefined)
			{
				Event.observe($(this.options.myclass+'_left'),'mousedown',function(){
					if($(this.options.myclass+'_left').src.indexOf(this.options.nav_buttons_ar['1_left_inact'])==-1)
						$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_down'];
				}.bind(this));
				Event.observe($(this.options.myclass+'_left'),'mouseup',function(){
					if($(this.options.myclass+'_left').src.indexOf(this.options.nav_buttons_ar['1_left_inact'])==-1)
						$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_over'];
				}.bind(this));
			}
		}
		if($(this.options.myclass+'_right')!=undefined)
		{
			Event.observe($(this.options.myclass+'_right'),'click',function(){
				this.auto_nav = 0;
				this.calc_nav('up');
			}.bind(this));
			if(this.options.nav_buttons_ar!=undefined && this.options.nav_buttons_ar['1_right_over']!=undefined)
			{
				Event.observe($(this.options.myclass+'_right'),'mouseover',function(){
					if($(this.options.myclass+'_right').src.indexOf(this.options.nav_buttons_ar['1_right_inact'])==-1)
						$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_over'];
				}.bind(this));
				Event.observe($(this.options.myclass+'_right'),'mouseout',function(){
					if($(this.options.myclass+'_right').src.indexOf(this.options.nav_buttons_ar['1_right_inact'])==-1)
						$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_act'];
				}.bind(this));
			}
			if(this.options.nav_buttons_ar!=undefined && this.options.nav_buttons_ar['1_right_down']!=undefined)
			{
				Event.observe($(this.options.myclass+'_right'),'mousedown',function(){
					if($(this.options.myclass+'_right').src.indexOf(this.options.nav_buttons_ar['1_right_inact'])==-1)
						$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_down'];
				}.bind(this));
				Event.observe($(this.options.myclass+'_right'),'mouseup',function(){
					if($(this.options.myclass+'_right').src.indexOf(this.options.nav_buttons_ar['1_right_inact'])==-1)
						$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_over'];
				}.bind(this));
			}
		}
		if(this.options.nav_buttons==2) 
		{
			if($(this.options.myclass+'_2_left')!=undefined)
			{
				Event.observe($(this.options.myclass+'_2_left'),'click',function(){
					this.auto_nav = 0;
					this.calc_nav('down');
				}.bind(this));
			}
			if($(this.options.myclass+'_2_right')!=undefined)
			{
				Event.observe($(this.options.myclass+'_2_right'),'click',function(){
					this.auto_nav = 0;
					this.calc_nav('up');
				}.bind(this));
			}
		}
		if(this.options.page_buttons==1)
		{
			for(i=1;i<=this.options.total;i++)
			{
				if($(this.options.myclass+'_'+i)!=undefined)
				{
					Event.observe($(this.options.myclass+'_'+i),'click',function(e){
						this.auto_nav = 0;
						this.calc_nav(e.target.id.substr(e.target.id.lastIndexOf('_')+1));
					}.bind(this));
				}
			}
		}
	},
	calc_nav: function(a)
	{
		go_to = 0;
		if(a=='down' && this.curent>1)
		{
			go_to = this.curent-1;
			this.vnew = this.options.item_width;
		}
		else if (a=='up' && this.curent<this.options.total)
		{
			go_to = this.curent+1;
			this.vnew = -this.options.item_width;
		}
		else if (a!='down' && a!='up')
		{
			go_to = parseInt(a);
			if(go_to>this.curent) this.vnew = (this.curent-a)*this.options.item_width;
			else if(go_to<this.curent) this.vnew = (this.curent-a)*this.options.item_width;
			
			if(this.auto_nav==1 && a==1)
			{
				this.vnew = 0-this.options.item_width;
				this.correct_first_position_circular = 1;
			}
		}
		if(go_to<1 || go_to>this.options.total || this.curent==go_to) return;
		this.curent = go_to;
		if(this.options.nav_buttons) this.redraw_nav2();
		if(this.width_correction>0)
		{
			this.vnew = this.vnew - this.width_correction;
			this.width_correction = 0;
		}
		this.go_nav();
	},
	reset: function()
	{
		this.calc_nav(1);
	},
	redraw_nav: function()
	{
		if(this.options.nav_buttons_ar==undefined) return;
		if(this.curent==1)
		{
			$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_inact'];
			if(this.options.nav_buttons==2) $(this.options.myclass+'_2_left').src = this.options.nav_buttons_ar['2_left_inact'];
		}
		else //active
		{
			if(this.options.nav_buttons_ar['1_left_over']==undefined || $(this.options.myclass+'_left').src.indexOf(this.options.nav_buttons_ar['1_left_over'])==-1)
				$(this.options.myclass+'_left').src = this.options.nav_buttons_ar['1_left_act'];
			if(this.options.nav_buttons==2) $(this.options.myclass+'_2_left').src = this.options.nav_buttons_ar['2_left_act'];
		}
		if(this.curent==this.options.total)
		{
			$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_inact'];
			if(this.options.nav_buttons==2) $(this.options.myclass+'_2_right').src = this.options.nav_buttons_ar['2_right_inact'];
		}
		else //active
		{
			if(this.options.nav_buttons_ar['1_right_over']==undefined || $(this.options.myclass+'_right').src.indexOf(this.options.nav_buttons_ar['1_right_over'])==-1)
				$(this.options.myclass+'_right').src = this.options.nav_buttons_ar['1_right_act'];
			if(this.options.nav_buttons==2) $(this.options.myclass+'_2_right').src = this.options.nav_buttons_ar['2_right_act'];
		}
		if(this.options.page_buttons==1)
		{
			for(i=1;i<=this.options.total;i++)
			{
				$(this.options.myclass+'_'+i).src = this.options.nav_buttons_ar['bull_inact'];
			}
			$(this.options.myclass+'_'+this.curent).src = this.options.nav_buttons_ar['bull_act'];
		}
	},
	redraw_nav2: function()
	{
		this.redraw_nav();
		if(this.options.page_buttons_links!=1) return;
		for(i=1;i<=this.options.total;i++)
		{
			//$(this.options.myclass+'_'+i).src = '/images/casete/video_bull_1_scroll.gif';
			$(this.options.myclass+'_'+i).className = 'number';
			if($(this.options.myclass2+'_'+i)!=null) $(this.options.myclass2+'_'+i).className = 'desc';
			//if(i==this.curent) Effect.Fade($(this.options.myclass2+'_'+i), { duration: 3.0 });
		} 
		//$(this.options.myclass+'_'+this.curent).src = '/images/casete/video_bull_2_scroll.gif';
		$(this.options.myclass+'_'+this.curent).className = 'number act';
		if($(this.options.myclass2+'_'+this.curent)!=null) $(this.options.myclass2+'_'+this.curent).className = 'desc act';
		//Effect.Appear($(this.options.myclass2+'_'+this.curent), { duration: 3.0 });		

	},	
	go_auto_nav: function()
	{
		if(this.auto_nav!=1) return;
		if(this.curent<this.options.total && this.timeout!=0)
			this.calc_nav('up');
		else if(this.timeout!=0)
			this.calc_nav(1);
		this.timeout = setTimeout(function(){this.go_auto_nav()}.bind(this),this.options.slide_time);
	}
}

