
var webPath = 'http://www.pgn.com.pl/';
var imagesPath = 'http://www.pgn.com.pl/templates/pgn2009/images/';
var htmlPath = 'http://www.pgn.com.pl/templates/pgn2009/html/';
var ajaxTablePath = 'http://www.pgn.com.pl/remote/fishtable.php';

var emailDefault = 'Wpisz swój e-mail...';
var loginDefault = 'Login...';
var passDefault = 'Hasło...';

var loginPopupTimer;


var insertLoader = function(id,height) {
	if (!id) return;
	
	var element = $(id);
	if (!element) return;
	
	if (height && !isNaN(height) && typeof height=='number') element.setStyle('height',height+'px');
	element.setStyle('background','url('+imagesPath+'loader.gif) no-repeat center');
};


var clearLoader = function(id,resetHeight) {
	if (!id) return;
	
	var element = $(id);
	if (!element) return;
	
	element.setStyle('background','none');
	
	if (resetHeight && resetHeight==true) element.setStyle('height','auto');
};


var searchInit = function() {
	
	var text = 'znajdź w serwisie';
	var minLength = 3;
	
	$('q').set('value',text).addEvents({
		'focus': function() {
			if (this.get('value')==text) this.set('value','');
		},
		'blur': function() {
			if (this.get('value')=='') this.set('value',text);
		}
	});
	$('search-b').addEvents({
		'click': function(e) {
			new Event(e).stop();
			
			var q = $('q').get('value').trim();
			
			if (q=='' || q==text) {
				new Fish.Dialog('Błąd!', 'Musisz podać frazę do wyszukania.', {
					type: 'notice'
				});
			} else if (q.length<minLength) {
				new Fish.Dialog('Błąd!', 'Szukana fraza musi się składać przynajmniej z '+minLength+' znaków.', {
					type: 'notice'
				});
			} else {
				$('f-search').submit();
			}
		},
		'focus': function() {
			this.blur();
		}
	});
	
	$('f-search').addEvent('submit', function(e) {
		var q = $('q').get('value').trim();
		if (q=='' || q==text) {
			new Event(e).stop();
			new Fish.Dialog('Błąd!', 'Musisz podać frazę do wyszukania.', {
				type: 'notice'
			});
		}
		if (q.length<minLength) {
			new Event(e).stop();
			new Fish.Dialog('Błąd!', 'Szukana fraza musi się składać przynajmniej z '+minLength+' znaków.', {
				type: 'notice'
			});
		}
	});
};


var closeLoginPopup = function() {
	$('login-popup-bg').destroy();
	$('login-popup-div').destroy();
	$clear(loginPopupTimer);
};



