/////////////// browser detection //////////////////////

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")!=-1) );

var MAC = navigator.platform.indexOf("Mac") != -1;

// what document model does the browser use?
if (document.getElementById) dById = true;
else if (document.all) var dAll = true;

// popup settings 
var inactive_state = '0px';
var active_state = '105px';

/////////////// init //////////////////////
var initOk = false; // main variable to enable functions
var loginPopToolbar;
var searchPopToolbar;
var lostPassPopToolbar;
var container;
var featureExists;
var featurePreloaded;

function init() {
	// registering obejcts in the page
	loginPopToolbar = getObj('loginPopToolbar');
	searchPopToolbar = getObj('searchPopToolbar');
	lostPassPopToolbar = getObj('lostPassPopToolbar');
	container = getObj('container');

	// we're ready  
	initOk = true;

	// starting the preload of feature images (if a feature exists)
	if (featureExists) featurePreloadImg();

}

//////////////////////////// common functions ////////////////////////////////////
// open a window with a specific size
function openWin(url,winName,wW,wH,opts) {
	if(!opts) opts = "toolbar=1,location=1,directories=0,status=1,menuBar=1,resizable=1,scrollBars=1";
	if (wW && wH) {
		sW = screen.availWidth;
		sH = screen.availHeight;
		wW = parseInt(wW) + 20; // add marginright
		wH = parseInt(wH) + 30; // add marginbottom

		// compare the req. window size with screen size (minus margin of 20 px).
		sbW=0;
		sbH=0;

		if (sW < wW-60) { // screen-width is smaller than requested window-width
			wW = sW - 60; // set window-width to screen-width minus a small tolerance-margin
			sbH = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to height
		}

		if (sH < wH-60) { // screen-height is smaller than requested window-height
			wH = sH - 60; // set window-height to screen-height minus a small tolerance-margin
			sbW = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to width;
		}

		// if window is smaller than requested show scrollbars
		// if (sbW || sbH) opts = opts + ",scrollBars=1";
		// else opts = opts + ",scrollBars=0";

		// add scrollbars to width and height if they appear
		wW = wW+sbW;
		wH = wH+sbH;
		wleft = ((screen.width-wW)/2)-15;
		wtop = ((screen.height-wH)/2)-15;
		opts = "width="+wW+",height="+wH+",left="+wleft+",top="+wtop+opts;
	}

	var win = window.open(url,"_blank",opts); // use this if you want a special window look
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}


//// form functions ////
function seaFieldClear(aField) {
	if(aField.value == "Suche") {
		aField.value = "";
	}
}

function seaFormCheck(aForm) {
	var seaWord = aForm.seaWord.value;

	if (aForm) {
		if (seaWord.length == 0) {
			alert(seaNoChars);
			return false;
		}
		else if (seaWord.length < 3) {
			alert(seaFewChars);
			return false;
		}
	}
	else {
		alert("Es ist ein Problem beim Überprüfen des Formulars aufgetreten.\nBitte informieren Sie ar@aspekt1.net.\n\nAn error occured while checking the form.\nPlease inform ar@aspekt1.net.");
		return false;
	}
	aForm.submit();
	return true;
}

function loginFormCheck(aForm) {
	var login = aForm.login.value;
	var pass = aForm.pass.value;
	if (aForm) {
		if (login.length == 0 || pass.length == 0) {
			alert(loginFormError);
			return false;
		}
	}	
	else {
		alert("Es ist ein Problem beim Überprüfen des Formulars aufgetreten.\nBitte informieren Sie ar@aspekt1.net.\n\nAn error occured while checking the form.\nPlease inform ar@aspekt1.net.");
		return false;
	}
	aForm.submit();
	return true;
}



// feature area functions                     
function cah(elem) {
	elem.style.backgroundColor = "#f4f4ed";
	return true;
}

