var photos_images = new Array();

function photos_ie() {
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	return navigator.appName == 'Microsoft Internet Explorer' && version < 7
}
function photos_next() {
	var im = document.getElementById('photos_second_right')

	if (im['photos_nr']<0) {
		this.style.display = 'none'
	}
	else {
		photos_switch(3)
	}
}
function photos_previous() {
	var im = document.getElementById('photos_second_left')

	if (im['photos_nr']<0) {
		this.style.display = 'none'
	}
	else {
		photos_switch(1)
	}
}
function photos_show() {
	var p = document.getElementById('photos_first_img');
	var foto = p['photos_nr']
	
	if (foto<0 || foto>photos.length) {
		return
	}
	else if (photos_images[foto].complete || photos_images[foto].width>0) {
		var wid = photos_images[foto].width+100
		var hei = photos_images[foto].height+100
		
		if (wid<=0 || hei<=0) {
			return
		}
		
		if (wid+20>screen.width) {
			var ratio = hei/wid
				
			wid = screen.width-20
			hei = wid*ratio
		}
		else if (hei+20>screen.height) {
			var ratio = wid/hei
				
			hei = screen.height-20
			wid = hei*ratio
		}
		var win = window.open("","photos_"+p['photos_nr'],'width=' + wid + ',height=' + hei + ',resizable=0,scrollbars=no,menubar=no,left=0,top=0' );
		win.document.write("<html><head><title>Karol Kępka - Artystyczna fotografia ślubna</title></head><body style='margin: 0;padding: 0;'>");
		win.document.write("<img src='"+p.src+"' alt='' onclick='window.close()' style='width:"+wid+"px; height:"+hei+"px; cursor: pointer;' />");
		win.document.write("</body></html>");
	}
	else {
		window.open(p.src, '')
	}
}
function photos_load(nr) {
	var img = new Array();
	
	img[0] = document.getElementById('photos_third_left');
	img[1] = document.getElementById('photos_second_left');
	img[2] = document.getElementById('photos_first_img');
	img[3] = document.getElementById('photos_second_right');
	img[4] = document.getElementById('photos_third_right');
	
	var foto = img[nr]['photos_nr']
	
	if (foto<0 || foto>=photos.length || photos_images[foto] == null) {
		clearInterval(img[nr]['photos_load'])
	}
	else if (photos_images[foto].complete == true) {
		clearInterval(img[nr]['photos_load'])
		img[nr].src = photos_images[foto].src
		document.getElementById('photos_first').style.width = img[2].offsetWidth + 'px';
	}	
}

function photos_switch(nr) {
	var img = new Array();
	
	img[0] = document.getElementById('photos_third_left');
	img[1] = document.getElementById('photos_second_left');
	img[2] = document.getElementById('photos_first_img');
	img[3] = document.getElementById('photos_second_right');
	img[4] = document.getElementById('photos_third_right');
	
	//SETTING NUMBERS
	var foto = img[nr]['photos_nr']-2;
		
	for (i=0;i<5;i++) {
		clearInterval(img[i]['photos_load'])
	
		if (foto<0 || foto>=photos.length) {
			img[i]['photos_nr'] = -1
			img[i].style.display = 'none'
		}
		else {
			img[i]['photos_nr'] = foto
			img[i].style.display = 'block'
			img[i].title = photos[foto][1]
		}
		foto++
	}
	
	//LOADING IMAGES
	foto = img[2]['photos_nr']-3;
	
	if (foto>=0 && photos_images[foto] == null) {
		photos_images[foto] = new Image()
		photos_images[foto].src = photos[foto][0]
	}
	foto++
	
	for (i=0;i<5;i++) {
		if (foto>=0 && foto<photos.length) {
			if (photos_images[foto] == null) {
				photos_images[foto] = new Image()
				photos_images[foto].src = photos[foto][0]
			}
			if (photos_images[foto].complete == true) {
				img[i].src = photos_images[foto].src
				document.getElementById('photos_first').style.width = img[2].offsetWidth + 'px';
			}
			else {
				img[i].src = photos_loading.src
				img[i]['photos_load'] = setInterval("photos_load("+i+")", 1000);
			}
		}
		foto++
	}
	
	//OTHERS
	if (foto<photos.length && photos_images[foto] == null) {
		photos_images[foto] = new Image()
		photos_images[foto].src = photos[foto][0]
	}
	if (!photos_ie() && img[1]['photos_nr']<0) {
		document.getElementById('photos_previous').style.display = 'none'
	}
	else {
		document.getElementById('photos_previous').style.display = 'block'
	}
	if (!photos_ie() && img[3]['photos_nr']<0) {
		document.getElementById('photos_next').style.display = 'none'
	}
	else {
		document.getElementById('photos_next').style.display = 'block'
	}
}
function photos_build(nr) {
	for (i=0;i<photos.length;i++) {
		photos_images[i] = null;
	}

	var img = new Array();
	
	img[0] = document.getElementById('photos_third_left');
	img[1] = document.getElementById('photos_second_left');
	img[2] = document.getElementById('photos_first_img');
	img[3] = document.getElementById('photos_second_right');
	img[4] = document.getElementById('photos_third_right');
	
	img[0].onclick = function() { photos_switch(0) }
	img[1].onclick = function() { photos_switch(1) }
	img[2].onclick = photos_show
	img[3].onclick = function() { photos_switch(3) }
	img[4].onclick = function() { photos_switch(4) }
	
	for (j=0;j<5;j++) {
		img[j]['photos_load'] = null;
		img[j]['photos_nr'] = -1;
	}
	document.getElementById('photos_previous').onclick = photos_previous
	document.getElementById('photos_next').onclick = photos_next

	if (nr<0 || nr>=photos.length) {
		nr = 0
	}
	img[2]['photos_nr'] = nr
	photos_switch(2)
}

//SLIDESHOW
var photos_slide_ac
var photos_slide_wait

function photos_slide() {
	var p = document.getElementById('photos_second_right');
	var nr = p['photos_nr']
	
	if (nr<0 || nr>=photos.length || photos_images[nr] == null) {
		photos_slide_stop()
		return false;
	}
	else if (photos_images[nr].complete == true || photos_slide_wait>2) {
		photos_slide_wait = 0
		photos_switch(3);
	}
	else {
		photos_slide_wait++
	}
}
function photos_slide_stop() {
	clearInterval(photos_slide_ac)
}
function photos_slide_play(nr) {
	if (nr<0 || nr>=photos.length) {
		return false;
	}
	/*var p = document.getElementById('photos_first_img');
	
	p['photos_nr'] = nr
	photos_switch(2)*/
	
	photos_slide_wait = 0
	photos_slide_ac = setInterval("photos_slide()", photos_interval*1000)
}