/*  Flirtlife.de JavaScript Effekts, Ajax and Helpers
 *  by Felix Schuchmann (schuchmann@gmail.com)
 *  thanks to mad4milk for moo.fx and to Sam Stephenson for prototype
/*--------------------------------------------------------------------------*/

function find_buddy(nick) {
	if(nick.length>=2 && !IsNumeric(nick)) {
		if (typeof timeoutID == 'number') window.clearTimeout(timeoutID);
		timeoutID = window.setTimeout("find_buddy_request('"+escape(nick)+"')", 500);
	} else {
		$('namecheck').innerHTML = '';
		$('namecheck').style.display = "none";
	}
}
function find_buddy_request(nick) {
	$('namecheck').innerHTML = '<img src="/images/loading.gif"/>';
	$('namecheck').style.display = "block";
	loadcode('namecheck', '/style/ajax/find_buddy.php?nick='+(nick));	
}

function find_username(nick) {
	if(nick.length>=3 && !IsNumeric(nick)) {
		if (typeof timeoutID == 'number') window.clearTimeout(timeoutID);
		timeoutID = window.setTimeout("find_username_request('"+escape(nick)+"')", 500);
	} else {
		$('namecheck').innerHTML = '';
		$('namecheck').style.display = "none";
	}
}

function find_username_request(nick) {
	$('namecheck').innerHTML = '<img src="/images/loading.gif"/>';
	$('namecheck').style.display = "block";
	loadcode('namecheck', '/style/ajax/find_username.php?nick='+(nick));	
}

function find_mailuser(nick) {
	if(nick.length>=2 && !IsNumeric(nick)) {
		if (typeof timeoutID == 'number') window.clearTimeout(timeoutID);
		timeoutID = window.setTimeout("find_mailuser_request('"+escape(nick)+"')", 500);
	} else {
		$('namecheck').innerHTML = '';
		$('namecheck').style.display = "none";
	}
}

function find_mailuser_request(nick) {
	$('namecheck').innerHTML = '<img src="/images/loading.gif"/>';
	$('namecheck').style.display = "block";
	loadcode('namecheck', '/style/ajax/find_mailuser.php?nick='+(nick));
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

var entered=false;
function noDoubleEnter() {
	if(window.event && window.event.keyCode == 13) {
		if(entered==true)  return false;
		else{entered=true; return true;}
	}else return true;
}
function fenster(URL,Name,breite,hoehe,scroll,resize) {
	if(scroll != 'No') {
		scroll='Yes';
	}
	if(resize != '0') {
		resize='1'; 
	}
	window.open(URL,Name,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+scroll+",resizable="+resize+",copyhistory=0,height="+hoehe+",width="+breite+"");
}

function loadcode(into, url) {
	new ajax (url, {
		update: into, 
		method: 'get',
		onComplete: hideLoader
	});
	if($("loading")) $("loading").style.visibility="visible";
}
function hideLoader() {
	if($("loading")) $("loading").style.visibility="hidden";
}

/* Checking Functions for register */
function check_plz(plz) {
	var country = $("country").options[$("country").selectedIndex].value;
	if((plz.length==5 && country=="DE") || (plz.length==4 && (country=="AT" || country=="CH")))
		loadcode('ort', '/style/ajax/check_plz.php?plz='+plz+'&country='+country);
}

/* Mail Voting */
function mail_vote(mailid, vote, from, to, code) {
	$('mail_vote').innerHTML = "...";
	loadcode('mail_vote', '/style/ajax/mail_vote.php?mailid='+mailid+'&vote='+vote+'&from='+from+'&to='+to+'&code='+code);
}

/* Match in Profile */
function dynmatch(fromid, toid, code) {
	$('match').innerHTML = "lade...";
	loadcode('match', '/style/ajax/match.php?fromid='+fromid+'&toid='+toid+'&code='+code); //an alle Skripter, lasst es sein! Oder ihr bekommt Probleme! :evil:
	return false;
}

function setStylesheet(styleTitle){
	var currTag;

	if (document.getElementsByTagName){
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++){
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")){
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)	{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
}

function saveUCS(byteField, dataField) {
	var cookiestring = getCookie('ucs');
	//fill the Cookie with _
	if(cookiestring.length<byteField) {
		for(i=0;i<byteField;i++) {
			if(cookiestring.charAt(i)=='') {
				cookiestring+='_';
			}
		}
	}
	var vorne  = cookiestring.substring(0, byteField);
	var hinten = cookiestring.substring(byteField+1, cookiestring.length);
	
	saveCookie('ucs', vorne.concat(dataField).concat(hinten));
}

function getCookie(n){
	k = document.cookie.split('; '); res = '';
	for (i = 0;i < k.length; ++i) {
		j = k[i].split('=');
		cookiename = j[0];
		cookiewert = unescape(j[1]);
		if(n == cookiename){res = cookiewert; return res;}
	}
	return res;
}
function saveCookie(name, data){
	var Verfallszeit = 1000 * 60 * 60 * 24 * 365;
	var jetzt = new Date();
	var Auszeit = new Date(jetzt.getTime() + Verfallszeit);
	document.cookie = name + "="+data+"; expires=" + Auszeit.toGMTString() + ";domain=.flirtlife.de";
}

function exptoggle(elm) {
	if($(elm).src) {
		if($(elm).src.search('expand')>0)	$(elm).src='/images/collapse.gif';
		else					$(elm).src='/images/expand.gif';
	}else{
		if($(elm).innerHTML.search(/\+/)>0)	$(elm).innerHTML = $(elm).innerHTML.replace(/\+/, "-");
		else if($(elm).innerHTML.search('-')>0)	$(elm).innerHTML = $(elm).innerHTML.replace('-', "+");	
	}
}

var tFx = Array();
function toggle(elm) {
	if(arguments[1]) exptoggle(arguments[1]);
	//works only with header "http://www.w3.org/TR/html4/loose.dtd" or in real browsers :P
	if(/Firefox/.test(navigator.userAgent) && typeof(MooTools) == "undefined") {
		if($(elm).style.display=='none') {
			tFx[elm] = new fx.Height(elm, {notHigher: true});
			tFx[elm].hide();
			$(elm).style.display='';
		}else if(!tFx[elm]){
			tFx[elm] = new fx.Height(elm, {notHigher: true});			
		}
		tFx[elm].toggle();
		return false;
	}else{
		if($(elm).style.display == 'none') {
			$(elm).style.display = '';
		} else {
			$(elm).style.display = 'none';
		}
	}
}


function picgalDel(picid) {
	if(confirm("Willst du dieses Bild wirklich löschen?")) {
		delpic = new fx.Width('pic_'+picid);
		delpic.toggle();
		new ajax ('/bildergalerie.php', {
			postBody: 'ajax=true&del='+picid
		});
	}
	return false;
}

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){
						if(this.update.value) {this.update.value = this.transport.responseText;}
						else {this.update.innerHTML = this.transport.responseText;}
					}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

var fx = new Object();
//base
fx.Base = function(){};
fx.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		notHigher: false,
		transition: fx.sinoidal
	}
	Object.extend(this.options, options || {});
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	hide: function() {
		this.now = 0;
		this.increase();
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	}
}

