$(document).ready(function() {
	function formatItem(row) {
		if(row[2].length==0)
			return ""+row[0];
		else
			return "<img src='/zdjecie/4,"+row[2]+"' height='30' align='left' />"+row[0];
	}
	function formatResult(row) {
		return row[0];
	}
	$("#sinput").autocomplete('/autocomplete.php', { width:450, multiple:false, matchContains:true, formatItem:formatItem, formatResult:formatResult });
	$("#sinput").hover(function() { $(".searcht").fadeOut(); });

	$(".img_powieksz").fancybox({'titleShow':false, 'hideOnContentClick':true, 'transitionIn':'elastic', 'transitionOut':'elastic', 'titleShow':true, 'titlePosition':'over'});		
	$("a[rel=example_group]").fancybox({'transitionIn':'elastic', 'transitionOut':'elastic', 'titlePosition':'over', 'titleFormat':function(title, currentArray, currentIndex, currentOpts) { return '<span id="fancybox-title-over">Zdjęcie ' +  (currentIndex + 1) + ' z ' + currentArray.length + ' | ' + title + '</span>'; }});
	$(".various").fancybox({'width':'70%', 'height':'100%', 'autoScale':false, 'transitionIn':'none', 'transitionOut':'none', 'type':'iframe', 'transitionIn':'elastic', 'transitionOut':'elastic', 'titleShow':true, 'titlePosition':'over'});
});

