/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
// -----------------------------------------------------------------------------------
/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'rel' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'extern' : val;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oLink.onclick = JSTarget.openWin;
				}
			}
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	}
};
// -----------------------------------------------------------------------------------
//	PageTrans v 0.2custom - 2008-02-25
//	by Sindre Wimberger - wimberger@sindre.at
//
var PageTrans = {	
	clickstate: 0,
	init: function(ContainerID,effect,loadStatus,loadLocation,loadID,LoadContent) {
		PageTrans.ContainerID = ContainerID;
		PageTrans.EffectContainerID = ContainerID;
		PageTrans.fx = effect;
		PageTrans.LStatus = loadStatus; 
		if (loadLocation == "" || loadLocation == null) {PageTrans.LLocation = "body";} else {PageTrans.LLocation = loadLocation;}
		PageTrans.LID = loadID;
		PageTrans.LContent = LoadContent; 
		PageTrans.getLinks();
	},
    getLinks: function(contentid) {
		if(PageTrans.ContainerID) {
			if (PageTrans.LStatus == true) {PageTrans.LoaderInit();}
			$$('#detailAJAX a','#detailAJAX2 a','#c-snav a').invoke('observe', 'click', 
			 function(event){
				Event.stop(event);
				if (PageTrans.clickstate==0) {
					PageTrans.clickstate = 1;
					PageTrans.disappearFX(this.getAttribute('href'));	
					PageTrans.removeClass(this);
					Event.element(event).up('li').addClassName('active');
				}
			});
		} else {
			PageTrans.init();
		}
	}, 	
    removeClass: function(el) {
		el.up('li').siblings().invoke('removeClassName', 'active');
//		$$('#detailAJAX .active').invoke('removeClassName', 'active');
	}, 	
    request: function(url,params) {
		if (PageTrans.LStatus == true) {PageTrans.LoaderAppear();}		
			params = 'ajax=1';
		new Ajax.Request(url, {
		 	parameters : params,
			method: 'get',
			onFailure : function(resp) {PageTrans.redirect(url,resp.statusText);},
			onException : function(resp,ex) {PageTrans.redirect(url,ex);},
			onComplete:function(req){	
				PageTrans.insert(req.responseText);
		}});
	},
    insert: function(txt) {
		$(PageTrans.ContainerID).innerHTML = txt;
		if (PageTrans.LStatus == true) {
			PageTrans.LoaderDISAppear();			
		} else {
        	PageTrans.appearFX();
		}
	},
    disappearFX: function(url) {
		if (PageTrans.fx==1) {new Effect.Fade(PageTrans.EffectContainerID,
									 { duration:'0.2',
 									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (PageTrans.fx==2) {new Effect.BlindUp(PageTrans.EffectContainerID,
									 { duration:'0.8',
									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (PageTrans.fx==3) {new Effect.SlideUp(PageTrans.EffectContainerID,
									 { duration:'1.2',
									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (PageTrans.fx==4) {new Effect.Shrink(PageTrans.EffectContainerID,
									{ duration:'1.2',
									  afterFinish: function(){PageTrans.request(url)},
									  queue: { scope: 'aktive', position: 'front'}
									});
		}
		if (PageTrans.fx==5) {PageTrans.request(url);}
		
	},
    appearFX: function() {
		if (PageTrans.fx==1) {new Effect.Appear(PageTrans.EffectContainerID,
									 { duration:'0.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (PageTrans.fx==2) {new Effect.BlindDown(PageTrans.EffectContainerID,
									 { duration:'0.8',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (PageTrans.fx==3) {new Effect.SlideDown(PageTrans.EffectContainerID,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (PageTrans.fx==4) {new Effect.Grow(PageTrans.EffectContainerID,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (PageTrans.fx==5) {new Effect.Pulsate(PageTrans.EffectContainerID,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
	},
    LoaderInit: function() {
		if (!$(PageTrans.LID)){
			if (PageTrans.LLocation=="body") {var objBody = document.getElementsByTagName("body").item(0);}
			else {var objBody = $(PageTrans.LLocation);}
			objLoading = document.createElement("div");
			objLoading.setAttribute('id',PageTrans.LID);
			objLoading.style.display = "none";
			objLoading.innerHTML = PageTrans.LContent;
			objBody.appendChild(objLoading);
		} else {
			objLoading = $(PageTrans.LID);			
			objLoading.style.display = "none";
		}		
	},	
    LoaderAppear: function() {
		objLoading.style.display = "block";
	},
    LoaderDISAppear: function() {
		objLoading.style.display = "none";
		PageTrans.appearFX();
	},
    redirect: function(url,res) {
		alert(res);
//		window.location.href = url;
	},
    menuAccordion: function() {
		$$('#menu li div').invoke('setStyle', {display: "none"});	
		$$('#menu > li').invoke('setStyle', {opacity:'10'});
		$$('#menu li h3.ntoggle')[0].addClassName('active');
		/*$$('#menu li')[0].addClassName('active');*/
		$('menu').addClassName('menujs');	
		PageTrans.menu = new Effect.Accordion("menu", {duration: '0.4', eventlistener:'click',
					downBeforestart: function(e) { 
//					   e.element.up().addClassName('active');
					   e.element.up().addClassName('active').setStyle({opacity:'10'});
					},
					upAfterfinish: function(e) { 
//					   e.element.up().removeClassName('active');
					   e.element.up().removeClassName('active').setStyle({opacity:'10'});
	       			}
			});
		PageTrans.menuinit = false;		
	},

	functionJS: function(){
		this.clickstate = 0;				
		myLightbox.initialize();
	}
};

// -----------------------------------------------------------------------------------
function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The "+element.name+" field must be a valid email address.");
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}

//----------------------------Roman Test
// -----------------------------------------------------------------------------------
//	SelectToggle v 0.1 - 2008-03-27
//	by Sindre Wimberger - wimberger@sindre.at
//
var SelectToggle = {
	init: function(id) {
		if(!$(id)) { return; }
		this.container = $(id);
//		Event.observe(this.container, 'click', this.show.bindAsEventListener(this));
		Event.observe(this.container, 'mouseout', this.delayedHide.bindAsEventListener(this));
		Event.observe(this.container, 'mouseover', this.show.bindAsEventListener(this));
	},
	show: function(e) {
		Event.stop(e);
		this.clearHide();
		if (this.visible) {
//			this.hide();
		}
		else {
//			try {
//				Effect.Appear(this.container.down('ul'), {duration:0.3});
//			} catch(e) {
//				Element.show(this.container.down('ul'));
//			}
			this.visible = true;
			this.container.toggleClassName('active');
		}
	},
	hide: function(){
		if (this.visible) {
//			try {
//				Effect.Fade(this.container.down('ul'), {duration:0.3});
//			} catch(e) {
//				Element.hide(this.container.down('ul'));
//			}
			this.visible = false;
			this.container.toggleClassName('active');
		}
	},
	delayedHide: function(e) {
		if (this.visible){
			this.clearHide();
			this.delay = setTimeout(this.hide.bind(this), 350);
		}
	},
	clearHide: function(){
		if (this.delay) {
			clearTimeout(this.delay);
			this.delay = null;
		}
	}
};

// -----------------------------------------------------------------------------------
document.observe("dom:loaded", function() {		
	if($("menu")) {
		PageTrans.menuAccordion();
	}
	// Ajax Übergänge
	if($('detailAJAX') || $('c-snav')) {
//		PageTrans.init("c-content",1,false,"","pagetransloading",'<p class="center"><img src="/media/kube/assets/content/ajax-loader.gif" alt="Laden" /><br /><br />Seite wird geladen...</p>');	
	}
	if ($('print')) {
		$('print').show();
	}
//	initLightbox();
	JSTarget.init();
	prepareForms();
	focusLabels();
	SelectToggle.init('refselect');
	googlemaps();
});
// -----------------------------------------------------------------------------------

function googlemaps() {
 if(document.getElementById("googlemap")) {	
 document.getElementById("googlemap").style.width = "100%";
 document.getElementById("googlemap").style.height = "25em";
 document.getElementById("googlemap").style.marginTop = "2em";
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("googlemap"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(48.226430946874416, 16.351840496063232), 16);
	// Create our "tiny" marker icon
	var icon = new GIcon();
	icon.image = "/assets/content/icon-googlemaps.png";
//		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(50, 50);
//		icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(10, 35);
	icon.infoWindowAnchor = new GPoint(5, 1);		
	// Creates a marker at the given point with the given number label
	function createMarker(point) {
	  var marker = new GMarker(point, icon);
	  GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml('<strong>Sanatorium Hera</strong><br /> 1090 Wien<br />L&ouml;blichgasse 14 ');
	  });
	  return marker;
	}				
   map.addOverlay(createMarker(new GLatLng(48.226430946874416, 16.351840496063232)));
  }
 }
}
