var main_xml;
var photogroups_mc;
var menu_array;
var image_array 			= new Array();
var menu_array	 			= new Array();
var categories_array 	= new Array();

var recent_image 			= 0;
var image_category 		= 0;
var start_category		= -1;
var pg_index 					= 0;
var pg_sub_index 			= 0;
var slideshow_status 	= 0;
var imprint						= 0;

function init(){

	showNaviElements('off');
	
	var myAjax = new Ajax();
	myAjax.url = 'src/functions.php';
	myAjax.onSuccess=fillContent;
	myAjax.onError=errorHandler;
	myAjax.params = 'case=content';
	myAjax.method = 'POST';
	myAjax.doRequest();
}

function errorHandler(e){
	alert('Error: ' + e);
}

function fillContent(txt, xml){
	
	main_xml = xml;
	initInterface();
	loadCategory();
	window.onresize = ImageResize;
}

function initInterface(){
	
	//Website - Title
	document.title = getTextContentByTagNames(main_xml, 'sitetitle');
	
	//Menu - Pictures
	var navi_container = document.getElementById('navi_container');
	var element = main_xml.getElementsByTagName('photogroups');
  element = element[0];
  
	var dom_parent = initPhotogroup();
	
	xml2Navi(element, dom_parent);
	
	
	if(start_category == -1){
		
		//zufällige Zuordnung der Kategorie des ersten Aufrufs
		image_category = Math.floor(Math.random()*menu_array.length);
		setNaviButtonStatus(menu_array[image_category], 'on');
		//zufälliger Aufruf eines Bildes der zufälligen Kategorie
		var given_images = categories_array[image_category].getElementsByTagName('image');
		recent_image = Math.floor(Math.random()*given_images.length);
	}else{

		image_category = start_category
		setNaviButtonStatus(menu_array[image_category], 'on');
		recent_image = 0;
	}

	//Menu - Contact
	var contact_mc = document.createElement('div');
	contact_mc.className = 'navi_head_mc';
	var contact_folder = document.createElement('h1');
	menu_array.push(contact_folder);
	contact_folder.innerHTML = getTextContentByTagNames(main_xml, 'contact', 'title');
	contact_folder.onclick = function(){
	
		var slide_mc = document.getElementById('slide_mc');
		slide_mc.onStatus = 1;
		slide_mc.onclick();
		setNaviButtonStatus(this, 'on');
		showImprint('on');
	}
	
	contact_mc.appendChild(contact_folder);
	navi_container.appendChild(contact_mc);
	
	// Navigation - Pictures
	var prev_mc = document.getElementById('prev_mc');
	var next_mc = document.getElementById('next_mc');
	
	prev_mc.innerHTML = getTextContentByTagNames(main_xml, 'pictureNavi', 'prev');
	next_mc.innerHTML = getTextContentByTagNames(main_xml, 'pictureNavi', 'next');
	
	prev_mc.onclick = function(){
			preloadImage(getImageNumber('prev'));
	}
	
	next_mc.onclick = function(){
			preloadImage(getImageNumber('next'));
	}
	
	//Navigation - Slideshow
	var slide_mc = document.getElementById('slide_mc');
	slide_mc.innerHTML = getTextContentByTagNames(main_xml, 'slideshow', 'title');
	slide_mc.onStatus = 0;
	slide_mc.onclick = function(){
	
		setButtonToggleStatus(this);
		slideshow_status = this.onStatus;
		setSlideshow();
	}
	
	//Picture Description
	var description_head_mc = document.getElementById('description_head');
	description_head_mc.innerHTML = getTextContentByTagNames(main_xml, 'description_head');
	
	//Imprintdaten setzen
	var imprint_mc = document.getElementById('impressum_head');
	imprint_mc.innerHTML = getTextContentByTagNames(main_xml, 'contact', 'head');
	
	var name_mc = document.getElementById('contact_function');
	name_mc.innerHTML = getTextContentByTagNames(main_xml, 'contact', 'name') + '<br />' + getTextContentByTagNames(main_xml, 'contact', 'function');
	
	var adress_mc = document.getElementById('contact_impressum');
	adress_mc.innerHTML = getTextContentByTagNames(main_xml, 'contact', 'adress');
}

