function SpamCodeGetter(idEl) {
    var el=$(idEl);
    if (el==null)
        return false;
    el.value="";
    el.scg = this;
    $(el).observe("click", this.listen);
}
SpamCodeGetter.prototype.listen=function(e) {
    var d=Event.findElement(e,"input");
    if (d==null) return;

    new Ajax.Request("http://"+ document.location.host+"/exec/ajax/getcode.php", {
        method: "get",
        onSuccess: function(response) {
            d.value=response.responseText;
        }
    });
}
String.prototype.reverse = function() {
    var newCena = new String();
    for (var j=this.length-1;j>-1;j--)
        newCena+=this.substr(j,1);
    return newCena.valueOf();
}
function ChangeSum(id, elem, sourceId) {
    var col = $(id).getElementsByTagName("span");
    var inp = $(id).getElementsByTagName("input");

    for(i=0;i<col.length;i++) {

        inp[i].price = parseFloat(col[i].innerHTML.replace(/ /gi,"").replace(/,/gi,"."));
        inp[i].elem = elem;
        inp[i].sourceId = sourceId;
        inp[i].onclick = function() {
            var suma = parseFloat( $(this.sourceId).value.replace(/ /gi,"").replace(/,/gi,"."));

            if ($("sleva"))
                suma -= parseInt($("sleva").innerHTML.replace(/ /gi,""));

            var total = suma+this.price;
            var Cena = new String(total);
            Cena = Cena.reverse().replace(/(\d{3})/gi, "$1 ").reverse().replace(/\./gi,",").replace(/,(\d{1})$/gi,",$10");
            if (total-parseInt(total)==0) Cena+=",-";
            for (var i=0;i<elem.length; i++)
                $(elem[i]).innerHTML = Cena;
        }
    }
}
var SearchBox = Class.create();
SearchBox.prototype = {
    start:true,
    initialize:function(id){
        var n=$(id), SB=this;
        if (n!=null)
            n.observe("focus", function(){ if (SB.start) n.value=""; SB.start=false; } );
    }
}

