var ZWP = new Object();

var ZWPApp = null;

function init() {

  if (arguments.callee.done)
    return;  
  arguments.callee.done = true;
  
  ZWPApp = new ZWP.App();

  (document.all && !window.print) ? null : replaceSelect('dd_suche');
}

document.observe("dom:loaded", function() {
	init();
	ZebraTable.stripe('tabelle');
    try {
        lightWindowInit();
    } catch(e) {}
    try {
        mylightWindow.initializeWindow('LinkEmpfehlen');
    } catch(e) {}
});

//----------------------------------------------------------------------------
//
// ZWP Class
//
//----------------------------------------------------------------------------

ZWP.App = Class.create();

ZWP.App.prototype = {
	
	initialize: function() {
		ZWPApp = this;	
	},

    loadPageData: function(id) {
        new Ajax.Request('/ajax/pagedata', {
           parameters: {'id' : id},
           onSuccess: function(response) {
               pagedata = response.responseJSON;
               try {
                $('Vorschaubild').src='/ressources/uploads/'+pagedata.data.Bild.value;
               } catch(e) {}
               text = pagedata.data.Text.value;
               $('IVorschaubild').value=pagedata.data.Bild.value;
               $('IHeadline').value=pagedata.data.Headline.value.unescapeHTML();
               $('IText').value=text.stripTags().unescapeHTML().truncate(350);
           }
        });
    },

    loadTeaserData: function(target,parameters) {
        new Ajax.Updater(target,$(target).readAttribute('rel'),{
            parameters: parameters
        });
    },

    loadProdukte: function(rubrik_id,produkt_id) {
		new Ajax.Request('/ajax/produkte', {
			parameters: {'rubrik': rubrik_id},
			onLoading: function() {
                $('ProduktListe').update('');
			},
			onSuccess: function(response) {
                $('ProdukteListeAlle').href='?mode=search&rubrik='+response.responseJSON[0].rubrik_id;
                var myTemplate = new Template('<li><a href="?mode=search&rubrik=#{rubrik_id}&produkt=#{id}" rel="#{id}">#{name}</a></li>');
                if (response.responseJSON[0].rubrik_id>0) {
                    response.responseJSON.each(function(el) {
                        $('ProduktListe').insert(myTemplate.evaluate(el));
                    });
                    $('ProduktListe').select('a').each(function(el) {
                        if (el.readAttribute('rel')==produkt_id) {
                            el.addClassName('active');
                        }
                    });
                }
			}
		});
    },

    selectCheckboxes: function(selector) {
        $$(selector).each(function(el) {
           $(el).checked=true;
        });
    },

    deselectCheckboxes: function(selector) {
        $$(selector).each(function(el) {
           $(el).checked=false;
        });
    },

    produktinfosSlider: function(element) {
        $(element).select('slide_manager_thumb').each(function(el) {el.hide();});
    }
}

ZWP.ProduktinfosSlider = Class.create();
ZWP.ProduktinfosSlider.prototype = {

  initialize: function() {
    properties = $A(arguments);
    this.element = properties[0];
    this.imagePath = properties[1].imagePath;
    this.active_element = 1;
    this.setPreviewImage();
    Event.observe($('SliderPrev'),'click',this.imagePrev.bindAsEventListener(this,$('SliderPrev')));
    Event.observe($('SliderFwd'),'click',this.imageFwd.bindAsEventListener(this,$('SliderFwd')));
  },

  imageFwd: function(e) {
    this.active_element++;
    if(!$('SliderBild'+this.active_element)) {
        this.active_element=1;
    }
    this.setActiveImage();
    this.setPreviewImage();
    return false;
  },

  imagePrev: function(e) {
    this.active_element--;
    if(!$('SliderBild'+this.active_element)) {
        this.active_element = $(this.element).select('.slide_manager_thumb').size();
    }
    this.setActiveImage();
    this.setPreviewImage();
    return false;
  },

  setActiveImage: function() {
      $('Bild').src = this.imagePath+$('SliderBild'+this.active_element).readAttribute('rel');
  },

  setPreviewImage: function() {
    $(this.element).select('.slide_manager_thumb').each(function(el) {
        el.hide();
    });
    if($('SliderBild'+(this.active_element+1))) {
        $('SliderBild'+(this.active_element+1)).show();
    } else {
        $('SliderBild1').show();
    }
  }

}

