Event.observe(window, 'load', function() {
	doTimer();
});


var timer_is_on = 0;

function timedImageChange(seq_nr) {
	
	if (seq_nr == '') {
		var seq_nr = 1;
	} else {
		var seq_nr = seq_nr;
	}
	
	// get Image
	var options = {
		method: 'get',
		requestHeaders: {Accept: 'application/x-json'},
		onSuccess  : onRequestSuccess,
		onFailure  : onRequestFailure,
		onCreate   : onCreate,
		on404      : on404,
		parameters : {seq_nr: seq_nr}
	};
	
	new Ajax.Request('portfolio_scripts/portfolio_ajax_image_switcher.php', options);
}

function doTimer() {
	if (!timer_is_on) {
		timer_is_on = 1;
		setTimeout(function() {timedImageChange('');}, 3000);
    }
}


// AJAX functions
function onRequestSuccess(transport, json) {
	var response = transport.responseText || 'no repsonse text';
	
	var json = response.evalJSON(true); 
	var seq_nr = json.comp_seqnr;

	var Bild = new Image();
    Bild.src = json.picture_dir + '/' + json.comp_pic;
	
	new Effect.Fade('imagecontainer', {duration: 0.4, afterFinish: function() {
		$('image').src = json.picture_dir + '/' + json.comp_pic;
		$('image').width = json.pic_width;
		$('image').height = json.pic_height;
        $('fotographer').update(json.fotographer);
        $('component_name').update(json.comp_descr);

        // change in home.inc.php also (line 50)
        $('imagecontainer').setStyle({width: json.pic_width + 400 + 'px'});
        $('imagecontainer').hide(); //not shure if really necessary...
       // $('image').hide();
             
        new Effect.Appear('imagecontainer', {duration: 0.3});
      }
    });
	
	aktiv = setTimeout(function(){timedImageChange(seq_nr, json.project_id);}, 4000);
}

function onRequestFailure(transport) {
    alert('an error occured');
}

function on404(transport) {
    alert('not found');
}

function onCreate(transport) {
    //$('ajax_message').update('Anfrage wird bearbeitet...');
}
