if($.cookie('css')) {
    $('#changeCSS').attr('href', 'css/'+$.cookie('css'));
}

$(document).ready(function(){		
	checkCondition('#karteOverlay');
	
	displayAccessibilityToolbar();
	
	$('.switcherItem').click(function() { 
		$('#changeCSS').attr('href','css/'+$(this).attr('rel'));
		$.cookie('css', $(this).attr('rel'), {expires: 60, path: '/'});		
		return false;
	});	
	
	displayPrintLink('serviceNavi'); 	
	$("#print").click(function() {
		window.print();	
	});
	

}); //$(document).ready(function(){

function checkCondition(id) {
    if ( $(id).css('width') == '320px') {
	rebuildKarteWrapper(id);
	
	var fadeInTime = 500;	
	
	var areas = new Array(
		'barnim',
		'berlin',
		'brandenburg-an-der-havel',
		'cottbus',
		'dahme-spreewald',
		'elbe-elster',
		'frankfurt-oder',
		'havelland',
		'maerkisch-oderland',
		'oberhavel',
		'oberspreewald-lausitz',
		'oder-spree',
		'ostprignitz-ruppin',
		'potsdam',
		'potsdam-mittelmark',
		'prignitz',
		'spree-neisse',
		'teltow-flaeming',
		'uckermark'
	);	
	
	var numberOfAreas = areas.length;	
	for (var i = 0; i < numberOfAreas; i++) {		
		activateArea(areas[i], fadeInTime);		
	}
	
	$('#land-brandenburg').mouseout(function() {		
		mouseoutActionLand();			
	});
	
    }
}

function rebuildKarteWrapper(id) {
	$('#karteOverlay').css('display', 'none');
	var o = '<img id="karteOverlayImg" src="images/karte/1x1.gif" alt="Übersichtskarte Land Brandenburg" usemap="#brandenburg" />';    
    
	//$('#serviceNavi').html('<div id="testOut">0</div>');
    
	$(id).html(o);
}



function activateArea(area, fadeInTime) {	
	$('#'+area).mouseover(function() {
		isOut = false;
		$('#karteOverlay').css('display', 'none');		
		$('#karteOverlayImg').attr('src', 'images/karte/'+area+'.gif');
			$('#karteOverlay').fadeIn(fadeInTime, function() {		
				// einblenden fertig		    
			});		
		getAnzahlKSG(area);
		
	}).mouseout(function(){	
		mouseoutActionRegion();		
	});
}

function mouseoutActionLand() {
	refillInfo();	
	
	$('#karteOverlayImg').attr('src', 'images/karte/1x1.gif');	
}

var isOut = true;

function mouseoutActionRegion() {
	isOut = true;
	window.setTimeout(refillInfo, 3000);	
}

function refillInfo() {
	if (isOut) {
		$('#karteOverlayImg').attr('src', 'images/karte/1x1.gif');
		var o = "<p>Um Selbsthilfegruppen oder Selbsthilfekontaktstellen zu finden, wählen Sie bitte eine Region auf der Übersichtskarte Brandenburgs aus.</p>";
		o += "<p>Alternativ können Sie auch über <a href=\"selbsthilfegruppen.html\">Selbsthilfe<i>Gruppen</i></a> oder <a href=\"selbsthilfekontaktstellen.html\">Selbsthilfe<i>Kontaktstellen</i></a> entsprechende Informationen abrufen oder Sie nutzen die Suche.</p>"
    
		$("#info").html(o);    	
	}    
}
function clearInfo() {
    var o = "";
    
    $("#info").html(o);    
}

function outputAnzahlKSG(data) {
    var o = "<table class=\"region\">";
    o += "<caption>"+data.name+" ("+data.kfz+")</caption>";
    o += "<tr><td>Selbsthilfekontaktstellen:</td><td>"+data.ks+"</td></tr>";
    o += "<tr><td>Selbsthilfegruppen:</td><td>"+data.g+"</td></tr>";
    o += "</table>"

    $("#info").html(o);	
}

/* Live */
function getAnzahlKSG(url) {
	clearInfo();
	
	$.ajax({
		// the URL for the request (REDAXO article with AJAX Template)		
		url : 'index.php?article_id=29',
	
		// the data to send
		// (will be converted to a query string)
		data : { 'url' : url },
	
		// whether this is a POST or GET request
		type : 'POST',
	
		// the type of data we expect back
		//dataType : 'html',
		dataType : 'json',
	
		// code to run if the request succeeds;
		// the response is passed to the function
		success : function(data) {
			outputAnzahlKSG(data);
		},
	
		// code to run if the request fails;
		// the raw request and status codes are
		// passed to the function
		error : function(xhr, status) {			
			//refillInfo();
		},
	
		// code to run regardless of success or failure
		complete : function(xhr, status) {
			//	request is complete!
		}
	});
}

function displayAccessibilityToolbar() {
	if (navigator.cookieEnabled) {
		var temp = $('#headerContent').html();
		
		var add = '<div id="accessBar" style="position: absolute; right: 25px;">Schriftgröße ändern: ';
		add += '<ul style="display: inline;">';
		add += '<li><a id="switchNormal" href="#" class="switcherItem" rel="style.css">normal</a> | </li>';
		add += '<li><a id="switchBig" href="#" class="switcherItem" rel="style_alt.css">groß</a></li>';
		add += '</ul>';
		
		//add += '<div id="testOut"></div>';
		
		add += '</div>';
		
		$('#headerContent').html(add+temp);
	}
}

function displayPrintLink(id) {
	var pl = '';
	pl += '<li><a id="print" href="javascript:void(0)">Drucken</a> | </li>';
	
	var html = $('#'+id).html();
	
	$('#'+id).html(pl+html);
}
