/* **************************************************
   *                                                *
   *    Copyright © 2006-2011 by Dominic Griesel    *
   *                                                *
   ************************************************** */




function enableHover() {
    for (var i = 0; i < document.images.length; i++) {
        var img = document.images[i];
        if (img.className == "navi") {
            // Events verknüpfen
            img.onmouseover = swapImage;
            img.onmouseout = swapImage;
        }
    }
}

function swapImage() {
    var bgUrl = this.style.backgroundImage;
    bgUrl = bgUrl.substr(bgUrl.indexOf('(') + 1);
    bgUrl = bgUrl.substr(0, bgUrl.indexOf(')'));
    if (bgUrl.indexOf('"') > -1)
        bgUrl = bgUrl.substr(1, bgUrl.length - 2);
    this.style.backgroundImage = 'url(\"' + this.src + '\")';
    this.src = bgUrl;
}

function addEventListenerEx(evt, fun) {
    if (window.addEventListener)
        window.addEventListener(evt, fun, false)
    else if (window.attachEvent)
        window.attachEvent("on" + evt, fun);
}

addEventListenerEx("load", enableHover);



// Browser herausfinden
var is_ie, is_nc, is_moz, is_opera, is_saf;
findBrowsers();
var supportsFade = (is_ie || is_moz);

var queryString = new Array();
var search = location.search;
if (search != "") search = search.substr(1, search.length - 1);
var parts = search.split("%26");
for (i = 0; i < parts.length; i++) {
    if (parts[i].indexOf("=") > -1) {
        queryString[parts[i].split("=")[0]] = parts[i].split("=")[1];
    }
    else {
        queryString[parts[i]] = "";
    }
}

function findObj(n, d) {
    var i, x;
    if (!d) d = document;
    if (!(x = d[n]) && d.all)
        x = d.all[n];
    for (i = 0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = findObj(n, d.layers[i].document);
    if (!x && d.getElementById)
        x = d.getElementById(n);
    return x;
}

function fadeIn(id, time) {
    var speed = Math.round(time / 100);
    var obj = findObj(id);
    var opac = getOpacity(obj);
    if (opac == -1 && obj.className == "opacity0") {
        setOpacity(id, 0);
        opac = 0;
    }


    var number = 0;
    for (op = opac; op <= 99; op++) {
        setTimeout("setOpacity('" + id + "', " + op + ")", (speed * number));
        number++;
    }
    //Mozilla-Flacker-Bug
    setTimeout("setOpacity('" + id + "', 99.9)", (speed * number));

}



function fadeOut(id, time) {
    var speed = Math.round(time / 100);
    var obj = findObj(id);
    var opac = getOpacity(obj);
    if (opac == -1 && obj.className != "opacity0") {
        setOpacity(id, 99.9);
        opac = 100;
    }

    var number = 0;
    for (op = opac; op >= 0; op--) {
        setTimeout("setOpacity('" + id + "', " + op + ")", (speed * number));
        number++;
    }
}

function setOpacity(id, op) {
    var obj = findObj(id);
    var x = op / 100;
    obj.style.opacity = x;
    obj.style.MozOpacity = x;
    obj.style.KhtmlOpacity = x;
    obj.style.filter = "alpha(opacity=" + op + ")";
}

function getOpacity(obj) {
    var ret;
    if (obj.style.opacity) {
        ret = obj.style.opacity * 100;
    }
    else if (obj.style.MozOpacity) {
        ret = obj.style.MozOpacity * 100;
    }
    else if (obj.style.KhtmlOpacity) {
        ret = obj.style.KhtmlOpacity * 100;
    }
    else {
        ret = -1;
    }

    if (ret == 99.9) ret = 100;
    return ret;
}

function findBrowsers() {
    if (document.ids) is_nc = true;
    else if (document.all && !document.getElementById) is_ie = true;
    else if (window.opera && !document.createElement) is_opera = true;
    else if (window.opera && window.getComputedStyle) is_opera = true;
    else if (window.opera && document.compatMode) is_opera = true;
    else if (window.opera && document.releaseEvents) is_opera = true;
    else if (window.getSelection && window.atob) is_moz = true;
    else if (window.getSelection && !document.compatMode) is_moz = true;
    else if (window.clipboardData && document.compatMode) is_ie = true;
    else if (window.clipboardData) is_ie = true;
    else if (document.getElementById && !document.all) is_opera = true;
    else if (document.images && !document.all) is_moz = true;

    var userAgent = navigator.userAgent.toLowerCase();
    is_saf = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));

}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll() {

    var yScroll;

    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('', yScroll);
    return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}



if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}

String.prototype.startsWith = function (str) {
    return this.length >= str.length && this.indexOf(str) == 0;
}

String.prototype.endsWith = function (str) {
    return this.length >= str.length && this.lastIndexOf(str) + str.length == this.length
}


