//quicksearch
var qds;
var qresultTpl;
var qsearch;
var qsearchInit=false;

Ext.override(Ext.form.ComboBox, {
	onTypeAhead : function() {
		if(this.store.getCount() > 0) {
			var r = this.store.getAt(0);
			var newValue = r.data[this.displayField];
			var len = newValue.length;
			var selStart = this.getRawValue().length;
			if(selStart != len){
				this.setRawValue(newValue);
				this.selectText(selStart, newValue.length);
			}
			this.onViewClick();
			this.expand();
		}
	}
});



if (document.getElementById && document.getElementsByTagName) {
if (window.addEventListener) window.addEventListener('load', appinit, false);
else if (window.attachEvent) window.attachEvent('onload', appinit);
}

function appinit() {
     if (!qsearchInit) doQuicksearch();
     

     if (typeof(doPLZ)!="undefined") doPLZ();
     if (typeof(doArtist)!="undefined") doArtist();
     if (typeof(initArtist)!="undefined") initArtist();

    if (typeof(kalender2)!="undefined") kalender2();
    if (typeof(kalender2)!="undefined") kalender2();
    if (Ext.get('calbox')!=null) {
       Ext.get("calbox").load("/new_calendarbox.html");
    }

}



var SkinSelect = function(){

    // define some private variables
    var dialog, showBtn;

    // return a public interface
    return {
        init : function(){
             showBtn = Ext.get('skin');
             if (showBtn!=null) showBtn.on('click', this.showDialog, this);

        },

        showDialog : function(){
            if(!dialog){
                dialog = new Ext.BasicDialog("s-dlg", {
                        shadow:true,
                        width:400,
                        height:160,
                        x:235,
                        y:300,
                        collapsible:false,
                        resizable:false,
                        modal:true
                });

            }
            dialog.show(showBtn.dom);
        } ,
        hideDialog : function(){
            if(dialog){
               dialog.hide(showBtn.dom);
            }

        }
    };
}();

var GenPurposeDialog = function(){


    //var dialog, showBtn;
    var dialog;

    return {

        test:function() {
            alert('Hello');
        },

        showDialog : function(url,title,width,height,params){

            dialog = new Ext.BasicDialog("dialog-dlg", {
                        shadow:true,
                        autoCreate:true,
                        width:width,
                        height:height,
                        title:title,
                        collapsible:false,
                        resizable:false,
                        x:((870-width)/2),
                        y:200,
                        modal:true
                });
            dialog.addButton('Schließen', dialog.hide, dialog);
            dialog.body.load({
                 url: url,
                 params: params,
                 discardUrl: false,
                 nocache: true,
                 text: "Loading...",
                 timeout: 30,
                 scripts: false
            });
            dialog.setTitle(title);
            dialog.show();
        },

        showMessage : function(title,message,width,height){

            dialog = new Ext.BasicDialog("dialog-dlg", {
                        shadow:true,
                        autoCreate:true,
                        width:width,
                        height:height,
                        title:title,
                        collapsible:false,
                        resizable:false,
                        x:((870-width)/2),
                        y:200,
                        modal:true
                });
            dialog.addButton('Schließen', dialog.hide, dialog);
            dialog.setTitle(title);
            dialog.body.dom.innerHTML = message;
            dialog.show();
        }

    };

}();


