/***********************************************************/
/* This is rollovered menu which contains all categories   */
/* It uses accordion effect                                */ 
/***********************************************************/
var scroll = '';
var my_bookmarks = null;

function initialize_bookmarks(controller, action, id, title, page) {
	scroll = new Fx.Scroll(window, { wait: false, duration: 1400, transition: Fx.Transitions.Quad.easeInOut });

	var drag_bookmark_cont = $('bookmarks'); //kontener zawierajacy wszyzstkie zakladki
	var drag_bookmarks = $$('#bookmarks .bookmark'); //each bookmark
	//console.log(drag_bookmarks);
	my_bookmarks = new Bookmarks(drag_bookmark_cont, drag_bookmarks, controller, action, id, title, page);

};

var Bookmarks = new Class ({
	active: '0', // aktualnie aktywna zakladka
	last_active: '0', //ostatnio aktywna zakladka
	init_width: '130', //startowa szerokosc zakladki bez bokow
	start_z_index: '10', //startowy z-index pierwszej zakladki. do kazdej kolejnej bedzie dodawana wartosc i 1 wyzsza
		
	left_offset: 18, // dzieki temu jedna zakladka nachodzi na druga. left offset it's like position: absolute; left: -18
	left: 148, // left position kolejnych zakladek 
	total_left: 148, //calkowite odsuniecie od lewej stronie. zazwyczaj wskazuje na ostatnia zakladke. Potrzebne aby znac lewa pozycje kolejnej zakladki
	max_width: '812', //szerokosc kontenera w ktorym znajduja sie wszystkie zakladki
	
	max_without_resizing: 5, //maxymalna ilosc zakladek bez skalowania 
	max_resizing: 6, //maxymalna ilosc zakladek ktore beda skalowane. Kolejne juz nie beda skalowane tylko zostanie stworzony scroll do przewijania
	current_scroll: 0,
	scroll_offset: 2,
	
	single_width: 148, //startowal dlugosc pojedynczej zakladki (pozniej w trakcie skalowania ta wartosc ulega zmianie)
	fx_move: [],
	fx_move2: [], // tablica animacja dla przesuwania elementow w trakcie przeciagania zakladki
	all_left: [], //tablica wartosci marginesow od lewej strony dla kazdego elementu. Musimy znac docelowa left position zanim zaczniemy przesuwac zakladke
	current_dragged_left: null, // left position dla aktualnie przeciaganej zakladki
	fx_all_move: [], //tablica animacji dla przesuwania elementow w trakcie dodawania nowych zakladek
	fx_all_resize: '',
	qtn: 0,
	request_busy: false,
	queen_open: new Array (),
	queen_close: new Array (),
	bm_cookie: new Array (),
	show_hint_delay: '',
	
	
	//konstruktor inicjalizuje wszystkie niezbedne parametry
	initialize: function (drag_bookmark_cont, drag_bookmarks, controller, action, id, title, page) {
		this.product = new Products();
		this.product.fadeCoverOut();
		this.drag_bookmarks = drag_bookmarks;
		this.drag_cont = drag_bookmark_cont;
		this.hint =$('bookmark_hint');
		this.bookmark_scroller = $('bookmarks_scroller');
		this.left_arrow = $('bookmark_left');
		this.right_arrow = $('bookmark_right');

		//zainicjalizuj animacje do rozwijania hinut (podpowiedzi dla zakladek)
		this.hint_fx = new Fx.Tween (this.hint, {
			duration: 400,
			transition: Fx.Transitions.Quad.easyInOut,
			link: 'cancel'
		})
		this.hint_fx2 = new Fx.Tween (this.hint, {
			duration: 200,
			transition: Fx.Transitions.Quad.easyInOut,
			link: 'cancel'
		})		
		
		var my_cookie = Cookie.read ('bookmarks');

		if (!$defined(my_cookie)) {
			my_cookie = '{ "root": []}';
			Cookie.write ('bookmarks', my_cookie, {duration: 0, path: '/'});
		}
		
//		/console.log(my_cookie);
		
		this.cont_width = this.drag_cont.getStyle('width').toInt();
	
		my_cookie = eval('(' + my_cookie + ')');

		var active_cookie = Cookie.read('active');
		
		var viewed_bookmark = false;
		if (my_cookie.root.length > 0) {
			my_cookie.root.each(function(item, it){
				if (id != item.id || controller != item.controller || action != item.action) {
					this.createBookmark.run([item.controller, item.action, item.id, item.page, item.title, false], this);
				}
				else {
					viewed_bookmark = true;
					this.createBookmark2.run([controller, action, id, page,title, false], this);
					var cont = $(controller + "_" + action + "_" + id)
					cont.inject(cont.getParent(), 'bottom')
				}
			}.bind(this))
		} else {
			if (id && controller && action) {	
				viewed_bookmark = true;
				this.createBookmark2.run([controller, action, id, page, title, false], this);
				var cont = $(controller + "_" + action + "_" + id);
				cont.inject (cont.getParent(), 'bottom');
			}			
		}
		
		if (id && controller && action && !viewed_bookmark) {	

			this.createBookmark2.run([controller, action, id, page, title, true], this);
			var cont = $(controller + "_" + action + "_" + id)
			cont.inject (cont.getParent(), 'bottom');
			active_cookie = $('bookmarks').getElements('.bookmark').length -1;
			
		}				
		this.writeCookie(active_cookie);

		
		if (this.drag_bookmarks.length > 0) {

			this.last_active = this.active;
			
			this.deactivateElements.bind(this).run();
			
			
			//jesli nie wywolujemy bezposrednio widoku
		//	if ($defined(active_cookie)&& (!controller && !action && !id)) {
			if (!$defined(active_cookie) || active_cookie == 'null') {
				active_cookie = 0;
			}

			this.active = active_cookie.toInt()

		
			if ($defined(my_cookie.root[active_cookie])) {

				var c_contr = my_cookie.root[active_cookie].controller;
				var c_act = my_cookie.root[active_cookie].action;
				var c_id = my_cookie.root[active_cookie].id;
				var c_page = my_cookie.root[active_cookie].page;
				
				this.activateElement.run([this.drag_bookmarks[active_cookie], active_cookie], this);
				var prod_elem = $('view_content').getChildren()[active_cookie];
				
				var my_cookie = Cookie.read('bookmarks');
				
				//var item_class = prod_elem.getProperty('id').split('_');

				if (controller != c_contr || action != c_act || id != c_id) {
					this.request_view(c_contr, c_act, c_id, c_page, prod_elem);
				} else {
					product.activate(active_cookie)
				}

			} else {
				if ($defined(active_cookie)) {
					product.activate(active_cookie)
				}
			}
			
		}
		

		$$('#bookmarks .bookmark').each (function (elem, index) {
			if (!this.fx_move[index]) {

				this.fx_move[index] = new Fx.Tween (elem, {
						duration:500,
						fps: 60,	
						link: 'cancel',
						transition: Fx.Transitions.Quart.easeOut											
				})
				this.all_left [index]= index * this.single_width						
			}	

			if (!this.fx_all_move[index]) {
				this.fx_all_move[index] = new Fx.Tween(elem, {
					transition: Fx.Transitions.Quart.easeOut,
					duration: 500
				})
			}
			
		}.bind(this))			
		this.init_bookmark_scroll (this.drag_cont);

	},
	
	init_bookmark_scroll: function (bookmark) {

		this.scroll_fx = new Fx.Scroll (bookmark);
		$('bookmark_left').addEvent('click', function () {
			this.scroll_left();
		}.bind(this))
		$('bookmark_right').addEvent('click', function () {
			this.scroll_right();
		}.bind(this))
	},
	
	createNew: function (controller, action, id, page, title, request) {

		if (!$defined(request)) {
			request = true;
		}

		var elem = this.createBookmark.run([controller, action, id, page, title, true], this);

	},
	
	createBookmark: function (controller, action, id, page, title, request) {

		if ($$('#'+ controller + '_' + action + '_' + id).length < 1) {

				//ukrywa wszystkie widoki produktow, w ich miejsce pokaze sie loader 
				this.product.hideAll();

				
				//pobierz aktualna wysokosc koszyka - konteren nie moze byc mniejszy !
				//var he = ($('cart').getStyle('height').toInt())
				
				/* stworz nowy element (konternet) dla nowego widuku produuktu */
				var prod_cont = new Element('div', {
					'class': 'product_list',
					'id': controller + '_' + action + '_' + id,
					'styles': {
						'display': 'none'
					}
				});
				
				
				//wstaw kontener produktu do glownego kontenera
				prod_cont.inject($('view_content'), 'bottom');
				
				//otworz widok kategorii
				if (request) {		
					prod_cont.set('html', '&nbsp');
					this.request_view (controller, action, id, page, prod_cont, this.qtn);
				}
				

					//create new bookmark element
					var bookmark = new Element('div', {
//						'class': 'bookmark list_' + id,
						'class': 'bookmark '  +controller + '_' + action + '_' + id,
						'html': '<div class="left_on"></div>' +
						'<div class="center_on" style="width: 0px">'+ decode(decodeURIComponent(title)) +'</div>' +
						'<div class="right_on"><div class="close"></div></div>'
					
					});
					//set z-index
					bookmark.style.zIndex = '100';
					
					//set active and deactive element
					this.last_active = this.active;
					this.active = this.drag_bookmarks.length;

					// attach new element
					bookmark.inject(this.bookmark_scroller, 'bottom');

					// set draggable	
					this.set_draggable(bookmark);
					
					//set left offset, needs to set left position each element
					total_width = 0;
					elem = []; //arrays of center element
					this.drag_bookmarks.each(function(item, i){
						elem[i] = (item.getFirst('.center_off')) ? item.getFirst('.center_off') : item.getFirst('.center_on');
						total_width += item.getStyle('width').toInt();
						if ($defined(this.fx_move[i])) {
							this.fx_move[i].cancel();
						}
						if (this.qtn >= this.max_without_resizing) {
							if ($defined(this.fx_move2[i])) {
								this.fx_move2[i].cancel();
							}
						}
					}.bind(this));

					if (this.qtn >= this.max_without_resizing) {
					
						if (this.qtn < this.max_resizing) {
							//push bookmark to the collection
							this.drag_bookmarks.push(bookmark);
							elem.push(bookmark.getFirst('.center_on'));
							
							if (this.fx_all_resize) {
								this.fx_all_resize.pause();
							}
							var all_bookmarks = $$('#bookmarks .bookmark');
							all_bookmarks.each (function (item) {
								item.setStyle('width', this.single_width + 36)		
							})
							
							
							
							//resize all bookmarks
							this.fx_all_resize = new Fx.Elements(elem, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500								
							})
							
							var available_width = this.max_width - ((this.left_offset) * (this.qtn + 5));
							this.single_width = available_width / (this.qtn + 1);

							obj = {};
							
							elem.each(function(item, i){
								obj[i] = {
									'width': this.single_width
								}
							}.bind(this));
							
							//skaluj zakladki
							this.fx_all_resize.start(obj);
							
							
							var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
							if (scroller_width > 610) {
								this.bookmark_scroller.setStyle('width', scroller_width);
							}
							
							bookmark.setStyle('left', 248);
							//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
							all_bookmarks.each(function(item, i){
								if (this.fx_all_move[i]) {
								
									this.fx_all_move[i].cancel();
								}
								
								this.fx_all_move[i] = new Fx.Tween(item, {
									transition: Fx.Transitions.Quart.easeOut,
									duration: 500
								})
															
								new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
								bookmark.setStyle('left', new_left_offset);
								this.fx_all_move[i].start('left', new_left_offset);
								this.all_left[i] = new_left_offset.toInt()
							}
.bind(this));
						} else  {
							//push bookmark to the collection
							this.drag_bookmarks.push(bookmark);
							elem.push(bookmark.getFirst('.center_on'));
							
							if (this.fx_all_resize) {
								this.fx_all_resize.pause();
							}
							
							var all_bookmarks = $$('#bookmarks .bookmark');

							this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b3])				
							this.show_outbookmarks(all_bookmarks, this.b4);
							this.show_outbookmarks(all_bookmarks, this.b1)			
							this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b2]);		
							
							all_bookmarks.each (function (item) {
								item.setStyle('width', this.single_width + 36)		
							})							
							
							//resize all bookmarks
							this.fx_all_resize = new Fx.Elements(elem, {
								transition: Fx.Transitions.Quart.easeOut,
								
								duration: 500
							})
							
							var available_width = this.max_width - ((this.left_offset) * (10));
							this.single_width = Math.ceil(available_width / this.max_resizing);
							
							obj = {};
							
							elem.each(function(item, i){
								obj[i] = {
									'width': this.single_width
								}
							}.bind(this));
							
							//skaluj zakladki
						
							

							
							
							var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
							if (scroller_width > 610) {
								this.bookmark_scroller.setStyle('width', scroller_width);
							}
							this.fx_all_resize.start(obj);
							bookmark.setStyle('left', 248);
							//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
							all_bookmarks.each(function(item, i){
								if (this.fx_all_move[i]) {
								
									this.fx_all_move[i].cancel();
								}
								
								this.fx_all_move[i] = new Fx.Tween(item, {
									transition: Fx.Transitions.Quart.easeOut,
									duration: 500
								})
								
								new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
								bookmark.setStyle('left', new_left_offset);
								
								item.setStyle('width', this.single_width + 36)
								this.fx_all_move[i].start('left', new_left_offset);
								this.all_left[i] = new_left_offset.toInt()
							}.bind(this));				

							var scroller_width_standard = this.max_resizing * (this.single_width + this.left_offset) + this.left_offset;
							//this.scroll_fx.start(scroller_width - scroller_width_standard , 0)	
							this.bookmark_scroller.set('tween', {
								duration: 300,
								onComplete: function () {
									this.hide_outbookmarks(all_bookmarks,this.b1)	
								}.bind (this)
								
							}).tween('left', -(scroller_width - scroller_width_standard))
							

							this.b1 = (this.current_scroll+1 - (this.max_resizing) > 0) ? this.current_scroll+1 - (this.max_resizing - 1) - 2 : -1;
							
							this.left_arrow.setStyle('visibility', 'visible');
							this.right_arrow.setStyle('visibility', 'visible');							
						}
											
					}
					else {
						bookmark.setStyle('left', this.total_left);

						var center_elem = bookmark.getFirst('.center_on')
						
						var k = this.active;
				    	if ($type(this.fx_move2[k]) == 'object') {
							this.fx_move2[k].cancel();
						}
						
							this.fx_move2[k] = new Fx.Tween(center_elem, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500,
								link: 'cancel'
							});
						
						this.fx_move2[k].start('width', this.init_width);
						
						this.total_left = this.total_left.toInt() + this.left.toInt();
						this.drag_bookmarks.push(bookmark);
						
						var all_bookmarks = $$('#bookmarks .bookmark');
						var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
						if (scroller_width > 610) {
							this.bookmark_scroller.setStyle('width', scroller_width);
						}
						
						
						all_bookmarks.each(function(item, i){
							this.all_left[i] = i * this.single_width;
						}.bind(this))
						
						//anuliuj przsuwanie zwiazane z przeciaganiem zakladki 
						
						all_bookmarks.each(function(item, i){
							if ($type(this.fx_all_move[i]) == 'object') {
								this.fx_all_move[i].cancel();
							}
							
							this.fx_all_move[i] = new Fx.Tween(item, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500
							})

							new_left_offset = this.all_left[i];
							item.setStyle('width', this.single_width + 36)							
							bookmark.setStyle('left', new_left_offset);
							this.fx_all_move[i].start('left', new_left_offset);
							this.all_left[i] = new_left_offset.toInt();
						}.bind(this));
							
					}
					

