////-----------------
//	Copyright: Sebastian Hoeninger - Sone-works.de
////-----------------

// set Debugging Mode
var DEBUG = false;

var lastPage = false;
var syboGalleries;

var showBox2 = false;
var showBoxGallery = false;
if(typeof(showContentBox2) == undefined || typeof(showContentBox2) == 'undefined') {
	var showContentBox2 = false;
}

if(typeof(showContentBox) == undefined || typeof(showContentBox) == 'undefined') {
	var showContentBox = true;
}

var sauterSlideshow = null;

var boxGallery = 'box_left_gallery';
var contentBox2 = 'content2';
var contentBox2Content = 'contentBox2Content';
var contentBoxContent = 'contentInner';

// TODO: reinit slideshow after leaving gallery

function sauterMakeMenuItemsInactive() {
	var menu = syboGetById('mainmenu');
	for(var i = 0; i < menu.children.length; i++) {
		menu.children[i].children[0].className = 'inactive';
	}	
}
// Update menu onclick
function sauterUpdateMenu(clickedElement, parentPage, page, submenu) {
	showContentBox = true; // hide on load without specified page
	
	if(lastPage == 'gallery' || lastPage == 'galerie') {
		//sauterSlideshow.reinit("modules/sybo_gallery/mediafeed.php?id=all");
	}
	
	if(typeof(submenu) == undefined) {
		submenu = false;
	}
	
	if(!submenu) {
		var menu = syboGetById('mainmenu');
		var menuItems = menu.children;
		for(var i = 0; i < menuItems.length; i++) {
			menuItems[i].children[0].className = 'inactive';
		}
		clickedElement.className = 'active';
		
		var submenuBox = syboGetById('submenu');
		var found = false;
		if(submenuBox != null) {
			for(var i = 0; i < submenuBox.children.length; i++) {
				if(submenuBox.children[i].id == 'submenu_'+parentPage) {
					var menu = syboGetById('submenu_'+parentPage).children[0];
					var menuItems = menu.children;
					for(var j = 0; j < menu.children.length; j++) {
						menuItems[j].children[0].className = 'inactive';
					}
					menuItems[0].children[0].className = 'active';
					submenuBox.children[i].style.display = 'block';
					found = true;
				} else {
					submenuBox.children[i].style.display = 'none';
				}
			}
		}
		showBox2 = found;
		
	} else {
		var menu = syboGetById('submenu_'+parentPage).children[0];
		var menuItems = menu.children;
		for(var i = 0; i < menu.children.length; i++) {
			menuItems[i].children[0].className = 'inactive';
		}
		clickedElement.className = 'active';
	}
	
	if(page == 'gallery' || page == 'galerie') {
		showBoxGallery = true;
	} else {
		showBoxGallery = false;
	}
	if(page == 'projects' || page == 'projekte') {
		showContentBox2 = true;
	} else {
		showContentBox2 = false;
	}
	lastPage = page;
}

// Load new content and replace current one
function sauterLoadContent(queryString, submenu) {
	var box2 = 'box_left_2';
	var boxMain = 'content';
	var offset = 0;
	
	if(typeof(submenu) == undefined) {
		submenu = false;
	}
	

	if(showContentBox2 || syboGetById(contentBox2).offsetWidth > 0) {
		offset += 110;
		setTimeout(function() {
			sybo_toggle_vis_by_width(contentBox2, 50, 0);
		}, 8);
		
		setTimeout(function() {
			syboGetById(contentBox2).style.overflow = 'hidden';
		}, 3);
	}
	
	if(!showBoxGallery && syboGetById(boxGallery).offsetHeight > 0) {
		setTimeout(function() { 
			sybo_toggle_vis_by_height(boxGallery, 100, 0);	
		}, offset + 200);
	}
	
	if(!submenu) {
		syboGetById('submenu').style.display = 'none';
		setTimeout(function() { 
			sybo_toggle_vis_by_height(box2, 100, 0);	
		}, offset + 200);
	}
	
	if(showBoxGallery) {
		setTimeout(function() {
			sybo_toggle_vis_by_width(boxMain, 100, 0);
		}, offset + 10);
	}
	setTimeout(function() {
		syboGetById(boxMain).style.overflow = 'hidden';
	}, offset + 5);
	if(!showBoxGallery) {
		sybo_toggle_vis(contentBoxContent, false, 'hide');
	}
	
	
	sauterContentLoader.init('ajax/content.php'+queryString+'&ct='+Math.round(Math.random() * 100000), sauterReplaceContent)
}