//stretchers
fx.Layout = Class.create();
fx.Layout.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.style.overflow = "hidden";
		this.iniWidth = this.el.offsetWidth;
		this.iniHeight = this.el.offsetHeight;
		this.setOptions(options);
	}
});

fx.Height = Class.create();
Object.extend(Object.extend(fx.Height.prototype, fx.Layout.prototype), {	
	increase: function() {
		if(this.options.notHigher && this.now!=0 && this.now>=this.el.scrollHeight){
			this.el.style.height = "";
		}else
			this.el.style.height = this.now + "px";
	},

	toggle: function() {
		if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
		else this.custom(0, this.el.scrollHeight);
	}
});

fx.Width = Class.create();
Object.extend(Object.extend(fx.Width.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.width = this.now + "px";
	},

	toggle: function(){
		if (this.el.offsetWidth > 0) this.custom(this.el.offsetWidth, 0);
		else this.custom(0, this.iniWidth);
	}
});

//fader
fx.Opacity = Class.create();
fx.Opacity.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.now = 1;
		this.increase();
		this.setOptions(options);
	},

	increase: function() {
		if ( this.el.style.display = "none" ) this.el.style.display = "";
		if (this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
		this.setOpacity(this.now);
	},
	
	setOpacity: function(opacity) {
		if (opacity == 0 && this.el.style.visibility != "hidden") this.el.style.visibility = "hidden";
		else if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
		if (window.ActiveXObject) this.el.style.filter = (opacity==1?"":"alpha(opacity=" + opacity*100 + ")");
		this.el.style.opacity = opacity;
	},

	toggle: function() {
		if (this.now > 0) this.custom(1, 0);
		else this.custom(0, 1);
	}
});

//transitions
fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}
fx.linear = function(pos){
	return pos;
}
fx.cubic = function(pos){
	return Math.pow(pos, 3);
}
fx.circ = function(pos){
	return Math.sqrt(pos);
}

function number_format(a, b, c, d) { a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b); e = a + ''; f = e.split('.'); if (!f[0]) {  f[0] = '0'; } if (!f[1]) {  f[1] = ''; } if (f[1].length < b) {  g = f[1];  for (i=f[1].length + 1; i <= b; i++) {   g += '0';  }  f[1] = g; } if(d != '' && f[0].length > 3) {  h = f[0];  f[0] = '';  for(j = 3; j < h.length; j+=3) {   i = h.slice(h.length - j, h.length - j + 3);   f[0] = d + i +  f[0] + '';  }  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));  f[0] = j + f[0]; } c = (b <= 0) ? '' : c; return f[0] + c + f[1];}

function playWebCam(userID, username) {
	if(opener && opener.top.frames['frame_chat']) {
		opener.top.frames['frame_chat'].playStream(userID, username);
	}else if(top.frames['frame_chat']) {
		top.frames['frame_chat'].playStream(userID, username);
	}else{
		alert('Der Chatframe wird aktiviert. Danach bitte erneut auf die Cam klicken!');
		top.location.href='/frameloader.php?chat=true&userid='+userID+'&username='+username+'&url='+top.frames['hauptfenster'].location.href
	}
	return false;
}
