// JavaScript Document

// Images Preload
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];}}
}

// Show Gallery Items
var g_timer = new Array();
function showGallery(id) {
	id = id||'';
	document.getElementById('galleryItems'+id).style.display = 'block';
	id = (id == '') ? 1 : id;
	window.clearTimeout(g_timer[id]);
}
function hideGallery(id) {
	id = id||'';
	id2 = id||1;
	g_timer[id2] = window.setTimeout(function(){hide(id);}, 5000);
}
function hide(id) {
	document.getElementById('galleryItems'+id).style.display = 'none';
}

// toggle Areas Object

function ToogleAreas() {
	this.activArea = '0';
}

ToogleAreas.prototype.show = function(area) {
	if(this.activArea != '0') {
		document.getElementById('area_'+this.activArea).style.display = 'none';
		document.getElementById('areaButton_'+this.activArea).className = 'areaButton';
	}
	document.getElementById('area_'+area).style.display = 'block';
	document.getElementById('areaButton_'+area).className = 'areaButton activ';
	this.activArea = area;
}

// Form inputs focus:blur
var iValue = '';
function inputF(obj) {
	obj.style.backgroundColor = '#FFFFFF';
	iValue = obj.value;
	if(inputF.arguments.length == 2) {
		obj.setAttribute('maxlength','5');
	}
	obj.value = '';
}
function inputB(obj) {
	obj.style.backgroundColor = '#E8D09B';
	if(obj.value == '') {
		if(inputB.arguments.length == 2) {
			obj.setAttribute('maxlength','8');
		}
		obj.value = iValue;
	}
}

// Card change format
function changeFormat(obj,id) {
	var format = obj[obj.selectedIndex].value;
	var ajax = new Ajax();
	ajax.url = 'include/ajax_do.php';
	ajax.params = 'do=changeformat&pid='+id+'&format='+format;
	ajax.onError = ajaxErrorHandler;

	ajax.onSuccess = function(txt,xml,headers) {
		if(ajaxError(txt)) {
			var age = ajaxGetError(txt);
			alert(age);
		} else {
			document.getElementById('price_'+id).innerHTML = txt;
		}
	}
	ajax.doRequest();
}

// Card change pices
function changePices(obj,id) {
	var pices = obj.value;
	var ajax = new Ajax();
	ajax.url = 'include/ajax_do.php';
	ajax.params = 'do=changepices&pid='+id+'&pices='+pices;
	ajax.onError = ajaxErrorHandler;

	ajax.onSuccess = function(txt,xml,headers) {
		if(ajaxError(txt)) {
			var age = ajaxGetError(txt);
			alert(age);
		} else {
			if(txt == "delete") {
				window.location.href = "warenkorb.html";
			} else {
				document.getElementById('price_'+id).innerHTML = txt;
				setPices(pices,'+');
				var elm = document.getElementById('button_order');
				var ielm = document.getElementById('info');
				if(photo_pices >= min_pices) {
					elm.innerHTML = '<input type="image" src="images/button_order.jpg" />';
					ielm.innerHTML = '&nbsp;';
				} else {
					elm.innerHTML = '<img src="images/button_order_min.jpg" onclick="alert(\''+min_pices_info+'\');" />';
					ielm.innerHTML = '<p>'+min_pices_info+'</p>';
				}
			}
		}
	}
	ajax.doRequest();
}
function setPices(p,o) {
	var p = parseInt(p);
	if(o == '+') {
		photo_pices += p;
	}
	if(o == '-') {
		photo_pices -= p;
	}
	if(setPices.arguments[2] == 'clp') { document.getElementById('cl_pices').innerHTML = '('+photo_pices+' Fotos)'; }
}

// Gallery Object
function Gallery(objname) {
	this.objname = objname;
	this.pid = 0;
	this.photoCount = 0;
	this.photosPath = "";
	this.gdir = "";
	this.photos = new Array();
	this.init = 1;
	this.loadImg = new Image();
	this.loadImg.src = 'images/load.gif';
}

Gallery.prototype.initG = function(init) {
	if(init == 2 || init == 3) {
		this.photoElement = document.getElementById('photo');
		this.nextElement = document.getElementById('next');
		this.prevElement = document.getElementById('prev');
		this.init = init;
	}
	this.infoElement = document.getElementById('info');

}

Gallery.prototype.setPid = function(pid) {
	this.pid = pid;
}

Gallery.prototype.setPhotoCount = function(c) {
	this.photoCount = c;
}

Gallery.prototype.setPhotosPath = function(p) {
	this.gdir = p;
	this.photosPath = 'galleries/'+p+'/photos/';
}