$(window).addEvent('domready',function() {
	
	
	searchInit();
	
	/**
	 * Odpalamy chmurki
	 */
	Fish.Tips();
	
	if ($('slides')) {
		new Request.JSON({
			url: webPath+'ajax/ModuleNews.getStartNews',
			evalResponse: false,
			evalScripts: false,
			secure: false,
			onFailure: function(err) {
			}.bind(this),
			onSuccess: function(r) {
				
				if (r && r.length>0) {
					var length = r.length.toInt();
					
					var ul = new Element('ul').injectInside('slides');
					
					$each(r,function(v,k) {
						
						var li = new Element('li',{
							'id': 'slide-'+(k+1)
						}).injectInside(ul);
						
						new Element('span',{
							'html': v.mn_title
						})
						.addClass('news-title')
						.injectInside(li);
						
						new Element('div',{
							'html': v.mn_short
						})
						.addClass('news-text')
						.injectInside(li);
						
						var a = new Element('a',{
							'class': 'more',
							'text': '',
							'href': webPath+v.mi_id+','+v.mn_id+'/'+v.mi_code+'.html'
						}).injectInside(li);
						
						new Element('img',{
							'src': imagesPath+'more.png',
							'alt': ''
						}).injectInside(a);
						
						a.appendText(' więcej');
					});
					
					var pages = Math.ceil(length/2);
					
					for(i=pages;i>=1;i--) {
						
						new Element('a',{
							'text': i,
							'class': i==1?'sel':'',
							'href': i,
							'events': {
								'click': function(e) {
									new Event(e).stop();
									var id = this.get('href').toInt();
									var pId = 'slide-'+(id+(id-1));
									
									selPage = id+(id-1);
									selPageAnch = id;
									
									if (!$(pId)) return;
									
									new Fx.Scroll('slides',{
										transition: Fx.Transitions.Quad.easeInOut,
										duration: 1000
									}).toElement(pId);
									
									$$('div#pages a').removeClass('sel');
									this.addClass('sel');
								},
								'focus': function() {
									this.blur();
								}
							}
						}).injectInside('pages');
					}
					
					var pages = $$('div#slides ul li').length.toInt();
					var selPage = 1;
					var selPageAnch = 1;
					
					var timer = (function() {
						selPage = selPage+2;
						selPageAnch++;
						
						if (selPage>pages) {
							selPage = 1;
							selPageAnch = 1;
						}
						
						var pId = 'slide-'+selPage;
						new Fx.Scroll('slides',{
							transition: Fx.Transitions.Quad.easeInOut,
							duration: 2000
						}).toElement(pId);
						
						$$('div#pages a').removeClass('sel');
						$$('div#pages a').each(function(el) {
							if (el.get('href').toInt()==selPageAnch) el.addClass('sel');
						});
					}).periodical(10000);
				}
			}
		}).post();
	}
	
	$$('a.pop-login').addEvents({
		'click': function(e) {
			new Event(e).stop();
			
			var bg = new Element('div',{
				'class': 'login-popup-bg',
				'id': 'login-popup-bg',
				'events': {
					'click': function() {
						this.destroy();
						div.destroy();
						$clear(loginPopupTimer);
					}
				}
			}).injectInside(document.body);
			
			var div = new Element('div',{
				'class': 'login-popup',
				'id': 'login-popup-div'
			}).injectInside(document.body);
			
			var img = new Element('img',{
				'src': imagesPath+'login-popup-bg.png'
			}).injectInside(div);
			
			var con = new Element('div',{
				'class': 'con'
			}).injectInside(div);

			/*
			alert('http://localhost/pgn/templates/pgn2009/html/box-partner-popup-login.html');
			
			new Request.HTML({
				evelScripts: false,
				evalResponse: false,
				url: 'http://localhost/pgn/templates/pgn2009/html/box-partner-popup-login.html',
				method: 'GET',
				onFailure: function(err) {
					alert(err);
				},
				onSuccess: function(t,e,h) {
					alert(h);
					//con.set('html', h);
				}
			}).post();
			*/

		   var form = '<h3 class="header">STREFA PARTNERA</h3><div class="line"></div>Login&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hasło<form id="login" action="http://pgn.isp.net.pl/index.php?s=l" method="post"><input type="hidden" name="referer" value="" /><input type="text" class="i-login" name="username" value="" />&nbsp;<input type="password" class="i-login" name="password" value="" /><br /><input type="submit" class="i-submit" value="Zaloguj" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="i-submit" value="Zamknij" onclick="closeLoginPopup()" /></form>';
		   con.set('html', form);

			var page = getSize();
			var sc = getScrollSize();
			var scroll = getScroll();
			
			bg.setStyles({
				'height': sc.y.toInt()+'px',
				'width': sc.x.toInt()+'px'
			});
			
			div.setStyles({
				'top': scroll.y+(page.y/2-(div.getStyle('height').toInt()/2))+'px',
				'left': scroll.x+(sc.x/2-(div.getStyle('width').toInt()/2))+'px'
			});
			
			loginPopupTimer = (function() {

				var page = getSize();
				var sc = getScrollSize();
				var scroll = getScroll();
				
				var top = scroll.y+(page.y/2-(div.getStyle('height').toInt()/2));
				var left = scroll.x+sc.x/2-(div.getStyle('width').toInt()/2);
				
				new Fx.Morph(div, {
					duration: 500,
					transition: Fx.Transitions.Cubic.easeInOut
				}).start({
					'top': top+'px',
					'left': left+'px'
				});
			}).periodical(600);
		},
		'focus': function() {
			this.blur();
		}
	});
	
	
	/*
	$$('div#pages a').each(function(el) {
		el.addEvents({
			'click': function(e) {
				new Event(e).stop();
				var id = this.get('href').toInt();
				var pId = 'slide-'+(id+(id-1));
				
				if (!$(pId)) return;
				
				var scroll = new Fx.Scroll('slides');
				scroll.toElement(pId);
				
				$$('div#pages a').removeClass('sel');
				this.addClass('sel');
			},
			'focus': function() {
				this.blur();
			}
		});
	});
	*/
	
	/*
	CFInstall.check({
		preventPrompt: true,
		onmissing: function( ){
			new Fish.Dialog('Internet Explorer!','Używasz przeglądarki Interner Explorer. JEBNIJ SIĘ!',{
				type: 'confirm', 
				text: 'Zamknij',
				confirm: {
					text: 'Instaluj',
					action: function() {
						window.open("http://www.google.com/chromeframe/eula.html","GoogleFrame");
					}
				}
			});
		}
	});
	*/
});