/* login */
var lgd=null;
var lgdialog=null;
/* var pwd=null; */
function showLoginForm(customRedirect) {


    if (lgd==null) {

    lgd = new Ext.form.Form({
        labelAlign: 'top',
        waitMsgTarget:true
    });

     function specialKey( field, e ) {
        if ( e.getKey() == e.RETURN || e.getKey() == e.ENTER ) {
        	lgd.submit({url:'/dologin.php', waitMsg:'Bitte warten'})
        }
    }

     lgd.add(
        new Ext.form.TextField({
            fieldLabel: 'E-Mail',
            name: 'un' ,
            allowBlank:false,
            blankText:'Pflichtfeld',
            width:"200",
            listeners:{
				'specialkey':specialKey,
				scope:this
			}
        }),

        new Ext.form.TextField({
            fieldLabel: 'Passwort',
            name: 'pa',
            inputType:'password',
            allowBlank:false ,
            blankText:'Pflichtfeld',
            width:"200",
            listeners:{
				'specialkey':specialKey,
				scope:this
			}
        }) ,
        new Ext.form.Checkbox({
            fieldLabel:"Eingeloggt bleiben",
            boxLabel: 'Login auf diesem Computer merken',
            name: 'staylogged',
            checked:'checked'
        })
    );


    lgd.addButton({
        text: 'Login',
        handler: function(){
            lgd.submit({url:'/dologin.php', waitMsg:'Bitte warten',params:{forward:customRedirect}});
            document.getElementById('lgderr').style.display = "none";

        }
    });

    lgd.on("actioncomplete", function(form,action) {
        window.location.replace(action.result.redir);
     });
     lgd.on("actionfailed", function(form,action) {
        document.getElementById('lgderr').style.display = "block";
        lgd.reset();
     });

     lgd.render('lgd-ct');

    pwd = new Ext.form.Form({
        labelAlign: 'top',
        waitMsgTarget:true
    });
    pwd.add(
        new Ext.form.TextField({
            fieldLabel: 'E-Mail',
            name: 'email' ,
            allowBlank:false,
            width:"200"
        })
    );

    pwd.addButton({
        text: 'Zugangsdaten anfordern',
        handler: function(){
            pwd.submit({url:'/dolostpwd.php', waitMsg:'Bitte warten'});
            document.getElementById('lpwderr').style.display = "none";
        }
    });

     pwd.on("actioncomplete", function(form,action) {
        Ext.MessageBox.alert("Zugangsdaten verschickt", action.result.msg);
        document.getElementById('lpwdcont').style.display = "none";

     });
     pwd.on("actionfailed", function(form,action) {
        document.getElementById('lpwderr').style.display = "block";
        pwd.reset();
     });

     pwd.render('lpwd');


     
    }
    if (lgdialog==null) {
    lgdialog = new Ext.BasicDialog("lgd-dlg", {
                        shadow:true,
                        width:250,
                        height:300,
                        collapsible:false,
                        resizable:false,
                        x:370,
                        y:180,
                        modal:true
                });
    lgdialog.addButton('Schließen', lgdialog.hide, lgdialog);
    }
    lgdialog.show();

}

function renderpwd() {
    document.getElementById('lpwdcont').style.display = 'block';
}

function unrenderpwd() {
    document.getElementById('lpwdcont').style.display = 'none';
 }


var msnd=null;
var msnddialog=null;
function showMailForm(sender,ct,id) {

    if (msnd==null) {

    msnd = new Ext.form.Form({
        labelAlign: 'top',
        waitMsgTarget:true
    });
    if (sender!='') {
      msnd.add(new Ext.form.TextField({
            fieldLabel: 'Deine E-Mail',
            name: 'email' ,
            value: sender,
            allowBlank:false,
            blankText:'Pflichtfeld',
            width:"210"
        }));
    } else {
        msnd.add(new Ext.form.TextField({
            fieldLabel: 'Deine E-Mail',
            name: 'email' ,
            vtype:'email',
            allowBlank:false,
            blankText:'Pflichtfeld',
            invalidText:'Bitte eine korrekte E-Mail-Adresse eingeben' ,
            width:"210"
        }));
    }
    msnd.add(
            new Ext.form.TextField({
            fieldLabel: 'Betreff',
            name: 'sub' ,
            allowBlank:false,
            blankText:'Pflichtfeld',
            width:"210"
            }),
            new Ext.form.TextArea({
            fieldLabel: 'Text',
            name: 'body',
            allowBlank:true,
            maxLength:3000,
            width:"300",
            height:"200"
        })
    );

    msnd.addButton({
        text: 'Senden',
        handler: function(){
            msnd.submit({url:'/domsend.php?ct='+ct+'&id='+id+'&h='+secfh, waitMsg:'Bitte warten'});
        }
    });

     msnd.on("actioncomplete", function(form,action) {
         msnddialog.hide();
         //Ext.MessageBox.alert("Versand", "Nachricht wurde geschickt");
     });
     msnd.on("actionfailed", function(form,action) {
        Ext.MessageBox.alert("Fehler", "Ein Fehler ist aufgetreten");
     });
     msnd.render('msend-ct');
    }
    if (msnddialog==null) {
    msnddialog = new Ext.BasicDialog("msend-dlg", {
                        shadow:true,
                        width:380,
                        height:450,
                        collapsible:false,
                        resizable:false,
                        x:180,
                        y:210,
                        modal:true
                });
    msnddialog.addButton('Schließen', msnddialog.hide, msnddialog);
    }
    msnddialog.show();

}


