$(function(){

/*
 * This is currently deactivated beacause we embed the json directly into this file.
 *
if ($("#flads")) {
	$.getJSON("http://werbung.flabs.org/liste.json", function(json){
		var random = Math.floor(Math.random()*json.ads.length);
		var pick = json.ads[random];
		
		$("#flads").append(
				'<a href="'+pick.url+'" class="werbegrafik"><img src="'+pick.img+'" alt="'+pick.name+'"/></a>'
			).append(
				'<p class="werbetext"><a href="'+pick.url+'">'+pick.name+'</a> &mdash; '+pick.text+'</p>'
			);
	});
}
*/

/*
 * This is the complete map of advertisment campaign with names, urls, images and the advert texts.
 */

var ads = [
	{
		"name": "Flusicr",
		"url": "http://flusicr.de/",
		"img": "http://werbung.flabs.org/img/flusicr.png",
		"text": "FLabs Tech Talk — Der FLabs Nerd Blog und Kriegsgebiet von Python vs. PHP"
	},
	{
		"name": "Teamshare",
		"url": "http://teamsharehq.com/",
		"img": "http://werbung.flabs.org/img/teamshare.png",
		"text": "Damit Projekte wieder Spaß machen!"
	},
	{
		"name": "Pascal Hertleif",
		"url": "http://pascalhertleif.de/",
		"img": "http://werbung.flabs.org/img/pascal.png",
		"text": "Frei arbeitender Webdesigner und Hobby-Fotograf"
	},
	{
		"name": "This is Rock and Roll Radio",
		"url": "http://radio.nickloose.de/",
		"img": "http://werbung.flabs.org/img/radio.png",
		"text": "Ein Blog über Punk, Alternativ, Indie und anderen Musik-Kram!"
	},
	/*{
		"name": "Netcup",
		"url": "http://gotonetcup.flabs.org/",
		"img": "http://werbung.flabs.org/img/netcup.png",
		"text": "Der Hoster unseres Vertrauens mit prima Preisen, Leitung und Support!"
	},*/
	/*{
		"name": "Zeugs &amp; Chaos",
		"url": "http://zeugsundchaos.de/",
		"img": "http://werbung.flabs.org/img/zeugsundchaos.png",
		"text": "Jaaa... Zeugs halt. Frisch aus dem großen Chaos."
	},*/
]

/*
 * Chose a random ad from the above list
 */

function makeapick(ads){
	var random = Math.floor(Math.random()*ads.length);
	if (ads[random].url == "http://"+document.domain+"/") {
		return makeapick(ads);
	} else {
		return ads[random];
	}
}

var pick = makeapick(ads);

/*
 * Now add that random ad (haha, 'add that ad') to an element called #flads (should be a div).
 * There will be a linked image ('.werbegrafik') and a short text with the name of the campaign
 * plus 100 characters of advert text ('.werbetext').
 */

$("#flads").append(
		'<a href="'+pick.url+'" class="werbegrafik"><img src="'+pick.img+'" alt="'+pick.name+'"/></a>'
	).append(
		'<p class="werbetext"><a href="'+pick.url+'">'+pick.name+'</a> &mdash; '+pick.text+'</p>'
	);

/*
 * The really cool thing about our way to tell the FLAds server that something happend with an ad
 * is simply, that we load 0 KB files and the server recognises with the refer and the parameters
 * in the URL what happend!
 */
	
// tell FLAds that we just displayd an ad!
$.get("http://werbung.flabs.org/angezeigt.empty");

// tell FLAds that there was a click on a campaign link!
$("#flads a").click(function(){
	$.get("http://werbung.flabs.org/geklickt.empty?link="+escape( $(this).attr("href") ) );
	return true;
});


});