var bwKeyHandler = [];
var overelement = null;
var allas = null;
var inoverprogress = false;
var currentproductimage = 0;
var fadeinWait = false;

function initZwoZero()
{
	//fix all link without lng= parameter
	var alla = document.getElementsByTagName("a");
	for(var n = 0; n < alla.length; n++)
	{
		alla[n].href = getUrlWithLng(alla[n].href);
	}
}

function getUrlWithLng(url)
{
	if(url.search('lng=') > -1)
	{
		
	}
	else
	{
	}

	return url;
}

// JavaScript Document
function spamprot(na,dom,lnd) {
 var spam = 'mailto:'+na+'@'+dom+'.'+lnd;
 document.location.href=spam;
}

function openwindow_incenter(url, name, width, height)
{
	leftVal = (screen.width-width) / 2;
	topVal = (screen.height-height) / 2;
	return window.open(url, name, 'width=' + width + ',height=' + height + ',left='+leftVal+',top='+topVal);

}



function overproduct(menua)
{
	return;
	if(inoverprogress == true)
	{
		return;
	}
	
	if(allas == null)
	{
		allas = document.getElementsByTagName("a");
	}

	for(n = 0; n < allas.length; n++)
	{
		if(allas[n].href == menua.href && allas[n] != menua)
		{
				overelement = allas[n];
				inoverprogress = true;
				if(overelement.className == "imageborder")
				{
					//new style
					overelement.className = "imageborder_over";
				}
				else
				{
					overelement.onmouseover();
				}
				
				inoverprogress = false;
			
		}
	}
}

function outproduct()
{
	if(overelement !== null)
	{
		if(overelement.className == "imageborder_over")
		{
			//new style
			overelement.className = "imageborder";
		}
		else
		{
			overelement.onmouseout();
		}
		overelement = null;
	}
}

function showproductgallery(index)
{
	var o = document.getElementById("productgallery" + currentproductimage);
	var n = document.getElementById("productgallery" + index);
	if(o !== null)
	{
		o.style.display = "none";
	}
	if(n !== null)
	{
		n.style.display = "block";
		currentproductimage = index;
	}
	
}

function initDefault()
{
	initZwoZero();
	
	var content = document.getElementById("content");
	var s = document.location.href;
	
	$('#admincontent').each(function(){
		if($(this).height() > $('#content').height())
		{
			//scrollbar active, resize content
			$('#content').css('width', '450px');
		}
	});
	$('#content p img').each(function(){
		var toWidth = 400;
		var toHeight = 400;
		var imgWidth = $(this).width();
		var imgHeight = $(this).height();
		
		if(imgWidth <= toWidth && imgHeight <= toHeight && imgWidth > 0)
		{
			var h = parseInt((toHeight-imgHeight)/2, 10);
			var w = parseInt((toWidth-imgWidth)/2, 10);
			$(this)
				.css('margin-left', w + 'px')
				.css('margin-top', h + 'px')
				.wrap('<div class="centerimage"></div>');
				

		}				
	});
	
	//image map handler
	$('area').each(function(){
		if($(this).attr('href') == null || $(this).attr('href') =='undefined')
		{
			if($(this).attr('alt').length > 0)
			{
				//search menu point
				
				var lnk = ($("a:contains('" + $(this).attr('alt') + "')").attr('href'));
				
				if(lnk.length > 0)
				{
					$(this).attr('href', lnk);
					$(this).attr('target', '_self');
				}
			}
		}
	});
	//fix händler OPERA Bug
	if(content !== null && (s.replace("haendler", "") != s))
	{
		content.style.overflow = "auto";
	}
	
	$('img.hoverstyle').each(function(){
		$(this).mouseover(function(){ $(this).addClass('hover'); });
		$(this).mouseout(function(){ $(this).removeClass('hover'); });
	});
	
	$('.thumbgallery a').lightBox({fixedNavigation:true});
	$('a[@rel*=lightbox]').lightBox({fixedNavigation:true});
	//check for products
	if(i = $('.intro', $('div.project')))
	{
		i.mouseover(function(){
			 $(this).addClass('over');

		});
		i.mouseout(function(){
			 $(this).removeClass('over');

		});
		i.click(function(){
			var projectId = parseInt($('input[type=hidden]:first', $(this)).val(), 10);
			
			if(projectId > 0)
			{
				document.location.href = 'index.php?p=projects&m=' + projectId;
			}
		});

	}

}


function browserIE()
{
	var rv = 0;
	var ua = null;
	var re = null;
	if(navigator)
	{
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			ua = navigator.userAgent;
			re  = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
			if (re.exec(ua) !== null)
			{
				rv = parseFloat( RegExp.$1 );
			}
		}
	}
	
return rv;

}

function bw_add_keyhandler(el)
{
	bwKeyHandler[bwKeyHandler.length] = el;
};

