$(document).ready(function(){
	$('input[placeholder],textarea[placeholder]').placeholder();
	
	if(typeof Piwik!='undefined'){
		var piwikTracker = Piwik.getTracker(PP.linkTo('piwik','piwik.php'), 1);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();
	}
});

PP={
	webroot:pp_webroot,
	coreApps:['void','common','forum','login','logout','profile','register','settings','data','piwik'],
	/**
	 * get an absolute url to a file in an appen
	 * @param app the id of the app the file belongs to
	 * @param file the file path relative to the app folder
	 * @return string
	 */
	linkTo:function(app,file){
		return PP.filePath(app,'',file);
	},
	/**
	 * get the absolute url for a file in an app
	 * @param app the id of the app
	 * @param type the type of the file to link to (e.g. css,img,ajax.template)
	 * @param file the filename
	 * @return string
	 */
	filePath:function(app,type,file){
		var isCore=PP.coreApps.indexOf(app)!=-1;
		app+='/';
		var link=PP.webroot+'/';
		if(!isCore){
			link+='apps/';
		}
		link+=app;
		if(type){
			link+=type+'/'
		}
		link+=file;
		return link;
	},
	/**
	 * get the absolute path to an image file
	 * @param app the app id to which the image belongs
	 * @param file the name of the image file
	 * @return string
	 */
	imagePath:function(app,file){
		return PP.filePath(app,'img',file);
	},
	/**
	 * load a script for the server and load it
	 * @param app the app id to which the script belongs
	 * @param script the filename of the script
	 * @param ready event handeler to be called when the script is loaded
	 *
	 * if the script is already loaded, the event handeler will be called directly
	 */
	addScript:function(app,script,ready){
		var path=PP.filePath(app,'js',script+'.js');
		if(PP.addScript.loaded.indexOf(path)==-1){
			PP.addScript.loaded.push(path);
			if(ready){
				$.getScript(path,ready);
			}else{
				$.getScript(path);
			}
		}else{
			if(ready){
				ready();
			}
		}
	},
	/**
	 * load a css file and load it
	 * @param app the app id to which the css style belongs
	 * @param style the filename of the css file
	 */
	addStyle:function(app,style){
		var path=PP.filePath(app,'css',style+'.css');
		if(PP.addStyle.loaded.indexOf(path)==-1){
			PP.addStyle.loaded.push(path);
			var style=$('<link rel="stylesheet" type="text/css" href="'+path+'"/>');
			$('head').append(style);
		}
	},
}
PP.addStyle.loaded=[];
PP.addScript.loaded=[];

function getUrlVars() {
	var vars = {};
	var parts = window.location.search.replace(/[?&]+([^=&]+)=([^&#]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}
	else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function setCaretToPos (input, pos) {
	setSelectionRange(input, pos, pos);
}

new function($){
	$.fn.getCursorPosition=function(){
		var pos = 0;
		var el = $(this).get(0);
		// IE Support
		if (document.selection) {
			el.focus();
			var Sel = document.selection.createRange();
			var SelLength = document.selection.createRange().text.length;
			Sel.moveStart('character', -el.value.length);
			pos = Sel.text.length - SelLength;
		}
		// Firefox support
		else if (el.selectionStart || el.selectionStart == '0')
			pos = el.selectionStart;
		
		return pos;
	}
	$.fn.insertAtCaret=function(myValue){
		return this.each(function(i) {
			if (document.selection) {
				//For browsers like Internet Explorer
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			}
			else if (this.selectionStart || this.selectionStart == '0') {
				//For browsers like Firefox and Webkit based
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				var scrollTop = this.scrollTop;
				this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
				this.focus();
				this.selectionStart = startPos + myValue.length;
				this.selectionEnd = startPos + myValue.length;
				this.scrollTop = scrollTop;
			} else {
				this.value += myValue;
				this.focus();
			}
		})
	}
}(jQuery);

function getSelectedText() {
	var txt = '';
	if (window.getSelection) {
		txt = window.getSelection();
	} else if (document.getSelection) {
		txt = document.getSelection();
	} else if (document.selection) {
		txt = document.selection.createRange().text;
	} else return;
	if(txt.toString){
		txt=txt.toString();
	}
	return txt;
}

function getSelectionParent() {
	var parentEl = null;
	if (window.getSelection) {
		var sel = window.getSelection();
		if (sel.rangeCount) {
			parentEl = sel.getRangeAt(0).commonAncestorContainer;
			if (parentEl.nodeType != 1) {
				parentEl = parentEl.parentNode;
			}
		}
	} else if (document.selection && document.selection.type != "Control") {
		parentEl = document.selection.createRange().parentElement();
	}
	return parentEl;
}