//--------/* Po najechaniu myszka powinien pokazac sie caly tytul zakladki */----------------//
					bookmark.addEvent('mouseover', function(event){
						event = new Event(event);
						$clear(this.show_hint_delay);
						this.show_hint_delay = this.showHint.delay(500, this, decode(decodeURIComponent(title)));							
						event.stop();
						event.stopPropagation();					

					}.bind(this))
					
					bookmark.addEvent('mouseout', function (event) {
						event = new Event(event);


						if ((event.target != bookmark.getElements('div')[1] || event.relatedTarget != bookmark.getElements('div')[2]) &&
							(event.target != bookmark.getElements('div')[2] || event.relatedTarget != bookmark.getElements('div')[1]) &&
							
							(event.target != bookmark.getElements('div')[0] || event.relatedTarget != bookmark.getElements('div')[1]) &&
							(event.target != bookmark.getElements('div')[1] || event.relatedTarget != bookmark.getElements('div')[0])  &&

							(event.target != bookmark.getElements('div')[2] || event.relatedTarget != bookmark.getElements('div')[2].getFirst('div')) &&
							(event.target != bookmark.getElements('div')[2].getFirst('div') || event.relatedTarget != bookmark.getElements('div')[2]) 							
													
						) {
							$clear(this.show_hint_delay);
							
							this.hideHint();
						}
						event.stopPropagation();

					}.bind(this))
					
					bookmark.addEvent('mousemove', function (e) {
						if (!e) var e = window.event

						var x = e.page.x;
						var y = e.page.y;						
						
						this.hint.setStyle('left', x + 10);
						this.hint.setStyle('top', y - 5);
						
					}.bind(this))
					
					
					bookmark.addEvent('mousedown', function(){
						$clear(this.show_hint_delay);
						this.hideHint();
						
						var ind = bookmark.getProperty('class').split('_')
						
						this.activate_page(ind[0], ind[1], ind[2])
						var i = this.drag_bookmarks.indexOf(bookmark);
			
						if (this.active != i) {
							this.activateElement.bind(this, [bookmark, i]).run();
							this.deactivateElements.run([],this);
						}
						
						
						//po klikniecu na zakladke szukamy w divach z trescia odpowiedniego elementu poruwnujac klase zakladki z id contentu
						var ind = '';
						var bookm_class = bookmark.getProperty('class').split(' ');
						var bookm_class2 = bookm_class[1].split('_');	
						
						var contents = $('view_content').getChildren();
						$$('#bookmarks .bookmark').each (function(other, j) {
							var tmp_class2 = contents[j].getProperty('id').split('_');
							if (bookm_class2[0] == tmp_class2[0] && bookm_class2[1] == tmp_class2[1] && bookm_class2[2] == tmp_class2[2]) {					
								ind = j;
							}
						})	
						var prod_elem = contents[ind];

						var item_class = prod_elem.getProperty('id').split('_');
						
						
						//nie odswiezaj widoku jesli znajduje sie juz w nim jakis kod HTML, dzieki temu odswiezamy tylko raz dany widok na zakladce
						// chyba ze jest to widok koszyka to odswiezaj zawsze po to aby po dodaniu do koszyka zawsze znajdowaly
						//sie tam swieze informacje
						var cont_class = prod_elem.getProperty('class');
						cont_class = cont_class.split(' ');
						if (cont_class[1] == "refresh") {
							prod_elem.setProperty('class', 'product_list');
						}						
						
						if (prod_elem.get('html').trim() == '' || (item_class[0] == 'cart' && item_class[1] == 'show' && prod_elem.get('html').trim() != '' && prod_elem.get('html').trim() != '&nbsp;') || cont_class[1] == "refresh" /*|| (item_class[0] == 'orders' && item_class[1] == 'history')*/) {
							// id, controller, update element
							this.request_view(item_class[0], item_class[1], item_class[2], page, prod_elem);
						} else {							
							this.product.fadeCoverOut();
						}

						this.writeCookie(ind);							
						
					}.bind(this))

					bookmark.getElements('.close')[0].addEvent('click', function(event){						
						$clear(this.show_hint_delay);
						this.hideHint();
						this.product.fadeCoverIn(); //akcja z mousedown probuje zamknac cover

						if ($$('#bookmarks .bookmark').length > 1) {
							var a = bookmark;
							this.close.run(bookmark, this);

							this.activateElement.run([this.drag_bookmarks[this.drag_bookmarks.length - 1], this.drag_bookmarks.length - 1], this);
							var id_to_close = a.getProperty('class').split('_');
							//var id = this.drag_bookmarks[this.drag_bookmarks.length - 1].getProperty('class').split('_');

								
							this.product.remove(controller, action, id);

							this.product.activate(this.drag_bookmarks.length - 1);
							
							var prod_elem = $('view_content').getChildren()[this.drag_bookmarks.length - 1];
							
							var item_class = prod_elem.getProperty('id').split('_');							
							
							//nie odswiezaj widoku jesli znajduje sie juz w nim jakis kod HTML, dzieki temu odswiezamy tylko raz dany widok na zakladce
							// chyba ze jest to widok koszyka to odswiezaj zawsze po to aby po dodaniu do koszyka zawsze znajdowaly
							//sie tam swieze informacje
							var cont_class = prod_elem.getProperty('class');
							cont_class = cont_class.split(' ');
							if (cont_class[1] == "refresh") {
								prod_elem.setProperty('class', 'product_list');
							}		
						
							if (prod_elem.get('html').trim() == '' || (item_class[0] == 'cart' && item_class[1] == 'show') || cont_class[1] == "refresh" /*|| (item_class[0] == 'orders' && item_class[1] == 'history')*/) {
								var pg = this.product.findPage (item_class);
								this.request_view(item_class[0], item_class[1],  item_class[2], pg, prod_elem);
							} else {
								this.product.fadeCoverOut();
								$('product_loader').setStyle('visibility', 'hidden');//pokaz loader									
							}
							
							this.writeCookie(this.drag_bookmarks.length - 1);
						}

					}.bind(this));
					
					//deactivate last actived element
					this.deactivateElements.run([],this);
					this.writeCookie(this.active);	
					this.createTransition.run([],this);

					this.qtn++;
					this.current_scroll = this.qtn;

			//	}
				this.writeBookmarkCookie.pass([controller,action,id,page,this.qtn, title]).run();
			} else {

				this.activate_page.run(["bookmark " + controller, action, id], this);
				var prod_elem = $('view_content').getChildren()[this.active];
				
				var cont_class = prod_elem.getProperty('class');
				cont_class = cont_class.split(' ');
				
				if (controller == 'orders' && action == 'history') {
					cont_class[1] = "refresh";
				}				

				if (cont_class[1] == "refresh" ) {
					prod_elem.setProperty('class', 'product_list');
				}
				if (prod_elem.get('html').trim() == '' || (controller == 'cart' && action == 'show' && prod_elem.get('html').trim() != '' && prod_elem.get('html').trim() != '&nbsp;') || cont_class[1] == "refresh"/* || (controller == 'orders' && action == 'history')*/) {
					this.request_view(controller, action, id, page, prod_elem);
				}
			}		
			
			this.historyWrite.pass([controller,action,id,page,this.qtn, title]).run();			
			
			return bookmark;
	},
	createBookmark2: function (controller, action, id, page, title) {
		if ($$('.'+ controller + '_' + action + '_' + id).length < 1) {
				bookmark = $$('.'+ controller + '_' + action + '_' + id)[0]
				if (this.qtn < this.max_resizing) {

					//create new bookmark element
					var bookmark = new Element('div', {
//						'class': 'bookmark list_' + id,
						'class': 'bookmark '  +controller + '_' + action + '_' + id,
						'html': '<div class="left_on"></div>' +
						'<div class="center_on" style="width: 0px">'+ decode(decodeURIComponent(title)) +'</div>' +
						'<div class="right_on"><div class="close"></div></div>'
					
					});
					//set z-index
					bookmark.style.zIndex = '100';

					//set active and deactive element
					this.last_active = this.active;
					this.active = this.drag_bookmarks.length;

					// attach new element
					bookmark.inject(this.bookmark_scroller, 'bottom');
									

					// set draggable	
					this.set_draggable(bookmark);

					//set left offset, needs to set left position each element
					total_width = 0;
					elem = []; //arrays of center element
					this.drag_bookmarks.each(function(item, i){
						elem[i] = (item.getFirst('.center_off')) ? item.getFirst('.center_off') : item.getFirst('.center_on');
						total_width += item.getStyle('width').toInt();
						if ($defined(this.fx_move[i])) {
							this.fx_move[i].cancel();
						}
						if (this.qtn >= this.max_without_resizing) {
							if ($defined(this.fx_move2[i])) {
								this.fx_move2[i].cancel();
							}
						}
					}.bind(this));

					if (this.qtn >= this.max_without_resizing) {
						if (this.qtn < this.max_resizing) {
						//push bookmark to the collection
						this.drag_bookmarks.push(bookmark);
						elem.push(bookmark.getFirst('.center_on'));

						if (this.fx_all_resize) {
							this.fx_all_resize.pause();
						}
						//resize all bookmarks
						this.fx_all_resize = new Fx.Elements(elem, {
							transition: Fx.Transitions.Quart.easeOut,
					
							duration: 500
						})
						
						var available_width = this.max_width - ((this.left_offset) * (this.qtn + 5));
						this.single_width = available_width / (this.qtn + 1);
						
						obj = {};
						
						elem.each(function(item, i){
							obj[i] = {
								'width': this.single_width
							}
						}.bind(this));
						
						//skaluj zakladki
						this.fx_all_resize.start(obj);
						
						var all_bookmarks = $$('#bookmarks .bookmark');
							var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
							if (scroller_width > 610) {
								this.bookmark_scroller.setStyle('width', scroller_width);
							}
						
						bookmark.setStyle('left', 248);
						//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
						all_bookmarks.each(function(item, i){
							if (this.fx_all_move[i]) {
							
								this.fx_all_move[i].cancel();
							}
							
							this.fx_all_move[i] = new Fx.Tween(item, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500
							})
								item.setStyle('width', this.single_width + 36)
							new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
							bookmark.setStyle('left', new_left_offset);
							this.fx_all_move[i].start('left', new_left_offset);
							this.all_left[i] = new_left_offset.toInt()
						}.bind(this));
							
	} else  {
							//push bookmark to the collection
							this.drag_bookmarks.push(bookmark);
							elem.push(bookmark.getFirst('.center_on'));
							
							if (this.fx_all_resize) {
								this.fx_all_resize.pause();
							}
							
							var all_bookmarks = $$('#bookmarks .bookmark');

							this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b3])				
							this.show_outbookmarks(all_bookmarks, this.b4);
							this.show_outbookmarks(all_bookmarks, this.b1)			
							this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b2]);		
							
							all_bookmarks.each (function (item) {
								item.setStyle('width', this.single_width + 36)		
							})							
							
							//resize all bookmarks
							this.fx_all_resize = new Fx.Elements(elem, {
								transition: Fx.Transitions.Quart.easeOut,
								
								duration: 500
							})
							
							var available_width = this.max_width - ((this.left_offset) * (10));
							this.single_width = Math.ceil(available_width / this.max_resizing);
							
							obj = {};
							
							elem.each(function(item, i){
								obj[i] = {
									'width': this.single_width
								}
							}.bind(this));
							
							//skaluj zakladki
						
							

							
							
							var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
							if (scroller_width > 610) {
								this.bookmark_scroller.setStyle('width', scroller_width);
							}
							this.fx_all_resize.start(obj);
							bookmark.setStyle('left', 248);
							//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
							all_bookmarks.each(function(item, i){
								if (this.fx_all_move[i]) {
								
									this.fx_all_move[i].cancel();
								}
								
								this.fx_all_move[i] = new Fx.Tween(item, {
									transition: Fx.Transitions.Quart.easeOut,
									duration: 500
								})
								
								new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
								bookmark.setStyle('left', new_left_offset);
								
								item.setStyle('width', this.single_width + 36)
								this.fx_all_move[i].start('left', new_left_offset);
								this.all_left[i] = new_left_offset.toInt()
							}.bind(this));				

							var scroller_width_standard = this.max_resizing * (this.single_width + this.left_offset) + this.left_offset;
							//this.scroll_fx.start(scroller_width - scroller_width_standard , 0)	
							this.bookmark_scroller.set('tween', {
								duration: 300,
								onComplete: function () {
									this.hide_outbookmarks(all_bookmarks,this.b1)	
								}.bind (this)
								
							}).tween('left', -(scroller_width - scroller_width_standard))
							

							this.b1 = (this.current_scroll+1 - (this.max_resizing) > 0) ? this.current_scroll+1 - (this.max_resizing - 1) - 2 : -1;
							
						}							
											
					}
					else {
						bookmark.setStyle('left', this.total_left);

						var center_elem = bookmark.getFirst('.center_on')
						
						var k = this.active;
				    	if ($type(this.fx_move2[k]) == 'object') {
							this.fx_move2[k].cancel();
						}
						
							this.fx_move2[k] = new Fx.Tween(center_elem, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500,
								link: 'cancel'
							});
						
						this.fx_move2[k].start('width', this.init_width);
						
						this.total_left = this.total_left.toInt() + this.left.toInt();
						this.drag_bookmarks.push(bookmark);
						
						var all_bookmarks = $$('#bookmarks .bookmark');
							var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
							if (scroller_width > 610) {
								this.bookmark_scroller.setStyle('width', scroller_width);
							}
						
						all_bookmarks.each(function(item, i){
							this.all_left[i] = i * this.single_width;
						}.bind(this))
						
						//anuliuj przsuwanie zwiazane z przeciaganiem zakladki 
						
						all_bookmarks.each(function(item, i){
							if ($type(this.fx_all_move[i]) == 'object') {
								this.fx_all_move[i].cancel();
							}
							
							this.fx_all_move[i] = new Fx.Tween(item, {
								transition: Fx.Transitions.Quart.easeOut,
								duration: 500
							})
								item.setStyle('width', this.single_width + 36)
							new_left_offset = this.all_left[i];
							bookmark.setStyle('left', new_left_offset);
							this.fx_all_move[i].start('left', new_left_offset);
							this.all_left[i] = new_left_offset.toInt();
						}.bind(this));
							
					}
					