var ButtonHover = Class.create();
ButtonHover.prototype = {
    initialize:function(){
        $$("button").each( function(node) {
                                node.observe("mouseover", function() {node.addClassName("hover");} );
                                node.observe("mouseout", function() {node.removeClassName("hover");} );
                        });
    }
}
var Submitter = Class.create();
Submitter.prototype = {
    initialize:function(formID, cl){
        $$("#"+formID+" ."+cl).each( function(node) {
            var nn=node.nodeName, ac="click";
            if (nn=="SELECT") ac="change";
            node.observe(ac, (
                function(e){
                    if (nn=="A") { var c=$(this.parentNode.htmlFor); c.checked=!c.checked; }
                    Event.stop(e);
                    $(formID).submit();
                    return false;
                }).bind(node))});
    }
}
var CheckBoxToggler = Class.create();
CheckBoxToggler.prototype = {
    sw:null,
    state:false,
    elems:null,
    initialize:function(switcherID, elemsIDprefix) {
        this.sw = $(switcherID);
        this.elems = $$("input[id^=\""+elemsIDprefix+"\"]");
        if (this.sw==null || this.elems==null)
            return;
        this.state=!this.sw.checked;
        this.sw.observe("click", (function(event) { this.resetElems(); }).bind(this) );
        var cbt=this;
        this.elems.each(function(node) {node.observe("click", (function(event) { this.resetMain(); }).bind(cbt) )} );
    },
    resetElems:function() {
        this.state=!this.state;
        if (!this.state) {
            var s=this.state;
            this.elems.each(function(node) {node.checked=s});
        }
    },
    resetMain:function(){
        if (this.sw.checked) {
            this.sw.checked=false;
            this.state=true;
        }
    }
};
var CheckDisabler = Class.create();
CheckDisabler.prototype = {
    elems:null,
    c:null,
    initialize:function(checkId,parentID) {
        this.c = $(checkId);
        if (this.c==null) return;
        this.elems = !Object.isString(parentID) ? $$(parentID.join(",")) : $$("#"+parentID+" input,#"+parentID+" select,#"+parentID+" textarea");
        this.c.observe("click", (function(e) {this.make(e)}).bind(this));
        this.makeDis();
    },
    make:function(e){
        var d=Event.findElement(e,"input");
        if (d==null) return;
        this.makeDis();
    },
    makeDis:function() {
        var C=this.c;
        this.elems.each(
            function(node) {
                node.disabled=!C.checked;
                if (node.type=="text" && node.disabled)
                    node.value="";
            });
    }
}
var ShClass = Class.create();
ShClass.prototype = {
    elems:null,
    rClass:null,
    initialize:function(checkId,parentID, rClass) {
        this.rClass=rClass;
        var c = $(checkId);
        if (c==null) return;
        c.showed=false;
        this.elems=$$("#"+parentID+" input,#"+parentID+" select,#"+parentID+" textarea");
        this.makeIt(!c.checked);
        c.observe("click", (function(e) {this.show(e)}).bind(this));
    },
    show:function(e) {
        var d=Event.findElement(e,"input");
        if (d==null) return;
        this.makeIt(d.showed);
        d.showed=!d.showed;
    },
    makeIt:function(sh){
        var rc=this.rClass;
        this.elems.each( function(node) { if (sh) node.removeClassName(rc); else node.addClassName(rc); } );
    }
}
var Slider = Class.create();
Slider.prototype = {
    sw:null,
    pn:null,
    startClosed:false,
    ie6m:0,
    initialize:function(switcherID, panelID, ie6m, startClosed) {
        this.sw = $(switcherID);
        this.pn = $(panelID);
        if (this.sw==null || this.pn==null)
            return;
        this.ie6m=ie6m;
        if (startClosed!=null)
            this.startClosed=startClosed;
        this.init();
    },
    init:function() {
        if (this.startClosed) this.pn.setStyle({display: "none"});
        var d = this.pn.getStyle("display");
        this.pn.closed = !(d!=null && d=="block");
        this.startClosed=this.pn.closed;
        this.sw.observe("click", (function(event) { this.dispatch(event); }).bind(this) );
    },
    dispatch:function(e){
        var a=Event.element(e);
        a.blur();
        this.makeIt(this.pn.closed, a);
    },
    makeIt:function(open, a){
        var HF = this;
        if (open) {
            new Effect.SlideDown(this.pn, { scaleContent: false, queue: "end"
                                                ,afterFinish:
                                                    function(){
                                                        if (navigator.userAgent.indexOf("MSIE 6.0")!=-1 && HF.startClosed)
                                                            $("footerbanner").setStyle({bottom:"-"+(HF.pn.getHeight()-HF.ie6m)+"px"});
                                                        HF.startClosed=false;
                                                        a.addClassName("up");
                                                    }
                                            } );

        }
        else
            new Effect.SlideUp(this.pn, { scaleContent: false, queue: "end", afterFinish: function() { a.removeClassName("up"); } });

        this.pn.closed=!this.pn.closed;
    }
}
var HideFilter = function(switcherID, panelID, ie6m) {
    this.sw = $(switcherID);
    this.pn = $(panelID);
    if (this.sw==null || this.pn==null) return;
    this.ie6m=ie6m;
    this.init();
}
HideFilter.prototype=new Slider;
HideFilter.prototype.constructor=HideFilter;
HideFilter.prototype.dispatch=function(e){
    var a=Event.element(e).up();
    a.blur();
    this.makeIt(this.pn.closed,a);
    if (!this.pn.closed) a.addClassName("down"); else a.removeClassName("down");
    new Ajax.Request("exec/set-filter-hide.php", {parameters:"h="+(this.pn.closed?"a":"n")});
}
