// JavaScript Document

	function ScrollTo( obj ) {
    
        new Effect.ScrollTo( obj );
    
    }
    
	function light_image( url ) {
		
		var img = new Image();
		loading();
		
		$( 'light_content' ).innerHTML = '<img onClick="light_close()" style="cursor:pointer;  border:1px solid #000000;" src="' + url + '" id="light_image_tag" onLoad="light()" onError="loading_hide(); alert(\'Image not found\')" />';
		
		return true;
		
	}
	
	var gallery_new_x;
	var gallery_interval = false;
	
	function light_gallery( gallery_id ) {
		
		loading();
		co_operate( 'gallery', 'view', { gallery_id: gallery_id }, null, function( json ) {
							
							$( 'light_content' ).innerHTML = json.result.html;
							$( 'gallery_light_view' ).onmousedown = function(e){ 
							  e = e || window.event; 
							  e.preventDefault(); 
							} 
							light();
							
							if( $( 'gallery_i' ) ) {
								light_gallery_move( $( 'gallery_i' ).value );
							}
																		
						}, function( error ) {
							
							loading_hide();
							alert( error );
							
						} );
		
	}
	
	function light_gallery_next() {
		
		new_i = parseInt( $( 'gallery_i' ).value ) + 1;
		if( new_i >= $( 'gallery_count' ).value ) {
			new_i = '0';
		}
		
		light_gallery_change( new_i );
		
	}
	
	function light_gallery_prev() {
		
		new_i = $( 'gallery_i' ).value - 1;
		if( new_i < 0 ) {
			new_i = $( 'gallery_count' ).value - 1;	
		}
		
		light_gallery_change( new_i );
		
	}
	
	function light_gallery_move( new_i ) {
		
		$( 'gallery_i' ).value = new_i;
		gallery_new_x = new_i * -70;
		gallery_new_x = gallery_new_x + ( 70 * 3 );
		
		if( gallery_new_x > 0 ) {
			gallery_new_x = 0;
		}
		
		if( !gallery_interval ) {
			gallery_interval = window.setInterval( "light_gallery_interval()", 50 );
		}
		
	}
	
	function light_gallery_interval() {

		var x = parseInt( $( 'gallery_move_list' ).style.left );
		
		var diff = Math.ceil( ( gallery_new_x - x ) / 6 );
		var new_x = x + diff;
		
		if( Math.abs( gallery_new_x - x ) < 2 ) {
			$( 'gallery_move_list' ).style.left = gallery_new_x + 'px';
			window.clearInterval( gallery_interval );
			gallery_interval = false;
		}
		
		$( 'gallery_move_list' ).style.left = new_x + 'px';
		
	}
	
	function light_gallery_change( new_i ) {
		
		for( i=0; i<99999; i++ ) {
			
			if( !$( i + '_gallery_thumb' ) ) {
				break;	
			}
			
			$( i + '_gallery_thumb' ).className = 'state_normal';
			
		}
		
		gallery_id = $( new_i + '_gallery_thumb' ).name;
		$( new_i + '_gallery_thumb' ).className = 'state_active';
		light_gallery_move( new_i );
		
		loading();
		$( 'gallery_image' ).src = '';
		$( 'gallery_image' ).hide();
		
		co_operate( 'gallery', 'view_single', { gallery_id: gallery_id }, null, function( json ) {
							
							$( 'gallery_image' ).src = json.result.path;
							$( 'gallery_title' ).innerHTML = json.result.title;
							$( 'gallery_image' ).onload = loading_hide;
							$( 'gallery_id' ).value = json.result.gallery_id;
							$( 'gallery_image' ).show();
																		
						}, function( error ) {
							
							loading_hide();
							alert( error );
							
						} );
		
	}
	
	function light_co( _co, _action, _data ) {
		
		loading();
		co_operate( _co, _action, _data, null, function( json ) {
							
							$( 'light_content' ).innerHTML = json.result.html;
							light();
																		
						}, function( error ) {
							
							loading_hide();
							alert( error );
							
						} );
		
	}
	
	///////////////////////////////
	/// ALF //  LIGHT  ////////////
	///////////////////////////////

	var light_box_open = false;
	var light_reload_on_close = false;
	
	function light_create() {
	
		if( $( 'light_lightbox' ) != undefined ) {
			return true;	
		}
		
		///////// Blackbox
		var light_blackbox = document.createElement( 'div' );
		light_blackbox.setAttribute( 'id', 'light_blackbox' );
		light_blackbox.style.display = 'none';
		light_blackbox.onclick = light_close;
		document.body.appendChild( light_blackbox );
		
		///////// >> Light Box
		var light_center = document.createElement( 'div' );
		light_center.setAttribute( 'id', 'light_center' );
		light_center.style.display = 'none';
		document.body.appendChild( light_center );
		
		var light_lightbox = document.createElement( 'div' );
		light_lightbox.setAttribute( 'id', 'light_lightbox' );
		light_lightbox.style.display = 'none';
		light_center.appendChild( light_lightbox );
		
			////////// Light Content
			var light_content = document.createElement( 'div' );
			light_content.setAttribute( 'id', 'light_content' );
			light_lightbox.appendChild( light_content );
		
		///////// Loading
		var light_loading = document.createElement( 'div' );
		light_loading.setAttribute( 'id', 'light_loading' );
		light_loading.style.display = 'none';
		light_loading.innerHTML = '<img src="img/alf/loading.gif" height=32 width=32 />';
		document.body.appendChild( light_loading );
		
		if( window.light_create_ie6 ) {
			light_create_ie6();
		}
	
	}

	function light() {
		
		light_create();
		
		if( light_box_open ) {
			loading_hide();
			return true;
		}
		
		if( window.hide_inputs ) {
			hide_inputs();
		}
		
		//$( 'light_button_back' ).style.display = 'none';
		light_box_open = true;
	
		loading_hide();
		blackbox();
		
		$('light_center').style.display = '';
		
		$('light_lightbox').style.display = 'none';
		$('light_lightbox').style.opacity = '1';
		$('light_lightbox').style.filter = 'alpha(opacity=100)';
		$('light_content').style.opacity = '0';
		$('light_content').style.filter = 'alpha(opacity=0)';
		
		$('light_lightbox').style.left = '-' + ( $('light_lightbox').getWidth() / 2 ) + 'px';
		//$('light_lightbox').style.top = '-' + ( $('light_lightbox').getHeight() / 2 ) + 'px';
		
		new Effect.Grow('light_lightbox', { duration: 0.7 });
		//setTimeout( 'show_light_header()', 1000 );
		setTimeout( 'show_light_content()', 500 );

	}
	
	function show_light_content() {
		new Effect.Opacity('light_content', { from: 0, to: 1, duration: 0.7 });
	}
	
	/*
	function show_light_header() {
		new Effect.Opacity('light_header', { from: 0, to: 1 });
		//new Effect.Opacity('light_feeter', { from: 0, to: 1 });
	}
	*/
	
	function light_close() {
	
		if( light_reload_on_close ) {
			window.location.reload();
		}
	
		light_box_open = false;
		//new Effect.Opacity('light_header', { from: 1, to: 0 });
		//new Effect.Opacity('light_feeter', { from: 1, to: 0 });
		//new Effect.Opacity('light_content', { from: 1, to: 0 });
		new Effect.Opacity('light_lightbox', { from: 1, to: 0 });
		loading_hide();
		blackbox_hide();
		//setTimeout( "$('light_blackbox').style.display='none'", 1000 );
		setTimeout( "$('light_center').style.display='none'", 1000 );
	
		if( window.show_inputs ) {
			show_inputs();
		}
	
	}
	
	function loading() {
		light_create();
		$('light_loading').show();
	}
	
	function loading_hide() {
		light_create();
		$('light_loading').style.display='none';
	}
	
	function blackbox() {
		light_create();
		$('light_blackbox').style.opacity = '0';
		$('light_blackbox').style.filter = 'alpha(opacity=0)';
		new Effect.Opacity('light_blackbox', { from: 0, to: 0.4 });
		$('light_blackbox').style.display = '';
	}
	
	function blackbox_hide() {
		light_create();
		new Effect.Opacity('light_blackbox', { from: 0.4, to: 0, duration: 0.7 });
		setTimeout( "$('light_blackbox').style.display='none'", 700 );
		//$('light_blackbox').style.display = 'none';
	}
	
	var int_pos = 0;
	var ziel = 0;
	var scroll_dif = 80;
	function scroll_to( num, dif, start ) {
	
		scroll_div = dif;
		ziel = start + ( num - 5 ) * scroll_div;
		if( ziel < 0 ) {
			ziel = 0;
		}
		
		$('selected_img').style.left = ( num * scroll_div ) + 'px';
		
		if( scrolltimer == '' ) {
			scrolltimer = setInterval( "scroll_it()", 50 );
		}
	
	}
	
	var scrolltimer = '';
	function scroll_it() {
		
		if( int_pos == ziel ) {
		
			clearInterval( scrolltimer );
			scrolltimer = '';
		
		}
		
		var dif = ziel - int_pos;
		
		if( Math.abs( dif ) < 2 ) {
			int_pos = ziel;
		} else {
			int_pos = int_pos + Math.floor( dif / 8 );
		}
		
		if( document.getElementById( 'gallery_list_driver' ) )
			document.getElementById( 'gallery_list_driver' ).style.left = - int_pos + 'px';
	
	}

