var Vrst = Class.create({
	initialize: function() {
		this.vrstva_meno="shop_vrstva";
		this.vrstva_div_meno="shop_div_vrstva";
		this.zobrazenie_detailu="zobrazenie";
		this.scolor="#ffffff";
		this.ecolor="#b2b2b2";
		this.rcolor="#b2b2b2";
	},
	zobrazVrstvu: function(kam) {
		if ($(this.vrstva_meno)) {
			$(this.vrstva_meno).remove();
			$(this.vrstva_meno+'_telo').remove();
		}
		else {
			el=Builder.node('div',{id:this.vrstva_meno});
			el2=Builder.node('div',{id:this.vrstva_meno+'_telo',style:'display: none'},[
				Builder.node('div',{className:'ovladanie'},[
					Builder.node('img',{alt:'obrazok',src:server+'/admin/img/dizajn/close_red.png',onClick:'Vrstva.zobrazVrstvu();'})
				]),
			    Builder.node('div',{id: this.vrstva_meno+'_formular'}),
			    Builder.node('div',{className:'spodok'})
			]);
			$(kam).appendChild(el);
			new Effect.Appear(this.vrstva_meno, { duration: 0.3, from: 0.0, to: 0.8 });
			$(kam).appendChild(el2);
			//$(this.vrstva_meno).setStyle('top: '+(($(kam).getHeight()/2)).round()+'px; left: '+($(kam).cumulativeScrollOffset()[0]+(($(kam).getWidth()/2)-($(this.vrstva_meno).getWidth()/2)))+'px');
		}
	},
	nacitajDoVrstvy: function(post,get) {
		get=get!=undefined && get!=null && get!=""?"?"+get:"";
		new Ajax.nacitajDoVrstvy(this.vrstva_meno+'_formular',aj+get,{parameters: post});
	},
	zobrazDiv: function(kam) {
		if ($(this.vrstva_div_meno+'_telo')) {
			$(this.vrstva_div_meno+'_telo').remove();
		}
		else {
			this.zalozka();
			el=Builder.node('div',{id:this.vrstva_div_meno+"_telo",className:'stred_zobrazene'},[
			    Builder.node('div',{id: this.vrstva_div_meno+'_formular'})
			]);
			$(kam).appendChild(el);
			
		}
	},
	nacitajDoDivu: function(post) {
		new Ajax.Updater(this.vrstva_div_meno+'_formular',ShopAdresa,{parameters: post});
	},
	umiestniVrstvu: function() {
		vyska=$(this.vrstva_meno+'_telo').getHeight();
		sirka=$(this.vrstva_meno+'_telo').getWidth();

		vyska=vyska<200?200:vyska;
		vyska_povod=vyska;
		
		arr_okno=getPageSize();
		arr_scroll=getPageScroll();
		
		sirka=(arr_okno[2]/2)-(sirka/2)+arr_scroll[0];
		vyska=(arr_okno[3]/2)-(vyska/2)+arr_scroll[1];
		$(this.vrstva_meno).setStyle('width: '+arr_okno[0]+'px; height: '+arr_okno[1]+'px;');
		$(this.vrstva_meno+'_telo').setStyle('top: '+vyska.round()+'px; left: '+sirka.round()+'px');
		$(this.vrstva_meno+'_telo').show(true);
	},
});
var Vrstva = new Vrst();

Ajax.nacitajDoVrstvy = Class.create(Ajax.Request, {
  initialize: function($super, container, url, options) {
    this.container = {
      success: (container.success || container),
      failure: (container.failure || (container.success ? null : container))
    };

    options = Object.clone(options);
    var onComplete = options.onComplete;
    options.onComplete = (function(response, json) {
      this.updateContent(response.responseText);
      if (Object.isFunction(onComplete)) onComplete(response, json);
    }).bind(this);

    $super(url, options);
  },

  updateContent: function(responseText) {
    var receiver = this.container[this.success() ? 'success' : 'failure'],
        options = this.options;

    if (!options.evalScripts) responseText = responseText.stripScripts();

    if (receiver = $(receiver)) {
      if (options.insertion) {
        if (Object.isString(options.insertion)) {
          var insertion = { }; insertion[options.insertion] = responseText;
          receiver.insert(insertion);
        }
        else options.insertion(receiver, responseText);
      }
      else receiver.update(responseText);
    }
    Vrstva.umiestniVrstvu();
  }
});