function sauterReplaceContent() {
	if(document.all) { 
		launchJavascript(sauterContentLoader.xmlFeed);
	}
	
	var box = document.getElementById(contentBoxContent);
	box.innerHTML = sauterContentLoader.xmlFeed;
	syboGetById('submenu').style.display = 'block';
	
	
//	window.execScript(box);
//	var scripts = syboGetById('scriptGallery');
//	alert(scripts);
//	for(var i = 0; i < scripts.length; i++) {
//		alert(scripts[i].className+'--'+scripts[i].innerHTML);
//		if(scripts[i].className == "dynamic") {
//			alert(scripts[i].innerHTML);
//		}
//		eval(scripts[i].innerHTML);
//	}
	
	setTimeout(function() {
		sauterShowContentBoxes();
	}, 50);
}
function launchJavascript(responseText) {
	  // RegExp from prototype.sonio.net
	  var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
	           
	  var match    = new RegExp(ScriptFragment, 'img');
	  var scripts  = responseText.match(match);

    if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
            var match = new RegExp(ScriptFragment, 'im');
            js += scripts[s].match(match)[1];
        }
        eval(js);
    }
}

// Adapt background image dimensions
function sauterAdaptBackground() {
	// bg-image-id
	var imageId = 'slideshowImage';
	var tempImageId = 'slideshowBgImage';
	var bgImage = document.getElementById(imageId);
	var bgTempImage = document.getElementById(tempImageId);
	
//	bgTempImage.width = bgImage.width;
//	bgTempImage.height = bgImage.height;
//	bgTempImage.style.position = 'absolute';
//	bgTempImage.style.top = bgImage.style.top;
//	bgTempImage.style.left = bgImage.style.left;
//	
	var tempImage = new Image();
	tempImage.src = bgImage.src;
	var tempImage2 = new Image();
	tempImage2.src = bgTempImage.src;
	
	sauterAdaptBackgroundImage(bgImage, tempImage);
	sauterAdaptBackgroundImage(bgTempImage, tempImage2);
}

function sauterAdaptBackgroundImage(bgImage, tempImage) {
	var pageSize = getPageSize(); // width, height, xvis, yvis
	var center = true;
	
	var naturalImageWidth = tempImage.width;
	var naturalImageHeight = tempImage.height;
	
	//alert(naturalImageWidth + '-height: '+naturalImageHeight);
	//alert(pageSize[0]+'-'+pageSize[1]+'-'+pageSize[2]+'-'+pageSize[3]);
	
	if((naturalImageHeight / naturalImageWidth) != (bgImage.width / bgImage.height)) {
		bgImage.height = 0;
	}
	
	if(center) {
		bgImage.style.position = 'absolute';
	}
	
	if(bgImage.height > pageSize[3] && bgImage.width > pageSize[2]) {
		bgImage.width = pageSize[2];
		bgImage.height = bgImage.width/naturalImageWidth * naturalImageHeight;
		
		if(center) {
			bgImage.style.top = (- (bgImage.height - pageSize[3])/2) + 'px';
			bgImage.style.left = '0px';
		}
	}
	if(bgImage.height < pageSize[3]) {
		bgImage.height = pageSize[3];
		bgImage.width = bgImage.height/naturalImageHeight * naturalImageWidth;
		
		if(center) {
			bgImage.style.left = (- (bgImage.width - pageSize[2])/2)+'px';
			bgImage.style.top = '0px';
		}
	}
	if(bgImage.width < pageSize[2]) {
		bgImage.width = pageSize[2];
		bgImage.height = bgImage.width/naturalImageWidth * naturalImageHeight;
		
		if(center) {
			bgImage.style.top = (- (bgImage.height - pageSize[3])/2)+'px';
			bgImage.style.left = '0px';
		}
	}
}
window.onresize = function() { sauterAdaptBackground() };

