var timerRunning = false;
var myTimer = null;

$(function() {

    $(window).resize(function() {
    	backshow();
    });
    
    backshow();
	rotate();
});

function rotate() {

	var image = $('#backshow img:first');
	image.hide();
	image.appendTo($('#backshow'));
	image.fadeIn('slow', function() {
	    $('#start .info .popup .box').hide();
	    var box = $('#start .info .popup .box:first');
	    box.show();
	    box.insertBefore('#start .info .popup .button');
	});

	myTimer = setTimeout('rotate()', 11000);
	timerRunning = true;
}


function toggleTimer(){
	switch(timerRunning){
		case true:
		$('#toggleTimer').text('Starta');
		
		$('#toggleTimerImg').attr({
			'alt': 'Starta',
			'src': '_img/start_knap.png'
		});
		
        clearTimeout(myTimer);
		timerRunning = false;
		break;
		
		case false:
		$('#toggleTimer').text('Stoppa');
		
		$('#toggleTimerImg').attr({
			'alt': 'Stop',
			'src': '_img/stopp_knap.png'
		});
		
		rotate();
		break;
		
		
		
	}
}



function backshow() {
    var i = $('#backshow img:first'),
    	ih = i.height(),
    	iw = i.width(),
    	ir = 1300 / 900,
    	wh = $(window).height(),
    	ww = $(window).width(),
    	wr = ww / wh;

    if (ir < wr) {
    	iw = ww;
    	ih = iw / ir;
    } else {
    	ih = wh;
    	iw = ih * ir;
    }

    ih = Math.ceil(ih);
    iw = Math.ceil(iw);

    $('#backshow img').css({ 'height': ih + 'px', 'left' : -((iw - ww) / 2) + 'px', 'position' : 'absolute', 'top' : -((ih - wh) / 2) + 'px', 'width': iw + 'px' });
}
