//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() {
     doQuicksearch();
      if (typeof(additionalInit)!="undefined") {
        additionalInit();
    }
     /*
     if (!qsearchInit) doQuicksearch();
     if (typeof(SkinSelect)!='undefined')  SkinSelect.init();
     if (typeof(forceSkin)!='undefined') SkinSelect.showDialog();

     if (typeof(doPLZ)!="undefined") doPLZ();
     if (typeof(doArtist)!="undefined") doArtist();
     if (typeof(initArtist)!="undefined") initArtist();
     */
     if (typeof(popup)!="undefined") {
     GenPurposeDialog.showDialog('/ostern.html','Osterspecial',540,380);
     }

}


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,

                        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();
        }

    };

}();

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,
                        height:400,
                        collapsible:false,
                        resizable:false,
                        x:((870-380)/2),
                        modal:true
                });
    snddialog.addButton('Schließen', snddialog.hide, snddialog);
    }
    snddialog.show();

}

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: '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.TextField({
            fieldLabel: 'Überschrift',
            name: 'name',
            allowBlank:false ,
            blankText:'Pflichtfeld',
            width:"210"
        }) ,
        new Ext.form.TextArea({
            fieldLabel: 'Kommentar',
            name: 'body',
            allowBlank:false ,
            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');
}




function doQuicksearch() {
if (qsearchInit==true) return;

qds = new Ext.data.Store({

        proxy: new Ext.data.HttpProxy({
            url: '/livesearch.php?'
        }),
        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: 200,
        value:'Produktsuche ...',
        pageSize:0,
        autoCreate:true,
        hideTrigger:true,
        fieldClass:'qsearch',
        emptyClass:'',
        invalidClass:'',
        tpl: qresultTpl,
        onSelect: function(record){
            window.location =
                String.format('{0}', record.data.urlname);
        }

    });
    //alert("hi");
    //qsearch.applyTo('qsearch');
    qsearch.render('qsearchct');
    qsearch.on("focus", function(el) {
        el.setRawValue('');
     });
     qsearch.getEl().dom.className="qsearch";
     //qsearch.getEl().addClass("qsearch");
     qsearchInit=true;
}

function doCart(id) {
    GenPurposeDialog.showDialog('/addcart.php?id='+id,'Warenkorb',300,190);
}

/*
Ext.onReady(function(){


    if (typeof(additionalInit)!="undefined") {
        //additionalInit();
    }

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



        doQuicksearch();




});
*/

