var i,j,temp,cut,ul,li,liCollection,tools;
var aHref,img,imgSrc,tmpImg,titleStr,paramStr,iContainer,aCollection,loader,swirl;

function createElement(element) {
  if (typeof document.createElementNS != 'undefined') {
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
  if (typeof document.createElement != 'undefined') {
    return document.createElement(element);
  }
  return false;
}

function getUrlParam(aHref,param) {
	paramStr = '';
	temp = new Array();
	temp = aHref.split('?');
	temp = temp[1].split('&');
	for (i=0; i<temp.length; i++) {
		cut = new Array();
		cut = temp[i].split('=');
		if(cut[0]==param) {
			paramStr = cut[1];
			break;
		}
	}
	return paramStr;
}

function createLoader() {
	loader = document.createElement('div');
	loader.id = "loader";	
	iContainer.appendChild(loader);	
}

function switchImg(aHref) {
	imgSrc = getUrlParam(aHref,'photo');
	if(imgSrc && img) {
		if(!document.getElementById('loader')) {
			createLoader();
		}
		document.getElementById('loader').style.display = 'block';
		tmpImg = img.cloneNode(true);		
		tmpImg.src = '/_media/photos/' + imgSrc + '.jpg';
		//tmpImg.src = '/_media/photos/photo.php?photo=' + imgSrc + '.jpg';		
		img.parentNode.replaceChild(tmpImg,img);		
		img = iContainer.getElementsByTagName('img')[0];
		img.onload = function() {
			document.getElementById('loader').style.display = 'none';
		}
	}
}

function setCaller(li){
	ul = li.parentNode;
	liCollection = ul.getElementsByTagName('li');
	for(i=0;i<liCollection.length;i++){
		liCollection[i].className="";
	}
	li.className="on";
}

function setSwitcher() {
	iContainer = document.getElementById('photoZone');
	img = iContainer.getElementsByTagName('img')[0];
	if(iContainer) {
		aCollection = iContainer.getElementsByTagName('a');
		for(i=0; aCollection[i]; ++i) {
		   aCollection[i].onclick = function () {
		   		if(this.blur)
		   			this.blur();
				setCaller(this.parentNode);
				switchImg(this.href);                   
				return false;
			}
		}
	}
}

function setTools() {
	tools = document.getElementById('tools').getElementsByTagName('a');
	for(i=0;i<tools.length;i++){
		if(typeof window.print != 'undefined'){
			tools[i].style.display = "block";
			tools[i].onclick = function () {
				window.print();
				return false;
			}			
		}		
	}
}

var dimx,dimy,xPos,yPos,featur,xEcran=800,yEcran=600;
if(screen.width && screen.height){
	xEcran = screen.width;
	yEcran = screen.height;
}
function ow(aHref,type) {
	if(type == 'kontakt') {
		dimx=519; dimy=440;
	}
	if(type == 'anreise') {
		dimx=638; dimy=yEcran;
	}
	if(type == 'credits') {
		dimx=328; dimy=308;
	}
	xPos=(xEcran - dimx)/2; yPos=(yEcran - dimy)/2;
featur = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX="+xPos+",left="+xPos+",screenY="+yPos+",top="+yPos+",width="+ dimx + ",height=" + dimy + "";
	if(type == 'kontakt') {
		var zoomHTkontakt = window.open(aHref,'RemoteHTkontakt',featur);
		if (window.focus && zoomHTkontakt) {zoomHTkontakt.focus();}
	}
	if(type == 'anreise') {
		var zoomHTanreise = window.open(aHref,'RemoteHTanreise',featur);
		if (window.focus && zoomHTanreise) {zoomHTanreise.focus();}
	}
	if(type == 'credits') {
		var zoomHTcredits = window.open(aHref,'RemoteHTcredits',featur);
		if (window.focus && zoomHTcredits) {zoomHTcredits.focus();}
	}
	return false;
}

function setOws() {
	if(document.getElementById('t1')) {
		document.getElementById('t1').getElementsByTagName('a')[0].onclick = function () {
			ow(this.href,'kontakt');
			return false;
		}
	}
	if(document.getElementById('t2')) {
		document.getElementById('t2').getElementsByTagName('a')[0].onclick = function () {
			ow(this.href,'anreise');
			return false;
		}
	}
	if(document.getElementById('credits')) {
		document.getElementById('credits').getElementsByTagName('a')[1].onclick = function () {
			ow(this.href,'credits');
			return false;
		}
	}
}

function setLinks(linkTags){
	var linkCollection = [];
	for (var j = 0; linkTags[j]; j++) {
		linkCollection[j] = getByTag('',linkTags[j]);
		for (var i = 0; linkCollection[j][i]; i++) {
			if(/bookmark|external|corporate|sponsor|partner/.test(linkCollection[j][i].getAttribute('rel'))) {
				linkCollection[j][i].onclick = function(e) {
					if (!e) var e = window.event;
					if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey)
						return true;
					window.open(this.href);
					return false;
				}
			}
		}
	}
}