// Menu anlegen
function initPhotogroup(){
	
	pg_index ++;
	pg_sub_index ++;
	
	var navi_container = document.getElementById('navi_container');
	
	photogroups_mc = document.createElement('div');
	photogroups_mc.id = 'pg_' + pg_index + '_' + pg_sub_index;
	photogroups_mc.className = 'navi_head_mc';

	var photogroup_folder = document.createElement('h1');
	photogroup_folder.innerHTML = getTextContentByTagNames(main_xml,'title');
	photogroups_mc.appendChild(photogroup_folder);
	navi_container.appendChild(photogroups_mc);
	
	return photogroups_mc;
}

// Menustruktur erstellen
function xml2Navi(xmlnode, parent, param){
	
	var searchParam = param ? param : 'child';
	
	var childNode 	= xmlnode.firstChild;
	var sibling 		= xmlnode.nextSibling;

	if(childNode && searchParam == 'child'){
		
		if(xmlnode.nodeName == 'photogroup'){
			
			pg_index ++;
			parent = setDOMParent(xmlnode, parent);
		}
		
		xml2Navi(childNode, parent, 'child');
	}else{
		
		if(sibling){
		
			xml2Navi(sibling, parent);
		}else{
		
			if(!xmlnode.parentNode){
				return;
			}
			
			if(xmlnode.parentNode.nodeName == 'photogroup'){
				pg_index --;
			}
			
			xml2Navi(xmlnode.parentNode, parent, 'parent');
		}
	}
}

function addCategory(xmlnode){

	var length = categories_array.push(xmlnode);
	return (length - 1);
}

function checkChildByName(xmlnode, searchParam){
	
	var img_array = new Array();
	
	if(xmlnode.nodeType == 1){
	
		img_array = xmlnode.getElementsByTagName(searchParam);
	}
	
	if(img_array.length > 0){
			
		return true;
	}else{
		
		return false;
	}
}

function setDOMParent(xmlnode, parent){
	
	var photogroup_array = xmlnode.getElementsByTagName('photogroup');
		
	if(checkChildByName(xmlnode, 'image') == false){		
		return parent;
	}
	
	var photogroups_mc = document.createElement('div');
	photogroups_mc.className = 'cat_mc';
	
	var photogroup_folder = document.createElement('h2');
	photogroup_folder.innerHTML = getTextContentByTagNames(xmlnode,'title');
	photogroup_folder.id = 'pg_folder' + pg_index + '_' + pg_sub_index;
	photogroup_folder.parent = parent;
	
	if(photogroup_array.length == 0){
	
		menu_array.push(photogroup_folder);
		
		var start = getAttributeByTagNames('start', xmlnode);
		var tmp_images = xmlnode.getElementsByTagName('image');
		
		if(start == 1 && tmp_images.length > 0){
				
				start_category = start_category == -1 ? (menu_array.length-1) : start_category;
		}
		
		photogroup_folder.category = addCategory(xmlnode);
		photogroup_folder.pg_index = pg_index;
		photogroup_folder.pg_sub_index = pg_sub_index;
		photogroup_folder.onclick = function(){
			
			setNaviButtonStatus(this, 'on');
			image_category = this.category;
			recent_image = 0;
			showImprint('off');
			loadCategory();
		}
	}
	
	photogroups_mc.appendChild(photogroup_folder);
	
	var parentNode;
	
	if(pg_index == 2){

		parentNode = document.getElementById( 'pg_1_1');
	}else{
	
		parentNode = document.getElementById( 'pg_' + (pg_index - 1) + '_' + pg_sub_index);
	}
	
	
	parentNode.appendChild(photogroups_mc);
	
	if(photogroup_array.length > 0){
		
		if(pg_index == 2){
			
		 	pg_sub_index ++;
		}
		
		var sub_mc = document.createElement('div');
		sub_mc.id = 'pg_' + pg_index + '_' + pg_sub_index;
		sub_mc.className = 'sub_mc';
		photogroups_mc.appendChild(sub_mc);
		
		parent = sub_mc;
	}else{
		
		parent = photogroups_mc;
	}
	
	return parent;
}

