/* =============================================================================
 * JS DROPDOWN MENU BY http://www.leigeber.com/
 * -------------------------------------------------------------------------- */
var DDSPEED = 10;
var DDTIMER = 15;

var h = null;
var lastH = null;

// main function to handle the mouse events //
function ddMenu(id,d){
  lastH = h;
  h = document.getElementById(id + '-ddheader');  
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
  	// set class name of header  	
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);      
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);    
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;  
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }  
  c.style.height = currh + (dist * d ) + 'px';
    
  //c.style.opacity = currh / c.maxh;
  //c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);    
  }
  if (d == -1 && currh + (dist * d ) <= 4) {
  	c.style.height = '0px';  	  	
  }
  
  if (c.style.height == '0px' && d == -1) {  	
  	h.className = '';
  	lastH.className = '';  	
  } else {
  	h.className = 'active';  	
  } 
}

/* =============================================================================
 * OUR OWN JS STUFF
 * -------------------------------------------------------------------------- */
function $N(name) {
	return document.getElementsByName(name);
}

var FlipBook = Class.create();

FlipBook.prototype = {
		
	initialize: function(headTag, bodyTag, startIndex, bImages) {
		this._currentIndex = startIndex > 0 ? 0 : startIndex;
		this._currentImgIndex = 0;
		this._aHead = $A($$(headTag+'.fb-head'));
		this._aBody = $A($$(bodyTag+'.fb-body'));
		this._bImages = bImages ? bImages : false;
		this._aImage = new Array();
				
		var self = this;		
		
		// register onclick events		
		this._aHead.each(function(item, index) {			
			item.onclick = function(e) { self._setActive(index) };			
		});
		
		// if we have images to show, register the imageclick events
		if (this._bImages) {
			// collect images elements
			var images = $A($$('div.fb-images'));
			// create an Images Object for each of them
			images.each(function(img) {
				self._aImage.push(new FlipBook.Images(img, $$('table.fb-img-ctrl')[0]));
			});
			
			try {
				document.getElementsByName('fb-img-ctrl-previous')[0].onclick = function(e) { self._previousImage(); }
				document.getElementsByName('fb-img-ctrl-next')[0].onclick = function(e) { self._nextImage(); }				
			} catch (ex) {}
			
		}
		
		// active the requested index		
		this._setActive(startIndex);
	},
	
	_setActive: function(index) {		
		// set the others inactive
		this._setInactive();
		this._hideBody();
		
		// set us active with "bold active" style
		this._aHead[index].addClassName('bold active');
		
		// show body
		this._showBody(index);
		
		if (this._bImages == true) {
			this._aImage[this._currentIndex]._hide();
			this._aImage[index]._show();
		}
		
		// set currentIndex
		this._currentIndex = index;
	},
	
	_setInactive: function() {
		this._aHead[this._currentIndex].removeClassName('bold active');
	},
	
	_showBody: function(index) {
		this._aBody[index].show();
	},
	
	_hideBody: function() {
		this._aBody[this._currentIndex].hide();
	},
	
	_nextImage: function() {		
		this._aImage[this._currentIndex].nextImage();
	},
	
	_previousImage: function() {
		this._aImage[this._currentIndex].previousImage();
	}
}

FlipBook.Images = Class.create();
FlipBook.Images.prototype = {
		
	initialize: function(source, controls, slideshow) {
		this._image = source;		
		this._aImages = source.childElements();
		this._index = 0;		
		this._bSlideshow = slideshow ? slideshow : false;
		this._controls = controls;
		
		// hide all images!
		this._aImages.each(function(img) {
			img.hide();
		});
	},
	
	nextImage: function() {		
		this._hideImage();
		this._index++;
		if (this._index >= this._aImages.size())
			this._index = 0;
		this._showImage();
	},
	
	previousImage: function() {
		this._hideImage();
		this._index--;
		if (this._index < 0)
			this._index = this._aImages.size() - 1; 
		this._showImage();
	},
	
	_hideImage: function() {		
		this._aImages[this._index].hide();
	},
	
	_showImage: function() {
		this._aImages[this._index].show();
	},
	
	_hide: function() {
		this._image.hide();
		if (this._controls != null)
			this._controls.hide();
	},
	
	_show: function() {
		if (this._controls != null && this._aImages.size() > 1)
			this._controls.show();
		this._image.show();
		this._showImage();
	}
}

var Projects = {
	currentCompany: null,
	currentInfo: 0,
	lastInUse: 0,
	
	display: function(id) {	
		// hide current
		if (this.currentCompany != null) {
			$('company-' + this.currentCompany).className = '';
			$('company-projects-' + this.currentCompany).style.display = 'none';			
		}
		this.currentCompany = id;
		
		$('company-' + this.currentCompany).className = 'active';
		$('company-projects-' + this.currentCompany).style.display = 'block';
	},
	
	info: function(id) {
		if (id == 0 && this.currentInfo > 0) {
			$('project-info-' + this.currentInfo).style.display = 'none';
		}
	
		// show it
		var elem = $('project-info-' + id);
		if (elem != null) {
			elem.style.display = 'block';
			this.currentInfo = id;
		}		
	},
	
	inUse: function(elem) {
		if (this.lastInUse != null && elem != null) {
			this.lastInUse.className = '';			
		}		
		elem.className = 'active';
		this.lastInUse = elem;
	}
}

/* FOR IE 6 - Image Swap */
var menuListImage0 = null;
var menuListImage1 = null;

if (navigator.appVersion.lastIndexOf('MSIE 6') != -1) {
	menuListImage0 = new Image();
	menuListImage1 = new Image();
	menuListImage0.src = './../img/ie6/navi_arrow_0.gif';
	menuListImage1.src = './../img/ie6/navi_arrow_1.gif';
}

function swapImage(id,img) {
	var obj = $ID(id);
	if (obj != null)
		obj.src = img.src;
}

/* CONTACT POPUP */
var ContactPopup = {
	open: function(url, hook, title) {
		var pParams = 'act=cases&mode=popup&title='+escape(title);
	
		new Ajax.Request('http://'+url+'async.php', {
			encoding: 'ISO-8859-1',
			method: 'get',
			parameters: pParams,
			onSuccess: function(request, json) {							
				$(hook).update(request.responseText);				 
			},
			onFailure: function() { alert("Es ist ein unerwarteter Fehler aufgetreten."); }			
		});
	},
	
	send: function(form, hook) {
	//send: function() {
		//var form = document.getElementById('contactForm');
		//var hook = 'popup-hook';
		form.request({
			onSuccess: function(request,json) {
				$(hook).update(request.responseText);		
				
				// reset the form
				try {
					form.reset();
				} catch (e) {}					
			},
			onFailure: function() { alert("Es ist ein unerwarteter Fehler aufgetreten."); return false; }
				
		});
		return false;
	}
}

var Profile = {
	autologin: function(form) {
		form.request({
			onSuccess: function(request,json) {
			return false;
			}			
		});
		return false;
	}
}