/**
 * @author brain2xml
 */
//var timewait = 3000; 
//var timechange = 100; 
$(document).ready(function(){

    $('#picture3, #l_1, #l_2, #l_3, #l_4, #l_5').pngFix();
    needtostart = true;
    sl_length = $('#slide_show div').length;
    step = sl_length;
	if(sl_length > 1){
    	to_init($('#slide_show'));
    	timeout = setTimeout(function(){
        	to_play($('#slide_show'))
    	}, timewait);
	}
    function to_init(div){
        div.find("div:not(:last)").css({
            zIndex: 0
        });
        $('#controls a').click(function(){
            a = $(this).attr('id').substr(2);
            if (step != a) {
                step = a;
                clearTimeout(timeout);
                to_play(div, a);
            }
			return false;
        });
        
    }

    function to_play(div, a){
		
        if (a > 0) {
            current = prev;
            prev = div.find("div:nth-child(" + a + ")");
			step = a;
        }
        else {
            current = div.find("div:nth-child(" + step + ")");
            if (step == 1) {
                prev = div.find("div:last");
            }
            else {
                prev = current.prev();
            }
			
            step--;
        }
        prev.css({
            zIndex: 0,
            opacity: 1
        });
        current.animate({
            opacity: 0
        }, timechange, function(){
            current.css({
                zIndex: 0
            });
            prev.css({
                zIndex: 10
            });
			var on = $("#controls td:nth-child(" + step + ") img")[0];
			switch(step){
				case 1: $(on).attr("src", "/themes/miro/i2/on_01.png");
				break;
				case 2: $(on).attr("src", "/themes/miro/i2/on_02.png");
				break;
				case 3: $(on).attr("src", "/themes/miro/i2/on_03.png"); 
				break;
				case 4: $(on).attr("src", "/themes/miro/i2/on_04.png");
				break;
				case 5: $(on).attr("src", "/themes/miro/i2/on_05.png");
				break;
			}
			$('#slide_links :nth-child(' + step + ')').show();
			
			
        });
        if (step == 0) {
            step = sl_length;
        }
	$('.picture_link').each(function(){
	    $(this).hide();
	});		
	
	$("#controls td img").each(function(i){
			i++;
			switch(i){
				case 1: $(this).attr("src", "/themes/miro/i2/off_01.png");
				break;
				case 2: $(this).attr("src", "/themes/miro/i2/off_02.png");
				break;
				case 3: $(this).attr("src", "/themes/miro/i2/off_03.png");
				break;
				case 4: $(this).attr("src", "/themes/miro/i2/off_04.png");
				break;
				case 5: $(this).attr("src", "/themes/miro/i2/off_05.png");
				break;
			}	
		});
        timeout = setTimeout(function(){
            to_play(div)
        }, timewait);
    }
});
