jQuery(document).ready(function() {
	var likeButtons = ["fbLike", "gplus", "tweet"];
	for(var i=0,l=likeButtons.length; i<l; i++)
	{
		jQuery("#"+likeButtons[i]).bind({
			mouseenter: showLikeInfo,
			mouseleave: function(){
				jQuery(this).attr("title", jQuery("#likeInfoBox").text()); // tooltip wiederherstellen
				jQuery("#likeInfoBox").fadeOut("fast");
			}
		});
		if (hasAllowdLike(likeButtons[i]))
			enableLike(likeButtons[i]);
	}
});

/**
	zeige Inhalt vom title Attribut als Info-Box
*/
function showLikeInfo(aEvent)
{
	var box = document.getElementById("likeInfoBox");
	if (!box)
	{
		var box = document.createElement("div");
		box.id = "likeInfoBox";
		jQuery("body").append(box);
	}
	var pos = jQuery(this).position();	// position des like buttons
	jQuery(box).text(jQuery(this).attr("title")).css("top", pos.top).css("left", pos.left+jQuery(this).width()+10);	// kopiere title-attribut in infobox und setzte absolute position
	jQuery(this).removeAttr("title");	// verhindere das standard-tooltip angezeigt wird
	jQuery(box).fadeIn("slow");
}
/**
	setze Cookie
	
	@param what String die ID des like buttons
*/
function allowLike(what)
{
	var cookieSettings = {expires: 23, path: '/'};
	switch(what)
	{
		case "gplus":
		case "fbLike":
			jQuery.cookie(what, "1", cookieSettings);
			/*var expire = new Date(); expire.setDate(expire.getDate()+cookieSettings.expires);
			document.cookie = what+"=1; expires="+expire+"; path="+cookieSettings.path;*/
			break;
		case "tweet":
			jQuery.cookie("twitter", "1", cookieSettings);
			/*var expire = new Date(); expire.setDate(expire.getDate()+cookieSettings.expires);
			document.cookie = "twitter=1; expires="+expire+"; path="+cookieSettings.path;*/
			break;
	}
}
/**
	lösche Cookie
	
	@param what String die ID des like buttons
*/
function unallowLike(what)
{
	var cookieSettings = {path: '/'};
	switch(what)
	{
		case "gplus":
		case "fbLike":
			jQuery.cookie(what, null, cookieSettings);
			//document.cookie = what+"; expires=-1; path="+cookieSettings.path;
			break;
		case "tweet":
			jQuery.cookie("twitter", null, cookieSettings);
			//document.cookie = "twitter; expires=-1; path="+cookieSettings.path;
			break;
	}
}
/**
	wurde like button aktiviert?
	prüft ob der Cookie gesetzt ist
	
	@param what String die ID des like buttons
*/
function hasAllowdLike(what)
{
	switch(what)
	{
		case "gplus":
		case "fbLike":
			return (jQuery.cookie(what) == "1");
			//return (document.cookie.indexOf(what+"=1") >= 0);
		case "tweet":
			return (jQuery.cookie("twitter") == "1");
			//return (document.cookie.indexOf("twitter=1") >= 0);
	}
}
/**
	aktiviert like button und lädt externen JS-Code nach
	
	@param what String die ID des like buttons
*/
function enableLike(what)
{
	jQuery("#likeInfoBox").hide();
	switch(what)
	{
		case "gplus":
			jQuery("#gplus").remove();
			/*var button = document.getElementById("gplus");
			button.parentNode.removeChild(button);*/

			jQuery.getScript("https://apis.google.com/js/plusone.js");	// cross-domain geht erst ab jQuery 1.2 (http://docs.jQuery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getScript)
			/*var scr = document.createElement("script");
			scr.src = "https://apis.google.com/js/plusone.js";
			scr.async = true;
			scr.type = "text/javascript";
			scr.innerHTML = "{lang: 'de'}";	// settings
			var head = document.getElementsByTagName("head")[0];
			head.appendChild(scr);*/
			break;
		case "fbLike":
			jQuery("#fbLike").remove();
			/*var button = document.getElementById("fbLike");
			button.parentNode.removeChild(button);*/

			jQuery.getScript("https://connect.facebook.net/de_DE/all.js", function(){
				FB.init({
					appId  : "YOUR_APP_ID",
					xfbml  : true, // parse XFBML
				});
			});	// cross-domain geht erst ab jQuery 1.2 (http://docs.jQuery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getScript)
			/*var scr = document.createElement("script");
			scr.src = "https://connect.facebook.net/de_DE/all.js#appId=YOUR_APP_ID&xfbml=1";
			scr.async = true;
			scr.type = "text/javascript";
			var fb_root = document.getElementById("fb-root");
			fb_root.appendChild(scr);*/
			break;
		case "tweet":
			jQuery("#tweet").removeAttr("onclick");
			/*var button = document.getElementById("tweet");
			button.removeAttribute("onclick");*/

			jQuery.getScript("https://platform.twitter.com/widgets.js");	// cross-domain geht erst ab jQuery 1.2 (http://docs.jQuery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getScript)
			/*var scr = document.createElement("script");
			scr.src = "https://platform.twitter.com/widgets.js";
			scr.async = true;
			scr.type = "text/javascript";
			var head = document.getElementsByTagName("head")[0];
			head.appendChild(scr);*/
			break;
		default:
			return false;
	}
	allowLike(what);
	return false;
}

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

/*!
 * liScroll 1.0
 * Examples and documentation at: 
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
 * 2007-2010 Gian Carlo Mingati
 * Version: 1.0.2.1 (22-APRIL-2011)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.2.x or later
 * 
 */

jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 1;
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar and Fabien Volpi
				});
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};
