/**
 * @author Arek Macak, 
 * @author jeden z trików stosowanych w galerii został zaporzyczone z biblioteki lytebox 
 * 
 * 
 * @version 1.1
 * @changelog
 * 1.1 - uzupelnienie o przewijanie zdjec.
 * 1.0 - wersja nie pozwalajaca przewija sie miedzy zdjeciami
 * @todo
 * - dodac preloadera dla klasy zdjec "photog"
 * - dodac przemieszczanie sie miedzy zdjeciem w przod w tyl.
 * - dodac zamykanie galerii po kliknieciu na ramke boczna.
 */
var photo = Class.create({
	autoResize : true,//Automatyczne skalowanie zdjecia w celu pokazania jak najwiekszej fotki bez wychodzenia poza strone.
	bgColor : '#ECDDB6',//HEX, kolor tla polprzezroczystego
	bgOpacity : 0.8,//przezroczystosc - IE6 nie wspiera przezroczystosci !
	descfont : 'bold 12px tahoma;',//font opisu
	icBorder : '3px solid #9E9989',//rodzaj ramki
	preload : true, //czy mam wykorzystywac mechanizm preload do zaladowania nastepnego zdjecia ?
	
	bg : null,
	ic : null,
	icn : null,
	icp : null,
	im : null,
	url : null,
	tlist : null,
	next : -1,
	prev : -1,
	gclass : 'img.photog',
	started : false,
	run : function(){
		this.bg = new Element('div',{id:'pg_a'});
		with(this.bg.style){
			height = parseInt(document.body.clientHeight) + 60 + 'px';
			display = 'none';
		}
		this.bg.setOpacity(this.bgOpacity);
		this.ic = new Element('div',{id:'pg_b'});

		dc = new Element('div');
		with(dc.style){
			bottom = '3px';
			right = '10px';
			position = 'absolute';
			display = 'block';
		}
		dc.innerHTML = '<b>zamknij</b>';
		dc.onclick = function () {
			pg.hide();
			this.parentNode.style.display = 'none';
			return false; 
		}
		this.ic.appendChild(dc);
		document.body.appendChild(this.bg);
		document.body.appendChild(this.ic);
		this.started = true; 
		//wyszukaj wszystkie obiektu z wskazanej klasy
		this.tlist = new Array();
		if(this.preload){
			var t = $$(this.gclass);
			t.each(function(item){
				pg.tlist[pg.tlist.length] = item;
			});
		}
	},
	hide : function(){
		$('pg_a').style.display = 'none';
		$('pg_c').remove();
		$('pg_d').remove();
		if($('pg_cn')){
			$('pg_cn').remove();
		}
		if($('pg_cp')){
			$('pg_cp').remove();
		}
		
	},
	show : function(e){
		var o = Event.element(e);
		this.open(o.getAttribute('longDesc'),o.getAttribute('alt'));
	},
	reopen : function(url,alt){
		this.hide();
		this.open(url,alt);
	},
	open : function(url,alt){
		if(!this.started){
			this.run();
		}
		this.url = url;
		if(this.url == 'undefined'){
			return false;
		}
		this.im = new Image();
		this.im.id = 'pg_c';
		this.ic.appendChild(this.im);
		this.bg.style.display = 'block';
		this.ic.style.display = 'block';
		
		this.desc = new Element('div');
		this.desc.id = 'pg_d';
		this.desc.innerHTML = alt;
		this.desc.style.display = 'none';
		this.desc.style.margin = '0px 50px 0px 0px';
		this.desc.style.font = this.descfont;
		this.desc.appendChild(new Element('div',{className : 'break'}));
		
		
		px = (document.viewport.getDimensions().height - 210)/2 + document.viewport.getScrollOffsets().top;
		if(px < 10){
			px = 10;
		}
		with(this.ic.style){
			width = '400px';
			height = '220px';
			zIndex = 91;
			position = 'absolute';
			left = (document.viewport.getDimensions().width - 220)/2 + document.viewport.getScrollOffsets().left + 'px';
			background = 'url(/kernel/tpl/_gfx/system/loader.gif) no-repeat center center #FFFFFF';
			padding = '10px';
			top = px  + 'px';
			border = this.icBorder;
			display = 'block';
		}
		this.im.style.display = 'none';
		tmp = new Image();
		tmp.onload = function(e){
			var w = tmp.width;
			var h = tmp.height;
			//pg.desc.innerHTML.length
			if (pg.autoResize) {
				var x = document.viewport.getDimensions().width - 150;
				var y = document.viewport.getDimensions().height - 150;
				if (w > x) {
					h = Math.round(h * (x / w));
					w = x; 
					if (h > y) { 
						w = Math.round(w * (y / h));
						h = y; 
					}
				} else if (h > y) { 
					w = Math.round(w * (y / h));
					h = y; 
					if (w > x) {
						h = Math.round(h * (x / w));
						w = x;
					}
				}
			}
			var rows = parseInt(pg.desc.innerHTML.length * 7 / (w - 50));
			//d(rows);
			if(rows > 2){
				pg.ic.setStyle({padding : '10px 10px ' + ((rows - 2) * 24) +'px 10px'});
			}else{
				pg.ic.setStyle({padding : '10px 10px 15px 10px'});
			}
			pg.im.src = pg.url;
			pg.im.style.display = 'block';
			pg.im.style.width = w + 'px';
			var asfre = pg.tlist[0];
			pg.im.width = w;
			pg.im.height = h;
			pg.resizeIC(w,h);
			pg.desc.style.display = 'block';
			tmp.onload = function() {/* zerowanie zawartosci funkcji */};
			if(pg.tlist){
				pg.prev = -1;
				pg.next = -1;
				var i =0; 
				pg.tlist.find(function(o){if(o.getAttribute('longDesc') == pg.url){return true;}i++;});
				if(i > 0){
					pg.prev = i-1;
					pg.icp = new Element('a',{id : 'pg_cp'});
					with(pg.icp.style){
						left = '0px';
						width = w/2 + 13 + 'px';
						height = h + 30 + 'px';
					}
					pg.icp.onclick = function () {
						pg.reopen(pg.tlist[pg.prev].getAttribute('longDesc'),pg.tlist[pg.prev].getAttribute('alt'));
						return false; 
					}
					Event.observe(pg.icp,'mouseover',function(){
						pg.icp.setOpacity(1);
						return false;
					});
					Event.observe(pg.icp,'mouseout',function(){
						pg.icp.setOpacity(0);
						return false;
					});
					pg.icp.setOpacity(0);
					pg.ic.appendChild(pg.icp);
				}
				if(i < (pg.tlist.size() -1)){
					pg.next = i+1;
					pg.icn = new Element('a',{id : 'pg_cn'});
					with(pg.icn.style){
						left = 13 + parseInt(pg.im.style.width)/2 + 'px';
						width = w/2 + 'px';
						height = h + 30 + 'px';
					}
					pg.icn.onclick = function () {
						pg.reopen(pg.tlist[pg.next].getAttribute('longDesc'),pg.tlist[pg.next].getAttribute('alt'));
						return false; 
					}
					Event.observe(pg.icn,'mouseover',function(){
						pg.icn.setOpacity(1);
						return false;
					});
					Event.observe(pg.icn,'mouseout',function(){
						pg.icn.setOpacity(0);
						return false;
					});
					pg.icn.setOpacity(0);
					pg.ic.appendChild(pg.icn);

				}
			}
			
			if(pg.preload){
				
				//doczytanie nowego zdjecia w tle.
				var i = 0;
				pg.tlist.find(function(o){if(o.getAttribute('longDesc') == pg.url){return true;}i++;});
				if(i> -1 && i < (pg.tlist.size()-1)){
					//preload obrazka nastepnego
					var ti = new Image();
					ti.src = pg.tlist[i+1].getAttribute('longDesc');
				}
				if(i > 1){
					//preload obrazka nastepnego
					var ti2 = new Image();
					ti2.src = pg.tlist[i-1].getAttribute('longDesc');
				}
			}
		};
		tmp.src = this.url;
		this.ic.appendChild(this.desc);
		
	},
	resizeIC : function(x,y){
		with(this.ic.style){
			width = x+'px';
			height = y+25+'px';
			left = (document.viewport.getDimensions().width - x - 10)/2 + 'px';
			top = (document.viewport.getDimensions().height - y - 10)/2 + document.viewport.getScrollOffsets().top + 'px';
		}
	}
});
var pg = new photo();
