/**
 * Enthält universelle Methoden.
 * @author Lutz Hoffarth
 * @since 04.08.2011 Erstellt
 */

/**
 * @version 08.03.2011
 * @returns int
 */

/** @since 03.09.2011 Lutz Hoffarth | Erstellt */
//var isDebug = true;

/**
 * Browserbreite ohne Scrollbar.
 * @returns
 * @since 03.09.2011 Lutz Hoffarth | Erstellt
 */
function getBrowserWidth() {
	// return window.innerWidth || (window.document.documentElement.clientWidth
	// || window.document.body.clientWidth);
	return $(window).width();
};

/**
 * 
 * @returns
 * @since 22.10.2011 Lutz Hoffarth | Erstellt
 */
function getBrowserOuterWidth(){
	return $(window).width() + $.scrollbarWidth();
}

/*!
* jQuery Scrollbar Width v1.0
*
* Copyright 2011, Rasmus Schultz
* Licensed under LGPL v3.0
* http://www.gnu.org/licenses/lgpl-3.0.txt
*/
(function($){
	$.scrollbarWidth = function() {
//	  if (!$._scrollbarWidth) {
//	     var $body = $('body');
//	    var w = $body.css('overflow', 'hidden').width();
//	    $body.css('overflow','scroll');
//	    w -= $body.width();
//	    if (!w) w=$body.width()-$body[0].clientWidth; // IE in standards mode
//	    $body.css('overflow','');
//	    $._scrollbarWidth = w;
//	  }
//	  return $._scrollbarWidth;
		
	    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>'); 
	    // Append our div, do our calculation and then remove it 
	    $('body').append(div); 
	    var w1 = $('div', div).innerWidth(); 
	    div.css('overflow-y', 'scroll'); 
	    var w2 = $('div', div).innerWidth(); 
	    $(div).remove(); 
	    return (w1 - w2);
	  
	};
})(jQuery);

/**
 * @version 08.03.2011
 * @returns int
 */
function getBrowserHeight() {
	// return window.innerHeight ||
	// (window.document.documentElement.clientHeight ||
	// window.document.body.clientHeight);
	return $(window).height();
};

// -- Preloader -------------------------------------------------------------------------
///**
// * Wird in der Methode: 'preloadImage(image)' benötigt.
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//var preloadImages = [];
///**
// * Wird in der Methode: 'preloadImage(image)' benötigt.
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//var preloadImageInd = 0;
///**
// * Der Preloader legt automatisch und einmalig ein verstecktes DIR mit der
// * id="preload" im BODY an.
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//
///**
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//var preloadId_preload;
//
///**
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//$(document).ready(function(){
//	preloadId_preload = $("#preload");
//});
///**
// * @param image
// * @since 05.08.2011
// * @deprecated 26.08.2011 Lutz Hoffarth ersetzt durch class Preloader.
// */
//function preloadImage(image) {
//	debug("DEPRACTED: maria.js.preloadImage("+image+")");
//	Preloader.preloadImage(image);
////	if (preloadId_preload.length <= 0) {
////		// debug("existiert nicht: preload");
////		// im Body anlegen.
////		$("body").prepend("<div id='preload' style='display: none;' ></div>");
////		preloadId_preload = $("#preload");
////	}
////
////	// debug("preloadImageInd:"+preloadImageInd);
////	preloadImages[preloadImageInd] = $('<img />').attr('src', image);
////	preloadImages[preloadImageInd].load(function() {
////		preloadId_preload.append($(this));
////	});
////	preloadImageInd++;
//}

/**
 * Gibt den längstmöglichen Textausschnitt zurück, der in den angegebenen Container passt.
 * Die gewünschte Höhe des Containers muss als CSS Attribut gesetzt sein.
 * Der Link am Textende, der zum vollständigen Text führt, muss ein <code>&gt;span&lt;</code> - Element mit der CSS-Klasse "teaser_suffix" sein.
 * 
 * Die Methode gibt true zurück, falls der Text zu lang ist.
 * @author Adam Laszlo
 * @since 11.08.11 | Erstellt
 * @since 29.08.11 | Zeilenumbrüche gefixt
 * @param container
 * @param cut -> true: Text wird abgeschnitten
 * 								false: Es wird nur zurückgegeben, ob der Text zu lang ist
 * 
 */
function cutToTeaserText(container, cut){
	if (cut == undefined){
		cut = true;
	}
	$.each(container, function(index, value){
		var element = $(value);
		var height = element.height();
		element.css({
			height : 'auto'
		});
		var suffix = element.find("span.teaser_suffix");
		var words = element.text().split(' ');
		element.show();
		if (cut == false && element.height() > height){
			return true;
		}
		while (element.height() > height){
			words.pop();
			element.text(words.join(" ")).append(suffix);
		}
		var text = element.text().replace(/\r?\n/g, "<br>");
		element.html(text);
	});
}

/**
 * Erstellt eine Liste aus einem Text mit zeilenumbrüchen.
 * @param container
 */
function makeListFromText(container){
	$.each(container, function(index, value){
		var element = $(value);
		var text = element.text().trim().replace(/\r?\n/g, "</li><li>");
		element.html("<li>"+text+"</li>");
	});
}


/**
 * StartsWith Funktion
 * @author Adam Laszlo
 * @since 10.08.11
 * @param prefix
 * @returns {Boolean}
 */
String.prototype.startsWith = function(prefix) {
	return this.indexOf(prefix) === 0;
};

/**
 * StartsWith Funktion
 * @author Adam Laszlo
 * @since 10.08.11
 * @param prefix
 * @returns {Boolean}
 */
String.prototype.endsWith = function(suffix) {
	return this.match(suffix + "$") == suffix;
};

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
};