ZWP.TextboxDefaultText = Class.create();

// ZWP.TextboxDefaultText(id,{params});
ZWP.TextboxDefaultText.prototype = {
  
  initialize: function() {
	properties = $A(arguments); 
    this.default_text = properties[1].text;
    this.alert_text = properties[1].alert;
    this.search_box = $(properties[0]);
    this.doValidation = properties[1].validate;
   	this.search_form = $(this.search_box.form);

    if (this.search_box.value == '') {
      this.search_box.value = this.default_text;
    }
    
    Event.observe(this.search_box, "focus", this.textboxFocus.bind(this), false);
    Event.observe(this.search_box, "blur", this.textBoxBlur.bind(this), false);
    
    Event.observe(this.search_form, "submit", this.validateSearchForm.bind(this), false);
  },
  
  textboxFocus: function(ev) {
    if (this.search_box.value == this.default_text) {
      this.search_box.s=this.search_box.className;
      this.search_box.value='';
      //this.search_box.className='inputnormal';
    }
  },
  
  textBoxBlur: function(ev) {
    if (this.search_box.value == '')
    {
      var c=this.search_box.className;
      /*
      if (c != (this.search_box.s?this.search_box.s:"inputdimmed"))
      {
        this.search_box.className=this.search_box.s?this.search_box.s:"inputdimmed";
      }
          */
      this.search_box.value=this.default_text;
    }
  },
  
  validateSearchForm: function(ev) {
    if (this.search_box.value == this.default_text)
    {
    	this.search_box.value='';
      //alert(this.alert_text);
      //Event.stop(ev);
    }
  }
}


// Old Functions

function OpenWindow(pfad,wname,w,h,scroll) {
	var wf = "";
	var links=screen.width/2-w/2;
	var oben=screen.height/2-h/2;
	wf = wf + "width=" + w;
	wf = wf + ",height=" + h;
	wf = wf + ",resizable=no";
	wf = wf + ",scrollbars="+scroll;
	wf = wf + ",menubar=no";
	wf = wf + ",toolbar=no";
	wf = wf + ",directories=no";
	wf = wf + ",location=no";
	wf = wf + ",status=no";
	wf = wf + ",top="+oben;
	wf = wf + ",left="+links;
	var MyWin =	window.open(pfad,wname,wf);
	MyWin.focus();
}

		function Hactive(img) {
			try {
				$('Navi'+img).src="/ressources/images/mnu_" + img + "_a.gif";
			} catch(e) {
			}
		}

		function Hinactive(img) {
			try {
				$('Navi'+img).src="/ressources/images/mnu_" + img + ".gif";
			} catch(e) {
			}
		}

		function StandardWert(el,wert) {
			if ($F(el)=='') {
				$(el).value=wert;
			}
		}

		function OpenDialog(url,width,height) {
  			Dialog.alert({url: PATH_PR_H+url, options: {method: 'get'}},
               {className: "mac_os_x_dialog", width: 350, okLabel: "Schlie&szlig;en", top:0,
                showEffect: Effect.ModalSlideDown, hideEffect: Effect.ModalSlideUp, showEffectOptions: {duration: 0.7, delay: 0.3}, hideEffectOptions: {duration: 0.3}});
		}


		var win;
		function OpenWin(wname,pfad,w,h) {
			win = new Window({
				//className:'mac_os_x', title: wname, top:270, left:300, width:w, height:h, url: pfad, showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}
				className:'mac_os_x', title: wname, top:270, left:300, width:w, height:parseInt(window.innerHeight*0.9), url: pfad, showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}
			});
			win.setZIndex(150);
			win.showCenter();
		}

		function AdminGotoPage(mode,params) {

			new Ajax.Request('/admin/ajax/'+mode, {
				parameters:params,
				evalScripts:true,
				asynchronous:true,
				onSuccess: function(transport) {
					if (transport.responseText.match(/Location:(.+)/)) {
						document.location.href=$1;
					}
					if (transport.responseText.match(/News:(\d+)/)) {
						document.location.href='?news='+RegExp.$1;
					}
				}
			});
		}