/**********************************************/

	var objDrag = null;     // Element, �ber dem Maus bewegt wurde
	var objDrag2 = null;     // sizer
	var objDrag2b = null;     // main
	var objDrag3 = null;     // Pic

  var mouseX   = 0;       // X-Koordinate der Maus
  var mouseY   = 0;       // Y-Koordinate der Maus

  var offX = 0;           // X-Offset der Maus zur linken oberen Ecke des Elements
  var offY = 0;           // Y-Offset der Maus zur linken oberen Ecke des Elements

  // Browserweiche
  IE = document.all&&!window.opera;
  DOM = document.getElementById&&!IE;

  // Initialisierungs-Funktion
  function init(){
    // Initialisierung der Überwachung der Events
    document.onmousemove = doDrag;  // Bei Mausbewegung die Fkt. doDrag aufrufen
    document.onmouseup = stopDrag;  // Bei Loslassen der Maustaste die Fkt. stopDrag aufrufen
  }
  
  var normX;
  var normY;
  var last_x;
  var last_y;
  var last_width;
  var stateX;
  
  ///// Flächen-Größe setzen
	///// Vergößerungs-div und Das Bild selber
	function init_drag2( obj, objm ) {
		obj.style.left = ( parseInt( objm.style.width ) - 3 ) + 'px';
		obj.style.top = '-3px';
	}
	
	var obj_image;
	var obj_image_div;
	var obj_stage;
	var obj_div;
	var dragMode;
	
  // Wird aufgerufen, wenn die Maus über einer Box gedrückt wird
  function startDragMove( _obj_image, _obj_image_div, _obj_stage ) {
  
    // Objekt der globalen Variabel zuweisen -> hierdurch wird Bewegung möglich
    obj_image = _obj_image;
    obj_image_div = _obj_image_div;
    obj_stage = _obj_stage;

    // Offsets im zu bewegenden Element ermitteln
    offX = mouseX - obj_image_div.clientLeft - parseInt(obj_image_div.style.left);
    offY = mouseY - obj_image_div.clientTop - parseInt(obj_image_div.style.top);
    
    dragMode = 'move';
	
  }
  
  var startX;
  var startY;
  var maxX = 810;
  
  function startDragStageSize( _obj_div, _obj_image_div, _obj_stage ) {
  
    // Objekt der globalen Variabel zuweisen -> hierdurch wird Bewegung möglich
    obj_div = _obj_div;
    obj_image_div = _obj_image_div;
    obj_stage = _obj_stage;

    // Offsets im zu bewegenden Element ermitteln
	 offX = mouseX - obj_div.clientLeft - parseInt(obj_div.style.left);
     offY = mouseY - obj_div.clientTop - 1;
	 startX = parseInt(obj_stage.style.width);
	 startY = parseInt(obj_stage.style.height);
    
     dragMode = 'stageSize';
     
  }
  
  function startDragSize( _obj_image, _obj_image_div, _obj_stage, _enum ) {
  
    // Objekt der globalen Variabel zuweisen -> hierdurch wird Bewegung möglich
    obj_image = _obj_image;
    obj_image_div = _obj_image_div;
    obj_stage = _obj_stage;

    // Offsets im zu bewegenden Element ermitteln
	 offX = mouseX; // - objPic.clientLeft;
	 stateX = parseInt(obj_image.width);
	 normX = parseInt($('image_width_'+_enum).value);
	 normY = parseInt($('image_height_'+_enum).value);
	 
	 stateX = parseInt(obj_image.width);
    
     dragMode = 'size';
    
  }

  // Wird ausgeführt, wenn die Maus bewegt wird
  function doDrag(ereignis) {
    // Aktuelle Mauskoordinaten bei Mausbewegung ermitteln
    mouseX = (IE) ? window.event.clientX : ereignis.pageX;
    mouseY = (IE) ? window.event.clientY : ereignis.pageY;

	/////// VERSCHIEBEN
    if (dragMode == 'move') {
      // Element neue Koordinaten zuweisen
		divX = mouseX - offX;
		divY = mouseY - offY;
		
		/////// Das Bild kann nicht aus der sichtbaren Fläche verschoben werden
		if( divX < ( obj_image.width * -1 ) + 8 ) {
			divX = ( obj_image.width * -1 ) + 8;
		} else if( divX > obj_stage.clientWidth - 8 ) {
			divX = obj_stage.clientWidth - 8;
		}
		if( divY < ( obj_image.height * -1 ) + 8 ) {
			divY = ( obj_image.height * -1 ) + 8;
		} else if( divY > obj_stage.clientHeight - 8 ) {
			divY = obj_stage.clientHeight - 8;
		}
		
        obj_image_div.style.left = divX + "px";
        obj_image_div.style.top = divY + "px";
        
    }
    
    // Wurde die Maus über einem Element gedrück, erfolgt eine Bewegung
    if (dragMode == 'stageSize') {
    
		 newX = (mouseX - offX);
		 newY = startY + (mouseY - offY);
		 if( newX < 10 ) newX = 10;
		 if( newY < 10 ) newY = 10;
		 if( newX > maxX ) newX = maxX;
		// Element neue Koordinaten zuweisen
		obj_div.style.left = newX - 3 + "px";
		//objDrag2.style.top = (mouseY - offY - 2) + "px";
		obj_stage.style.width = newX + "px";
		obj_stage.style.height = newY + "px";

      // Position in Statusleiste ausgeben
      //window.status = "Box-Position: " + objDrag.style.left + ", " + objDrag.style.top;
    }
    
    if (dragMode == 'size') {
    
		 newX = stateX + Math.round( mouseX - offX );
		 
		 if( newX < 30 ) newX = 30;
		 if( newX > 1500 ) newX = 1500;
		 
		 newY = Math.round( newX / normX * normY );
		 
      // Element neue Größe zuweisen
		obj_image.width = newX;
		obj_image.height = newY;

    }
    
  }

  // Wird ausgeführt, wenn die Maustaste losgelassen wird
  function stopDrag(ereignis) {
  
  	 /////////////////////////////////////////////////
     ////// nachdem man das Bild verschoben hat
	 if( dragMode == 'move' ) {
		 
		 nleft = parseInt( obj_image_div.style.left );
		 ntop = parseInt( obj_image_div.style.top );
		 
		 var magnet = 8;
		 
		 /////////// MAGNET X
		 
		 //// Rechts
		 if( obj_stage.clientWidth - magnet < nleft + obj_image.width && obj_stage.clientWidth > nleft + obj_image.width ) {
		 	obj_image_div.style.left = ( obj_stage.clientWidth - obj_image.width ) + "px";
		 }
		 //// Links (dominierend)
		 if( nleft > 0 && nleft < magnet ) {
      		obj_image_div.style.left = "0px";
		 }
		
		 /////////// MAGNET Y
		 
		 //// Untent
		 if( obj_stage.clientHeight - magnet < ntop + obj_image.height && obj_stage.clientHeight > ntop + obj_image.height ) {
		 	obj_image_div.style.top = ( obj_stage.clientHeight - obj_image.height ) + "px";
		 }
		 
		 //// Oben (dominierend)
		 if( ntop > 0 && ntop < magnet ) {
      		obj_image_div.style.top = "0px";
		 }
		
	 }
    
    dragMode = null;
    
  }
  
  /**************/
  
     //////// Texteingabe Filtern
     MS_restrict_field = function(formname, id_or_name, chars) {
  
         var obj = (document.getElementById && document.getElementById(id_or_name) != null)
                 ? document.getElementById(id_or_name) : ((document[formname][id_or_name] != null)
                 ? document[formname][id_or_name] : '');
      
         if(obj.type == "text" || obj.type == "textarea") {
      
            obj.timer = "";
            obj.chars = chars;
            obj.onkeypress = obj.onkeydown = function() {
               var self = this;
               controll = function() {
                  for(var t='',x=0; x<self.value.length; ++x) {
                     if(self.chars.indexOf(self.value.charAt(x))>-1) {
                        t += self.value.charAt(x);
                     }
                  }
                  self.value = t;
               };
               this.timer = setTimeout(controll,1);
            };
            obj.onkeyup = function() {
               clearTimeout(this.timer);
            };
         }
      };
      
