/**
 * JSON Sitemap Loader 
 *
 * @copyright       Copyright 2009, Deutscher Ring
 * @link            http://www.deutscherring.de
 * @project         Internet
 * @author         	mediaman technology GmbH (http://www.mediaman.de)
 * @version         1.0
 * @revision        $Rev: 402 $
 * @lastmodified    $Date: 2009-09-16 09:35:37 +0200 (Mi, 16. Sep 2009) $
 */

// Reading the Sitemap
$(document).ready(function()
{
	if($('#drFooterSitemapSrc').get(0))
	{
		$(function()
		{
			// Ajax Setup: Override MimeType to text/plain to stop Firefox from throwing errors 
			// for the json file not being well formed
			$.ajaxSetup({'beforeSend': function(xhr)
			{
				if (xhr.overrideMimeType) xhr.overrideMimeType("text/plain");
			}
			});
			// Fetch json data
			var jsonSrc = $('#drFooterSitemapSrc').attr('href');
			$.getJSON(jsonSrc, function(json) 
			{
				// anonymous functions for getting specific response data
				jJSON["text"]=(function() 
				{
					response={values:[],count: 0};
					$.each(json.sitemap.links,function(i,item) 
					{
						if (item.text!="undefined") 
						{
							response.count++;
							response.values[i]=item.text;
						}
					});
					return response;
				})();
				jJSON["href"]=(function() 
				{
					response={values:[],count: 0};
					$.each(json.sitemap.links,function(i,item) 
					{
						if (item.href!="undefined") 
						{
							response.count++;
							response.values[i]=item.href;
						}
					});
					return response;
				})();
				jJSON["style"]=(function() 
				{
					response={values:[],count: 0};
					$.each(json.sitemap.links,function(i,item) 
					{
						if (item.style!="undefined") 
						{
							response.count++;
							response.values[i]=item.style;
						}
					});
					return response;
				})();		
				index=0;
				headCounter=0;
				// Fill the Sitemap
				$(".drFooterSitemap div").each(function()
				{
					do
					{
						// Headlines need to be checked since only 2 Headlines appear in a column
						if (jJSON.getValue("style",index)=="bold") headCounter++;
						// Third Headline breaks the loop and starts anew
						if(headCounter%3==0) break;
						$(this).append('<a href="'+jJSON.getValue("href",index)+'" class="'+jJSON.getValue("style",index)+'">'+jJSON.getValue("text",index)+'</a>');
						index++;
					}
					while (jJSON.getValue("href",index))
				});
				// show the separator line for footer index
				$('div.drFooter div.drHide').removeClass('drHide');
			});
		});
	}
});