Gallery.prototype.setPhotos = function(p) {
	this.photos = p;
}

Gallery.prototype.next = function() {
	this.pid += 1;
	var b = (this.next.arguments.length > 0) ? this.next.arguments[0] : 'x';
	this._load(b);
	if(this.init == 2) { document.getElementById('pid').value = this.pid; }
	this._clearInfo();
	this.nextElement.style.display = ((this.pid+1) < this.photoCount) ? "inline" : "none";
	if(this.pid > 0) {
		this.prevElement.style.display = "inline";
	}
}

Gallery.prototype.prev = function() {
	this.pid -= 1;
	this._load();
	if(this.init == 2) { document.getElementById('pid').value = this.pid; }
	this._clearInfo();
	this.prevElement.style.display = (this.pid <= 0) ? "none" : "inline";
	if(this.pid < this.photoCount) {
		this.nextElement.style.display = "inline";
	}
}

Gallery.prototype._clearInfo = function() {
	if(this.infoElement != null) {
		this.infoElement.innerHTML = "&nbsp;";
	}
}

Gallery.prototype._load = function(border) {
	this.photoElement.src = this.loadImg.src;
	var obj = this.photoElement;
	var newPhoto = new Image();
	newPhoto.src = this.photosPath+this.photos[this.pid];
	newPhoto.onload = function() {
		obj.src = newPhoto.src;
	}
	/*
	while(this.imgIsLoaded(newPhoto) == true) {
		this.photoElement.src = this.loadImg.src;
	}
	this.photoElement.src = newPhoto.src;
	*/
}

Gallery.prototype.addToBasket = function() {
	this._clearInfo();
	if(this.init == 1 && document.getElementById('p'+this.pid)) {
		document.getElementById('p'+this.pid).innerHTML = "Im Warenkorb";
	}
	var ajax = new Ajax();
	ajax.url = 'include/ajax_do.php';
	ajax.params = 'do=adtobasket&pid='+this.pid;
	ajax.onError = ajaxErrorHandler;
	_this = this;

	ajax.onSuccess = function(txt,xml,headers) {
		if(ajaxError(txt)) {
			var age = ajaxGetError(txt);
			_this.infoElement.innerHTML = age;
		} else {
			_this.infoElement.innerHTML = txt;
			setPices(1,'+','clp');
		}
	}
	ajax.doRequest();
}

Gallery.prototype.slideshow = function(dir) {
	this.pid =0;
	this.photosPath = 'slideshows/'+dir+'/';

	var ajax = new Ajax();
	ajax.url = 'include/ajax_do.php';
	ajax.params = 'do=slideshow&path='+dir;
	ajax.onError = ajaxErrorHandler;
	_this = this;

	ajax.onSuccess = function(txt,xml,headers) {
		if(ajaxError(txt)) {
			var age = ajaxGetError(txt);
			alert(age);
		} else {
			var json = eval('('+txt+')');
			_this.photoCount = json.c;
			var tmp = String(json.p);
			_this.photos = tmp.split(",");
			_this.sShow('block');
		}
	}

	ajax.doRequest();
}

