//Robert Penner Easings (gibt keine besseren!)
jQuery.extend(jQuery.easing, {
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeInCubic: function(x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function(x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});
function initSlidesBrowser(o) {
	var c = $("#"+o.id);
	if(!c || !c.length) return;
	o._width = c.width();
	var idx = 0;
	o.timer = 0;
	o.slides = c.find(".slides .holder");
	o.menu = c.find(".slideMenu");
	o.length = c.find(".slide").length;
	c.find(".holder").width(o._width*o.length);
	
	o.menu.find("a").each(function() { 
		this.idx = parseInt(this.rel)}
	).click(function() {
		o.select(this.idx,false,0);
		return false;
	});
	c.find(".buttonFwd").click(function() {
		o.select(0,1,0);
	});
	c.find(".buttonBwd").click(function() {
		o.select(0,-1,0);
	});
	o.select = function(i,d,t) {
		if(this.timer > 0) {
			clearTimeout(this.timer);
			this.timer = 0;
		}
		var tx = 0;
		if(d) {
			o.cur += d;
			if(o.cur >= o.length) o.cur = 0;
			if(o.cur < 0) Math.max(o.cur = o.length-1,0);
		} else {
			o.cur = i;
		}
		tx = -o.cur*o._width;
		
		o.slides.animate({left:tx},900,"easeInOutCubic",function() {
			if(t > 100) {
				o.timer = setTimeout(function() {o.next(t)},t);
			}
		});
		o.menu.find("a").each(function() {
			this.className = this.idx==o.cur ? "active" : "";
		});
	}
	o.next = function(t) {
		this.select(0,1,t);
	}
	if(o.length < 2) {
		o.pause = 0;
		o.menu.hide();
	}
	o.cur = 0;
	o.select(0,false,o.pause);
}


FormHandler = {
	form_page:null,
	thanks_page:null,
	hasThanks:false,
	ident:"none",
	_form:null,
	init:function(fid) {
		this.ident = fid;
		var h = $(".form_pages_"+fid);
		this.form_page = h.find(".form_page");
		this.thanks_page = h.find(".thanks_page");
		this.hasThanks = jQuery.trim(this.thanks_page.text()) != "";
		this._form = this.form_page.find("form");
		if(!this._form[0]) return;
		var handler = this;
		$(this._form).submit(function() {
			return handler.beforeSubmit(this);
		}).trigger("reset");
		var fn = this["init_"+fid];
		if(fn) fn.apply(this);
	},
	beforeSubmit:function(f) {
		var req = f.required;
		var ok = true;
		if(req) {
			var r = req.value.split(",");
			ok = this.checkFields(f,r);
		}
		if(ok) this.doSubmit();
		return false;
	},
	checkFields:function(fm,fa) {
		var ok = true;
		for(var i=0;i<fa.length;i++) {
			var f = fm[fa[i]];
			if(!f) continue;
			var v = jQuery.trim(f.value);
			if(v=="") {
				ok = false;
				f.value="Bitte ausfüllen";
				$(f).focus(function() {
					this.value="";
					$(this).removeClass("error");
					$(this).unbind("focus");
				}).addClass("error");
			}
		}
		return ok;
	},
	doSubmit:function() {
		var url = this._form[0].url.value;
		this._form[0].ident.value = this.ident;
		var d = this._form.serialize();
		$.ajax({
			type:"POST",
			url:url,
			data:d,
			cache:false,
			context:this,
			success:this.onSent,
			error:this.onError,
			dataType:"text"
		});
	},
	onSent:function(d,st,requ) {
		var rep="";
		if(d.indexOf("msg=") > 0) {
			var pp = d.split("&");
			for(var i=0;i<pp.length;i++) {
				var p = pp[i];
				if(p.substr(0,4)=="msg=") {
					rep = decodeURI(p.substr(4));
					break;
				}
			}
		}
		if(this.hasThanks) {
			this.form_page.hide();
			var html = this.thanks_page.html();
			html = html.replace(/###ANTWORT###/gi,rep);
			this.thanks_page.html(html);
			this.thanks_page.show();
		} else {
			alert("Vielen Dank! Ihre Anfrage wurde gesendet.");
			if(this.ident == "haendleranfrage") parent.$.fancybox.close();
		}
	},
	onError:function(requ,st,err) {
		onSent("",st);
	},
	init_haendleranfrage:function() {
		var f = this._form[0];
		var plz = f.plz;
		if(plz) {
			$(this._form).find("area").click(function() {
				plz.value = this.hash.split("=")[1];
			});
		}
	}
	
}
function checkForm(ev) {
	//this == form
	var req = this.required;
	var ok = true;
	if(req && req.value != "") {
		var rf = req.value.split(",");
		for(var i=0;i<rf.length;i++) {
			var n = rf[i];
			var elm = this[n];
			if(!elm && typeof(elm.value)!="undefined") continue;

			if(elm.type=="text" || elm.type=="textarea") {
				var v = jQuery.trim(elm.value);
				if(v=="") {
					ok = false;
					elm.value="Bitte ausfüllen";
					$(elm).focus(function() {
						this.value="";
						$(this).removeClass("error");
						$(this).unbind("focus");
					}).addClass("error");
				}
			}
		}
	}
	return ok;
}
function init_form(fid) {
	FormHandler.init(fid);
}
function init_panes(pid) {
	var p = $("#"+pid);
	if(!p) return;
	p.find(".content_panes_menu a").click(function() {
		p.find(".content_pane").hide();
		p.find("#"+this.rel).show();
		$(this).siblings().removeClass("active");
		$(this).addClass("active");
		return false;
	});
	var h = location.hash;
	if(h) {
		h = h.substr(1);
		p.find("a[rel=p"+h+"]").click();
	}
}
function init_lok_words(pid) {
	if(typeof(lok_words)=="undefined") return;
	var w = lok_word_list;
	var s = "\\b("+w.join("|")+")\\b";
	var reg = new RegExp(s,"gi");
	$(".content,.content_wide,.content_narrow").find("div.rtf,p,li").each(function() {
		var par = $(this).context;
		var html = this.innerHTML.replace(reg,function(a) {
			var r = a;
			var entry = lok_words[a];
			if(entry.aid > 0) {
				if(entry.aid != pid) {
					var lnk = 'href="index.php?article_id='+entry.aid+'&clang='+entry.clang+'"';
					r = "<a class='lok_word' "+lnk+" title=\""+entry.text+"\">"+a+"</a>";
				}
			} else {
				r = "<a class='lok_word' title=\""+entry.text+"\">"+a+"</a>";
			}
			return r;
		});
		this.innerHTML = html;
	});
	/*
	$(".content,.content_wide").find("div,p,li,strong").each(function() {
		var len = this.childNodes.length;
		if(len > 0) {
	 		var ch = this.firstChild;
	 		var re = {s:"",r:""};
	 		while(ch) {
	 			if(ch.nodeType==3 && ch.nodeValue != "") {
	 				var t = ch.nodeValue.replace(/^\s+|\s+$/,"");
	 				if(t != "") {
	 					var nv = ch.nodeValue;
	 					re.s = ch.nodeValue;
		 				re.r = ch.nodeValue.replace(reg,function(a,b,p) {
		 					var entry = lok_words[a];
		 					trace("-- replace "+a);
		 					if(entry.aid > 0) {
		 						var lnk = 'href="index.php?article_id='+entry.aid+'&clang='+entry.clang+'"';
		 					} else {
		 						var lnk = "";
		 					}
		 					return "<a class='lok_word' "+lnk+" title=\""+entry.text+"\">"+a+"</a>";
	 					});
	 				}
	 			}
		 		ch = ch.nextSibling;
		 	}
		 	if(re.r != "" && re.r != re.s) {
		 		var html = $(this).html().replace(re.s,re.r);
				$(this).html(html);
		 	}
		 }
	});
	*/
	$(".lok_word").tipTip();
}


//Energiespar-Rechner
Calculator = {
	values: {
		rahmenflaeche:0,
		glasflaeche:0,
		alter:0,
		glastyp:0,
		energietyp:0,
		preis:0
	},
	energie: ["Heizöl","Gas"],
	einheit: ["Liter","kWh"],
	uWertAltRa: 1.9,
	set_G: 3600,
	set_H: 11800,
	set_W: 0.75,
	resultSets: [
		{name:"Life/-Style/-Vision oder -Flair",wk:false,id:"cStandard",
			heizoel:0,
			rpreis:0,
			co2:0,
			uwert_rahmen:1.4,
			uwert_glas:1.1,
			psi:0.06,
			psi_text:"Aluminium-Randverbund",
			html:"",loaded:false
			},
		{name:"Warme Kante",wk:true,id:"cWK",
			heizoel:0,
			rpreis:0,
			co2:0,
			uwert_rahmen:1.4,
			uwert_glas:1.1,
			psi:0.045,
			psi_text:"Edelstahl-Randverbund",
			html:"",loaded:false
			},
		{name:"Klima Plus",wk:true,id:"cKP",
			heizoel:0,
			rpreis:0,
			co2:0,
			uwert_rahmen:1.2,
			uwert_glas:0.9,
			psi:0.045,
			psi_text:"Edelstahl-Randverbund",
			html:"",loaded:false
			},
		{name:"Passiv",wk:true,id:"cPassiv",
			heizoel:0,
			rpreis:0,
			co2:0,
			uwert_rahmen:1.2,
			uwert_glas:0.5,
			psi:0.045,
			psi_text:"Edelstahl-Randverbund",
			html:"",loaded:false
			},
		{name:"Sonderaktion: Life/-Style/-Vision oder -Flair mit Aktionsglas 0,7 UW",wk:false,id:"cAktion",
			heizoel:0,
			rpreis:0,
			co2:0,
			uwert_rahmen:1.4,
			uwert_glas:0.7,
			psi:0.06,
			psi_text:"Aluminium-Randverbund",
			html:"",loaded:false
			}
	],
	onResult:function(d,t,xhr) {
		this.update(xhr.cIdx,d);
	},
	onError:function(d) {
		this.update(xhr.cIdx,"&nbsp;");
	},
	update:function(idx,t) {
		var r = this.resultSets[idx];
		if(!r) return;
		r.loaded = true;
		r.html = t;
		$("#"+r.id).html(t).hide();
		var ok = true;
		for(var i=0;i<this.resultSets.length;i++) {
			ok = this.resultSets[i].loaded;
			if(!ok) break;
		}
		if(ok) this.show();
	},
	run:function(f) {
		var v;
		//Eingaben
		for(var n in this.values) {
			var ff = f[n];
			if(ff) {
				v = this.getValue(ff);
				this.values[n] = v;
				this.setValue(ff,this.fromFloat(v));
			}
		}
		//Berechnen
		var vv = this.values;
		var snd = {};
		for(var n in this.values) snd[n] = this.values[n];
		
		for(var i=0;i<this.resultSets.length;i++) {
			var r = this.resultSets[i];
			r.loaded = false;
			$("#"+r.id).hide();
			for(var nn in r) snd[nn] = r[nn];
			//var par = jQuery.param(snd);
			$.ajax({
				url:"js/kostenrechner.php",
				type:"post",
				cache:false,
				data:snd,
				context:this,
				cObj:this,
				cIdx:i,
				dataType:"text",
				beforeSend:function(xhr) {
					xhr.cIdx = i;
				},
				success:this.onResult,
				error:this.onError});
		}
		
	},
	show:function() {
		$("a[rel=cStandard]").click();
	},
	getValue:function(e) {
		var v = 0;
		var len = e.length;
		if(e.type == "text") {
				v = Math.max(1,this.toFloat(jQuery.trim(e.value)));
		} else if(e.type == "select-one") {
				v = this.toFloat(e.value);
		} else if(len > 0) {
			var el = e[0];
			for(var i=0;i<len;i++) {
				var el = e[i];
				if(el.checked) break;
			}
			v = this.toFloat(el.value);
		}
		return v;
	},
	setValue:function(e,v) {
		var len = e.length;
		if(e.type=="text") {
			e.value = v;
		} else if(len > 0) {
			var s = e.type=="select-one" ? "selected" : "checked";
			for(var i=0;i<len;i++) {
				var el = e[i];
				if(el.value==v) el[s] = true;
			}
		}
	},
	toFloat:function(v) {
		var d = parseFloat(v.replace(/[\.,]/g,'.'));
		if(isNaN(d)) d = 0;
		return d;
	},
	fromFloat:function(v) {
		return v.toString().replace(/\./g,',');
	},
	showHide:function(e) {
		var id = this.rel;
		$(this).siblings().removeClass("active");
		$(this).addClass("active");
		$(this).parent().parent().find(".tab").hide();
		$("#"+id).show();
		return false;
	}
}

function initCalculator() {
	var f = $("#calc");
	for(var i=0;i<Calculator.resultSets.length;i++) {
		var r = Calculator.resultSets[i];
		var id = r.id;
		$("#"+id).hide();
		$("a[rel="+id+"]").click(Calculator.showHide).css("cursor","pointer");
	}
	f.find("#runForm").click(function() {
		Calculator.run(this.form);
		return false;
	});
}

function initInterface(p) {
	return true;
}
function flGet(w,p) {
	if(w=='getData') {
		var d = window[p];
		if(!d) return false;
		return d;
	}
}

function trace(s) {
	//if(typeof console == "undefined") return;
	if(window.console) console.log(s);
}


/**
 * This script contains embed functions for common plugins. This scripts are complety free to use for any purpose.
 */
function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function writeEmbed(cls, cb, mt, p) {
	var h = '', n;
	p.src = '' + p.src;
	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}


/* Funktion Referenzen */

function refslideshow(referenz) {
$.fancybox(referenz, {
			'padding'			: 10,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'titlePosition'     : 'inside',
			'titleFormat'		: formatTitle,
			'type'              : 'image',
			'changeFade'        : 0
		});
}


function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="refdescription">' + (title && title.length ? title : '' ) + '<br /><em>Bild ' + (currentIndex + 1) + ' von ' + currentArray.length + '</em></div>';
}