//--------/* Po najechaniu myszka powinien pokazac sie caly tytul zakladki */----------------//
					bookmark.addEvent('mouseover', function(event){
						event = new Event(event);
						$clear(this.show_hint_delay);
						this.show_hint_delay = this.showHint.delay(500, this, decode(decodeURIComponent(title)));							
						event.stop();
						event.stopPropagation();					

					}.bind(this))
					
					bookmark.addEvent('mouseout', function (event) {
						event = new Event(event);


						if ((event.target != bookmark.getElements('div')[1] || event.relatedTarget != bookmark.getElements('div')[2]) &&
							(event.target != bookmark.getElements('div')[2] || event.relatedTarget != bookmark.getElements('div')[1]) &&
							
							(event.target != bookmark.getElements('div')[0] || event.relatedTarget != bookmark.getElements('div')[1]) &&
							(event.target != bookmark.getElements('div')[1] || event.relatedTarget != bookmark.getElements('div')[0])  &&

							(event.target != bookmark.getElements('div')[2] || event.relatedTarget != bookmark.getElements('div')[2].getFirst('div')) &&
							(event.target != bookmark.getElements('div')[2].getFirst('div') || event.relatedTarget != bookmark.getElements('div')[2]) 							
													
						) {
							$clear(this.show_hint_delay);
							
							this.hideHint();
						}
						event.stopPropagation();

					}.bind(this))
					
					bookmark.addEvent('mousemove', function (e) {
						if (!e) var e = window.event

						var x = e.page.x;
						var y = e.page.y;						
						
						this.hint.setStyle('left', x + 10);
						this.hint.setStyle('top', y - 5);
						
					}.bind(this))
					
					bookmark.addEvent('mousedown', function(){
						var ind = bookmark.getProperty('class').split('_')

						this.activate_page(ind[0], ind[1], ind[2])
						var i = this.drag_bookmarks.indexOf(bookmark);
			
						if (this.active != i) {
							this.activateElement.run([bookmark, i], this);
							this.deactivateElements.run([],this);
						}
						
						
						//po klikniecu na zakladke szukamy w divach z trescia odpowiedniego elementu poruwnujac klase zakladki z id contentu
						var ind = '';
						var bookm_class = bookmark.getProperty('class').split(' ');
						var bookm_class2 = bookm_class[1].split('_');	
						
						var contents = $('view_content').getChildren();
						
						$$('#bookmarks .bookmark').each (function(other, j) {
							var tmp_class2 = contents[j].getProperty('id').split('_');
							if (bookm_class2[0] == tmp_class2[0] && bookm_class2[1] == tmp_class2[1] && bookm_class2[2] == tmp_class2[2]) {					
								ind = j;
							}
						})	
						var prod_elem = contents[ind];

						var item_class = prod_elem.getProperty('id').split('_');
						
						
						//nie odswiezaj widoku jesli znajduje sie juz w nim jakis kod HTML, dzieki temu odswiezamy tylko raz dany widok na zakladce
						// chyba ze jest to widok koszyka to odswiezaj zawsze po to aby po dodaniu do koszyka zawsze znajdowaly
						//sie tam swieze informacje
						var cont_class = prod_elem.getProperty('class');
						cont_class = cont_class.split(' ');
						
						if (controller == 'orders' && action == 'history') {
							cont_class[1] = "refresh";
						}							
						
						if (cont_class[1] == "refresh") {
							prod_elem.setProperty('class', 'product_list');
						}						
						
						if (prod_elem.get('html').trim() == '' || (item_class[0] == 'cart' && item_class[1] == 'show' && prod_elem.get('html').trim() != '' && prod_elem.get('html').trim() != '&nbsp;') || cont_class[1] == "refresh" /*|| (item_class[0] == 'orders' && item_class[1] == 'history')*/) {
							// id, controller, update element
							this.request_view(item_class[0], item_class[1], item_class[2], page, prod_elem);
						} else {							
							this.product.fadeCoverOut();
						}

						this.writeCookie(ind);							
						
					}.bind(this))

					bookmark.getElements('.close')[0].addEvent('click', function(){						
						
						if ($$('#bookmarks .bookmark').length > 1) {
							var a = bookmark;
							this.close.run(bookmark, this);

							this.activateElement.run([this.drag_bookmarks[this.drag_bookmarks.length - 1], this.drag_bookmarks.length - 1], this);
							var id_to_close = a.getProperty('class').split('_');
							//var id = this.drag_bookmarks[this.drag_bookmarks.length - 1].getProperty('class').split('_');



							this.product.fadeCoverOut();
							$('product_loader').setStyle('visibility', 'hidden');//pokaz loader									
							this.product.remove(controller, action, id);

							this.product.activate(this.drag_bookmarks.length - 1);
							
							var prod_elem = $('view_content').getChildren()[this.drag_bookmarks.length - 1];

							var item_class = prod_elem.getProperty('id').split('_');							
							
							//nie odswiezaj widoku jesli znajduje sie juz w nim jakis kod HTML, dzieki temu odswiezamy tylko raz dany widok na zakladce
							// chyba ze jest to widok koszyka to odswiezaj zawsze po to aby po dodaniu do koszyka zawsze znajdowaly
							//sie tam swieze informacje
							var cont_class = prod_elem.getProperty('class');
							cont_class = cont_class.split(' ');
							if (cont_class[1] == "refresh") {
								prod_elem.setProperty('class', 'product_list');
							}		
						
							if (prod_elem.get('html').trim() == '' || (item_class[0] == 'cart' && item_class[1] == 'show') || cont_class[1] == "refresh" /*|| (item_class[0] == 'orders' && item_class[1] == 'history')*/) {
								var pg = this.product.findPage (item_class);
								this.request_view(item_class[0], item_class[1],  item_class[2], pg, prod_elem);
							}
							
							this.writeCookie(this.drag_bookmarks.length - 1);
						}
					}.bind(this));
					
					//deactivate last actived element
					this.deactivateElements.run([],this);
					this.writeCookie(this.active);	
					this.createTransition.run([],this);

					this.qtn++;
					this.current_scroll = this.qtn;

				}
				
				this.writeBookmarkCookie.pass([controller,action,id,page,this.qtn, title]).run();
			} else {

				this.activate_page.run(["bookmark " + controller, action, id], this);
				var prod_elem = $('view_content').getChildren()[this.active];

				if (prod_elem.get('html').trim() == '') {
					this.request_view(controller, action, id, page, prod_elem);
				}
			}		

			return bookmark;
	},	
	
	close: function (id) {
		this.closeBookmark(id);

	},	
	
	closeBookmark: function (el) {

		var ind = '';
		var all_bookmarks = $$('#bookmarks .bookmark');
		all_bookmarks.each (function(other, j) {
			if (other == el) {		
				ind = j;
			}		
		});

		this.show_outbookmarks(all_bookmarks, this.b1);				
		this.show_outbookmarks(all_bookmarks, this.b2);
		this.show_outbookmarks(all_bookmarks, this.b3);				
		this.show_outbookmarks(all_bookmarks, this.b4);				
		
		//usun zakladke z tablicy oraz z tablicy animacji		
		this.drag_bookmarks.each (function (item, i) {
			if (item  == el) {
				this.drag_bookmarks[i] = null	
				this.fx_move[i] = null	
				this.fx_all_move[i] = null
			}
		}.bind (this))
		
		this.drag_bookmarks =		this.drag_bookmarks.clean();										
		this.fx_move = this.fx_move.clean();
		this.fx_all_move = this.fx_all_move.clean();
		
		el.destroy();	

		total_width = 0;
		elem = []; //arrays of center element
		this.drag_bookmarks.each(function(item, i){
			elem[i] = (item.getFirst('.center_off')) ? item.getFirst('.center_off') : item.getFirst('.center_on');
			total_width += item.getStyle('width').toInt();
			
			//anuluj przesuwanie elelemntow
			if ($defined(this.fx_move[i])) {
				this.fx_move[i].cancel();
			}
		}.bind(this));		
		
		if (this.qtn - 1 <= this.max_resizing) {
			this.left_arrow.setStyle('visibility', 'hidden');
			this.right_arrow.setStyle('visibility', 'hidden');	
		}
		

		if (this.qtn > this.max_without_resizing) {
			

			//resize all bookmarks
			if (this.qtn <= this.max_resizing) {
			
				fx_all_resize = new Fx.Elements(elem, {
					transition: Fx.Transitions.Quart.easeOut,
					duration: 500,
					onComplete: function () {
						$$('#bookmarks .bookmark').each (function (item, i) {
							item.setStyle('width', this.single_width + 36)
						})

					}					
				})

				var available_width = this.max_width - ((this.left_offset) * (this.qtn + 3));
				this.single_width = available_width / (this.qtn - 1);
				
				obj = {};
				
				elem.each(function(item, i){
					obj[i] = {
						'width': this.single_width
					}
				}.bind(this));
				
				//skaluj zakladki
				fx_all_resize.start(obj);

				
				//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
				$$('#bookmarks .bookmark').each(function(item, i){
					this.fx_all_move[i].cancel();
					this.fx_all_move[i] = new Fx.Tween(item, {
						transition: Fx.Transitions.Quart.easeOut,
						duration: 500
					})

					new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
					this.fx_all_move[i].start('left', new_left_offset);
					this.all_left[i] = new_left_offset.toInt();
				}.bind(this));
				

				
			} else {

				var available_width = this.max_width - ((this.left_offset) * (10));
				this.single_width = available_width / this.max_resizing;

				fx_all_resize = new Fx.Elements(elem, {
					transition: Fx.Transitions.Quart.easeOut,
					duration: 500,
					onComplete: function () {
						$$('#bookmarks .bookmark').each (function (item, i) {
							item.setStyle('width', this.single_width + 36)
						})

					}
				})


				obj = {};
				
				elem.each(function(item, i){
					obj[i] = {
						'width': this.single_width
					}
				}.bind(this));
				//skaluj zakladki
				fx_all_resize.start(obj);			
			
				var all_bookmarks = $$('#bookmarks .bookmark');
				
				
				var scroller_width = Math.ceil(all_bookmarks.length * (this.single_width + this.left_offset) + this.left_offset);
				if (scroller_width > 610) {
					this.bookmark_scroller.setStyle('width', scroller_width);
				}			

				//anuliuj przsuwanie zwiazane z pzreciaganiem zakladki 
				$$('#bookmarks .bookmark').each (function (item, i) {
					this.fx_all_move[i].cancel();
					this.fx_all_move[i] = new Fx.Tween (item, {
						transition: Fx.Transitions.Quart.easeOut,
						duration: 500										
					})
					item.setStyle('width', this.single_width + 40)		
					new_left_offset = (this.single_width + this.left_offset.toInt()) * i;
					this.fx_all_move[i].start('left', new_left_offset);
					this.all_left [i]= new_left_offset.toInt();		
				}.bind(this));					
				
				var scroller_width_standard = this.max_resizing * (this.single_width + this.left_offset) + this.left_offset;
				//this.scroll_fx.start(scroller_width - scroller_width_standard , 0)	
				if (this.qtn == this.current_scroll) {
					this.bookmark_scroller.tween('left', -(scroller_width - scroller_width_standard))
					this.current_scroll--;	
				}
			}		
		
			
		} else {
			this.single_width = 148;
			
			$$('#bookmarks .bookmark').each (function (item, i) {
				if (this.fx_all_move[i]) {
					this.fx_all_move[i].cancel();
				}
				this.fx_all_move[i] = new Fx.Tween (item, {
					transition: Fx.Transitions.Quart.easeOut,
					duration: 500										
				})
				new_left_offset = this.single_width * i;
								item.setStyle('width', this.single_width + 36)
				this.fx_all_move[i].start('left', new_left_offset);
				this.all_left [i]= new_left_offset.toInt();	
			}.bind(this));				

	
		}
		
//		var all_bookmarks = $$('#bookmarks .bookmark')

		this.b1 = (this.current_scroll - (this.max_resizing) > 0) ? this.current_scroll - (this.max_resizing - 1) - 2 : -1;
		this.b2 = this.current_scroll < this.qtn ? this.current_scroll : -1;


		this.hide_outbookmarks(all_bookmarks,this.b1)	
		this.hide_outbookmarks(all_bookmarks,this.b2)		

		this.removeBookmarkCookie(ind);
		this.qtn--;		
			
	},
	
	createTransition: function () {
		 $$('#bookmarks .bookmark').each (function (elem, index) {
			if (!this.fx_move[index]) {
				this.fx_move[index] = new Fx.Tween (elem, {
					duration:500,
					fps: 60,	
					link: 'cancel',
					transition: Fx.Transitions.Quart.easeOut											
				})
			}
		}.bind(this))			
	},
	
	//this function set element active (after click on it)
	activateElement: function (el, i) {
		
		
		
		this.last_active = this.active;

		this.active = i;
		el.style.zIndex = '999';
		var classes = ['left_on', 'right_on', 'center_on'];
		el.getElements('.left_off, .right_off, .center_off').each (function (elem, i) {
			elem.setProperty('class', classes[i]);
		});

	},
	
	//this function set element deactive (usually is perform after deactiveElement method)
	deactivateElements: function () {

		var classes = ['left_off', 'right_off', 'center_off'];
		
		this.drag_bookmarks[this.last_active].getElements('.left_on, .right_on, .center_on').each (function (elem, i) {
			elem.setProperty('class', classes[i]);
		});
					
		$$('#bookmarks .bookmark').each (function(other, j) {
			if (this.active != j) {
				other.style.zIndex = this.start_z_index.toInt() + j.toInt();
			}
		}.bind(this));
	},
	
	//it gives you, that you can drage, and move element. after when you drop bookmark his z-element is set on 100
	set_draggable: function (el) {
		var drag = new Drag.Move (el, {
			container: this.drag_cont,
			
			onStart: function (el) {
				var index_el = $$('#bookmarks .bookmark').indexOf(el)
				var a = this.drag_bookmarks.indexOf(el);
				this.current_dragged_left =	this.all_left[index_el];
				if ($defined(this.fx_move[a])) {
					this.fx_move[a].cancel();
				}
				this.fx_all_move[index_el].cancel()


			}.bind(this),
			
			onDrag: function (el) {

				var left = -(this.current_dragged_left - el.getStyle('left').toInt());

				if (left > (this.single_width / 2)) { //w prawo
				
					//get next element, which will be moved
					var next_el = '';

					if (next_el = el.getNext()) {
						var index_el = $$('#bookmarks .bookmark').indexOf(next_el);
						var a = this.drag_bookmarks.indexOf(next_el);

						this.fx_move[a].start('left', this.all_left[index_el - 1]);

						this.fx_all_move[index_el].cancel();
						var el_left = this.all_left[index_el];
						
						//perform change animation	
						this.changeOrder.run([el, next_el, el_left, 'next'], this);
						
					}
				}
				
				if (left < -(this.single_width /2)) { //w lewo
					var prev_el = el.getPrevious();
					var index_el = $$('#bookmarks .bookmark').indexOf(prev_el);

					var a = this.drag_bookmarks.indexOf(prev_el);				
					this.fx_all_move[index_el].cancel();	
					this.fx_move[a].start('left', this.all_left[index_el + 1]);
				
					var el_left = this.all_left[index_el];
					this.changeOrder.run([el, prev_el, el_left, 'prev'], this); 

				} 
			}.bind(this),
			
			onDrop: function (el) {
				var index_el =  this.drag_bookmarks.indexOf(el);				
				this.fx_move[index_el].start('left', this.current_dragged_left);

				$$('#bookmarks .bookmark').each (function(other, j) {
					if (this.active != j) {
						other.style.zIndex = this.start_z_index.toInt() + j.toInt();
					}
				}.bind(this));
		
				var ind = '';
				$$('#bookmarks .bookmark').each (function(other, j) {
					if (other == el) {		
						ind = j;
					}
				})						
				this.writeCookie(ind);			
		
			}.bind(this),
			
			onComplete: function (el) {
				el.style.zIndex = '100';
			}.bind(this)				 
		});
	},
	
	changeOrder: function (el, elem_to_change, el_left, direction) {
		if ($defined(el_left)) {
			this.current_dragged_left = el_left;
			
			if (direction == "next") {
				elem_to_change.inject(el, 'before');
				elem_to_change.style.zIndex = elem_to_change.style.zIndex - 1;
			} else {
				elem_to_change.inject(el, 'after');
				elem_to_change.style.zIndex = elem_to_change.style.zIndex.toInt()  + 1;
			}	 			
		}
		
		var ind = '';
		var ind2 = '';
		$$('#bookmarks .bookmark').each (function(other, j) {
			if (other == el) {		
				ind = j;
			}
			if (other == elem_to_change) {
				ind2 = j;
			}
		})
		this.swap.pass([ind, ind2]).run();

	},
	

	
	//funkcja aktywuje zakladke oraz ustawia odpowiedni widowk strony
	//
	//w momencie wywolania metody styl display jest ustawiony na block dla danego widoku produktu/listy produktow
	//oraz display: none dla wszystkich innych widokow
	activate_page: function (controller, action, id) {

		var indx;
		var elem_indx;
		
		this.drag_bookmarks.each (function (item, i) {
			var item_class = item.getProperty('class').split('_');

			if (controller == item_class[0] && action == item_class[1] && id == item_class[2]) {
				indx = i;
				elem_indx = item;
			}
		});						
		this.writeCookie(indx); 		

		if (this.active != indx) {
			this.activateElement.run([elem_indx,indx], this);
			this.deactivateElements.run([], this);
			this.product.hideAll ()

			this.product.activate.pass(indx).run();
		}		
	},
	
	writeCookie: function (index) {	
		Cookie.write ('active', index, {duration: 0, path: '/'});
	},
	
	
	historyWrite: function (controller, action, id, page, prior, title) {
	
	/*	var tmp = '{"controller" : "' + controller + '", "action": "' + action  + '", "title": "' + title + '" '; //, "id": "'+ id +'", "page": "' + page + '", "prior": "' + prior +'" }'
		if ($defined(id)) {
			tmp = tmp + ', "id": "'+ id +'"';
		}
		
		if ($defined(page)) {
			tmp = tmp + ', "page": "' + page + '"';
		}
		if ($defined(prior)) {
			tmp = tmp + ', "prior": "' + prior +'"';
		}
		
		tmp += '}';

		tmp = JSON.decode(tmp)	
		
		var t = Cookie.read ('bookmarks');
		t = eval('(' + t + ')');		

		try {
			//	t.root.each(function(item, i){
					//if (i == t.root.length - 1) {
					//	if (item.id == id && item.controller == controller && item.action == action) {
						//	exists = true;
					//		console.log (i);
					//		console.log (t.root.length);
					//	}
				//	}
				//})

		//	if (!exists) {
				t.root.include(tmp);
			//}
		} catch (e) {}		

			var t_cookie = JSON.encode(t);
			
			
			Cookie.write('bookmarks_history', t_cookie, {
				duration: 0,
				path: '/'
			})	*/
	
	},	
	
	history_back: function () {
		/*var t = Cookie.read ('bookmarks_history');		
		t = eval('(' + t + ')');
		
		if (t) {
			controller = t.root[t.root.length - 1].controller;
			action = t.root[t.root.length - 1].action;
			id = t.root[t.root.length - 1].id;
			page = t.root[t.root.length - 1].page;
			title = t.root[t.root.length - 1].title;
		}
					
		try {

			while (t.root[t.root.length - 1].controller == controller && t.root[t.root.length - 1].action == action && t.root[t.root.length - 1].id == id && page == t.root[t.root.length - 1].page) {		
				removeByIndex(t.root, t.root.length - 1);
			}
		controller = t.root[t.root.length-1].controller;
		action =  t.root[t.root.length-1].action;
		id =  t.root[t.root.length-1].id;
		page = t.root[t.root.length-1].page;
		title = t.root[t.root.length-1].title;			
			my_bookmarks.createNew(controller, action, id, page, title )

			var t_cookie = JSON.encode(t);
		} catch (e) {}
		Cookie.write ('bookmarks_history', t_cookie, {duration: 0, path: '/'})	*/	
	},
	
	
	writeBookmarkCookie: function (controller, action, id, page, prior, title) {
		var exists = false;
		var tmp = '{"controller" : "' + controller + '", "action": "' + action  + '", "title": "' + title + '" '; //, "id": "'+ id +'", "page": "' + page + '", "prior": "' + prior +'" }'
		if ($defined(id)) {
			tmp = tmp + ', "id": "'+ id +'"';
		}
		
		if ($defined(page)) {
			tmp = tmp + ', "page": "' + page + '"';
		}
		if ($defined(prior)) {
			tmp = tmp + ', "prior": "' + prior +'"';
		}
		
		tmp += '}';

		tmp = JSON.decode(tmp)
		
		var t = Cookie.read ('bookmarks');
		t = eval('(' + t + ')');
		

		try {
			
				t.root.each(function(item, i){
					if (item.id == id && item.controller == controller && item.action == action) {
						exists = true;
					}
				})

			if (!exists) {
				t.root.include(tmp);
			}
		} catch (e) {}		

			var t_cookie = JSON.encode(t);
			
			
			Cookie.write('bookmarks', t_cookie, {
				duration: 0,
				path: '/'
			})

		
	},
	
	removeBookmarkCookie: function (ind) {

		var t = Cookie.read ('bookmarks');
		t = eval('(' + t + ')');
		
		try {
			removeByIndex (t.root, ind);
			var t_cookie = JSON.encode(t);
		} catch (e) {}
		Cookie.write ('bookmarks', t_cookie, {duration: 0, path: '/'})
	},
	
	readBookmarkCookie: function () {
		return new Hash.Cookie ('bookmarks');
	},
	
	swap: function (index1, index2) {
		var t = Cookie.read ('bookmarks');
		t = eval('(' + t + ')');
		
		var tmp = t.root[index2]
		t.root[index2] = t.root[index1];
		t.root[index1] = tmp;
		
		var t_cookie = JSON.encode(t);
		Cookie.write ('bookmarks', t_cookie, {duration: 0, path: '/'})
	},
	
	request_view: function (controller, action, id, page, prod_cont, prior) {
		var url_address = controller +'/' + action;
		if ($defined(id) && id != "undefined") {
			url_address = url_address + '/' + id;
		}
		if ($defined(page) && page != "undefined") {
			url_address = url_address + '/' + page;
		}		

		new Request.HTML({
					url: url_address,
					method: 'post',
					link: 'cancel',
					update: ($(controller + '_' + action + '_' + id)) ? $(controller + '_' + action + '_' + id) : '',
					onRequest: function(){
			/* pokaz przykrywke, jest to bialy div o tle #ffffff o takich samych rozmiarach jak konterner glowny (id="contener")
			 * dzieki temu najpierw przykrywa widoku produktu, pojawia sie na nim loader, a pozniej animacyjnie zmieniamy jego
			 * alfa na 0. Jesli zmienialibysmy przezroczystosc calego widoku produktow z kodem html to prawdopodobnie bardzo wolno
			 * by to chodzilo. */
						if ($(controller + '_' + action + '_' + id)) {	
							$(controller + '_' + action + '_' + id).set('html', '&nbsp;');
							this.product.fadeCoverIn()
							prod_cont.setStyle('display', 'block')
							$('product_loader').setStyle('visibility', 'visible');//pokaz loader
						}
					}.bind(this),
					
					onSuccess: function(responseTree, responseElements, responseHTML){
			/* samo ukrycie wszystkich elementow podczas nowego elementu (widoku) nie wystarczy. W momencie kiedy uzytkownik
			/  szybko klikal zdarzalo sie ze dwa widoki na raz bylo aktywne. Dlatego dla bezpieczenstwa po wykonaniu zadania jeszcze raz
			/  staramy sie ukryc wszystkie widoki produktow */
						var tmp_bookmark;
						if (tmp_bookmark = $(controller + '_' + action + '_' + id)) {
							//var display = true;
						//	$$('.product_list').each (function (item, a) {

							//	if (item.getStyle('display') == "block" && item != tmp_bookmark) {
							//			display = false;	
							//	}
							//})
								
							//if (display) {
							
								//this.product.hideAll();

								//prod_cont.setStyle('visibility', 'hidden');

		
								this.product.fadeCoverOut();
								$('product_loader').setStyle('visibility', 'hidden');//ukryj loader
							//	prod_cont.setStyle('visibility', 'visible');
						//	}
						} else {

							this.product.fadeCoverOut();
							$('product_loader').setStyle('visibility', 'hidden');//pokaz loader							
						}
					}.bind(this)
				}).send()
		
	},
	
	cookie_move_active_to_end: function (controller, action, id) {
		var t = Cookie.read ('bookmarks');
		var bookmarks = JSON.decode (t);

		try {
			var last_ind = bookmarks.root.length - 1;
			var ind_swap = '';
			bookmarks.root.each (function (item, i) {
				if (controller == item.controller && action == item.action && id == item.id) {
					ind_swap = i;
				}
			})
			
			//swap elements
			var tmp = bookmarks.root[ind_swap];
			bookmarks.root[ind_swap] = bookmarks.root[last_ind];
			bookmarks.root[last_ind] = tmp;
		} catch (e) {}
		var t_cookie = JSON.encode(bookmarks);
		Cookie.write ('bookmarks', t_cookie, {duration: 0, path: '/'})
	},
	
	showHint: function (title) {
		
		var width = decode(title).length * 7;
		this.hint_fx2.cancel();
		this.hint.set('html', decode(title));
		this.hint.setStyle('width', width);		
		
		this.hint_fx.onComplete = function () {
		}
		this.hint.setStyle('visibility', 'visible');
		this.hint_fx.start('opacity', 0.8);
	},

	hideHint: function () {
		
		this.hint_fx.onComplete = function () {
			this.hint.setStyle('visibility', 'hidden');
		}.bind(this)
		
		this.hint_fx.cancel();
		this.hint_fx2.start('opacity', 0)
	},	
	
	scroll_left: function () {
		var scroller_width; 
		var offset_sub = 0;

		if (this.current_scroll > this.max_resizing) {
			var all_bookmarks = $$('#bookmarks .bookmark');
			this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b3])				
			this.show_outbookmarks(all_bookmarks, this.b4);
			this.show_outbookmarks(all_bookmarks, this.b1)			
			this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b2]);			

			if (this.current_scroll - this.scroll_offset >= this.max_resizing) {
				scroller_width = Math.ceil((this.current_scroll - this.scroll_offset) * (this.single_width + this.left_offset) + this.left_offset);
				offset_sub = this.scroll_offset;
			} else {
				for (var k = 1; k <= this.scroll_offset; k++) {
					if (this.current_scroll - this.scroll_offset + k >= this.max_resizing) {
						scroller_width = Math.ceil((this.current_scroll -  this.scroll_offset + k) * (this.single_width + this.left_offset) + this.left_offset);
						offset_sub = k
				
						break;
					}	
								
				}
				
			}	

			var scroller_width_standard = this.max_resizing * (this.single_width + this.left_offset) + this.left_offset;
			this.bookmark_scroller.set('tween', {
				onComplete: function () {
					this.hide_outbookmarks(all_bookmarks, this.b3)
				}.bind(this)	
			}).tween('left', -(scroller_width - scroller_width_standard))
			this.current_scroll -= offset_sub;
			
			this.b3 = (this.current_scroll >= this.max_resizing) ? this.current_scroll : -1;

			this.b4 = (this.current_scroll - (this.max_resizing) > 0) ? this.current_scroll - (this.max_resizing - 1) - offset_sub : -1;			

			this.hide_outbookmarks(all_bookmarks, this.b4)				
		
		}

	},
	
	scroll_right: function () {
		var scroller_width; 
		var offset_sub = 0;



		if (this.current_scroll < this.qtn) {
			var all_bookmarks = $$('#bookmarks .bookmark');
			this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b1])				
			this.show_outbookmarks(all_bookmarks, this.b2);
			this.show_outbookmarks(all_bookmarks, this.b3)				
			this.show_outbookmarks.delay(100, this, [all_bookmarks, this.b4]);			
			
			if (this.current_scroll + this.scroll_offset <= this.qtn) {
				scroller_width = Math.ceil((this.current_scroll + this.scroll_offset) * (this.single_width + this.left_offset) + this.left_offset);
				offset_sub = this.scroll_offset;
			
			} else {

				for (var k = 1; k <= this.scroll_offset; k++) {
					if (this.current_scroll + this.scroll_offset - k >= this.qtn) {
						scroller_width = Math.ceil((this.current_scroll +  this.scroll_offset - k) * (this.single_width + this.left_offset) + this.left_offset);

						offset_sub = k
						break;
					}				
				}
					
			}
						
			var scroller_width_standard = this.max_resizing * (this.single_width + this.left_offset) + this.left_offset;
			this.bookmark_scroller.set('tween', {
				onComplete: function () {
					this.hide_outbookmarks(all_bookmarks, this.b1)
				}.bind(this)
			}).tween('left', -(scroller_width - scroller_width_standard))		
			
			this.current_scroll += offset_sub;
	
			this.b1 = (this.current_scroll - (this.max_resizing) > 0) ? this.current_scroll - (this.max_resizing - 1) - 2 : -1;
			this.b2 = this.current_scroll < this.qtn ? this.current_scroll : -1;

			this.hide_outbookmarks(all_bookmarks,this.b2)
			
		}		
	},
	
	
	hide_outbookmarks: function (bookmarks,b1) {
		bookmarks.each (function (item, i) {
			if (i == b1) {
				item.setStyle('visibility', 'hidden');
			}
		})
	},
	
	show_outbookmarks: function (bookmarks, b2) {
	
		bookmarks.each (function (item, i) {
			if (i == b2) {
				item.setStyle('visibility', 'visible');
			}
		})		
	}

});

function removeByIndex(arrayName,arrayIndex){ 
	 arrayName.splice(arrayIndex,1); 
}

function decode(str) {
     return unescape(str.replace(/\+/g, " "));
}