Gallery.prototype.sShow = function(display) {
	var html = document.getElementsByTagName('html')[0];
	var body = document.getElementsByTagName('body')[0];
	var popup = document.getElementById('popup');
	var slide = document.getElementById('slideshow');
	switch(display) {
		case "block":
			var c = '<table class="photo" style="width:850px;">'
						+'<tr>'
							+'<td class="prev" style="width:101px;"><img id="prev" src="images/prev.gif" onclick="'+this.objname+'.prev();" onmouseover="'+this.objname+'.pShow(\'over\');" onmouseout="'+this.objname+'.pShow(\'out\');" style="display:none;cursor:pointer;" alt="Zurück" /></td>'
							+'<td class="photo"><img id="photo" src="'+this.photosPath+this.photos[0]+'" alt="" onmouseover="'+this.objname+'.pShow(\'over\');" onmouseout="'+this.objname+'.pShow(\'out\');" /></td>'
							+'<td class="next" style="width:101px;"><img id="next" src="images/next.gif" style="display:inline;cursor:pointer;" onclick="'+this.objname+'.next();" onmouseover="'+this.objname+'.pShow(\'over\');" onmouseout="'+this.objname+'.pShow(\'out\');" alt="Weiter" /></td>'
						+'</tr>'
					+'</table>';

			slide.innerHTML = c;
			var onclick = document.createAttribute("onclick");
			onclick.nodeValue = this.objname+".sShow('none')";
			popup.setAttributeNode(onclick);
			html.style.overflow = "hidden";
			body.style.overflow = "hidden";
			popup.style.width = "100%";
			windowW = (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth;
			windowH = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
			body.style.height = windowW+'px';
			popup.style.zIndex = '99999';
			popup.style.width = windowW+'px';
			popup.style.height = windowH+'px';
			popup.style.top = (window.pageYOffset) ? window.pageYOffset+'px' : document.documentElement.scrollTop+'px';
			popup.style.backgroundImage = 'url(images/bg_popup.png)';
			slide.style.width = '850px';
			slide.style.height = '540px';
			slide.style.left = ((windowW/2)-425)+'px';
			slide.style.top = ((windowH/2)-270)+'px';
			popup.style.display = display;
			popup.style.display = display;
			this.initG(3);
		break;

		case "none":
			popup.style.display = display;
			popup.removeAttribute("onclick");
			popup.style.width = 0;
			popup.style.height = 0;
			popup.style.top = 0;
			popup.style.backgroundImage = 'none';
			slide.style.width = 0;
			slide.style.height = 0;
			slide.style.left = 0;
			slide.style.top = 0;
			slide.innerHTML = '';
			html.style.overflow = "auto";
			body.style.overflow = "visible";
			body.style.width = "auto";
			body.style.height = "auto";
		break;
	}
}
Gallery.prototype.pShow = function(display) {
	var popup = document.getElementById('popup');
	var onclick = document.createAttribute("onclick");
	switch(display) {
		case "over":
			onclick.nodeValue = "";
			popup.setAttributeNode(onclick);
		break;
		
		case "out":
			onclick.nodeValue = this.objname+".sShow('none')";
			popup.setAttributeNode(onclick);
		break;
	}
}

Gallery.prototype.imgIsLoaded = function(img) {
	if(img.complete) {
		return true;
	} else {
		return false;
	}
}


function Fadeshow() {
	this.arrImages = [];
	this.intCount = 0;
	this.timer = null;
	
	return this;
}

Fadeshow.prototype.fadeIn = function(obj, elm, opac) {
	
	if(opac < 1) {
		opac += 0.05;
		elm.style.filter = "alpha(opacity="+parseInt(opac*100)+")";
		elm.style.KHTMLOpacity = opac;
		elm.style.MozOpacity = opac;
		elm.style.opacity = opac;
		window.setTimeout(function(){obj.fadeIn(obj, elm, opac);}, 50);
	} else {
		elm.style.opacity = 1;
		window.setTimeout(function(){obj.fadeOut(obj, elm, 1);}, 5000);
	}
}

Fadeshow.prototype.fadeOut = function(obj, elm, opac) {	
	
	if(this.timer == null) {
				if(this.intCount < this.arrImages.length-1) {
			this.intCount++;
		} else {
			this.intCount=0;
		}

		var n = this.arrImages[this.intCount];
		document.getElementById("headerillu").appendChild(n);
		obj.fadeIn(obj, n, 0);
	}
	
	if(opac > 0) {
		opac -= 0.05;
		elm.style.filter = "alpha(opacity="+parseInt(opac*100)+")";
		elm.style.KHTMLOpacity = opac;
		elm.style.MozOpacity = opac;
		elm.style.opacity = opac;
		this.timer = window.setTimeout(function(){obj.fadeOut(obj, elm, opac);}, 50);
	} else {
		elm.style.opacity = 0;
		document.getElementById("headerillu").removeChild(elm);
		this.timer = null;
	}
}

Fadeshow.prototype.init = function(obj, p) {
	var elm;
	p.sort();
	
	document.getElementById("headerillu").style.position = 'relative';
	document.getElementById("headerillu").style.height = '447px';
	
	if(p.length > 1) {
		for(var i = 0; i < p.length; i++) {
			var x = new Image();
			x.src = '/slideshows/index/'+p[i];
			x.filter = "alpha(opacity=1)";
			x.style.KHTMLOpacity = 0;
			x.style.MozOpacity = 0;
			x.style.opacity = 0;
			x.style.position = 'absolute';
			this.arrImages.push(x);
		}

		elm = this.arrImages[this.intCount];
		elm.filter = "alpha(opacity=1)";
		elm.style.KHTMLOpacity = 1;
		elm.style.MozOpacity = 1;
		elm.style.opacity = 1;
		document.getElementById("headerillu").appendChild(elm);
		window.setTimeout(function(){obj.fadeOut(obj, elm, 1);}, 5000);
	} else {
		elm = new Image();
		elm.src = '/slideshows/index/'+p[0];
		this.arrImages.push(x);
		document.getElementById("headerillu").appendChild(elm);
	}
}

