/* äöüß
 * ----------------------------------------------------------------------
 * Author:	Michael Diekmann
 * Version:	1.0
 * Date:	2011-06-04
 * Copyright:	2011 by www.aikon-media.de
 * ----------------------------------------------------------------------
 */

	var rotatingImagePath = '/.images/rotating/';
	var rotatingImages = new Array();

	// Mainmenu Slider
	var sliderOpen		= new Array();
	var sliderOffset	= new Object();
	sliderOffset.top	= 420;
	sliderOffset.left	= 0;

	var menuOpen		= new Array();
	var menuButton		= new Array();
	var menuTimer		= new Array();

	// JSON postions
	var currentPagePos	= [
		{ 'page' : 'start',	'position' : '1px' },
		{ 'page' : 'klinik',	'position' : '66px' },
		{ 'page' : 'praxisfs',	'position' : '175px' },
		{ 'page' : 'praxismc',	'position' : '320px' },
		{ 'page' : 'leistung',	'position' : '465px' },
		{ 'page' : 'kontakt',	'position' : '493px' }
	];

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

// thePage = 'home', 'praxis', 'clinic', 'labor', 'contact'

function getImagesHeadline (thePage) {

	if (thePage == 'home') {
		rotatingImages = new Array(
			'images-101.jpg',
			'images-112.jpg',
			'images-140.jpg',
			'images-154.jpg',
			'images-162.jpg'
		);
	}
	else if (thePage == 'clinic') {
		rotatingImages = new Array(
			'images-171.jpg',
			'images-125.jpg',
			'images-129.jpg',
			'images-132.jpg',
			'images-143.jpg',
			'images-172.jpg'
		);
	}
	else if (thePage == 'praxis') {
		rotatingImages = new Array(
			'images-104.jpg',
			'images-109.jpg',
			'images-120.jpg',
			'images-151.jpg',
			'images-158.jpg'
		);
	}
	else if (thePage == 'labor') {
		rotatingImages = new Array(
			'images-186.jpg',
			'images-185.jpg',
			'images-191.jpg',
			'images-189.jpg'
		);
	}
	else if (thePage == 'contact') {
		rotatingImages = new Array(
			'images-115.jpg',
			'images-170.jpg',
			'images-162.jpg'
		);
	}
	else {
		rotatingImages = new Array(
			'images-101.jpg',
			'images-112.jpg',
			'images-140.jpg',
			'images-154.jpg',
			'images-162.jpg'
		);
	}

	if (rotatingImages.length > 1) {
		dojox.image.preload([rotatingImagePath + rotatingImages[1]]);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function rotateImageHeadline () {

	if (imageTimer != false) {
		window.clearTimeout(imageTimer);
		imageTimer = false;
	}


	if (rotatingImages.length > 0) {

		var nextImage = currentImage + 1;
		if ((nextImage+1) > rotatingImages.length) {
			nextImage = 0;
		}

		if (rotatingImages[currentImage] != rotatingImages[nextImage]) {

			dojo.byId('imageSrc1').setAttribute('src', rotatingImagePath + rotatingImages[currentImage]);
			dojo.byId('imageSrc2').setAttribute('src', rotatingImagePath + rotatingImages[nextImage]);

			currentImage = nextImage;

			// Preload the image after next
			if (rotatingImages.length > (nextImage+1)) {
				dojox.image.preload([rotatingImagePath + rotatingImages[nextImage+1]]);
			}

			var img1 = dojo.fadeOut({
				node:		dojo.byId('boxImage1'),
				duration:	1500,
				start:		1,
				end:		0
			});

			var img2 = dojo.fadeIn({
				node:		dojo.byId('boxImage2'),
				duration:	2000,
				start:		0,
				end:		1
			});

			dojo.fx.combine([img1, img2]).play();
		}

		imageTimer = window.setTimeout("rotateImageHeadline()", timerRotatingImages);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function switchMenu (theOption, theMenuId, theSliderId, closeSlider) {

	if (theOption == 'show') {

		// Set the Menu to the starting position of the Slider
		var pos = getPositionOfElement(theSliderId);
		var offset = getPositionOfElement('boxNavigation');
		dojo.byId(theMenuId).style.top = (pos.top - offset.top + sliderOffset.top) +'px';
		dojo.byId(theMenuId).style.left = (pos.left - offset.left + sliderOffset.left) +'px';


		// Close all open slider
		for (var i in menuOpen) {
			if ( (menuOpen[i] == true) && (i != theSliderId) ) {
				if ( menuTimer[theSliderId] ) {
					clearMenuTimeout(i);
				}
				dojo.byId(sliderOpen[i]).style.display = 'none';
				menuOpen[i] = false;
				switchButton(i, false);
			}
		}

		if (menuOpen[theSliderId] != true) {

			// Add the open status
			menuOpen[theSliderId] = true
			sliderOpen[theSliderId] = theMenuId;

			var fade = dojo.fadeIn({
				node:		dojo.byId(theMenuId),
				duration:	150,
				start:		0,
				end:		0.9
			});

			var wipe = dojo.fx.wipeIn({
				node:		dojo.byId(theMenuId),
				easing:		dojo.fx.easing.linear,
				duration:	300
			});

			dojo.fx.combine([fade, wipe]).play();
		}
	}
	else {
		if (menuOpen[theSliderId] == true) {

			// Remove the open status
			menuOpen[theSliderId] = false;

			var fade = dojo.fadeOut({
				node:		dojo.byId(theMenuId),
				duration:	300,
				start:		0.9,
				end:		0,
				beforeBegin:	function () {
					clearMenuTimeout(theSliderId);
				},
				onEnd:		function () {
					dojo.byId(theMenuId).style.display = 'none';
					if (closeSlider == true) {
						switchButton(theSliderId, false);
					}
				}
			});

			var wipe = dojo.fx.wipeOut({
				node:		dojo.byId(theMenuId),
				easing:		dojo.fx.easing.linear,
				duration:	300
			});

			dojo.fx.combine([fade, wipe]).play();
		}
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function clearMenuTimeout (theSliderId) {
	window.clearTimeout(menuTimer[theSliderId]);
	menuTimer[theSliderId] = false;
	document.body.onclick = function (event) {};
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function setMenuTimeout (theMenuId, theSliderId, closeSlider) {
	if (menuTimer[theSliderId] == false) {
		menuTimer[theSliderId] = window.setTimeout("switchMenu('close','"+ theMenuId +"','"+ theSliderId +"',"+ closeSlider +")", 800);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function switchButton (theObject, theAction) {
	if (menuOpen[theObject] == true) {
		return;
	}

	if ( (theAction == false) && (menuButton[theObject] == true) ) {
		return;
	}
	else if (theAction == false) {
		if (theObject == 's00') {
			dojo.byId(theObject).className = 'button-start-off';
		}
		else if (theObject == 's01') {
			dojo.byId(theObject).className = 'button-klinik-off';
		}
		else if (theObject == 's02') {
			dojo.byId(theObject).className = 'button-praxisfs-off';
		}
		else if (theObject == 's03') {
			dojo.byId(theObject).className = 'button-praxismc-off';
		}
		else if (theObject == 's04') {
			dojo.byId(theObject).className = 'button-leistung-off';
		}
		else if (theObject == 's05') {
			dojo.byId(theObject).className = 'button-kontakt-off';
		}
	}
	else {
		if (theObject == 's00') {
			dojo.byId(theObject).className = 'button-start-on';
		}
		else if (theObject == 's01') {
			dojo.byId(theObject).className = 'button-klinik-on';
		}
		else if (theObject == 's02') {
			dojo.byId(theObject).className = 'button-praxisfs-on';
		}
		else if (theObject == 's03') {
			dojo.byId(theObject).className = 'button-praxismc-on';
		}
		else if (theObject == 's04') {
			dojo.byId(theObject).className = 'button-leistung-on';
		}
		else if (theObject == 's05') {
			dojo.byId(theObject).className = 'button-kontakt-on';
		}
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function getPositionOfElement (theId) {
	var elem = dojo.byId(theId);
	var tagname = '';

	var position = new Object();
	position.top = 0;
	position.left = 0;

	if ( (typeof elem == 'object') && (typeof elem.offsetTop != 'undefined') ) {
		while (elem && elem.tagName != 'BODY') {
			position.top  += parseInt( elem.offsetTop );
			position.left += parseInt( elem.offsetLeft );
			elem = elem.offsetParent;
		}
	}

	return position;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function DeCo ( e, o, s ) {
	var n = 0;
	var r = '';
	for( var i = 0; i < s.length; i++) {
		n = s.charCodeAt( i );
		if( n >= e ) {
			n = o;
		}
		r += String.fromCharCode( n - 1 );
	}
	return r;
}

function MaToDeCo ( e, o, s ) {
	location.href = DeCo( e, o, 'nbjmup;' ) + DeCo( e, o, s );
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function showLightbox (theWidth, theHeight, theUrl) {

	dojo.byId('boxDarklayer').style.width = document.getElementsByTagName("body")[0].offsetWidth +'px';
	dojo.byId('boxDarklayer').style.height = (document.getElementsByTagName("body")[0].offsetHeight + 20) +'px';
	dojo.byId('boxDarklayer').setAttribute('onclick', 'hideLightbox()');
	dojo.byId('boxDarklayer').style.display = 'block';


	var theInnerWidth = 0;
	var theInnerHeight = 0;
	var thePageYOffset = 0;
	if (window.innerWidth){
		theInnerWidth = window.innerWidth;
		theInnerHeight = window.innerHeight;
	}
	else{
		var objBody = (window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
		theInnerWidth = objBody.clientWidth;
		theInnerHeight = objBody.clientHeight;
	}

	if (window.pageYOffset) {
		thePageYOffset = window.pageYOffset;
	}
	else {
		thePageYOffset = (document.body.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop || null;
	}


	var result = ajaxRequest(theUrl, 'text');


	// Set content
	dojo.byId('boxLightbox').innerHTML = '<div class="header"><a href="javascript:void(0)" onClick="hideLightbox()" style="position:absolute;top:8px;left:10px;display:block">Fenster schließen</a></div><div style="padding:10px;padding-right:15px">'+ result +'</div>';

	// Set position
	dojo.byId('boxLightbox').style.top = parseInt(((theInnerHeight - theHeight) / 2) + thePageYOffset) +'px';
	dojo.byId('boxLightbox').style.left = parseInt((theInnerWidth - theWidth) / 2) +'px';
	dojo.byId('boxLightbox').style.width = theWidth +'px';
	dojo.byId('boxLightbox').style.height = theHeight +'px';

	dojo.fadeIn({
		node:		dojo.byId('boxLightbox'),
		duration:	500,
		start:		0,
		end:		1,
		beforeBegin:	function () {
			dojo.byId('boxLightbox').style.display = 'block';
		}
	}).play();


	return false;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function ajaxRequest (theUrl, handleAs) {

	// Do a XMLHttpRequest here
	var result = '';
	dojo.xhrGet({
			url: theUrl,
			sync: true,
			handleAs: handleAs,
			timeout: 60000,
			load: function(response, ioArgs) {

//			alert(theUrl +' '+ ioArgs.xhr.status +' '+ response);
			result = response;

			// Dojo recommends that you always return(response); to propagate
			// the response to other callback handlers. Otherwise, the error
			// callbacks may be called in the success case.
			return response;
		},
		error: function(response, ioArgs)
		{
			alert("Fehler:\nInhalt von "+ theUrl +" konnte nicht geladen werden...");
			return response;
		}
	});

	return result;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function hideLightbox () {

	dojo.byId('boxDarklayer').style.display = 'none';
	dojo.byId('boxLightbox').style.display = 'none';

	return false;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function handleCallback (theUrl) {

	var result = ajaxRequest(theUrl, 'text');
	dojo.byId('boxCallback').innerHTML = result;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function setCurrentPage (thePos, theTitle) {

	for (var i = 0; i < currentPagePos.length; i++) {
		if (currentPagePos[i].page == thePos) {
			dojo.byId('boxCurrentPage').style.left = currentPagePos[i].position;
			break;
		}
	}
	dojo.byId('boxCurrentPage').style.top = '58px';
	dojo.byId('boxCurrentPage').innerHTML = '<h1>'+ theTitle +'</h1>';
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function createMap (theElement, theLat, theLon, theAddress, theLink) {

	var geocoder = null;
	var baseCountryCode = 'DE';
	var defaultBeginZoom = 15;
	var defaultMaxZoom = 19;
	var defaultMinZoom = 5;
	var markers = new Array();

	var pos = new google.maps.LatLng(theLat, theLon);

	var myOptions = {
		zoom:			defaultBeginZoom,
		center:			pos,
		mapTypeId:		google.maps.MapTypeId.ROADMAP,
		mapTypeControl:		true,
		mapTypeControlOptions:	{
			style:		google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		scaleControl:		true,
		disableDoubleClickZoom:	false,
		scrollwheel:		false
	};
	map = new google.maps.Map(dojo.byId(theElement), myOptions);

	// Now attach the coordinate map type to the map´s registry
//	map.mapTypes.set('lanala', google.maps.MapTypeId.PHYSICAL);


	// Restricting the range of Zoom Levels
	google.maps.event.addListener(map, 'zoom_changed', function() {
		zoomLevel = map.getZoom();
		if (zoomLevel > defaultMaxZoom) {
			map.setZoom(defaultMaxZoom);
		}
		else if (zoomLevel < defaultMinZoom) {
			map.setZoom(defaultMinZoom);
		}
	});


	marker = new google.maps.Marker({
		position:	pos,
		map:		map,
		zIndex:		1
	});
	info = new google.maps.InfoWindow({
		content:	'<p style="margin-right:20px">'+ theAddress +'<br><a href="'+ theLink +'" target="_blank">Route berechnen</a><br><br></p>',
		maxWidth:	320
	});
	google.maps.event.addListener(marker, 'click', function() {
		info.open(map, marker);
	});
	info.open(map, marker);
}