function setSlideshow(){
	
	var slide_seconds = getAttributeByTagNames('interval', main_xml, 'slideshow');
	slide_seconds 		= slide_seconds ? slide_seconds * 1000 : 5000;
	
	if(slideshow_status == 1){
		
		preloadImage(getImageNumber('next'));
		window.setTimeout("setSlideshow()", slide_seconds);
	}
}

function getImageNumber(direction){
	
	if(direction == 'prev'){
		if(recent_image == 0){
			return image_array.length-1;
		}else{
			return (recent_image-1)%image_array.length;
		}
	}else{
		return (recent_image+1)%image_array.length;
	}
}

function loadCategory(){
	
	image_array = categories_array[image_category].getElementsByTagName('image');
	showNaviElements('on');
	preloadImage();
}

function ImageResize(){
	
	var bg_image_mc = document.getElementById('bg_img_container');
	var img 				= bg_image_mc.firstChild;
	
	var browser_w = document.body.clientWidth;
	var browser_h = document.body.clientHeight;
	
	var temp_img = new Image();
	temp_img.src = (img.getAttribute ? img.getAttribute("src") : false) || img.src;
	
	var image_w	= temp_img.width;
	var image_h	= temp_img.height;
	
	var new_w = image_w;
	var new_h = image_h;
	
	var min_w = getAttributeByTagNames('min_width', main_xml, 'photogroups') ? getAttributeByTagNames('min_width', main_xml, 'photogroups') : 1200;
	var min_h = getAttributeByTagNames('min_height', main_xml, 'photogroups') ? getAttributeByTagNames('min_height', main_xml, 'photogroups') : 1024;

	new_w = browser_w;
	new_h = (new_w/image_w) * image_h;
	
	if(new_w < min_w){
		
		new_w = min_w;
		new_h = (new_w/image_w) * image_h;
	}
	
	if(new_h < min_h){
		
		new_h = min_h;
		new_w = (new_h/image_h) * image_w;
	}
		
	img.height 	= new_h;
	img.width 	= new_w;
}

function setImage(img){

	var bg_image_mc = document.getElementById('bg_img_container');
	var bg_image 		= document.createElement('img');
	bg_image.src 		= img.src;
	bg_image.id 		= 'bg_img';
	bg_image_mc.innerHTML = '';
	bg_image_mc.appendChild(bg_image);
	setImageTitle();
	ImageResize();	
}

function preloadImage(num, url){
	
	recent_image 	= num != undefined ? num : recent_image;
	url 					= url != undefined ? url : getAttributeByTagNames('url', image_array[recent_image]);
	var img			 	= new Image();
	img.src 			= url;
	setPreloader('on');
	checkImageLoadingStatus(img);
}

function checkImageLoadingStatus(img){

	if(img.complete){
		setPreloader('off');
		setImage(img);
	}else{
		
		window.setTimeout(function(){ checkImageLoadingStatus(img); }, 500);
	}
}

function setPreloader(stat){

	var preloader_mc = document.getElementById('preloader_mc');
	
	if(stat == 'on'){
		preloader_mc.style.display = 'block';
	}else{
		preloader_mc.style.display = 'none';
	}	
}

function setImageTitle(){

	var image_title_mc = document.getElementById('image_title_mc');
	var title = getTextContentByTagNames(image_array[recent_image], 'title');
	
	if(title == '' || !title){
	
		showDescription('off');
	}else{
		
		showDescription('on');
		image_title_mc.innerHTML = title;
	}
	
}

function showImprint(stat){

	var imprint_mc = document.getElementById('contact_mc');	
	
	imprint = stat;
	
	if(stat == 'on'){
	
		// Hintergrundbild für Imprint laden
		var contact_array = main_xml.getElementsByTagName('contact');
		preloadImage(undefined, getAttributeByTagNames('url', main_xml, 'contact'));
		// Imprint einblenden
		imprint_mc.style.display = 'block';
		showNaviElements('off');
		
	}else{
	
		// Imprint ausblenden
		imprint_mc.style.display = 'none';
		showNaviElements('on');
	}
}

