	/*!
	   ____    _             __     _ __  
	  / __/___(_)__ ___  ___/ /__  (_) /__
	 / _// __/ / -_) _ \/ _  / _ \/ /  '_/
	/_/ /_/ /_/\__/_//_/\_,_/_//_/_/_/\_\ 
                                    												  
	* Version: 1.2 of 2010/03/31
	* Copyright(c) 2009-2010 , friendnik.com
	* http://friendnik.com
	* a project of vly group haftungsbeschränkt
	* http://www.vly-group.de/	
	* @author chris rose
	*/


function FRIENDNIK() {
	
	
	this.apiServer = 'http://friendnik.com/';
	this.ldidx=0;
	
	this.init = function() {
		this.initialized=true;
	}
	
	this.create = function(key,id,p) {
		
		this.key = key;
		this.elid = id;
		if(typeof p != 'object') p = {};
		this.params = p;
		this.params.key = key;
		
		if(!(F$(id))) { 
			if(this.ldidx>5) { 
				this.msg('error','No element-id found, please insert a valid eleent-id where we can put your links'); return false; 
			} else {
				setTimeout(function(){friendnik.create(friendnik.key,friendnik.elid,friendnik.params);}, 1000);
				return false;
			}
			this.ldidx++;
		}
		
		this.el = F$(id);

		if((!this.params.theme) || ((this.params.theme!='white') && (this.params.theme!='black') && (this.params.theme!='icons') && (this.params.theme!='blank'))) this.params.theme='black';
		this.addCSSUrl(this.apiServer+'api/api_'+this.params.theme+'.css');
		// set width
		if(this.params.width) this.params.width = parseInt(this.params.width);
		if(typeof this.params.width == 'number' && this.params.width > 50) {
			this.el.style.width = this.params.width+'px';
		}
		
		if (/WebKit/i.test(navigator.userAgent)) {
		  var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
			  clearInterval(_timer);
			  friendnik.onReady();
			}
		  }, 10);
		} else if(/MSIE/i.test(navigator.userAgent)) {
			this.addEvent(window,'load',function(event) {friendnik.onReady();});
		} else {
			this.addEvent(window,'load',function(event) {friendnik.onReady();});
		}
	}
	
	this.onReady = function() {
		friendnik.getContent();
	}
	
	//---------------------------------------------------------------------------------------
	this.getContent = function() {
		
		var apiUrl = this.apiServer+'api/x-friendnik.php';
		var p_str = '?xml=1';
		for(p in this.params) {
			if(typeof this.params[p] == 'object' || typeof this.params[p] == 'function') continue;
			p_str += '&'+p+'='+escape(this.params[p]);
		}
		apiUrl += p_str;
		
		this.contentscript = this.addScript(apiUrl);
		
		// I will leave this here, but ne do not need it...
		if(/MSIE/i.test(navigator.userAgent)) {
			this.addEvent(this.contentscript,'readystatechange',function(event) {
				if(friendnik.contentscript.readyState == 'complete') {
					var data = friendnik.contentscript.innerHTML;
					if(data.length) {
					}
				}	
			});		
		} else {
			this.addEvent(this.contentscript,'load',function(event) {
				var data = friendnik.contentscript.innerHTML;
				if(data.length) {
					//me.callback(Ext.decode(data));
				}			
			});
		}
	}	
	
	
	this.resultsLoaded = false;
	this.updateResults = function(data) {

		
		if(this.resultsLoaded == true) return;
		this.resultsLoaded = true;
		
		// remove listener? remove script!
		this.contentscript.parentNode.removeChild(this.contentscript);
		
		if((!(data)) || (typeof data != 'object') || (typeof data.user != 'object')) {
			friendnik.msg('error','No user found, please check your key in your configuration.'); 
			return false;
		}
		
		var fflogo = friendnik.apiServer+'api/img/friendnik_logo_slogan.png';
		var ffurl = friendnik.apiServer+''+data.user.nickname;
		
		// Links
		// Be my friend?
		// my profile @ friednik

		var uc = '';
		for(i=0;i<data.userservices.length;i++) {
			uc += data.userservices[i].socialservice;
			if(this.params.theme == 'blank' && (i+1) != data.userservices.length) {
				uc += ', ';	
			}
		}
		if(this.params.avatar === false || this.params.avatar === "false") data.img='';
		var tpl = '';

		switch(this.params.theme) {
			case'blank':
				uc = '<a href="'+ffurl+'" target="_blank" class="ff-service" title="friendnik.com - All my online identities in one place">friendnik</a>, '+uc;
				tpl += '<div id="friendnik">';
				tpl += '<p class="friendnik-icons">';
				tpl += uc;
				tpl += data.nosocialservicefound;
				tpl += data.socialservicesfooter;	
				tpl += '</p>';
				tpl += '</div>';
				break;
			case'icons':
				uc = '<a href="'+ffurl+'" target="_blank" class="ff-service" title="friendnik.com - All my online identities in one place"><img src="'+friendnik.apiServer+'favicon.ico" alt="Friendnik" /></a>'+uc;
				tpl += '<div id="friendnik">';
				tpl += '<p class="friendnik-icons">';
				tpl += uc;
				tpl += data.nosocialservicefound;
				tpl += data.socialservicesfooter;	
				tpl += '</p>';
				tpl += '</div>';
				break;
			default:
			tpl += '<div id="friendnik">';
			tpl += '<h3><a href="'+ffurl+'">'+data.img+'</a>'+data.youcanfindmehere+':</h3>';
			tpl += '<div class="ff-servicesview">';
			tpl += uc;
			tpl += data.nosocialservicefound;
			tpl += data.socialservicesfooter;
			tpl += '</div>';
			tpl += '<div class="ff-footer"><a title="friendnik.com - All my online identities in one place" href="'+ffurl+'"><img height="20" alt="friendnik.com - All my online identities in one place" src="'+fflogo+'" border="0" /></a></div>';
			tpl += '</div>';
		}
		this.el.innerHTML = tpl;
		
	}	
	
	//---------------------------------------------------------------------------------------
	this.addCSSUrl=function(cssUrl) {
		var scriptTag = document.createElement("link");
		scriptTag.type = "text/css";
		scriptTag.rel = "stylesheet";
		scriptTag.href = cssUrl;
		this.getScriptArea().appendChild(scriptTag);
	}

	this.addScript=function(scriptURL) {
		var scriptTag = document.createElement("script");
		scriptTag.type = "text/javascript";
		scriptTag.src = scriptURL;
		this.getScriptArea().appendChild(scriptTag);
		return scriptTag;
	}
	
	this.getScriptArea=function() {
		var parentElement = document.getElementsByTagName("head");
		if (!parentElement || parentElement.length < 1) {
			parentElement = document.body;
		}
		else {
			parentElement = parentElement[0];
		}
		return parentElement;
	}
	
	this.addCSS=function(css) {
		var styleTag = document.createElement("style");
		styleTag.type = "text/css";
		if (styleTag.styleSheet) { // IE only
			if (navigator.appVersion.indexOf("MSIE 5") != -1) { // IE 5 crashes if we add a style tag to the DOM
				document.write("<style type='text/css'>" + css + "</style>");
			}
			else {
				styleTag.styleSheet.cssText = css;
			}
		} else if (navigator.appVersion.indexOf("MSIE 5") != -1) {
			document.write("<style type='text/css'>" + css + "</style>");
		}
		else {
			var textNode = document.createTextNode(css);
			styleTag.appendChild(textNode);
		}
		this.getScriptArea().appendChild(styleTag);
	}
	
	//---------------------------------------------------------------------------------------
	this.msg = function(c,msg) {
		switch(c) {
			case'error':
			default:
				alert('Message from Friendnik - An error occured: '+"\n"+msg);
				break;
		}
	}

	//---------------------------------------------------------------------------------------
	this.addEvent = function(el,evname,func) {
	
		if (el.attachEvent) { // IE
			el.attachEvent("on" + evname, func);
		} else if (el.addEventListener) { // Gecko / W3C
			el.addEventListener(evname, func, true);
		} else {
			el["on" + evname] = func;
		}
	
	}	
}

friendnik = new FRIENDNIK();
friendnik.init();

lang = 'de';
lang_show_failed_keys = '';
if(typeof INT != 'object') INT = {};

function $L(key) {
	
	this.lang = lang;
	this.def_lang = 'en';
	
	if(!INT[key]) {
		return key+lang_show_failed_keys;
	} else if(INT[key]) {
		if(INT[key][this.lang]) return INT[key][this.lang];
		else if(INT[key][this.def_lang]) return INT[key][this.def_lang];
		else return key+lang_show_failed_keys;
	} else return key+lang_show_failed_keys;
}

function F$() {
  var elements = [];
  
  for (var i = 0; i < arguments.length; i++) {
	var element = arguments[i];
	if (typeof element == 'string')
	  element = document.getElementById(element);

	if (arguments.length == 1) 
	  return element;

	elements.push(element);
  }

  return elements;
}



