function externalLinks() {
    if( ! document.getElementsByTagName )
        return;

    var anchors = document.getElementsByTagName( "a" );
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if( anchor.getAttribute( "href" ) && anchor.getAttribute( "rel" ) == "external" )
                anchor.target = "_blank";
        }
}

window.onload = externalLinks;
var current_slide;
var next_slide;
var pauza;

function set_opacity_both (id, opacity) {
	$('s_'+id).setOpacity( opacity );
 }

function set_z_both (id, z) {
	$('s_'+id).setStyle({zIndex: z });
 }
 function get_opacity (id) {
	return $('s_'+id).getOpacity();
 }
function changeopacityOn(id){
	//set_opacity_both (id+'00', 1);
}

function changeopacityOff(id){
	//set_opacity_both (id+'00', 0.7);
}
function change_slide(id_from,id_to) {
	if(id_from != id_to){
		var op_from = 1;
		var op_to = 0;
		if(pauza == 1){
			set_opacity_both(id_from, 0);
			set_opacity_both(id_to, 1);
		}
		else{
			new PeriodicalExecuter( function (pe) {
				if (op_from > 0) {
					op_from = op_from - 0.1;
					set_opacity_both (id_from, op_from);
					if(op_from  >= 0.5){
					}
					op_to = op_to + 0.1;
					set_opacity_both(id_to, op_to);
					if(op_to <= 0.4){
					}
				} else pe.stop();
			}, 0.05 );
		}
		set_z_both (1, 1);
		set_z_both (2, 1);
		set_z_both (3, 1);
		set_z_both (4, 1);
		set_z_both (5, 1);
		set_z_both (id_to, 6);
	}
}
function stopAnimation(x){
	var chosen;
	pauza = 1;
 	if(x != 0){
		for(var j=1; j<=5; j++){
			if(get_opacity(j) >= 0.8){
				chosen = j;	
			}
		}
		//current_slide = chosen;
		next_slide = x;
		change_slide(current_slide, next_slide);
		current_slide = next_slide;
	}
}

function pauzaOut(){
	if(pauza == 2){
		//alert("NOWY INTERWAĹ, nastepna klatka: "+next_slide);
		pauza = 0;
		new PeriodicalExecuter( function (pe) {
				if(pauza == 1) {
					pauza = 2;
					pe.stop();		
					//alert("STOP INTERWAĹU");
				}
				else{
				var prev_slide = current_slide;
				next_slide = current_slide+1;
				if (next_slide > 5) next_slide = 1;
				change_slide(prev_slide,next_slide);
				current_slide = next_slide;
				}
				
		}, 3 );
	}
	else{
		pauza = 0;
	}
}

function slideshow() {
		set_opacity_both (1, 1);
		set_opacity_both (2, 0);
		set_opacity_both (3, 0);
		set_opacity_both (4, 0);
		set_opacity_both (5, 0);
		set_z_both (1, 4);
		set_z_both (2, 3);
		set_z_both (3, 3);
		set_z_both (4, 3);
		set_z_both (5, 3);
		current_slide = 1;
	
		new PeriodicalExecuter( function (pe) {
				if(pauza == 1) {
					pauza = 2;
					
					pe.stop();	
					//alert("STOP INTERWAĹU");
				}
				else{
				var prev_slide = current_slide;
				next_slide = current_slide+1;
				if (next_slide > 5) next_slide = 1;
				change_slide(prev_slide,next_slide);
				current_slide = next_slide;
				}
		}, 3 );
}