// Slideshow
function sauterInitSlideshow() {
	sauterSlideshow = new SlideshowController();
	sauterSlideshow.setRandom(true);
	sauterSlideshow.init("modules/sybo_gallery/mediafeed.php?id=all",
			document.getElementById('background_wrapper'),
			document.getElementById('slideshowImage'),
			document.getElementById('slideshowBgImage'),
			document.getElementById('untertitelText'),
			document.getElementById('untertitelText2'),
			0);
	//slideshow.setInfoWindow(document.getElementById('slideshowControl')); 
}

// Box animations
function sauterBoxAnimations() {
	var boxLogo = 'box_left_logo';
	var box1 = 'box_left_1';
	
	var box1Height = 130;
	
	//sybo_toggle_vis(box1, false, 'hide');
	//sybo_toggle_vis(box2, false, 'hide');
	//sybo_toggle_vis(boxMain, false, 'hide');
	setTimeout(function() {
		sybo_toggle_vis_by_height(box1, 1000, box1Height);
	}, 500);
	
	setTimeout(function() {
		sauterShowContentBoxes();
	}, 1200);
}

function sauterShowContentBoxes() {
	var box2 = 'box_left_2';
	var boxMain = 'content';
	
	var box2Height = 130;
	var boxMainWidth = 280;
	
	var offset = 0;
	
	if(showBox2 && syboGetById(box2).offsetHeight != box2Height) {
		setTimeout(function() { 
			sybo_toggle_vis_by_height(box2, 1000, box2Height);	
		}, 50);
		offset += 800;
	}
	
	if(showBoxGallery) {
		if(syboGetById(boxGallery).offsetHeight != box2Height) {
//			sauterInitGalleryNavi();
			setTimeout(function() { 
				sybo_toggle_vis_by_height(boxGallery, 100, box2Height);	
			}, offset);
		}
	}
	if(showContentBox) {
		if(syboGetById(boxMain).width == 0 || syboGetById(boxMain).style.width == 0 || syboGetById(boxMain).style.width == '0px') {
			setTimeout(function() {
				sybo_toggle_vis_by_width(boxMain, 1000, boxMainWidth);
			}, offset);
		}
		setTimeout(function() {
				sybo_toggle_vis(contentBoxContent, true, 'show', 5);
				syboGetById(contentBoxContent).style.display = 'block';
		}, offset);	
		setTimeout(function() {
			syboGetById(boxMain).style.overflow = 'auto';
			if($.browser.msie) {
				syboGetById(contentBoxContent).style.removeAttribute('filter');
			}
		}, offset + 700);
	}
	
	if(showContentBox2) {
		var el = syboGetById('categoryBlock2')
		if(el != null){
			syboGetById(contentBox2Content).innerHTML = el.innerHTML;
		} else {
			syboGetById(contentBox2Content).innerHTML = '';
		}
		setTimeout(function() {
			sybo_toggle_vis_by_width(contentBox2, 1000, boxMainWidth);
		}, offset + 1000);
		
		setTimeout(function() {
			syboGetById(contentBox2).style.overflow = 'auto';
		}, offset + 1000 + 700);
	}
}

