/* ---------------------------------------

  oag.de | Custom Scripts
  Datenwerk GmbH - www.datenwerk.de

  author:   marion.ihms@datenwerk.de

--------------------------------------- */

  //
  //
  //
  if (window.event) {window.event.cancelBubble = true;}


  //
  //
  //
  function localstart() {
		pngreplace();
		set_change_pic_event_handler();
		start_rotate_picchange();
  }

  //
  // get elements by tag and class name
  //
  function $cn (el, class_name) {

      var curr_class, all_obj, ret_obj = [], temp;

      all_obj =   document.getElementsByTagName(el);
      for (i = 0; i < all_obj.length; i++) {

          // no match at all
          //
          if(all_obj[i].className.indexOf (class_name) == -1) continue;

          // in case we have more then one class
          //
          temp =   "," + all_obj[i].className.split(" ").join(",") + ",";
          if (temp.indexOf("," + class_name + ",") == -1) continue;

          ret_obj[ret_obj.length] =   all_obj[i];
      }
      return ret_obj;
  }

	//
	// checks if the element has class
	//

	function hasClass(ele,cls) {
		return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	}


  //
  // replace png's for ie6 with imageLoader
  // loop all images owning the class "pngreplace"
  //
  function pngreplace() {

      var appname =  navigator.appName.toLowerCase();
      var appvers =  navigator.appVersion.toLowerCase();
      var ie6     =  !!(appname.indexOf("microsoft internet explorer") != -1 && appvers.indexOf("msie 6.") != -1 && !window.opera);
      if (!ie6) return; // the good-ones leave here

      var new_src =  "/images/blind.gif"
      var pngs =  $cn ("img", "pngreplace");
      for (var i = 0; i < pngs.length; i++) {

          var curr_src            =  pngs[i].src;
          pngs[i].src             =  new_src;
          pngs[i].style.filter    =  "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + curr_src + "', sizingMethod='scale')";
      }
  }

  //
  // make box clickable
  //
  function set_clickable_event_handler () {
    var a, div = $cn ("div", "clickable");
    for (var i = 0; i < div.length; i++) {
      div[i].onclick = function () {
        a = this.getElementsByTagName("a");
        if (typeof a[0] != "undefined") {
          if (a[0].onclick) 
          {
          	//bei onclick pruefen, ob function gallery_popup und dann ausführen
          	var onclick_str = a[0].onclick.toString();	
          	var ausgabe_gallery = onclick_str.match(/gallery_popup/);
						if(ausgabe_gallery != null)
						{
          		gallery_popup(a[0]);
          		return false;  
						}
          	var ausgabe_lexikon = onclick_str.match(/lexikon_popup/);
						if(ausgabe_lexikon != null)
						{
          		lexikon_popup(a[0]);
          		return false;  
						}    						        	        	
          } else {
         		if (a[0].target=="_blank"){
         			window.open (a[0].href,"_blank");
          		return false;          		
         		} else {
         			self.location.href = a[0].href;
          		return false;           		
        		}      		       
          }
        }
      }
    }
  }

  //
  // set external links target=blank
  //
  function set_external_links () {
    var a = document.getElementsByTagName("a");

    for (i=0; i < a.length; i++) {
      linkurl = a[i].href;
      eval("such = /" + window.location.host + "/;");
      if (!(such.test(linkurl))) {
        a[i].href = linkurl;
        a[i].setAttribute("target", "_blank");
      }
    }
  }
  
	
	//
	// rotate pic-change
	//		

	function start_rotate_picchange() 
	{
		var teaser =  $cn ("div", "pic-change");		    						
	  for (var i = 0; i < teaser.length; i++) {
			stop_rotation_c_xxl = 0;
			stop_rotation_hover_c_xxl = 0;
			var teaser_id = teaser[i].id;
	  	var teaser_div_cn =  $$("#" + teaser_id + " .item");  
	  	teaser_div_cn[0].style.display = "block";	
	
	 		if (!hasClass(teaser[i],'manual')){
				window.setTimeout("pic_change_paginate('"+teaser_id+"','next',"+stop_rotation_c_xxl+")", 4000);  		
	 		}
	  }		
	}

	function pic_change_paginate(teaser_id,pag,stop_rotation_c_xxl)
	{
		if(typeof stop_rotation_c_xxl=='undefined') {stop_rotation_c_xxl=1;}
		teaser_div_cn =  $$("#" + teaser_id + " .item");
		next_num = 0;
		var next_id_c = "";

		//nur ausführen, wenn teaser mehr als 1 bild besitzt!
		if(teaser_div_cn.length>1) {	

				for (var x = 0; x < teaser_div_cn.length; x++) {
					if (teaser_div_cn[x].style.display=="block")
					{
						act_id_c = teaser_div_cn[x].id;	
						next_num = x;
					}
				}
		
				//weiterblättern
				if (pag=="next")
				{
					next_num = next_num + 1;									
					check = 'teaser_div_cn['+next_num+']';
				  if (!eval(check)) 
				  { 
				  	next_num = 0; 
				  }
				}		

				//zurück blättern
				if (pag=="prev")
				{
					next_num = next_num - 1;					
					check = 'teaser_div_cn['+next_num+']';
				  if (!eval(check)) 
				  { 
				  	next_num = teaser_div_cn.length - 1; 
				  }
				}					

		  	next_id_c = teaser_div_cn[next_num].id;		

				if (stop_rotation_c_xxl==0)
				{
					if (stop_rotation_hover_c_xxl==1)
					{
						window.setTimeout ("pic_change_paginate('"+teaser_id+"','next',"+stop_rotation_c_xxl+")", 4000);
					} else {
					 	change_pic_slider(act_id_c,next_id_c);
						window.setTimeout("pic_change_paginate('"+teaser_id+"','next',"+stop_rotation_c_xxl+")", 4000);
					}	
				} else {
					change_pic_slider(act_id_c,next_id_c);
				}
			}
	}

		
	function change_pic_slider(act_id_c,next_id_c)
	{
		new Effect.Parallel([
			new Effect.Appear(next_id_c, {sync: true}),
	  	new Effect.Fade(act_id_c, {sync: true})
		], { 
	  	duration: 0.8,
			afterFinish:function(){
				$(next_id_c).style.display = 'block';
				$(act_id_c).style.display = 'none';
	  	}
		});
	}
	

	function set_change_pic_event_handler() {
		var div =  $$(".pic-change");
 		if (div.length>0){	 
    	for (var i = 0; i < div.length; i++) {
					
					var teaser_id = div[i].id;
					var teaser_items =  $$("#" + teaser_id + " .item"); 

					if (teaser_items.length>1)
					{
						//onmouseover
						div[i].onmouseover = function () {
							$$('#' + teaser_id + ' .pic-nav')[0].style.display = "block";
							stop_rotation_hover_c_xxl = 1;	
							stop_rotation_c_xxl = 1;
			
						};				
	
						//onmouseout
						div[i].onmouseout = function () {
							$$('#' + teaser_id + ' .pic-nav')[0].style.display = "none";
							stop_rotation_hover_c_xxl = 0;
						};
					}	
			}
		} 
  }

