//URL for AJAX-Call
var sUrl = "http://www.rc-magazin.de/ajax.php";

//Image-Elements
var actualElement = "Img1";
var oldElement = "Img2";

//aktuelles Image
var actualImage = "";

//Time between fades
var fadeTime = 15000;

//Seite
var site = '';
var group = '';

function initFade(actualSite, actGroup)
{
	//save site
	site = actualSite;
	group = actGroup;

	//initialise Fade
	window.setTimeout("fadePicture()", fadeTime);
}

function fadePicture()
{
	//make AJAX-Call
	var callback =
	{
		success: function(o)
		{

			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

			//Sysmessage
			if(json_msg.responsecode == 200)
			{
				//saving
				actualImage = 'http://www.rc-magazin.de/'+json_msg.image;

				//create new image-element
				newimg = document.getElementById(oldElement)
				newimg.src = 'http://www.rc-magazin.de/'+json_msg.image;


				//fading old out
				var fade1 = new YAHOO.util.Anim(actualElement, {
				    opacity: {from: 1, to: 0}
				}, 1, YAHOO.util.Easing.easeOut);
				fade1.duration = 3.0;
				fade1.animate();

				//fading new in
				var fade2 = new YAHOO.util.Anim(oldElement, {
				    opacity: {from: 0, to: 1}
				}, 1, YAHOO.util.Easing.easeOut);
				fade2.duration = 3.0;
				fade2.animate();

				//changing objects
				var tmp = actualElement;
				actualElement = oldElement;
				oldElement = tmp;

				window.setTimeout("fadePicture()", fadeTime);
			}
			else
			{
				alert(json_msg.error+" [Err #"+json_msg.responsecode+"]");
				window.setTimeout("fadePicture()", 5000);
			}
		},
		failure: function(o)
		{
			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

	        window.setTimeout("fadePicture()", 5000);
		}
	}

	var postData = 'action=getHeaderImage&old='+actualImage+'&site='+site+'&group='+group;

	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