var searchCleared1, searchCleared2;
function initSearchField1() {
	document.getElementById('inpFrom').onfocus = function () {
		if(!searchCleared1) {
			this.value = '';
		}
		searchCleared1 = true;
	}
}
function initSearchField2() {
	document.getElementById('inpTo').onfocus = function () {
		if(!searchCleared2) {
			this.value = '';
		}
		searchCleared2 = true;
	}
}

function getById(el) {
	return document.getElementById(el);
}

function getByTag(par,el) {
	var par = (par == '') ? document.body : par;
	return par.getElementsByTagName(el);
}

function addClass(obj,newClass) {
	if(!obj.className.match(new RegExp(newClass)))
		obj.className+=(obj.className.length>0? " ": "") + newClass;
}
	
function removeClass(obj,oldClass) {
	obj.className=obj.className.replace(new RegExp("( ?|^)"+oldClass+"\\b"), "");
}

function getByClass(cla,par,el) {
	var par = (par == '') ? document.body : par;
	var el = (el == '') ? '*' : el;
	if(/.*native code.*/.test(document.getElementsByClassName)) {
		return par.getElementsByClassName(cla);
	}
	else {
		var tagColl = par.getElementsByTagName(el);
		trimedColl = new Array;
		for (var i = 0; tagColl[i]; i++) {
			if(tagColl[i].className.match(new RegExp(cla))) {
				trimedColl[trimedColl.length]=tagColl[i];
			}
		}
		return trimedColl;
	}
}

var addEvent = function() {
  if (window.addEventListener) {
    return function(el, type, fn) {
      el.addEventListener(type, fn, false);
    };
  } else if (window.attachEvent) {
    return function(el, type, fn) {
      var f = function() {
        fn.call(el, window.event);
      };
      el.attachEvent('on' + type, f);
    };
  }
}();

function setFancyFields(){
	var labelColl = getByTag(getById('page'),'label');
	for(var i = 0; i < labelColl.length; i++) {
		if(/fancy/.test(labelColl[i].className)) {
			var spanI = getByTag(labelColl[i],'span')[0];
			var fieldI = getByTag(labelColl[i],'input')[0];
			if( spanI && fieldI ){
				fieldI.value = fieldI.title = spanI.childNodes[0].nodeValue;
				addEvent(fieldI, 'focus', function(e) {
					if(this.value==getByTag(this.parentNode,'span')[0].childNodes[0].nodeValue)
						this.value='';
						addClass(this,'straight');
				});
			}
		}
	}
}


/* Init
-------------------- */
var init = function() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if(!document.getElementsByTagName)
		return;
	if(document.getElementById('photoZone'))
		setSwitcher();
	if(document.getElementById('tools'))
		setTools();
	if(document.getElementById('t1')||document.getElementById('t2'))
		setOws();
	if(document.getElementsByTagName)
		setLinks(['a','area']);
	if(document.getElementById('inpFrom'))
		initSearchField1();
	if(document.getElementById('inpTo'))
		initSearchField2();
	setFancyFields();
}

if (/WebKit/i.test(navigator.userAgent)) {
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			init();
		}
	}, 10);
}

else if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, null);
}

else {
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init();
		}
	};
	/*@end @*/
	window.onload = init;
}