/*
	Global JS class for industriemagazin:netzwerk
	
	Version:		1.0		
	Last Update:	22-06-2010
	Developed by:	Jakub Jozefek
	
	Requires:		JQuery Framework
	
*/


function Imv() {
	/**** config *****/
	
	this.calendarRemoteUrl = "/shared/cal";
	this.serviceID = 2;
}

Imv.prototype.id = function(e) {
	return document.getElementById(e);
}

Imv.prototype.ajaxizeLink = function (url) {
	var tmp = url.split("?");
	var isAjaxed = false;
	if (tmp.length > 1) {
		var query = tmp[1].split("&");
		for (var i = 0; i < query.length; i++) {
			if (query[i] == "src=ajax") {
				isAjaxed = true;
			}
		}
		if (!(isAjaxed)) {
			url = url + "&t=" + gt();
		}
	} else {
		url = url + "?t=" + gt();
	}
	return url;
}

Imv.prototype.addToQuery = function (url, k, v) {
	var tmp = url.split("?");
	if (tmp.length > 1) {
		var query = tmp[1].split("&");
		if (query.length > 0) {
			url += "&"+k+"="+v;
		} else {
			url += k+"="+v;
		}
	} else {
		url += "?"+k+"="+v
	}
	return url;
}

Imv.prototype.form2QueryString = function (obj, queryString, jqPost) {
	var e = Imv.id(obj).elements;
	if (typeof(queryString) == "undefined") {
		queryString = "";
	}
	if (typeof(jqPost) == "undefined") {
		jqPost = false;
	}
	
	function _f2qsAttach(queryString, k, v, jqPost) {
		if ((jqPost)) {
			queryString += "&"+k+"="+v;
		} else {
			queryString = Imv.addToQuery(queryString, k, v);
		}
		return queryString;
	}
	
	for (var i = 0; i < e.length; i++) {
		if (e[i].type == "checkbox") {
			if (e[i].checked == true) {
				queryString = _f2qsAttach(queryString, e[i].name, encodeURIComponent(e[i].value), jqPost);
			}
		} else {
			queryString = _f2qsAttach(queryString, e[i].name, encodeURIComponent(e[i].value), jqPost);
		}
	}
	return queryString;
}

Imv.prototype.webhost = function() {
	var webhost = "";
	webhost = window.location.protocol;
	webhost += "//";
	webhost += window.location.host;
	return webhost;
}

Imv.prototype.formValues = function() {} 

Imv.prototype.formValues.e = function(e) {
	return document.getElementById(e);
}

Imv.prototype.formValues.attach = function(e, v) {
	if (this.e(e).value == "") {
		this.e(e).value = v;
		this.e(e).className = this.e(e).className + " prefilled";
	}
	this.e(e).onfocus = function() {
		if (this.value == v) {
			this.value = "";
			this.className = this.className.replace(/prefilled/gi, "");
		}
	};
	this.e(e).onblur = function() {
		if (this.value == "") {
			this.value = v;
			this.className = this.className + " prefilled";
		}
	};
}

Imv.prototype.formValues.insert = function(e, v) {
	g(e).value = v;
}

Imv.prototype.inputErrors = function() {};

Imv.prototype.inputErrors.add = function(e, txt, c) {
	if (Imv.id(c) != null) {
		if (Imv.id("ieUl_"+c) == null) {
			Imv.id(c).innerHTML = '<span class="errorContainer-head">Folgende Fehler sind aufgetreten:</span><ul id="ieUl_'+c+'"></ul>';
		}
	}
	var html = '<li id="ieParent_'+e+'">'+txt+'</li>';
	Imv.id("ieUl_"+c).innerHTML += html;
	Imv.id(e).className += " error";
}

Imv.prototype.inputErrors.remove = function(e, c) {
	Imv.id(e).className = Imv.id(e).className.replace(/ error/gi, "");
	$("+ieParent_"+e).remove();
	if (Imv.id("ieUl_"+c) != null) {
		var lis = Imv.id("ieUl_"+c).getElementsByTagName("li");
		if (lis.length < 1) {
			Imv.id(c).innerHTML = "";
		}
	}
}

Imv.prototype.inputErrors.validate = function(e, resp, c) {
	if (resp != false && resp != "false") {
		Imv.inputErrors.add(e, resp, c);
	} else {
		Imv.inputErrors.remove(e, c);
	}
}

