var slideShow = {
    cells    : null,
    lastCell : null,
    nextCell : null,
    actPic : null,
    lastPic : null,
    slidePics : [],
    effectLength : 5,
    nextPic : null,

    init : function() {
        var soundBtn = Ext.get("soundbtn");
        var navianim;
        Ext.select("div.navicontainer").each(function(nd){
            navianim = Ext.get(nd.dom.parentNode);
        });
        var pos = Ext.isSafari ? [4,40] : [4,-14];
        soundBtn.alignTo(navianim, "bl", pos);
        soundBtn.fadeIn();
        this.cells = Ext.select('td.slidetd');
        if (this.cells.elements.length===0) return;
        this.cells.each(this.getPics);
        this.actPic = this.slidePics.length-1;
        this.lastPic = -1;
        var dname = document.location.pathname.split('/')[1].split('.')[0];
        if (dname=="") dname="index";
        for ( var z=1; z<=3; z++ ) {
            this.slidePics.push('http://www.pizza-ria.de/images/slider/'+dname+z+'.jpg');
        }
        this.startSlideShow();
    },

    startSlideShow : function() {
        if (!this.nextCell) this.nextCell = this.getNextCell();
        if (!this.lastCell) this.lastCell = this.getLastCell();
        this.getNextPic();
        //alert(this.lastPic+" : "+this.actPic);
        var picSrc = '<img src="'+this.slidePics[this.actPic]+'" alt="" height="115" width="115" border="0" style="visibility:hidden;">';
        this.nextCell.innerHTML = picSrc;
        var fadePic = Ext.get(this.nextCell.firstChild);
        //slideShow.lastCell.style.border="1px solid red";
        fadePic.fadeIn({
            easing: 'easeOut',
            duration: slideShow.effectLength,
            callback:function() {
                slideShow.lastCell = slideShow.getLastCell();
                slideShow.nextCell = slideShow.getNextCell();
                var fadeOutPic = Ext.get(slideShow.lastCell.firstChild);
                fadeOutPic.fadeOut({
                    easing: 'easeIn',
                    duration: slideShow.effectLength/2,
                    callback:function() {
                        var rnd = Math.round(Math.random()*100);
                        var style = '';
                        if (rnd>50) {
                            style = ' style="border:none;"';
                        }
                        var spacer = '<img src="http://www.pizza-ria.de/images/spacer.gif" alt="" height="115" width="115" border="0"'+style+'>';
                        slideShow.lastCell.innerHTML = spacer;
                        slideShow.startSlideShow();
                    }
                });
            }
        });
    },

    getNextPic : function() {
        this.actPic++;
        this.lastPic++;
        if (this.actPic>=this.slidePics.length) {
            this.actPic = 0;
        }
        if (this.lastPic>=this.slidePics.length) {
            this.lastPic = 0;
        }
        return(this.actPic);
    },

    getLastCell : function() {
        var lastCell = null;
        this.cells.each(function(td){
            if (td.dom.firstChild && td.dom.firstChild.src && td.dom.firstChild.src==slideShow.slidePics[slideShow.lastPic]) {
                lastCell = td.dom;
            }
        });
        return(lastCell);
    },

    getNextCell : function() {
        var nextCell = null;
        var rnd = Math.random();
        rnd = Math.floor(rnd*(this.cells.elements.length-1));
        var found = false;
        while (!found) {
            if (this.cells.elements[rnd].firstChild.src.indexOf('spacer')>=0) {
                found = true;
            } else {
                rnd = Math.random();
                rnd = Math.floor(rnd*(this.cells.elements.length-1));
            }
        }
        nextCell = this.cells.elements[rnd];
        return(nextCell);
    },

    getPics : function(td) {
        var pic = td.dom.firstChild;
        if (pic && pic.src && pic.src.indexOf('spacer')<0) {
            slideShow.slidePics.push(pic.src);
            if (!slideShow.lastCell) slideShow.lastCell = td.dom;
            if (!slideShow.lastPic) slideShow.lastPic = pic;
        } else if(!pic) {
            td.dom.innerHTML = '<img src="http://www.pizza-ria.de/images/spacer.gif" alt="" height="115" width="115" border="0" style="border:none;">';
        }
    }
};

showsite = function() {
    var fa = Ext.get('flashanim');
    fa.dom.style.display="none";
    var el = Ext.get('content');
    el.fadeIn({
        endOpacity: 1, //can be any value between 0 and 1 (e.g. .5)
        easing: 'easeOut',
        duration: 1.5,
        callback: function() {
            var animfix = Ext.get('animfix');
            if (animfix) {
                animfix.alignTo('anmfxp', 'tl-tl', [6,6]);
                animfix.fadeIn();
            }
            var navi = Ext.get('navigation');
            navi.fadeIn({
                callback: function() {
                    var pic1 = Ext.get('pic1');
                    pic1.fadeIn({
                        callback: function() {
                            var pic2 = Ext.get('pic2');
                            pic2.fadeIn({
                                callback: slideShow.init()
                            });
                        }
                    });
                }
            });
        }});
}