function showNaviElements(stat){
	
	showDescription(stat);
	showSlideshow(stat);
}

function showDescription(stat){
	
	var description_mc = document.getElementById('description_container');
	if(stat == 'off' || imprint == 'on'){
	
		//Bildbeschreibung ausblenden
		description_mc.style.display = 'none';
	}else{
		
		//Bildbeschreibung einblenden
		description_mc.style.display = 'block';
	}
}

function showSlideshow(stat){
	
	var slideshow_mc = document.getElementById('controll_container');
	
	if(stat == 'off'  || imprint == 'on'){
	
		// Slideshow/Bildbeschreibung ausblenden
		slideshow_mc.style.display = 'none';
	}else{
		
		if(image_array.length >= 2){
			
			// Slideshow/Bildbeschreibung einblenden
			slideshow_mc.style.display = 'block';
		}else{
			
			slideshow_mc.style.display = 'none';
		}
	}
}

function getTextContentByTagNames(){
	
  var element = getTextContentByTagNames.arguments[0];
	
  for (i=1; i<getTextContentByTagNames.arguments.length; i++) {
		
  	element = element.getElementsByTagName(getTextContentByTagNames.arguments[i].toString());
  	element = element[0];
  }
  
  return element.text ? element.text : element.textContent;
}

function getAttributeByTagNames(){
	
	var attribute = getAttributeByTagNames.arguments[0];
  var element 	= getAttributeByTagNames.arguments[1];
  
  for (i=2; i<getAttributeByTagNames.arguments.length; i++) {
	
  	element = element.getElementsByTagName(getAttributeByTagNames.arguments[i].toString());
  	element = element[0];
  }
  
  return element != undefined ? element.getAttribute(attribute) : false;
}

function setNaviButtonStatus(btn, stat){
	
	this.btn = btn;
	
	if(stat == 'on'){
		
		resetAllButtons();
	
		this.btn.style.backgroundImage = "url(src/navi_bg_over.png)";
	
		this.btn.onmouseover =
		this.btn.onmouseout =  function(){
				
			this.style.backgroundImage = "url(src/navi_bg_over.png)";
		}

	}else{
		
		this.btn.style.backgroundImage = "url(src/navi_bg.png)";
		
		this.btn.onmouseover = function(){
				
			this.style.backgroundImage = "url(src/navi_bg_over.png)";
		}
		this.btn.onmouseout = function(){
			this.style.backgroundImage = "url(src/navi_bg.png)";
		}
	}
}

function resetAllButtons(){
	
	var buttons_array = new Array();
	
	for(var i in menu_array){
		
		buttons_array.push(menu_array[i]);
	}
	
	for(var i = 0; i < buttons_array.length; i++){
			
		buttons_array[i].style.backgroundImage = "url(src/navi_bg.png)";
	
		buttons_array[i].onmouseover = function(){
				
			this.style.backgroundImage = "url(src/navi_bg_over.png)";
		}
		buttons_array[i].onmouseout = function(){
			this.style.backgroundImage = "url(src/navi_bg.png)";
		}
	}
	
	
}

function setButtonToggleStatus(btn){
	
	this.btn = btn;
	
	if(this.btn.onStatus == 0){
	
		this.btn.style.backgroundImage = "url(src/navi_bg_over.png)";
		
		this.btn.onmouseover =
		this.btn.onmouseout =  function(){
				
			this.style.backgroundImage = "url(src/navi_bg_over.png)";
		}
		
		this.btn.onStatus = 1;
	}else{
		
		this.btn.style.backgroundImage = "url(src/navi_bg.png)";
		
		this.btn.onmouseover = function(){
				
			this.style.backgroundImage = "url(src/navi_bg_over.png)";
		}
		this.btn.onmouseout = function(){
			this.style.backgroundImage = "url(src/navi_bg.png)";
		}
		
		this.btn.onStatus = 0;
	}
}