var window_loaded = false;
var obj_titlepictures = null;
var current_pic = 0;
var titlepictures_loaded = new Object();
var gallery = null;
var standardWidth = 760;
var standardHeight = 500;

window_oldonload = window.onload;
window.onload = window_onload;
window.onresize = window_onresize;

function getAdr(prefix, postfix, text) {
        document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}

function swapImage(element, newimage) {
	var oldsrc = element.src
	element.src = newimage
	if (!element.onmouseout)
		element.onmouseout = function (event) { swapImage(this, oldsrc); };
}

var sel;
function HideSelects(x,y,w,h) {
	if(xIE4Up && !xMac) {
		var selx, sely, selw, selh, i
		if(!sel)
			sel = document.getElementsByTagName("SELECT");
		for(i = 0; i < sel.length; i++) {
			selx = xPageX(sel[i]);
			sely = xPageY(sel[i]);
			selw = sel[i].offsetWidth;
			selh = sel[i].offsetHeight;
			sel[i].style.visibility = (selx + selw > x && selx < x + w && sely + selh > y && sely < y + h) ? "hidden" : "visible";
		}
	}
}

function hideSelectBoxes(){
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden";}
}

function showSelectBoxes(){
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible";}
}

// deletes leading and trailing spaces in a string - adds the function directly to the String Object, so that all strings inherit this method
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
}

function checkEmail(val) {
	if (val) {
		var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
		var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
		var regex = "^"+usr+"\@"+domain+"$";
		var myrxp = new RegExp(regex);
		var check = (myrxp.test(val));
		if (check!=true) {
			return false;
		}
		else {
			return true;
		}
	}
}

/*
validates formfields if they have a value or not
to check for other options do the following
specialfields = new Object();
specialfields.fieldname = new Object();
specialfields.fieldname.check1 = 'function_to_call,error_message';
specialfields.fieldname.check2 = 'second_function_to_call,second_error_message';
specialfields.another_fieldname = new Object();
specialfields.another_fieldname.check1 = 'function_to_call,error_message';
*/
function validateForm(form,specialfields) {
	var errors = new Array();
	var fields = form.getElementsByTagName('label');
	for (i = 0; i < fields.length; i++) {
		var span = fields[i].getElementsByTagName('span')[0];
		if (span) {
			var label = span.firstChild.data;
			label = label.trim();
			// if there is a '*' in the label - this indicates the inputfield has to be filled
			if (label.charAt(label.length - 1) == '*'){
                                label = label.substring(0, label.length - 1).trim();
				// get the inputfield
				var obj_input = fields[i].getElementsByTagName('input');
				if (!obj_input[0])
					obj_input = fields[i].getElementsByTagName('select');
				if (!obj_input[0])
					obj_input = fields[i].getElementsByTagName('textarea');

				// if there is an inputfield
				if (obj_input && obj_input[0]) {
					input = obj_input[0];
					error = false;
					
					// check if the inputfield has a value
					if (!input.value || input.value.trim().length==0) {
						error = true;
						errors.push(label + ' nicht eingegeben');
					}
					
					// check the inputfield for special things (email, ...)
					if (!error && specialfields[input.name]){
						specialfield = specialfields[input.name];
						for (check in specialfield){
							check_function = specialfield[check].split(',')[0];
							check_message = specialfield[check].split(',')[1];
							if (!eval(check_function)(input.value)){
								error = true;
								errors.push(label + ' ' + check_message);
							}
						}
					}
		            
					// on error give the label the className 'error' otherwise delete the className 'error' (if exists)
					if (error){
						className = fields[i].className;
						if (className.length>0){
							className = className + ' ';
						}
						fields[i].className = className + 'error';
					} else {
		            	className = fields[i].className;
		                if (className.indexOf('error')>-1){
							className = className.replace(' error', '');
							className = className.replace('error', '');
							fields[i].className = className;
						}
		            }
				}
			}
		}
	}
    return errors;
}


function showFormErrors (errors) {
	error_message = '';
	for (i=0;i<errors.length;i++){
		error_message += errors[i] + '\n';
	}
	alert(error_message);
}

function search_submit(obj) {
    if (obj && obj.words && obj.words.value.trim().length>0){
        return true;
    }
    return false;
}

function window_onload() {
    window_loaded = true;
    if (titlepictures.length>1){
        titlepictures_init();
    }
    if (window_oldonload){
        window_oldonload();
    }
    
    window_onresize();
    
}

function window_onresize() {
    if (window_loaded){
        if (myHouseGallery && HouseGallery_started){
            myHouseGallery.onResize();
        }
        if (myGallery && Gallery_started){
            myGallery.onResize();
        }
    }
}

function titlepictures_init() {
    obj_titlepictures = document.getElementById('titlepictures');
    current_pic = Math.round(Math.random()*(titlepictures.length-1));
    titlepictures_handler();
}

function titlepictures_handler () {
    next_pic = current_pic + 1;
    if (next_pic>=titlepictures.length){
        next_pic = 0;
    }
    current_pic_id = 'titlepicture_' + current_pic;
    next_pic_id = 'titlepicture_' + next_pic;
    if (titlepictures_loaded[next_pic_id]){
        if (titlepictures_loaded[next_pic_id].loaded){
            if (titlepictures_loaded[current_pic_id]){
                titlepictures_loaded[current_pic_id].opacity_effect.custom(1,0);
            }
            titlepictures_loaded[next_pic_id].opacity_effect.custom(0,1);
            if (current_pic>=titlepictures.length-1){
                current_pic = 0;
            }else{
                current_pic++;
            }
            window.setTimeout(titlepictures_handler,10000);
        }
        else{
            window.setTimeout(titlepictures_handler,1000);
        }
    }
    else{
        temp_obj = document.createElement('img');
        temp_obj.id = next_pic_id;
        temp_obj.src = titlepictures[next_pic];
        temp_obj.alt = 'Sujet';
        temp_obj.style.visibility = 'hidden';
        obj_next_pic = obj_titlepictures.appendChild(temp_obj);
        titlepictures_loaded[next_pic_id] = new Object();
        titlepictures_loaded[next_pic_id].loaded = false;
        obj_next_pic.onload = function (event) { titlepictures_loadhandler(this); };
        if (titlepictures_loaded[current_pic_id]){
            window.setTimeout(titlepictures_handler,8000);
        }else{
            window.setTimeout(titlepictures_handler,100);
        }
    }
}

function titlepictures_loadhandler(obj) {
    titlepictures_loaded[obj.id].obj = obj;
    titlepictures_loaded[obj.id].opacity_effect = $(obj.id).effect('opacity');
    titlepictures_loaded[obj.id].loaded = true;
}

function gallery_open(galleryurl) {
    standardWidth = 760;
    standardHeight = 500;
    optionstring = 'width=' + standardWidth + ',height=' + standardHeight + ',resizable=yes';
    window.open(galleryurl,'gallery',optionstring);
}

function partneronmap_onmouseover(element,id) {
    document.getElementById('partneronmapinfo_' + id).style.visibility='visible';
    if (!element.onmouseout){
        element.onmouseout = function (event) {partneronmap_onmouseout(id)}
    }
}

function partneronmap_onmouseout(id){
    document.getElementById('partneronmapinfo_' + id).style.visibility='hidden';
}

function swapImage(element, newimage) {
	var oldsrc = element.src
	element.src = newimage
	if (!element.onmouseout)
		element.onmouseout = function (event) { swapImage(this, oldsrc); };
}