fadeSlogans = function(direction){
	if(direction == 'in'){
		fromVal = 0;
		toVal=1;
	}
	if(direction == 'out'){
		fromVal = 1;
		toVal=0;
	}
	$$('div#immobilien1').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 0.3,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#bewegen1').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            from:fromVal,
            to:toVal,
            delay:0.7
        });
	     
	});
	$$('div#uns1').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 1.3,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#immobilien2').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 1.0,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#bewegen2').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 1.6,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#uns2').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 1.9,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#immobilien3').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 2.0,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#bewegen3').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 2.3,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	$$('div#uns3').each(function(e){
		e.setOpacity(fromVal);
		new Effect.Opacity(e,{
            delay: 2.6,
            from:fromVal,
            to:toVal,
            duration:1
        });
	});
	if(direction == 'in'){
		setTimeout('fadeSlogansOut()', 5000);
	}
	else{
		setTimeout('fadeSlogansIn()', 5000);
	}
};


var spamProtectForm = function(form){
	secsEl = new Element('input',{'type':'hidden','name':'secsOnSite','value':0,'style':'display: none;'});
	secsElD = new Element('input',{'type':'hidden','name':'secsOnSiteD','value':0,'style':'display: none;'});
	form.appendChild(secsEl);
	form.appendChild(secsElD);
	minSecs = 10;
	submitButton = form.down('input[type=submit]');
	form.originalSubmitText = submitButton.value;
	remaining = minSecs;
	submitButton.value = "Warten...(" + remaining + ")";
	submitButton.disabled = 'disabled';
	submitButton.style.cursor = 'wait';
	
	new PeriodicalExecuter(function(form, minSecs,pe) {
		  if (isNaN(form.secsOnSite.value) == true) {
			  form.secsOnSite.value = 0;
	      } else {
	    	  secs = parseInt(form.secsOnSite.value);
	
	    	  if(secs < minSecs){
	    		  remaining = minSecs - secs;
	    		  submitButton.value = "Warten...(" + remaining + ")";
	    	  }
	    	  else{
	    		  submitButton = form.down('input[type=submit]');
	    		  submitButton.value = form.originalSubmitText;
	    		  submitButton.removeAttribute('disabled');
	    		  submitButton.style.cursor = 'pointer';
	    	  }
	
	    	  form.secsOnSite.value = secs + 1;
	    	  
	      }
		  form.secsOnSiteD.value = parseInt(form.secsOnSite.value) * 2; 
		}.curry(form, minSecs),
	1);
};
var fadeSlogansOut = function(){
	fadeSlogans('out');
};
var fadeSlogansIn = function(){
	fadeSlogans('in');
};
var extendTableSorting = function(element){
    SortableTable.init(element);
};
var replaceByCk = function(element){
	CKEDITOR.replace( element.name,window.ckconfig );
	CKEDITOR.config.width = 700;
	CKEDITOR.config.height = 600;
	CKEDITOR.config.contentsCss = '/static/css/tags.css';
	CKEDITOR.config.stylesCombo_stylesSet = 'hr_styles:/static/js/ckstyles.js';
};
var replaceByCkSidebar = function(element){
	CKEDITOR.replace( element.name,window.ckconfig );
	CKEDITOR.config.width = 700;
	CKEDITOR.config.height = 600;
	CKEDITOR.config.contentsCss = ['/static/css/tags.css','/static/css/sidebar.css'];
	CKEDITOR.config.stylesCombo_stylesSet = 'hr_styles:/static/js/ckstyles.js';
};
replaceMainImage = function(event){
	dirs = this.href.split('/');
	lastIndex = dirs.length-1;
	dirs[lastIndex] = 'thumbnail-1.'+dirs[lastIndex];
	Effect.Queues.get('previews').invoke('cancel');
	imgSrc = dirs.join('/');
	if($('main-img').src != imgSrc){
		if(this.lightBoxId != null){
			lightBoxId = this.lightBoxId;
			title = Event.findElement(event).title;
		}
		new Effect.Opacity('main-img', { from:1.0,to:0.1,duration: 0.2,queue:{scope:'previews',position:'end',limit:1},afterFinish:function(lightBoxId,title,e){
			$('main-img').src = imgSrc;
			$('main-img').lightBoxId = lightBoxId;
			$('main-img').stopObserving('click');
			$('main-img').up('div#main-img-frame').down('span').innerHTML = title;
			$('main-img').onclick =function(e){
				Event.stop(e);
				window.lightbox._show(this.lightBoxId);
			}.bindAsEventListener();
			new Effect.Opacity('main-img', { from:0.1,to:1.0,duration: 0.2,queue:{scope:'previews',position:'end',limit:1}});
		}.curry(lightBoxId,title) });
	}
};
slidePreviews = function(event){
	element = event.findElement();
	event.stop();
	element.blur();
	//offset = 72;
	offset = element.up('div.pic-pos').down('div.pic-clip').getHeight();
	effectDuration = 1.0;
	transitionType = Effect.Transitions.spring;
	ul = element.up('div.pic-pos').down('ul.picture-previews');
	currentValue = parseInt(ul.style.marginTop);
	
	if(isNaN(currentValue)) currentValue = 0;
	if(element.hasClassName('up')){
		if(currentValue == 0){
			return;
		}
		newValue = currentValue + offset;
		new Effect.Morph($(ul),{
			style: 'margin-top: ' + newValue+'px',
			duration: effectDuration,
			transition: transitionType
		});
		if(newValue == 0){
			element.removeClassName('up');
			element.addClassName('inactive');
		}
		downSlider = element.next('a.slider');
		downSlider.removeClassName('inactive');
		downSlider.addClassName('down');
	}
	if(element.hasClassName('down')){
		height = ul.getHeight();
		newValue = currentValue - offset;
		if(height > (0-newValue)){
			new Effect.Morph($(ul),{
				style: 'margin-top: ' + newValue+'px',
				duration: effectDuration,
				transition: transitionType
			});
			upSlider = element.previous('a.slider');
			upSlider.removeClassName('inactive');
			upSlider.addClassName('up');
			if(height < ((0-newValue)+offset)){
				element.removeClassName('down');
				element.addClassName('inactive');
			}
		}
	}
};
doOnloadHooks = function(){
	if($$('div#uns1').length){
 		fadeSlogans('in');
	}
    $$('table.sortable').each(extendTableSorting);
    $$('textarea.html').each(replaceByCk);
    $$('textarea.html-sidebar').each(replaceByCkSidebar);
    $$('a.main-img-changer').each(function(e){
    	e.observe('mouseover',replaceMainImage);
    });
    	
    $$('ul.immobilien > li').each(function(e){
    	e.observe('mouseover',function(e){
    		this.addClassName('hovered');
    	});
    	e.observe('mouseout',function(e){
    		this.removeClassName('hovered');
    	});
    	e.observe('click',function(e){
    		location.href = this.down('h1 > a').href;
    	});
    });
    $$('div.button').each(function(e){
    	e.observe('mouseover',function(e){
    		this.addClassName('hovered');
    		if(this.down('a')) this.down('a').addClassName('hovered');
    		if(this.down('input')) this.down('input').addClassName('hovered');
    	});
    	e.observe('mouseout',function(e){
    		this.removeClassName('hovered');
    		if(this.down('a')) this.down('a').removeClassName('hovered');
    		if(this.down('input')) this.down('input').removeClassName('hovered');
    	});
    	e.observe('click',function(e){
    		if(this.down('a')) location.href = this.down('a').href;
    		if(this.down('input')) this.down('input').form.submit();
    	});
    });
    $$('img#main-img').each(function(e){
    	$('main-img').onclick =function(event){
			
			window.lightbox._show(0);
			Event.stop(event);
			return false;
		}.bindAsEventListener();
    });
    if($$('a.slider').length){
    	noOfImages = $$('a.slider').first().up('div.pic-pos').down('ul.picture-previews').childElements().length;
    	if(noOfImages < 5){
    		$$('a.slider.down').each(function(e){
    			e.removeClassName('down');
    			e.addClassName('inactive');
    		});
    	}
	    $$('a.slider').each(function(e){
	    	e.observe('click',slidePreviews);
	    	
	    });
    }
    $$('input, textarea').each(function(e){
    	e.observe('focus',function(ev){
    		Form.Element.select(this);
    	});
    });
    $$('form.spam-protect').each(function(e){
    	spamProtectForm(e);
    });
};
if(window.Spica){
	Spica.Event.run(function() { 
		  window.lightbox = new Lightbox({
			    loadingimg:'/static/images/layout/lightbox/loading.gif',
			    expandimg:'/static/images/layout/lightbox/expand.gif',
			    shrinkimg:'/static/images/layout/lightbox/shrink.gif',
			    blankimg:'/static/images/layout/lightbox/blank.gif',
			    previmg:'/static/images/layout/lightbox/prev.gif',
			    nextimg:'/static/images/layout/lightbox/next.gif',
			    closeimg:'/static/images/layout/lightbox/close.gif',
			    effectimg:'/static/images/layout/lightbox/zzoop.gif',
			    effectpos:{x:-20,y:-20},
			    effectclass:'effectable',
			    resizable:true,
			    animation:true
			  });
		});
}
Event.observe(window,'load',doOnloadHooks);