var popups = function(){

    // define some private variables
    var dialog, dialog2, dialog3, showBtn, showBtn2, showBtn3;

    // return a public interface
    return {
        init : function(){
//            document.body.onunload = function() {
//                 __flash.TCallLabel('_root', 'setSoundPos');
//            }
            var dname = document.location.pathname.split('/')[1].split('.')[0];
            if (dname!="index" && dname!="") slideShow.init();
            if (dname=="speisen" || dname=="getraenke" || dname=="anfahrt") {
                showBtn = Ext.get('show-anfahrt-btn');
                if (showBtn) showBtn.on('click', this.showDialog, this);
                showBtn2 = Ext.get('show-karte-btn');
                if (showBtn2) showBtn2.on('click', this.showDialog2, this);
                showBtn3 = Ext.get('show-karteg-btn');
                if (showBtn3) showBtn3.on('click', this.showDialog3, this);
            }
            if (dname!="index" && dname!="") {
                var soundBtn = Ext.get("soundbtn");
                var navi;
                Ext.select("div.navicontainer").each(function(nd){
                    navi = Ext.get(nd.dom.parentNode);
                });
                var pos = Ext.isSafari ? [4,40] : [4,-14];
                soundBtn.alignTo(navi, "bl", pos);
            } else if (dname=="index" || dname=="") {
                var soundBtn = Ext.get("soundbtn");
                soundBtn.hide();
            }
        },

        showDialog : function(){
            if(!dialog){ // lazy initialize the dialog and only create it once
                dialog = new Ext.BasicDialog("anfahrt-dlg", {
                    autoTabs:false,
                    width:550,
                    height:430,
                    shadow:true,
                    minWidth:300,
                    minHeight:250,
                    proxyDrag: true
                });
                dialog.addKeyListener(27, dialog.hide, dialog);
                dialog.addButton('Schlie&szlig;en', dialog.hide, dialog);
            }
            dialog.show(showBtn.dom);
        },

        showDialog2 : function(){
            if(!dialog2){ // lazy initialize the dialog and only create it once
                dialog2 = new Ext.BasicDialog("karte-dlg", {
                    autoTabs:false,
                    width:740,
                    height:520,
                    shadow:true,
                    minWidth:300,
                    minHeight:250,
                    proxyDrag: true
                });
                dialog2.addKeyListener(27, dialog2.hide, dialog2);
                dialog2.addButton('Schlie&szlig;en', dialog2.hide, dialog2);
            }
            dialog2.show(showBtn2.dom);
        },

        showDialog3 : function(){
            if(!dialog3){ // lazy initialize the dialog and only create it once
                dialog3 = new Ext.BasicDialog("karteg-dlg", {
                    autoTabs:false,
                    width:640,
                    height:520,
                    shadow:true,
                    minWidth:300,
                    minHeight:250,
                    proxyDrag: true
                });
                dialog3.addKeyListener(27, dialog3.hide, dialog3);
                dialog3.addButton('Schlie&szlig;en', dialog3.hide, dialog3);
            }
            dialog3.show(showBtn3.dom);
        }
    };
}();

// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
Ext.onReady(popups.init, popups, true);

embedflash = function() {
    var flash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="800" height="540" id="070503_intro" align="middle">';
    flash+='  <param name="allowScriptAccess" value="sameDomain" />';
    flash+='  <param name="movie" value="images/070503_intro.swf" />';
    flash+='  <param name="quality" value="high" />';
    flash+='  <param name="bgcolor" value="#ffffff" />';
    flash+='  <embed src="images/070503_intro.swf" quality="high" bgcolor="#ffffff" width="800" height="540" name="070503_intro" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    flash+='</object>';
    document.write(flash);
}

var __flash;
embedsound = function() {
    var snd = getCookie("sound");
    var flash = '<div id="soundbtn" style="position:absolute; top:0; left:0;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="16" height="16" id="sound" align="middle">';
    flash+='  <param name="allowScriptAccess" value="sameDomain" />';
    flash+='  <param name="movie" value="images/sound.swf?snd='+snd+'" />';
    flash+='  <param name="quality" value="high" />';
    flash+='  <param name="bgcolor" value="#ffffff" />';
    flash+='  <embed src="images/sound.swf?snd='+snd+'" quality="high" bgcolor="#ffffff" width="16" height="16" name="sound" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    flash+='</object></div>';
    document.write(flash);
    __flash = Ext.get('sound').dom;
}

getCookie = function (cookieName)
{
    var cookieValue = '';
    var posName = document.cookie.indexOf(escape(cookieName) + '=');
    if (posName != -1) {
        var posValue = posName + (escape(cookieName) + '=').length;
        var endPos = document.cookie.indexOf(';', posValue);
        if (endPos != -1) {
            cookieValue = unescape(document.cookie.substring(posValue, endPos));
        } else {
            cookieValue = unescape(document.cookie.substring(posValue));
        }
    }
    return (cookieValue);
}

setCookie = function (cookieName, cookieValue, expires, path, domain, secure)
{
    document.cookie =
    escape(cookieName) + '=' + escape(cookieValue)
    + (expires ? '; expires=' + expires.toGMTString() : '')
    + (path ? '; path=' + path : '')
    + (domain ? '; domain=' + domain : '')
    + (secure ? '; secure' : '');
}

setsound=function(stat) {
    setCookie("sound", stat);
}