// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// Startpage animation
var IR;
function init() {
	IR = new ImgRotator();
}
Event.observe(window, 'load', init, false);

ImgRotator = Class.create();

ImgRotator.prototype = {
	
	initialize: function(options) {
		this.changeUi();
		this.setOptions(options);
		this.sIndex = Math.round(Math.random() * (this.options.images.length - 1));
		this.animDuration = 1.5;
	},
	
	changeUi: function() {
		var self = this;
		var img = $$("#content img").first();
		if (img && img.src.match(/slide/)) {
			var oldSrc = img.src;
			img.remove();
			new Insertion.After($$("#content h2").first(), "<div id='contentImage' class='cImg2'><img src=" + oldSrc + " /></div>");			
			/*var container = $N('div', { id: 'imgContainer' }, [
				$N('img', { src: oldSrc })
			]);*/
			self.startSlideShow();
		}
	},
	
	startSlideShow: function() {
	  var self = this;
		this.timer = new PeriodicalExecuter(this.nextImage.bind(this), 4);
	},
	
	nextImage: function() {
		var self = this;
		var images = self.options.images;
		var nSrc = "/images/slides/" + images[self.sIndex] + ".jpg";
		var newImage = $N('img', { src: nSrc, style: 'display:none;' });
		var oldImage = $$("#content img").first();
		$("contentImage").appendChild(newImage);
		new Effect.Appear(newImage, { duration: self.animDuration, delay: 2 });
		if (oldImage) {
			new Effect.Fade(oldImage, { 
				duration: self.animDuration,
				delay: 2,
				afterFinish: function() {
					oldImage.remove();
				}
			});
		}
		self.sIndex = (self.sIndex == (images.length - 1)) ? 0 : self.sIndex + 1;
	},
	
	setOptions: function(options) {
		this.options = {
			images: $A(["slide1", "slide2", "slide3", "slide4", "slide5", "slide6", "slide7", "slide8"])
		};
		Object.extend(this.options, options || {});
	}
	
};

function deleteentry(del) {
	var elid = del.split("_");
	var answer = confirm("Dieses Haus wirklich loeschen?");
	if (answer) {
		new Ajax.Request('/admin/house/delete/' + elid[1], { asynchronous:true, evalScripts:true } );
	}
}

function $N( arg1, arg2, arg3 ) { return Builder.node( arg1, arg2, arg3 ); };

function editHouse(id) {
	window.location.href = "/admin/house/edit/" + id;
}

function editStatic(id) {
	window.location.href = "/admin/static/edit/" + id;
}

function viewHouse(id) {
	var lang = $('language').innerHTML
	window.location.href = "/House/" + id + ".html";
}

function listByCategory(id) {
	var lang = $('language').innerHTML
	window.location.href = "/" + id + ".html";
}

function hlOn(element, color) {
	if (color) { 
		colorcode = color;
	}
	else {
		colorcode = "#E9F2FF";
	}
	element.style.background = colorcode;
}

function hlOff(element, color) {
	if (color) { 
		colorcode = color;
	}
	else {
		colorcode = "#FFFFFF";
	}
	element.style.background = colorcode;
}

function getSesId() {
	var session = document.cookie.match(/_session_id=(.*?);/).last();
	return session;
}

function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
  var flashi = [versionMajor,versionRevision]
	return flashi;
}
function checkFlash() {
	var linux = navigator.platform;
	var version = GetSwfVer();
	if(linux.match('Linux')) {
		if (parseInt(version[0]) < 9 || (parseInt(version[0]) == 9 && parseInt(version[1] < 60)) || typeof(version[0]) == "undefined") {
			alert('Du benötigst ein Flashplugin > Version 9.0.60 um Dateien hochzuladen');
			var MeinFenster = window.open('http://labs.adobe.com/downloads/flashplayer9.html', "Flashplugin", "width=300,height=400,left=100,top=200,resizable=yes");
		  //MeinFenster.focus();
		}	
	}
	else {
		if ((parseInt(version[0]) < 9)) {
			alert('Du benötigst das Flashplugin > Version 9.0 um Dateien hochzuladen');
			var MeinFenster = window.open('http://labs.adobe.com/downloads/flashplayer9.html', "Flashplugin", "width=300,height=400,left=100,top=200,resizable=yes");
			//MeinFenster.focus();
		}
	}	
}


var Flash = {
  // When given an flash message, wrap it in a list 
  // and show it on the screen.  This message will auto-hide 
  // after a specified amount of milliseconds
  show: function(flashType, message) {
    //new Effect.ScrollTo('flash-' + flashType);
    //$('flash-' + flashType).innerHTML = '';
    //if(message.toString().match(/<li/)) message = "<ul>" + message + '</ul>'
    $('flash-' + flashType).innerHTML = message;
    new Effect.Appear('flash-' + flashType, {duration: 0.3});
		setTimeout("Flash.fadeNotice()", 2000)
    //setTimeout(Flash['fade' + flashType[0].toUpperCase() + flashType.slice(1, flashType.length)].bind(this), 5000)
  },
  
  errors: function(message) {
    this.show('errors', message);
  },
	
  // Notice-level messages.  See Messenger. error for full details.
  notice: function(message) {
    this.show('notice', message);
  },
  
  // Responsible for fading notices level messages in the dom    
  fadeNotice: function() {
    new Effect.Fade('flash-notice', {duration: 0.3});
  },
  
  // Responsible for fading error messages in the DOM
  fadeError: function() {
    new Effect.Fade('flash-errors', {duration: 0.3});
  }
}

var Reflector = {
  reflect: function(element) {
    element = $(element);
    options = $H({
      amount: 1/3,
      opacity: 1/3
    }).merge(arguments[1] || {});
    
    var p = element.parentNode, n = element.nextSibling;
    var d = 1.0/(element.height*options.amount);
      
    (element.height*options.amount).times( function(line) {
      var h = Builder.node('div',{style:'height:1px;overflow:hidden'},
        [Builder.node('img',{src:element.src, 
          style:'margin-top:-'+(element.height-line-1)+'px'
        })]);
      p.insertBefore(h,n);
      $(h).setOpacity((1-d*line)*options.opacity);
    });
  }
}