function showSWF(plik, width, height, bgcolor, flashVars) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+width+'" height="'+height+'" id="'+plik+'">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+plik+'" />');
	document.write('<param name="id" value="'+plik+'" />');
	document.write('<param name="menu" value="false" />');
	if(flashVars.length>0)
		document.write('<param name="flashVars" value="'+flashVars+'"/>');
	document.write('<param name="quality" value="high" />');
	if(bgcolor!=''){
		document.write('<param name="bgcolor" value="'+bgcolor+'" />');
	} else {
		document.write('<param name="wmode" value="transparent" />');
	}
	document.write('<embed src="'+plik+'" quality="high" ');
	if (bgcolor.length>0){
		document.write('bgcolor="'+bgcolor+'"');
	} else {
		document.write('wmode="transparent"');
	}
	if(flashVars.length>0)
		document.write(' flashVars="'+flashVars+'"');
	document.write(' menu="false"');
	document.write(' width="'+width+'" height="'+height+'" name="'+plik+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function ShowYouTubeMovie(id){
	document.write('<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'+id+'&rel=0&color1=0x3a3a3a&color2=0x999999&hl=pl"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+id+'&rel=0&color1=0x3a3a3a&color2=0x999999&hl=pl" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>');
}

function showMail(cz1, cz2){
	document.write('<a href="mailto:'+cz1+'@'+cz2+'">'+cz1+'@'+cz2+'</a>');
}

function Animate(obj, properties, duration, func) {
	
	if ( ! Animate.i ) Animate.i = new Array();
	var nr = Animate.i.length;
	Animate.i[nr] = this;
	
	var pv = {}; // przechowuje aktualnie warto?ci animowanych parametr?w
	var delay = 14; // op?nienie dla setTimeout
	var steps;
	var isIE = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
	var stop = false;
	
	this.start = function () {
		steps = duration / delay;
		for (var x in properties) {
			this.setupAnim(x, properties[x]);
		}
	}
	
	// ustawia startowe warto?ci parametr?w
	this.setupAnim = function (propertyName, values) {
		pv[propertyName] = { 
			startValue: values[0], 
			currentValue: values[0], 
			valueTo: values[1],
			inc: values[0] < values[1],
			step: ((values[1] - values[0]) / steps)
		};
		if (values[2])
			pv[propertyName].sufix = values[2];
		this.anim(propertyName);
	}
	
	// animuje dan? w?a?ciwo??
	this.anim = function (property) {
		if (stop) return;
		if ((pv[property].inc && pv[property].currentValue >= pv[property].valueTo) || ( ! pv[property].inc && pv[property].currentValue <= pv[property].valueTo))
			if (this.finish(property))
				return;	
		if (typeof pv[property] == 'undefined')
			return;
		
		var nextVal = pv[property].currentValue + pv[property].step;
		if ((pv[property].inc && nextVal >= pv[property].valueTo) || ( ! pv[property].inc && nextVal <= pv[property].valueTo))
			pv[property].currentValue = pv[property].valueTo;
		else
			pv[property].currentValue = nextVal;
		if (isIE && property == 'opacity')
			obj.style.filter = 'alpha(opacity=' + (pv[property].currentValue * 100) + ')';
		else	
			obj.style[property] = pv[property].currentValue + (pv[property].sufix ? pv[property].sufix : '');
			
		setTimeout(function () { Animate.i[nr].anim(property); }, delay);
	}
	
	this.getCurrent = function (property) {
		return pv[property].currentValue;
	}
	
	// sprawdza czy zako?czono wszystkie animacje
	this.finish = function (property) {
		delete pv[property];
		if (empty(pv)) {
			this.cb();
			return true;
		}
		return false;
	}
	
	this.stop = function () {
		stop = true;
	}
	
	var empty = function (obj) {
		for (var x in obj)
			return false;
		return true;
	}

	this.cb = function () {
		if (typeof func == 'function')
			func(obj);
	}
}

function Slideshow(id, custom) {
	// konfiguracja domy?lna
	var c = {
		slideClass: 'slide',
		navigationClass: 'navi',
		controllClass: 'controll',
		animationTime: 500, // [ms]
		wait: 5000 // [ms]
	};
	// konfiguracja koniec
	
	if (custom)
		for (var setting in custom)
			if (c[setting])
				c[setting] = custom[setting];	
	if ( ! Slideshow.i ) Slideshow.i = {};
	Slideshow.i[id] = this;
	var slider;
	var slides = [];
	var navi = false;
	var controll = false;
	var controllBtn = {};
	var current = 0;
	var pages = [];
	this.moving = false;
	var autoh = false;
	var controllBtnA = {};
	
	this.setup = function () {
		slider = document.getElementById(id);
	//	slider.onmouseover = this.autoStop();
	//	slider.onmouseout = this.autoStart;
		var tmp = slider.getElementsByTagName('li');
		var cl = ' ' + c.slideClass + ' ';
		var nc = ' ' + c.navigationClass + ' ';
		var cc = ' ' + c.controllClass + ' ';
		for (var i = 0; i < tmp.length; i++) {
			if (tmp[i].className) {
				var tc = ' ' + tmp[i].className + ' ';
				if (tc.indexOf(cl) != -1)
					slides.push(tmp[i]);
				else if (tc.indexOf(nc) != -1)
					navi = tmp[i];
				else if (tc.indexOf(cc) != -1)
					controll = tmp[i];
			}
		}
		this.setupNavigation();
		this.controllSetup();
		this.highlightPage(0, 0);
		this.autoStart(c.wait);
	}
	
	this.autoStart = function (delay) {
		if (autoh) return;
		autoh = setTimeout(function () { Slideshow.i[id].auto(); }, delay);
		this.setControll('play');
	}
	
	this.autoStop = function () {
		if ( ! autoh) return;
		clearTimeout(autoh);
		autoh = false;
		this.setControll('pause');
	}
	
	this.setControll = function (a) {
		controllBtn[a].className += ' active';
		controllBtn[a == 'play' ? 'pause' : 'play'].className = controllBtn[a == 'play' ? 'pause' : 'play'].className.replace('active', '');
	//	this.buttonAnimation(true, true, a);
	//	this.buttonAnimation(false, true, a == 'play' ? 'pause' : 'play');
	}
	
	this.controllSetup = function () {
		controllBtn['play'] = controll.getElementsByTagName('span')[0];
		controllBtn['pause'] = controll.getElementsByTagName('span')[1];
	}
	
	this.buttonAnimation = function (a, dir, act) {
		if (a) {
			controllBtnA[act] = new Animate(controllBtn[act], { opacity: (dir ? [1, 0.1] : [0.1, 1]) }, 800, function () {
				Slideshow.i[id].buttonAnimation(true, ! dir, act);
			});
			controllBtnA[act].start();
		} else if (controllBtnA[act]) {
			controllBtnA[act].stop();
			controllBtnA[act] = new Animate(controllBtn[act], { opacity: [controllBtnA[act].getCurrent('opacity'), 1] }, 500);
			controllBtnA[act].start();
		}
	}
	
	this.auto = function () {
		var next;
		if (current + 1 == slides.length)
			next = 0;
		else
			next = current + 1;
		this.go(next);
		autoh = setTimeout(function () { Slideshow.i[id].auto(); }, c.wait);
	}
	
	this.setupNavigation = function () {
		if ( ! navi) return;
		for (var i = 0; i < slides.length; i++) {
			var btn = document.createElement('span');
			btn.className = 'slider_page';
			btn.page = i;
			btn.onclick = function () {
				Slideshow.i[id].go(this.page);
			};
			navi.appendChild(btn);
			pages.push(btn);
		}
	}

	this.go = function (slide_nr) {
		if (current == slide_nr || this.moving) return;
		this.moving = true;
		slides[slide_nr].style.display = 'block';
		slides[slide_nr].style.left = 0;
		var fadeIn = new Animate(slides[slide_nr], { opacity: [0, 1] }, c.animationTime);
		var fadeOut = new Animate(slides[current], { opacity: [1, 0] }, c.animationTime, function () {
			Slideshow.i[id].moving = false;
		});
		fadeIn.start();
		fadeOut.start();
		this.highlightPage(current, slide_nr);
		current = slide_nr;
	}
	
	this.highlightPage = function (current, slide_nr) {
		pages[current].className = pages[current].className.replace('active', '');
		pages[slide_nr].className += ' active';		
	}
	
	this.setup();
}