var snd=null;
var snddialog=null;
function showSendForm(sender,ct,id,singleImg) {

    if (snd==null) {

    snd = new Ext.form.Form({
        labelAlign: 'top',
        waitMsgTarget:true
    });
    if (sender!='') {
      snd.add(new Ext.form.TextField({
            fieldLabel: 'Deine E-Mail',
            name: 'email' ,
            value: sender,
            allowBlank:false,
            blankText:'Pflichtfeld',
            width:"210"
        }));
    } else {
        snd.add(new Ext.form.TextField({
            fieldLabel: 'Deine E-Mail',
            name: 'email' ,
            vtype:'email',
            allowBlank:false,
            blankText:'Pflichtfeld',
            invalidText:'Bitte eine korrekte E-Mail-Adresse eingeben' ,
            width:"210"
        }));
    }
    snd.add(
        new Ext.form.TextField({
            fieldLabel: 'Empfänger E-Mail',
            name: 'rec' ,
            vtype:'email',
            allowBlank:false,
            blankText:'Pflichtfeld',
            invalidText:'Bitte eine korrekte E-Mail-Adresse eingeben' ,
            width:"210"
        }),

         new Ext.form.TextArea({
            fieldLabel: 'Kommentar',
            name: 'comment',
            allowBlank:true,
            maxLength:2000,
            width:"300",
            height:"80"
        })
    );

    snd.addButton({
        text: 'Senden',
        handler: function(){
            snd.submit({url:'/dosend.php?ct='+ct+'&id='+id+'&singleImg='+singleImg, waitMsg:'Bitte warten'});
        }
    });

     snd.on("actioncomplete", function(form,action) {
         snddialog.hide();
         Ext.MessageBox.alert("Versand", "Der Link wurde an die angegebene Adresse geschickt");
     });
     snd.on("actionfailed", function(form,action) {
        Ext.MessageBox.alert("Fehler", "Ein Fehler ist aufgetreten");
     });
     snd.render('send-ct');
    }
    if (snddialog==null) {
    snddialog = new Ext.BasicDialog("send-dlg", {
                        shadow:true,
                        width:380,
                        x:180,
                        y:230,
                        height:400,
                        collapsible:false,
                        resizable:false,
                        modal:true
                });
    snddialog.addButton('Schließen', snddialog.hide, snddialog);
    }
    snddialog.show();

}

function showFeedbackForm(story_id,user_id,value) {
    var fbForm = new Ext.form.Form({
        labelAlign:'top',
        buttonAlign:'left',
        waitMsgTarget:true
    });
    fbForm.add(
        new Ext.form.TextArea({
            fieldLabel: 'Teile uns deine Meinung mit',
            name: 'feedback',
            allowBlank:true ,
            blankText:'Pflichtfeld',
            maxLength:10000,
            width:500,
            height:200,
            value:value

        })
    );
    fbForm.addButton({
        text: 'Feedback abschicken',
        handler: function(){
            if(user_id==null) {
                Ext.MessageBox.alert("Bitte einloggen", "Nur angemeldete Benutzer können Feedback abgeben");
                return;
            }
            fbForm.submit({url:'/feedback.php', waitMsg:'Bitte warten',params:{'story_id':story_id,'user_id':user_id,'_chk':1}});
        }
    });

     fbForm.on("actioncomplete", function(form,action) {
        Ext.MessageBox.alert("Vielen Dank", "Dein Feedback wurde eingetragen. Vielen Dank!");
        document.getElementById('fbdiv').style.display='none';
        document.getElementById('feedbacklink').style.display='inline';
        document.getElementById('feedbacktext_sent').style.display='block';
        document.getElementById('feedbacktext_notsent').style.display='none';
     });
     fbForm.on("actionfailed", function(form,action) {
        Ext.MessageBox.alert("Fehler", action.result.errorMessage);
     });

    fbForm.render('fbdiv');


}

function showCommentForm(ct,id,uid,size) {

var commform = new Ext.form.Form({
        labelAlign: 'top',
        buttonAlign: 'left',
        waitMsgTarget:true
    });
    if (uid==2) {
    commform.add(
        new Ext.form.TextField({
            fieldLabel: 'Dein Name',
            name: 'author' ,
            allowBlank:false,
            blankText:'Pflichtfeld',
            invalidText:'Bitte einen Namen eingeben' ,
            width:"210"
        })
     );
    commform.add(
        new Ext.form.TextField({
            fieldLabel: 'E-Mail',
            name: 'email' ,
            vtype:'email',
            allowBlank:false,
            blankText:'Pflichtfeld',
            invalidText:'Bitte eine korrekte E-Mail-Adresse eingeben' ,
            width:"210"
        })
     );
    }
     commform.add(

        new Ext.form.TextArea({
            fieldLabel: 'Kommentar',
            name: 'body',
            allowBlank:true ,
            blankText:'Pflichtfeld',
            maxLength:10000,
            width:(typeof("size")!="undefined"?275:350),
            height:120
        })
    );

    commform.addButton({
        text: 'Kommentar abschicken',
        handler: function(){
            commform.submit({url:'/comment.php', waitMsg:'Bitte warten',params:{'ct':ct,'id':id,'_chk':1}});
        }
    });

     commform.on("actioncomplete", function(form,action) {
        Ext.MessageBox.alert("Vielen Dank", "Dein Kommentar wurde eingetragen (anonyme Kommentare sind erst nach Freigabe sichtbar).");
        var d=Ext.get("comments");
        if (d!=null) d.load("/reloadcomments.php?ct="+ct+"&id="+id);
        commform.reset();
     });
     commform.on("actionfailed", function(form,action) {
        Ext.MessageBox.alert("Fehler", action.result.errorMessage);
     });

    commform.render('cdiv');
}