// ===== Tour ======
var terminid = "";

function showInfo(id) {

    if (terminid != "") slideOut(terminid, 0, 'afterTerminSlideOut("' + id + '")');

    if (terminid == id)
        terminid = "";
    else {
        terminid = id;
        var termininfo = findObj(id);
        if (termininfo == undefined) return;
        var termin = termininfo.parentNode;

        termin.className += " open";
        slideIn(id, 0);
    }
}

function hideInfo(id) {
    var terminid = id;
    var termininfo = findObj(id);
    if (termininfo == undefined) return;
    var termin = termininfo.parentNode;

    termin.className = termin.className.replace(" open", "");
    //termininfo.totalHeight = termininfo.scrollHeight;
    termininfo.style.height = "0px";
}

/* Animations-Funktionen */
function spline(p1, p2, t2, t) {
    if (t <= 0)
        return p1;
    if (t >= t2)
        return p2;
    else {
        var d = t / t2;
        return -2 * (p2 - p1) * Math.pow(d, 3) + 3 * (p2 - p1) * Math.pow(d, 2) + p1;
    }
}

function slideIn(id, min) {
    theObj = document.getElementById(id);
    var dt = 0.01;
    var start = min, end = theObj.scrollHeight; //totalHeight;
    var t2 = 0.3;
    step(id, start, end, t2, 0, dt);
}

function slideOut(id, min, codeafter) {
    theObj = document.getElementById(id);
    var dt = 0.01;
    var end = min, start = theObj.scrollHeight; //start = theObj.totalHeight;
    var t2 = 0.3;
    step(id, start, end, t2, 0, dt);
    window.setTimeout(codeafter, t2 * 1000);
}

function afterTerminSlideOut(id) {
    var theObj = findObj(id);
    var termin = theObj.parentNode;
    termin.className = termin.className.replace(" open", "");
}

function step(id, start, end, t2, t, dt) {
    var theObj = findObj(id);
    t += dt;
    theObj.style.height = spline(start, end, t2, t) + "px";
    var code = "step('" + id + "', " + start + ", " + end + ", " + t2 + ", " + t + ", " + dt + ")";
    if (t < t2)
        window.setTimeout(code, dt * 1000);

}


// ==================
// Shop


function showPrice(target, txtCount, price) {
    var Count = Number(txtCount.value);
    if (Count % 1 != 0) return;

    var tgt = findObj(target);
    if (tgt != undefined) tgt.innerHTML = (Count * price).toString();
}



// ==================
// Fotos

var galleryid = "";

function showGallery(id) {

    if (galleryid != "") slideOut(galleryid, 20, 'afterGallerySlideOut("' + id + '")');

    if (galleryid == id)
        galleryid = "";
    else {
        galleryid = id;
        var gallery = findObj(id);
        if (gallery == undefined) return;
        gallery.className += " open";
        //gallery.totalHeight = gallery.scrollHeight;
        slideIn(id, 20);
    }
}

function hideGallery(id) {
    var galleryid = id;
    var gallery = findObj(id);
    if (gallery == undefined) return;

    gallery.className = gallery.className.replace(" open", "");
    //gallery.totalHeight = gallery.scrollHeight;
    gallery.style.height = "20px";
}

function afterGallerySlideOut(id) {
    var gallery = findObj(id);
    gallery.className = gallery.className.replace(" open", "");
}

function resetGalleryHeight() {
//    $('div').ready(function () {
//        if (this.id && this.id.startsWith("gallery"))
//            this.totalHeight = this.scrollHeight;
//    });
//    //what.totalHeight = what.scrollHeight;
//    var divs = document.getElementsByTagName("div");
//    for (i in divs) {
//        if (divs[i].id && divs[i].id.startsWith("gallery"))
//            divs[i].totalHeight = divs[i].scrollHeight;
//    }
}

var supportsAjax = false;
var ajaxRequest = createAjaxRequest();

function createAjaxRequest() {
    try {
        req = new XMLHttpRequest();
    }
    catch (e) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed) {
                req = null;
            }
        }
    }
    if (req != null) {
        supportsAjax = true;
    }
    return req;
}

function __ajaxCallback(callback) {
    switch (ajaxRequest.readyState) {
        case 4:
            if (ajaxRequest.status != 200) {
                //alert("Fehler:" + ajaxRequest.status);
            } else {
                if (callback) callback(ajaxRequest.responseText);
            }
            break;

        default:
            return false;
            break;
    }
}



function LiveTickerRequestDone(html) {
    findObj('liveticker').innerHTML = html;
}

function ajax(url, callback) {
    ajaxRequest.onreadystatechange = function () { __ajaxCallback(callback); };
    ajaxRequest.open("GET", url, "true");
    ajaxRequest.send(null);
}

function loadLiveticker() {
    if (supportsAjax) ajax("liveticker.aspx", LiveTickerRequestDone);
}