function navOver(elem,k) {
	for (var i = 1; i <= 7; i++) document.getElementById("box"+i).style.backgroundColor = "#FFFFFF";
	elem.style.backgroundColor = "#f4f4ed";
	if (featurePreloaded) eval('document.images.featureImg.src = img'+k+'_on.src');
}

function navOut(elem) {
	elem.style.backgroundColor = "#FFFFFF";
	cah(elem);
}

// converting a name into an element
function getObj(name) {
	if (dById && document.getElementById(name))
		el = document.getElementById(name);
	else if (dAll && document.all[name])
		el = document.all[name];
	else el = false;
	return el;
}

// popping up the toolbar area and showing the requested form 
function popToolbar(el, hide, hide2) {
	// if browser can't use DHTML or page is not loaded complete we return
	if (!initOk || !el) return;

	if (el.style.visibility == 'visible') {
		container.style.top = inactive_state;
		el.style.visibility = 'hidden';
	} 
	else {
		container.style.top = active_state;
		el.style.visibility = 'visible';
		if (hide) hide.style.visibility = 'hidden';
		if (hide2) hide2.style.visibility = 'hidden';
	}
}

function ssafe(ssafe0, ssafe1, name) {
	if (name) {
		document.write('<a href=\"mailto:'+ssafe0+'@'+ssafe1+'\">');
		document.write(name+'</a>');
	}
	else {
		address = ssafe0+'@'+ssafe1;
		// add space for huge addresses (Firefox soft line break bug)
		if (address.length > 30) {
			address = address.replace(/@/, '@ ');
		}
		document.write('<a href=\"mailto:'+ssafe0+'@'+ssafe1+'\">');
		document.write(address+'</a>');
	}
	
}

function showBlock(id) {
	// if browser can't use DHTML or page is not loaded complete we return
	if (!initOk || !el) return;
	el = document.getElementById(id);
	el.style.visibility = 'visible';
	el.style.display = 'block';
}
function hideBlock(id) {
	// if browser can't use DHTML or page is not loaded complete we return
	if (!initOk || !el) return;
	el = document.getElementById(id);
	el.style.visibility = 'hidden';
}


// mdSlideshow

//
// Image functions
//

// set attributes for preloading the images
var imgPreload = new Array();
var imgCount = 0;

function preloadImage(fn,id) {
	imgPreload[id] = new Image();
	imgPreload[id].onload = function() {
		$(id).setAttribute('rel','ok');
	}
	$(id).setAttribute('rel','loading');
	imgPreload[id].src = fn;
	imgCount++;
}

// preload the images
function preloadImages(tagId) {
	var first = true;
	$$("#slideshow img").each(function(value,index) {
		preloadImage(value.src,value.id);
	});
}

// show the images
function showImg(idWithoutPrefix) {
	// fade in the right image

	var imgId = "img"+idWithoutPrefix;
	
	if ($(imgId)) {
		if ($(imgId).getAttribute('rel') != 'ok') {
			// if image isnt loaded yet, show loading image and try again later
			$('slidepreloading').setStyle({'display': 'block'});
			setTimeout(function() { showImg(idWithoutPrefix); }, 100) ;
		} else {
			// hide the loading image
			$('slidepreloading').setStyle({'display': 'none'});
			// fade-in
			$$("#slideshow img").each(function(value, index) {
				if(value.getAttribute('rel') == 'ok')
					new Effect.Fade(value.id,{duration:0.5, queue:{scope:'showreal'}});
			});
			// fade-out
			new Effect.Appear(imgId, {duration:0.8, queue:{position:'end',scope:'showreal'}});
		} 
	}
	
}

// start gallery with loop
var currentGalleryImgId = 0;


function showreel() {
	switchreel(); // call fade-in for first image without delay
	window.setInterval("switchreel()", 6000);	// switch every n milliseconds
}
function switchreel() {
	showImg(currentGalleryImgId);
	currentGalleryImgId++;
	// looping through the images
	// alert(currentGalleryImgId+" - "+imgCount);
	if (currentGalleryImgId >= imgCount) currentGalleryImgId = 0;
}