Imv.prototype.autosizeArea = function() {}

Imv.prototype.autosizeArea.attach = function (e, h) {
	if (typeof(h) == "undefined") {
		var mh = Imv.id(e).style.height.split("px").join("");
	} else {
		var mh = h;
	}
	var w = Imv.id(e).style.width;
	var tstamp = new Date();
	var id = "IMVAutosizeArea_"+tstamp.getTime();
	var src = '<div id="'+id+'" class="area" style="width: '+w+'; height: auto; display: none;"></div>';
	Imv.id("addcontent").innerHTML += src;
	$("#"+e).keyup(function() { Imv.autosizeArea.calculate(e, id, mh); });
	Imv.autosizeArea.calculate(e, id, mh);
}

Imv.prototype.autosizeArea.calculate = function (area, holder, mh) {
	var holder = Imv.id(holder);
	var area = Imv.id(area);
	var text = area.value + "&nbsp;";
	//text = text.replace(/\n/gi, "<br />");
	text = text.split("\n").join("<br />");
	holder.innerHTML = text;
	holder.style.display = "block";
	var h = holder.offsetHeight + 15;
	//var h = $(holder).height() + 20;
	holder.style.display = "none";
	if (h < mh) {
		h = mh;
	}
	if (h > 220) {
		h = 220;
	}
	area.style.height = h+"px";
}


Imv.prototype.setLoadingMessage = function(obj, t) {
	if (typeof(t) == "undefined") {
		t = 1000;
	}
	var w = $(obj).width();
	var oldMessage = obj.innerHTML;
	obj.innerHTML = "Lade...";
	obj.style.width = w+"px";
	if (typeof(obj.id) == "undefined" || obj.id == "") {
		obj.id = "IMVLoadingMessageComponent_"+new Date().getTime();
	}
	setTimeout("Imv.unsetLoadingMessage('"+obj.id+"', '"+oldMessage+"');", t);
}

Imv.prototype.unsetLoadingMessage = function(id, oldMessage) {
	Imv.id(id).innerHTML = oldMessage;
	Imv.id(id).style.width = "";
}

Imv.prototype.calendar = function() {}

Imv.prototype.calendar.show = function(obj, callback) {
	var v = obj.value;
	var url = Imv.ajaxizeLink(Imv.calendarRemoteUrl)+"&cb="+callback;
	if (g("calwrapper") == null) {
		var html = '<div id="calwrapper">';
			html += '	<div id="calwaiting" class="preloader">';
			html += '		<div class="calhead" id="cwcalhead">';
			html += '			<span class="calcurrent">Lade...</span>';
			html += '		</div>';
			html += '		<div id="calitemwrapper">';
			html += '			<img src="/media/images/indicators/tb.gif" width="32" height="32" />';
			html += '			<span class="main-caption">Kalenderdaten werden geladen...</span>';
			html += '			<span class="sub-caption">Bitte haben Sie einen Moment Geduld.</span>';
			html += '		</div>';
			html += '	</div>';
			html += '	<div id="ajaxcalcontent"></div>';
			html += '</div>'
		g("addcontent").innerHTML += html;
	}
	if (obj.id == null) {
		obj.id = "IMVCalParentObject_"+new Date().getTime();
	}
	IMVCalParentObject = obj.id;
	Imv.calendar.position();
	Imv.calendar.loadUrl(url, true);
	obj.value = v;
}

Imv.prototype.calendar.position = function() {
	if (typeof(IMVCalParentObject) != "undefined" && g(IMVCalParentObject) != null) {
		var pos = $("#"+IMVCalParentObject).position();	
		var height = $("#"+IMVCalParentObject).height();
		var width = $("#"+IMVCalParentObject).width();
		g("calwrapper").style.left = Number(pos.left)+"px";
		g("calwrapper").style.top = Number(pos.top+height+7)+"px";		
	}
}

Imv.prototype.calendar.loadUrlFromCal = function(obj) {
	Imv.calendar.loadUrl(obj.href, false);
}