var ZebraTable = {
	bgcolor: '',
	classname: '',
	stripe: function(el) {
		$$('#ContentText table').each(
			function(s) {
				rows=s.getElementsBySelector('tr');
				for (var i=1,len=rows.length;i<len;i++) {
					$(rows[i]).getElementsBySelector('td + td').each(
						function(s) {
							s.addClassName('rahmen');
						}
					);
					if (i % 2 == 0) {
						$(rows[i]).addClassName('even');
					} else {
						$(rows[i]).addClassName('odd');
					}
				}
			}
		);
		/*
		for (var i=1,len=rows.length;i<len;i++) {
			if (i % 2 == 0) rows[i].className = 'alt';
			//Event.add(rows[i],'mouseover',function() { ZebraTable.mouseover(this); });
			//Event.add(rows[i],'mouseout',function() { ZebraTable.mouseout(this); });
		}
		*/
	},
	mouseover: function(row) {
		this.bgcolor = row.style.backgroundColor;
		this.classname = row.className;
		addClassName(row,'over');
	},
	mouseout: function(row) {
		removeClassName(row,'over');
		addClassName(row,this.classname);
		row.style.backgroundColor = this.bgcolor;
	}
}

	var act_bild = 1;
	function change_bild(bild_no,el) {
		if(bild_no != this.act_bild) {
			//document.images["bildbtn" + this.act_bild].src = this.arr_bilder[act_bild+"_i"].src;
			this.act_bild = bild_no;
			//document.images["bildbtn" + this.act_bild].src = this.arr_bilder[act_bild+"_a"].src;
			document.images["bild"].src = this.arr_bilder[this.act_bild].src;
			$$('.galerienavi a').each(function(s) {
				$(s).removeClassName('galerie_active');
			});
			$(el).addClassName('galerie_active');
		}
	}
	

	function AddArchiv(page_id) {
		$('Archivieren_'+page_id).innerHTML='<img src="'+PATH_GLOBAL_IMAGES_H+'progress_snake.gif">';
		new Ajax.Updater('Archivieren_'+page_id, '/ajax/archivieren', {
			asynchronous:true,
			parameters: 'page_id='+page_id,
			onComplete: function() {
				Effect.Pulsate('Archivieren_'+page_id);
			}
		});

		return false;
	}

	function SendeEmpfehlung() {
		$('FormEmpfehlen').request({
			onComplete: function() {
				$('Danke').show();
				$('FormEmpfehlen').hide();
			}
		});
		return false;
   }
 
   
function fadeNAppearDiv(div){
	div_closed = div + "_open";
	div_link = div + "_link";
	
	if($(div_closed).style.display!="none"){
		$(div_link).className ="acco_down";
		new Effect.SlideUp(div_closed, {duration:0.3, queue: 'front'});
	}
	else{

		$(div_link).className ="acco_up";
		new Effect.SlideDown(div_closed, {duration:0.5, queue: 'front'});

	}
	
}

function togglePubDivs( ) {
    var show = false;
    $$('#pub_inhalt .pub_kurztext').each(function(item) {
        if($(item).visible()) {
            $(item).hide();
            show = true;
        } else {
            $(item).show();
            show = false;
        }
    });

    if(show) {
        $$('#pub_inhalt .pub_btn_kurztext').each(function(item) {
            $(item).innerHTML = 'kurztexte einblenden&nbsp;&nbsp;+';
        });
    } else {
        $$('#pub_inhalt .pub_btn_kurztext').each(function(item) {
            $(item).innerHTML = 'kurztexte ausblenden&nbsp;&nbsp;&minus;';
        });
    }
    
}