var scroller;
var scroller2;
var logowanie;

//inicjalizuje wszystkie zdarzenia na stronie
window.addEvent("domready", function (){
	//TOP MENU
	$('top_menu').getElements('a[class=link]').each(function (item) {
		
		item.addEvent('mouseover', function (event) {
			
			var img = item.getFirst('img');
			
			var src = img.src.split('.jpg');
			
		
			if (src[0]) {
				img.src = src[0] + '_over.jpg';
			}
			event.stop();
		})
		
		item.addEvent('mouseout', function (event) {
			var img = item.getFirst('img');
			var src = img.src.split('_over.');
			
			if (src[1]) {
				img.src = src[0] + '.' + src[1];
			}
			event.stop();
		})
	})
	
	//przewijane promocje
	try {	
		scroller = new Scroller();
	} catch (e) {}
	
	try {		
		scroller2 = new Scroller_Cat();
	} catch (e) {}
	
	try {
		logowanie = new Logowanie ();
	} catch (e) {}
	
})


	
var Scroller = new Class ({
	
//------/* Kontruktor */--------//
	initialize: function () {
		this.left_arrow = $('prom_left'); // strzalka w gore
		this.busy = false;
		this.right_arrow = $('prom_right'); // strzaka w dol
		
		this.content_scroll = $('promocje_box'); // to bedzie przewijane
		//this.content_scroll_w = this.content_scroll.getStyle('heig');
		this.ilosc = $$('.prom_prod_box').length;
		var width =  this.ilosc * 176;
		$('promocje_box_outer').setStyle('width', width);
		this.init_arrow();	
	},
	
	init_arrow: function () {
		this.fx_scroll = new Fx.Scroll(this.content_scroll,{
			duration: 600,
			transition: Fx.Transitions.Quad.easeInOut,
			onComplete: function () {
				this.busy = false
			}.bind (this)
		})
		this.fx_scroll.toLeft();
		
		this.left_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_left (176);
		}.bind(this))
		
		this.right_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_right (176);
		}.bind(this))	
		
		//numerki
		this.last_actived = 0;
		this.numbers = $('prom_center').getElements('.number');
		this.numbers.each(function (item, i) {
			item.addEvent('click', function (event) {
				this.fx_scroll.start (i*176);
				if (!this.busy) {
					this.numbers[i].setProperty('class', 'number active');
					this.numbers[this.last_actived].setProperty('class', 'number')
					this.last_actived = i;
				}
				this.busy = true;
				event.stop();
			}.bindWithEvent(this))
		}.bind(this))	
	},
	
	move_left: function (offset) {
		var x = this.content_scroll.getScroll().x;		
		this.fx_scroll.start (x-offset, 0);
				
		if (!this.busy && this.last_actived > 0) {
			this.numbers[this.last_actived].setProperty('class', 'number')
			this.last_actived--;
			this.numbers[this.last_actived].setProperty('class', 'number active');
		}
		
		this.busy = true;
	},
	
	move_right: function (offset) {
		
		var x = this.content_scroll.getScroll().x;
		this.fx_scroll.start (x+offset,0);	

		if (!this.busy && this.last_actived < (this.ilosc - 1)) {
			this.numbers[this.last_actived].setProperty('class', 'number')
			this.last_actived++;
			this.numbers[this.last_actived].setProperty('class', 'number active');
		}
		
		this.busy = true;
	}
})




var Scroller_Cat = new Class ({
	
//------/* Kontruktor */--------//
	initialize: function () {
		this.left_arrow = $('o_left'); // strzalka w gore

		this.right_arrow = $('o_right'); // strzaka w dol
		
		this.content_scroll = $('o_center'); // to bedzie przewijane
		this.ilosc = $('o_center_cont').getElements('.product').length;
		var width =  this.ilosc * 140;
		$('o_center_cont').setStyle('width', width);
		this.init_arrow();	
	},
	
	init_arrow: function () {
		this.fx_scroll = new Fx.Scroll(this.content_scroll,{
			duration: 600,
			transition: Fx.Transitions.Quad.easeInOut
		})
		this.fx_scroll.toLeft();
		
		this.left_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_left (280);
		}.bind(this))
		
		this.right_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_right (280);
		}.bind(this))	

	},
	
	move_left: function (offset) {
		var x = this.content_scroll.getScroll().x;		
		this.fx_scroll.start (x-offset, 0);

	},
	
	move_right: function (offset) {		
		var x = this.content_scroll.getScroll().x;
		this.fx_scroll.start (x+offset,0);
			
		this.busy = true;
	}
})

var Scroller_Familly = new Class ({
	
//------/* Kontruktor */--------//
	initialize: function () {
		this.left_arrow = $('f_left'); // strzalka w gore

		this.right_arrow = $('f_right'); // strzaka w dol
		
		this.content_scroll = $('f_center'); // to bedzie przewijane
		this.ilosc = $('f_center_cont').getElements('.product').length;
		var width =  this.ilosc * 140;
		$('f_center_cont').setStyle('width', width);
		this.init_arrow();	
	},
	
	init_arrow: function () {
		this.fx_scroll = new Fx.Scroll(this.content_scroll,{
			duration: 600,
			transition: Fx.Transitions.Quad.easeInOut
		})
		this.fx_scroll.toLeft();
		
		this.left_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_left (280);
		}.bind(this))
		
		this.right_arrow.addEvent('click', function (event) {
			event = new Event(event).stop();
			this.move_right (280);
		}.bind(this))	

	},
	
	move_left: function (offset) {
		var x = this.content_scroll.getScroll().x;		
		this.fx_scroll.start (x-offset, 0);

	},
	
	move_right: function (offset) {		
		var x = this.content_scroll.getScroll().x;
		this.fx_scroll.start (x+offset,0);
			
		this.busy = true;
	}
})

var Logowanie = new Class ({
	
	initialize: function () {
		this.log_btn = $('logowanie').getFirst('.logowanie');
		this.log_btn_on = $('logowanie').getFirst('.logowanie').getFirst('span');
		this.log_panel = $('panel');
		this.main_panel = $('panel_outer');
		this.log_panel3 = $('panel3');
		
		this.log_panel.fade('hide');
		this.log_panel3.fade('hide');
		this.log_btn_on.fade('hide');
		
		this.log_btn.addEvent('click', function (event) {
			this.toggle_panel();
			event.stop();
		}.bindWithEvent(this))		
		
	},
	

	
	toggle_panel: function () {
		
		var close = function (event) {
			event = (!event) ? window.event : event;
			
			if (!this.matchElements(event.target)) {
				this.toggle_panel();
				document.removeEvent('click', close);
			}
			
			event.stop();
		}.bind(this)
		
		if (this.log_panel.getStyle('opacity') == 0) {
			this.log_panel.fade(1);
			this.log_btn_on.fade(1);
			this.log_panel3.fade(1);
			document.addEvent('click', close);
		} else if (this.log_panel.getStyle('opacity') == 1) {
			this.log_btn_on.fade(0);
			this.log_panel.fade(0);
			this.log_panel3.fade(0);
		}
	},
	
	matchElements: function (target) {
		var body = document.body;
		var find = false;
		
		while (target != body) {
			if (this.main_panel == target) {
				find = true; break;
			}
			
			target = $(target).getParent();
		}
			
		return find;
	}
})