function sauterInitGalleryNavi(galleries) {
	sauterSlideshow.setRandom(false);
	syboGalleries = galleries;
	sauterSelectGallery(0, galleries[0].id);
//	
//	sauterSlideshow.reinit("modules/sybo_gallery/mediafeed.php?id="+syboGalleries[0].id);
//	
//	syboGetById('galleryNaviTitle').innerHTML = syboGalleries[0].title;
//	syboGetById('galleryNaviCategory').innerHTML = syboGalleries[0].category;
//	
//	syboGetById('galleryNaviPrev').onclick = function() {
//		sauterSelectGallery(syboGalleries.lentgh - 1, syboGalleries[syboGalleries.lentgh - 1].id);
//		return false;
//	};
//	syboGetById('galleryNaviNext').onclick = function() {
//		sauterSelectGallery(1 % syboGalleries.length, syboGalleries[1 % syboGalleries.length].id);
//		return false;
//	};
}
function sauterSelectGallery(index, id) {
	sauterSlideshow.reinit("modules/sybo_gallery/mediafeed.php?id="+id);
	
	syboGetById('galleryNaviTitle').innerHTML = syboGalleries[index].title;
	syboGetById('galleryNaviCategory').innerHTML = syboGalleries[index].category;
	syboGetById('galleryNaviPrev').onclick = function() {
		sauterSelectGallery((index + syboGalleries.length - 1) % syboGalleries.length, syboGalleries[(index + syboGalleries.length - 1) % syboGalleries.length].id);
		return false;
	};
	syboGetById('galleryNaviNext').onclick = function() {
		sauterSelectGallery((index + 1) % syboGalleries.length, syboGalleries[(index + 1) % syboGalleries.length].id);
		return false;
	};
}

// Ajax-Loader
//usind jquery.ajax
function SauterAjaxLoader() {
	var _this = this;
	var callBack = function() {};
	
	this.xmlFeed = '';
	
	this.init = function(xmlSrc, callBackF) {
		if(DEBUG) alert("Init model with " + xmlSrc);
		
		callBack = callBackF;
		
		// load xml-file
		jQuery.ajax({
			url: xmlSrc,
			dataType: "html",
			encoding:"UTF-8",
			beforeSend: xmlStart,
			success: xmlSuccess,
			error: xmlError,
			complete: xmlComplete
		});
		if(DEBUG) alert("after request launch");
	};
	
	function xmlSuccess(xmlFeed, status) {
		if(DEBUG) alert("Feed loaded. Start parsing.");
		_this.xmlFeed = xmlFeed;
		callBack.call();
	}
	function xmlStart(xhrInstance) { 
		 if(DEBUG) alert('xmlStart');
	}
	function xmlComplete(xhrInstance, status) { 
		 if(DEBUG) alert('xmlComplete');
	}
	function xmlError(xhrInstance, message, optional) {
		if(DEBUG) alert("Error: " + message + ' - ' + optional);
	}
	
	function xmlGetElements(xmlDoc, tag) {
		return xmlDoc.getElementsByTagName(tag);
	}
	function xmlGetItems(xmlItem, tag) {
		var items = new Array();
		tag = tag.toLowerCase();
		for(var i = 0; i < xmlItem.childNodes.length; i++) {
			if(xmlItem.childNodes[i].nodeName != "#" && xmlItem.childNodes[i].nodeName == tag) {
//				if(DEBUG) alert("Tag found: " + tag);
				items[items.length] = xmlItem.childNodes[i];
			}
		}
		return items;
	}
	function xmlGetValue(xmlItem, tag) {
		if(tag != undefined) {
			return xmlGetValue((xmlGetItems(xmlItem, tag))[0]);
		} else {
			if(xmlItem.childNodes != undefined && xmlItem.childNodes[0] != undefined) {
//				if(DEBUG) alert("NodeInfo:\n" + xmlItem.nodeName+" "+ xmlItem.childNodes[0].nodeValue);
				return xmlItem.childNodes[0].nodeValue.trim();
			} else {
				return null;
			}
		}
	}
	function xmlGetAttribute(xmlItem, tag) {
//		if(DEBUG) alert("Attribute: " + xmlItem.attributes.getNamedItem(tag).value);
		return xmlItem.attributes.getNamedItem(tag).value;
	}
}

var sauterContentLoader = new SauterAjaxLoader();