Imv.prototype.calendar.loadUrl = function(url, addevent) {
	if (typeof(addevent) == "undefined") {
		addevent = false;
	}
	if (g("calcontent") != null)  {
		calWasLoaded = true;
	} else {
		calWasLoaded = false;
	}
	
	if ((calWasLoaded)) {
		var oh = $("#calcontent").height();
		g("calitemwrapper").style.height = oh+"px";
	}
	g("calwaiting").style.display = "block";
	g("ajaxcalcontent").style.display = "none";
		
	$.post(url, { },
		function(data) {
			g("ajaxcalcontent").innerHTML = data;
			g("calwaiting").style.display = "none";
			g("ajaxcalcontent").style.display = "block";
			var nh = $("#calcontent").height();
			if ((calWasLoaded)) {
				g("calcontent").style.height = oh+"px";
				if (oh != nh) {
					$("#calcontent").animate({height: nh+"px"}, 150, function() { g("calcontent").style.height = ""; });
				}
			}
			if ((addevent)) {
				$(document).bind('click.calhideevent', function() { Imv.calendar.hide(); });
				$("#calwrapper").bind('click.calhideevent', function(e) { e.stopPropagation(); });
				$("#"+IMVCalParentObject).bind('click.calhideevent', function(e) { e.stopPropagation(); });
			}
		});
}

Imv.prototype.calendar.sendToInput = function(e, v) {
	Imv.formValues.insert(e, v);
	Imv.calendar.hide();
}

Imv.prototype.calendar.hide = function() {
	$("#calwrapper").fadeOut(250, function() {
		$("#calwrapper").remove();
		$(document).unbind('click.calhideevent');
	});
}

Imv.prototype.handleJsonResponse = function (data) {
	if (data.state != "0") {
		if (data.msg == "login") {
			// handle login request
		} else {
			alert(data.msg);
		}
	} else {
		return data;
	}
}


Imv.prototype.dateDiff = function (t) {
	var ct = Math.round(new Date().getTime()/1000);
	var diff = ct - t;
		t = new Date(t*1000);
	var ret;
	//alert(new Date(ct*1000));
	if (diff < 3600) {
		ret = Math.ceil(diff / 60);
		if (ret > 1) {
			ret = "Vor <strong>"+ret+"</strong> Minuten";
		} else {
			ret = "Vor <strong>einer</strong> Minute";
		}
	} else {
		if (diff < 86400) {
			ret = Math.ceil(diff / 3600);
			if (ret > 1) {
				ret = "Vor <strong>"+ret+"</strong> Stunden";
			} else {
				ret = "Vor <strong>einer</strong> Stunde";
			}
		} else {
			ret = t.getDate()+"."+(t.getMonth()+1)+"."+t.getFullYear()+" "+t.getHours()+":"+t.getMinutes();
		}
	}
	return ret;
}

Imv.prototype.submitForm = function(e) {
	g(e).submit();
}

Imv.prototype.readCk = function(name) {
	/* Powered by http://www.quirksmode.org/js/cookies.html */
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

Imv.prototype.setCk = function (name, value, days) {
	/* Powered by: http://www.quirksmode.org/js/cookies.html */
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/"
}

Imv.prototype.appendAf = function(obj, af) {
	if (obj.href != "" && obj.href != "#") {
		var t = obj.href.split("#");
		var turl = t[0].split("?");
		var dest = t[0];
		var newQsParts = new Array();
		if (typeof(turl[1]) != "undefined") {
			var qsparts = turl[1].split("&");
			var tmp;
			var ttmp;
			for (var i = 0; i < qsparts.length; i++) {
				tmp = qsparts[i].split("=");
				if (tmp[0] != "af" && tmp[0] != "analytics_from") {
					ttmp = tmp[0];
					if (typeof(tmp[1]) == "undefined") {
						ttmp += '';
					} else {
						ttmp += '='+tmp[1];
					}
				newQsParts.push(ttmp);
				} else {
					if (typeof(af) == "undefined" || af == '' || af == false) {
						af = tmp[1];
					}
				}
			}
		}
		dest = turl[0];
		dest = dest.split("www.").join("");
		//newQsParts.push("_r="+new Date().getTime());
		if (newQsParts.length > 0) {
			dest += "?"+newQsParts.join("&");
		}
		var nh = '';
		if (typeof(t[1]) != "undefined") {
			nh = ':'+t[1]; 
		}
		if (typeof(af) != "undefined") {
			nh = dest+'#'+'!af:'+af+nh;
			obj.href = nh;
		}
	}
}


$(document).ready(function() {
	Imv = new Imv();	   
});