function handleArrowKeys(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
	
	for(var i = 0; i < bwKeyHandler.length; i++)
	{
		bwKeyHandler[i].handleKey(evt);
	}
 
}

document.onkeyup = handleArrowKeys;

function bw_gallery(speclink, container)
{
	//add the object to the keyhandler! yooo
	bw_add_keyhandler(this);
	
	this.arrImages = [];	
	this.imgShowed = null;
	this.lnkShowed = null;
	this.specLink = speclink;
	this.elContainer = document.getElementById(container);
	this.elContainer.me = this;
	this.elContainerLoader = document.getElementById(container + "_loader");
	this.currentLeft = 0;
	this.currentIndex = 0;
	this.gotoLeft = 0;
	this.gotoPositiv = true;
	var x = 0;
	var w = this.elContainer.parentNode.offsetWidth;
	var me = this;
	
	//get images
	for(var i = 0; i < this.elContainer.childNodes.length; i++)
	{
		el = this.elContainer.childNodes[i];
		if(el && el.id)
		{
			if(el.id.substring(0, container.length) == container )
			{
				//el.style.width = this.elContainer.offsetWidth + "px";


				if( (w - el.offsetWidth)/2 >= 1)
				{
					el.style.paddingLeft = (w - el.offsetWidth)/2 + 'px';
					el.style.paddingRight = el.style.paddingLeft;
				}
				if(browserIE() && x == 0)
				{
					/*x = (w - el.offsetWidth)/2+50;
					this.currentLeft = x;
					this.elContainer.style.marginLeft = x + 'px';*/
				}
				
				el.bwWidth = el.offsetWidth;
				el.bwWidthInContainer = x;
				if(this.specLink)
				{
					el.onmouseover = function() { me.imageOver(this); };
					el.onmouseout = function() { me.imageOut(this); }
					}
				el.onclick = function() { return me.imageClick(); return false; }
				el.style.marginLeft = x + 'px';
				this.arrImages[this.arrImages.length] = el;
				x += el.offsetWidth + 20;
			}
			
		}
	}
	
	setTimeout(this.meStringFnc("endLoading()"), 500);

}

bw_gallery.prototype.endLoading = function()
{
		if(this.elContainerLoader)
	{
		this.elContainerLoader.style.display = "none";
	}
	
	this.elContainer.style.visibility = "visible";
};

bw_gallery.prototype.handleKey = function(key)
{
	if (key)
	{
        switch (key.keyCode) {
            case 37:
				//right
                this.imagePrev();
                break;      
            case 39:
				//left
                this.imageNext();
                break;    
         }
    }
};

bw_gallery.prototype.addNavigation = function(id, imgid)
{
	var el = document.getElementById(id);
	var me = this;
	if(el )
	{		
		el.onclick = function() { me.imageShow(imgid); return false;}
	}
};
bw_gallery.prototype.imageNext = function(imgid)
{
	this.imageShow(this.currentIndex+1);
};

bw_gallery.prototype.imagePrev = function(imgid)
{
	this.imageShow(this.currentIndex-1);
};

bw_gallery.prototype.imageShow = function(imgid)
{
	var img = this.arrImages[imgid];
	
	if(img != null)
	{
		this.gotoLeft = -1*img.bwWidthInContainer;
		if(this.gotoLeft < this.currentLeft)
		{
			this.gotoPositiv = false;
		}
		else if(this.gotoLeft > this.currentLeft)
		{
			this.gotoPositiv = true;
		}
		else
		{
			return;
		}
		
		this.moveStep();
		this.currentIndex = imgid;
	}
	
};

bw_gallery.prototype.meStringFnc = function(fnc)
{
	return "document.getElementById('" + this.elContainer.id + "').me." + fnc;
};

bw_gallery.prototype.moveStep = function()
{
	var c = true;
	var n = 1;
	var fac = this.currentLeft - this.gotoLeft;
	fac = (fac < 0 ? -1 : 1) * fac;
	fac = 1 + fac/20;
	if(this.gotoPositiv != true)
	{
		n = -1;
	}
	
	
	this.currentLeft += n * fac;
	
	
	
	if((this.currentLeft > this.gotoLeft && this.gotoPositiv) || (this.currentLeft < this.gotoLeft && this.gotoPositiv == false))
	{
		this.currentLeft = this.gotoLeft;
		c = false;
	}
	
	this.elContainer.style.marginLeft = this.currentLeft + 'px';
	
	if(c)
	{
		setTimeout(this.meStringFnc("moveStep()"), 10);
	}
};

bw_gallery.prototype.imageOver = function(img)
{
	if(img != null)
	{
		img.className = "gallery_image over";
	}
};
bw_gallery.prototype.imageOut = function(img)
{
	if(img != null)
	{
		img.className = "gallery_image";
	}
};

bw_gallery.prototype.imageClick = function()
{
	if(this.specLink && this.specLink.length > 0)
	{
		location.href = this.specLink;
		return false;
	}
};

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