Ext.onReady(function(){


    if (typeof(additionalInit)!="undefined") {
        additionalInit();
    }
    if (Ext.get('srch')!=null) {
    Ext.get('srch').on('click', function(e){
        Ext.MessageBox.prompt('regioactive.de durchsuchen', 'Suche nach:', doSrch);
    });
    }
    function doSrch(btn, text){
        if (btn=="ok") location.href="/search.php?searchstring="+text;
    };

    Ext.MessageBox.buttonText.yes="OK";
    Ext.MessageBox.buttonText.cancel="Abbruch";
    Ext.MessageBox.buttonText.no="Nein";
    Ext.form.VTypes.emailText="Falsches E-Mail Format";
    Ext.QuickTips.autoDismiss=false;
    Ext.QuickTips.showDelay=100;
    //Ext.form.ComboBox.queryDelay=5000;


    /* form element code */
    if (typeof(applyCSearch)!='undefined')  {
        applyCSearch();
    }

    if (!Ext.isIE) {
        doQuicksearch();
        if (typeof(doPLZ)!="undefined") doPLZ();
     if (typeof(doArtist)!="undefined") doArtist();
     if (typeof(initArtist)!="undefined") initArtist();
    }



});





function doQuicksearch(c) {
try {
if (typeof(c)=="undefined" || c=="") c=ct;
if (qsearch!=null) {
    qsearch.collapse();
    qsearch.destroy();
}
qds = new Ext.data.Store({

        proxy: new Ext.data.HttpProxy({
            url: '/livesearch.php?ct='+c
        }),
        reader: new Ext.data.XmlReader({
            totalRecords: 'results',
            record: 'row'
        }, [
            {name: 'name'},
            {name: 'sub'},
            {name: 'urlname'},
            {name: 'zusatz'},
            {name: 'shortinfo'}
        ])
    });

     // Custom rendering Template
    qresultTpl = new Ext.Template(
        '<div class="livesearch-item">',
            '<p>{sub}<b>{name}</b> {zusatz}',
            '<br>{shortinfo}',
        '</div>'
    );

    qsearch = new Ext.form.ComboBox({
        store: qds,
        displayField:'name',
        typeAhead: false,
        minChars:3,
        loadingText: 'Suche...',
        width: 250,
        value:'Live-Suche ...',
        pageSize:0,
        autoCreate:true,
        hideTrigger:true,
        tpl: qresultTpl,
        onSelect: function(record){
            window.location =
                String.format('{0}', record.data.urlname);
        }

    });

    //qsearch.applyTo('qsearch');
    var plh=Ext.get("qsearch");
    if (plh!=null) plh.remove();
    qsearch.render('qsearchct');
    qsearch.on("focus", function(el) {
        el.setRawValue('');
     });
    qsearchInit=true;
    var qt=Ext.get("qtrigger");
    if (qt!=null) {
        qt.on("click", function(el) {
        //Ext.get("qsearchct").highlight({duration: .2});
        //Ext.get("qsearchct").fadeIn({duration: .2});
        qsearch.focus();
     });
    }
}
catch (e) {}
}

function deleteRecord(ct,id,animfield) {

        if (ct=="user") {
            var ms='ACHTUNG! Der Account, dein Profil und alle von dir eingestellten Inhalte, Profile und Medien gehen unwiderbringlich verloren! Wirklich löschen?';
        }
        if (ct=="artist") {
            var ms='ACHTUNG! Das Artist-Profil geht unwiderbringlich verloren! Wirklich löschen?';
        }
        if (ct=="location") {
            var ms='ACHTUNG! Das Location-Profil geht unwiderbringlich verloren! Wirklich löschen?';
        }
        var msgb = Ext.MessageBox.getDialog();
    msgb.fixedcenter = false;
    msgb.on("beforeshow", function() {
        msgb.moveTo(200, 300);
    });

        Ext.MessageBox.show({
           title:'Account wirklich löschen?',
           msg: ms,
           buttons: Ext.MessageBox.YESNO,
           fn: doResult,
           maxWidth:400,
           animEl: animfield
       });


    function doResult(btn){
        if (btn=="yes") {
            location.href='/account.php?a=delete&ct='+ct+'&id='+id;
        }
    };

}



