function init() {
	var SERVER_NAME = location.href.substr(0, location.href.indexOf('.de/')+3);
	var JS_SELF = location.href.substr(SERVER_NAME.length);
	var QUERY_STRING = '';
	if(JS_SELF.indexOf('?') != -1) {
		QUERY_STRING = JS_SELF.substr(JS_SELF.indexOf('?'));
		JS_SELF = JS_SELF.substr(0, JS_SELF.indexOf('?'))
	}
	
	switch(JS_SELF) {
		case '/media.html':
			jsLoadFile(SERVER_NAME + '/js/slimbox2.js');
						
			
			ajaxGalleryLoader(SERVER_NAME + "/data/csv/galerien_punk182.csv");
			break;
	}

	// Show events loading
	$('#events').html('<div class="ajax_loading white"><img src="/img/fw/ajax_loading.gif" width="32" height="32" alt="loading..." /><br/>Lade...</div>');
	
	// Load events
	ajaxEventLoader(SERVER_NAME + "/data/csv/termine_punk182.csv");
	
	
}

function jsLoadFile(url) {
	$.get(url, function(text){
		eval(text);
	});
}

function ajaxEventLoader(url) {
	$.get(url, function(text){
		var showEvents = false;
		
		var str = '<ul id="inner_events">';
		var aLines = text.split('\n');
		
		for(key in aLines) {
			if(aLines[key].indexOf(';')==-1)
				continue;
			
			aRecord = aLines[key].split(';');
			if(aRecord.length != 2)
				continue;
			
			str+='<li><span class="date">' + aRecord[0] + '</span> ' + aRecord[1] + '</li>'; 
			showEvents = true;
		}

		str += '</ul>';
		
		if(showEvents) {
			$('#events').html(str);
		}
	});	
}



function showGallery(text) {
	
	var aValues = text.split(';');
	if(aValues.length != 3)
		return false;
	
	aValues[2] = parseInt(aValues[2]);
	
	if(aValues[2] <= 0)
		return false;
	

	var gallerytb = ''; //'<h3>' + aValues[0] + '</h3>';
	gallerytb += '<ul>';
		
	for(var i=1;i<=aValues[2];i++) {
		gallerytb += '<li><a href="/data/media/galleries/' + aValues[1] + '/' + i + '.jpg" rel="lightbox-' + aValues[1] + '"><img src="/data/media/galleries/' + aValues[1] + '/tb/' + i + '.jpg" alt="" /></a></li>';
	}
	
	gallerytb += '</ul>';
	
	$('#gallerytb').html(gallerytb);
	
	
	/*
	var index = document.getElementById('selectgallery').selectedIndex;
	
	// $('#prev_gallery').html('&nbsp;');
	//if(index > 0)
	//	$('#prev_gallery').html('<a href="javascript:document.getElementById(\'selectgallery\').selectedIndex--;showGallery(\'' + document.getElementById('selectgallery').options[index-1].value + '\');">&laquo; zurück </a>');

	// $('#next_gallery').html('&nbsp;');
	// if(index != document.getElementById('selectgallery').options.length)
	// $('#next_gallery').html('<a href="javascript:document.getElementById(\'selectgallery\').selectedIndex++;showGallery(\'' + document.getElementById('selectgallery').options[index+1].value + '\');">weiter &raquo;</a>');
	*/
	
	$("a[rel^='lightbox']").slimbox(
		{
			counterText: "Bild {x} von {y}<br/>Tipp: Verwenden Sie die Pfeiltasten zum Navigieren und ESC zum Beenden.",
			previousKeys: [37, 80, 90],
			nextKeys: [39, 78, 87]
		},
		null,
		function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});	
}


function ajaxGalleryLoader(url) {
	$.get(url, function(text){
		var showGalleries = false;
		var firstGallery = '';
		var str = 'Bildergalerien:<br/><select id="selectgallery" name="gallery" size="1" onchange="showGallery(this.value);">';
		var aLines = text.split('\n');
		
		for(key in aLines) {
			
			if(aLines[key].indexOf(';')==-1)
				continue;
			
			aRecord = aLines[key].split(';');
			// alert(aRecord);
			
			if(aRecord.length != 3)
				continue;
			
			str+='<option value="' + aLines[key] + '"> ' + aRecord[0] + ' ( ' + aRecord[2]+ ' Bilder ) </option>';

			if(!showGalleries) {
				firstGallery = aLines[key];
				showGalleries = true;
			}
		}

		str += '</select>';
		
		if(showGalleries) {
			$('#galleryselector').html(str);
			showGallery(document.forms[0].gallery.value);
		}

	});	
}


function ajaxTracklistLoader() {
	$(function() {
		$.ajax({
			type: "GET",
			url: "/data/xml/tracklist.xml",
			dataType: "xml",
			success: function(xml) {
				var str = '<select name="flashID" onchange="document.zanmantou.jumpAndPlay(this.value)">';
				var i = 0;
				$(xml).find('item').each(function() {
					// alert(this);
					
					var name = $(this).attr('name');
					var file = $(this).attr('filename');
					
					str += '<option value="'+ i +'">'+ name + '(' + file + ')<\/option>';
					i++;
				});
				
				str +='<\/select>';
				
				$('#trackselector').html(str);
			}
		});
	});	
}



