﻿$(function() {

	if($('#flashDomId').length > 0) {	
		$('#countrylinks img').remove();
	}

    var oldText = $('#javascriptlink').html();
	$('#javascriptlink').click(function() {
		var visible = visible || $('#availablelocations:visible').length > 0;
		
        $('#javascriptlink').toggleClass('no-background').toggleClass('show-background');
		if(visible === true) {
			$('#availablelocations').hide('slow');
			$('#javascriptlink').html(oldText);
		} else {
			$('#availablelocations').show('slow');
        	$('#javascriptlink').html("x close");
        }
		
	});

	// see wiki -> Fronius:.COM Startseite
	$.ajax( { 
		url: '/ext/startseite/data/locations.xml', 
		dataType: 'xml', 
		error: function(error, errorType) {
			//alert("Ajax error: " + error + "(" + errorType + ")");
		}, 
		success: function(data) {
			$('#countrylist').empty();
			var continentlist = $('<div />');
			$('continent[isstaticgeneric=true]', data).each(function() {
				var sublink = $(this).attr('name');
				var link = $(this).attr('link');
				
				if(link && link !== '') {
					sublink = '<a href="' + link + '">' + sublink + '</a>';
				}
				sublink = $('<div class="generic">' + sublink + '</div>');
				continentlist.append(sublink);
			});
			$('continent', data).each(function() {
				var countrylinks = $('country:not(link)', this).filter('[link!=""][isstaticgeneric!=true]');
				var countryWithValidSublinks = $('country:has(link[isstaticgeneric!=true])', this);
				if(countrylinks.length === 0 && countryWithValidSublinks.length === 0) {
					return;
				}
				// Add both groups of valid entries into one jquery array
				countrylinks = countrylinks.get();
				countrylinks.concat(countryWithValidSublinks.get());
				countrylinks = $($.unique(countrylinks));
				
				// create continent header (ignore links)
				var continent = $(this).attr('name');
				continent = $('<div class="continent"><div class="label">' + continent + '</div></div>');
				
				// iterate all valid countries
				countrylinks.each(function() {
					var country = $(this);
					var countryName = '';
					// fetching country name (filtering textcontent of child elements)
					for(var i = 0; i < this.childNodes.length; i++) {	
						if(this.childNodes[i].nodeType === 3) // Node.TEXT_NODE
						{
							countryName += this.childNodes[i].nodeValue;
						}
					}
					
					var sublinks = $('link[isstaticgeneric!=true]', this);
					var sublink = null;
					var link  = null;
					// generate entries from sublinks
					sublinks.each(function() {
						sublink = countryName + ' - ' + $(this).text();
						link = $(this).attr('value');
						sublink = '<a href="' + link + '">' + sublink + '</a>';
						sublink = $('<div class="link">' + sublink + '</div>');
						sublink.addClass(countryName.replace(" ", ""));
						continent.append(sublink);
					});
					
					// if no entries from sublinks have been created, create a country link
					if(sublinks.length === 0) {
						sublink = countryName;
						link = $(this).attr('link');
						sublink = '<a href="' + link + '">' + sublink + '</a>';
						sublink = $('<div class="link">' + sublink + '</div>');
						sublink.addClass(countryName.replace(" ", ""));
						continent.append(sublink);
					}
				});
				
				continentlist.append(continent);
			});	
			$('#availablelocations').append(continentlist);
			if(window.console) {
				console.warn('Generiere HTML Code ');
				console.log(continentlist);
			} 

		}
		});
		
});
