var slideshow_timer = 0;
var current_image = 0;
var image_array = new Array();
var last_image = 0;

switch_image = function(imagefile) {

  if( $('#thumbs').length > 0 ){
    $('#thumbs ul.thumbs li.selected').fadeTo('slow', 0.5);
//	$(document.getElementById('thumbs').getElementsByTagName('li')[last_image])
    $('#thumbs li.selected').removeClass('selected');

	if (imagefile == "next") {

		current_image = parseInt(current_image) + 1;
		imagecount = parseInt(image_array.length - 1);
		if (current_image > imagecount) {
			current_image = 0;
		}
		$("#jqgal_img").ImageSwitch( {
			Type : "FadeIn",
			NewImage : image_array[current_image],
			EndTop : 1,
			Speed : 2000
		});
		
		

	} else if (imagefile == "prev") {

		current_image = parseInt(current_image) - 1;
		imagecount = parseInt(image_array.length - 1);
		if (current_image < 0) {
			current_image = imagecount;
		}
		$("#jqgal_img").ImageSwitch( {
			Type : "FadeIn",
			NewImage : image_array[current_image],
			EndTop : 1,
			Speed : 2000
		});
		
		
		
	} else {

		clearInterval(slideshow_timer);
		$("#jqgal_img").ImageSwitch( {
			Type : "FadeIn",
			NewImage : image_array[imagefile],
			EndTop : 1,
			Speed : 2000
		});
		
		
		current_image = parseInt(imagefile);
	}

	//alt = $(document.getElementById('thumbs').getElementsByTagName('img')[current_image]).attr('alt');
    desc   = $('#thumbs .thumbs').find('img').eq(current_image).attr('alt');
    title = $('#thumbs .thumbs').find('img').eq(current_image).attr('title');
   
    $("#jqgal_img").attr({'alt' : title});
	$("#jqgal_img").attr({'title' : title});
	
	//$(document.getElementById('thumbs').getElementsByTagName('li')[current_image]).addClass('selected');
    $('#thumbs .thumbs').find('li').eq(current_image).addClass('selected');
	$('#thumbs ul.thumbs li.selected').fadeTo('slow', 1);
	last_image = current_image;
    $('#faderdesc div').fadeTo('normal',0, function(){
    //ie dont like atob, so i implement base64_decode
        newdata = base64_decode(desc);
        $('#faderdesc div')
            .html(newdata)
            .fadeTo('slow',1); 
    });
    
  } //end if
}

start_slideshow = function() {
	clearInterval(slideshow_timer);
	switch_image('next');
	slideshow_timer = setInterval('start_slideshow()', 6000);
}

start_slideshow_prev = function() {
	clearInterval(slideshow_timer);
	switch_image('prev');
	slideshow_timer = setInterval('start_slideshow_prev()', 6000);
}

doBlink = function(obj, start, finish) {
	jQuery(obj).fadeTo(400, 0.5).fadeTo(400, 1);
	if (start != finish) {
		start = start + 1;
		doBlink(obj, start, finish);
	}
}

bindOnClick = function() {
	image_array = new Array();
	$('.thumb').each( function() {
		var id = $('.thumb').index($(this));
		$(this).click( function() {
			clearInterval(slideshow_timer);
			switch_image(id);
			slideshow_timer = setInterval('start_slideshow()', 6000);
		});

		path = $(this).children('img').attr('src');
		pos = path.indexOf('/thumbs/');
		if(pos != -1){
			path = pathToImages + path.substring(pos + 8);	
		}
		
		image_array[id] = path;
	});
}

initializeGallery = function() {
	pathToImages = $('meta[name=galerieFolder]').attr("content");

	$('div.navigation').css( {
		'width' : '300px',
		'float' : 'left'
	});
	$('div.content').css('display', 'block');

	var onMouseOutOpacity = 0.5;
	$('#thumbs ul.thumbs li').css('opacity', onMouseOutOpacity).hover(
			function() {
				$(this).not('.selected').fadeTo('fast', 1.0);
			}, function() {
				$(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
			});

	var onMouseOutOpacityControl = 0.2;
	$('.controls').css('opacity', onMouseOutOpacityControl).hover( function() {
		$(this).not('.selected').fadeTo('fast', 0.5);
	}, function() {
		$(this).not('.selected').fadeTo('fast', onMouseOutOpacityControl);
	});

	$('.controls_prev').css('opacity', onMouseOutOpacityControl).hover(
			function() {
				$(this).not('.selected').fadeTo('fast', 0.5);
			},
			function() {
				$(this).not('.selected').fadeTo('fast',
						onMouseOutOpacityControl);
			});

	next_image = function() {
		switch_image('next');
		jQuery('.controls').fadeOut('fast');
		jQuery('.controls').fadeIn('slow');
	}

	prev_image = function() {
		switch_image('prev');
		jQuery('.controls').fadeOut('fast');
		jQuery('.controls').fadeIn('slow');
	}

	bindOnClick();
	current_image = -1;
	start_slideshow();
	$('#controls_next').click( function() {
		start_slideshow();
	});
	$('#controls_prev').click( function() {
		start_slideshow_prev();
	});
}

window.onload = function() {
	if (document.getElementById('thumbs')) {
		initializeGallery();
	}
}
