
var thisCounter = 0;
window.addEvent('domready', function(){
	var as = [];
	$$('.fl_preview_item img').each(function(el) {
		if (!thisCounter) {
			el.setStyle('border', '2px solid #d01e34');
			thisCounter = 1;
		}

        el.addEvents({
			'mouseover': function(el){
				clearfacelineBorder();
				this.setStyle('border', '2px solid #d01e34');
				if(this.getProperty('src').search(/thumbs/) != -1 ){
					if (this.getAttribute('alt')) $('fl_view_text').set('html', this.getProperty('alt'));
					if (this.getAttribute('alt')) $('view').set('title', this.getProperty('alt'));
				    $('view').setProperty('src', this.getProperty('src').replace(/thumbs\//g, ""));
				}
			}
		});

        el.addEvents({
			'mouseleave': function(el){
				this.setStyle('border', '2px solid #cccccc');
			}
		});
		if (el.getAttribute('title')) as.push(el);
	});
	new Tips(as, {maxOpacity: 0.6, maxTitleChars: 25});

	if ($('flform_eingabe')) {
		$('flform_eingabe').addEvent('keydown', function(event){
			if (event.key == 'enter') 
				showFacelineById($('flform_eingabe').getProperty('value').trim());
		});
	}
	if ($('ksform_eingabe')) {
		$('ksform_eingabe').addEvent('keydown', function(event){
			if (event.key == 'enter') 
				showSceneById($('ksform_eingabe').getProperty('value').trim(), 'galleryImage', showGroup);
		});
	}
});

function clearfacelineBorder(){
	$$('.fl_preview_item img').each(function(el) {
		el.setStyle('border', '2px solid #cccccc');
	});	
}

function setActiveStyleSheet(title){
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++)
  {
    if (a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("title"))
    {
      a.disabled = true;
      if (a.getAttribute("title") == title)
      {
        a.disabled = false;
      }
    }
  }
}

var swapReset = 0;
function rand ( n ){
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function swapRandomImage(cssClass){
        var galleryImages = $(document.body).getElements('img.'+cssClass);
		var j = 0
    	var	randomImage = rand(21);
		$('ksform_eingabe').setProperty('value', randomImage);
		var image_id2swap = 'g'+randomImage;
		var hoover_suffix ='_hover';
		var oldImageSrc = $(image_id2swap).getProperty('src');   
        var imageExtension = oldImageSrc.substring(oldImageSrc.lastIndexOf('.'),oldImageSrc.length);       
        var newImageSrc = oldImageSrc.replace(imageExtension, hoover_suffix + '' + imageExtension);		
		$(image_id2swap).setProperty('src', newImageSrc);
		galleryImages.each(function(el) {
		    $(el).addEvents({'mouseover': function(){if (swapReset == 0){$(image_id2swap).setProperty('src', oldImageSrc);swapReset = 1;}}});
		});
}

function showFacelineById(faceline_nr){
	var faceline_count = 16;
	if (faceline_nr >= 1 && faceline_nr <= faceline_count) {
		if (faceline_nr < 10 || faceline_nr.length < 2)faceline_nr = '0'+faceline_nr;
		document.window.location='facelines_'+faceline_nr+'.html'
	}else{alert('Vorhandene Facelines 1 - ' + faceline_count);}
}

function showSceneById(id, cssClass, mb){
	var galleryImages = $(document.body).getElements('img.'+cssClass);
	var p = galleryImages.length;
	var groupHrefID = 'mb' + id;
	var orgEl = $(groupHrefID);
	if ($chk(orgEl)) {
		var groupHref = orgEl.getProperty('href');
		var groupTitle = orgEl.getProperty('title');
		var mbAnker = $('showGroupPics');
		mbAnker.setProperty('href', groupHref);
		mbAnker.setProperty('title', groupTitle);
		mb.open(mbAnker);
	}else{alert('Vorhandene Kinderszenen 1 -' + p);}
}

function setView(id){
	$(id).setStyle('border','2px solid #d01e34');
	$(id).addEvents({'mouseleave': function(){$(id).setStyle('border','2px solid #cccccc');}});
    text =$(id).getProperty('title');
	org_src = $(id).getProperty('src');
	new_src = org_src.replace(/thumbs\//g, "");
	$('fl_view_text').innerHTML = text;
	$('fl_view_text').setProperty('html', text)
	$('view').setProperty('src', new_src);
	$('view').setProperty('title', text);
}		
function setPicSrc(id, new_src){
    old_src = $(id).getProperty('src');
	$(id).addEvents({'mouseleave': function(){$(id).setProperty('src', old_src);}})
	$(id).setProperty('src', new_src);
}		
