// (c) 2011 Sebastian Germesin
// eMail: sebastian@germes.in


(function() {
// Initial setup
// -------------
//
// The s7ng6n library is fully contained inside a s7ng6n namespace.
var s7ng6n = this.s7ng6n = {};

(function($, undefined) {
    $.widget('s7ng6n.socialize', {
    	
    	options: {
            animationDuration: 800,
            buttonSize : 20,
            margin: 5,
            opacity: 0.3,
    		social: {
	    		facebook: {
	    			
	    		},
	    		gplus: {
	    			lang: 'de'
	    		},
	    		twitter: {
	    			text: "Versicherungen vergleichen - Einfach gemacht!"
	    		},
	    		linkedin: {},
	    		delicious: {},
	    		/*reddit: {},
	    		digg: {},
	    		feedburner: {},
	    		livejournal: {}*/
    		}
    	},
    	
        _create: function () {
            var self = this;
            var elem = $(self.element);
            elem.empty();

            var parent = $('<ul>').appendTo(elem);
            
            self._loadScripts();
            
            self._appendSocialButtons(parent);
        },
        
        _loadScripts: function () {
        	var self = this;
        	//add Google Plus One script
    		if (self.options.social.gplus) {
        		//check if it is not loaded already
        		if (typeof gapi === 'undefined' || typeof gapi.plusone === 'undefined') {
        			var config = {parsetags: "explicit"};
        			if (self.options.social.gplus.lang) {
        				config["lang"] = self.options.social.gplus.lang;
        			}
        			var scriptElem = $('<script type="text/javascript" aysnc="true" src="https://apis.google.com/js/plusone.js">' + JSON.stringify(config) + '</script>');
        			$('body').append(scriptElem);
        		}
        	}
        },
        
        
        _appendSocialButtons: function (parent) {
        	var self = this;
            
        	for (var site in this.options.social) {
        		var opts = this.options.social[site];
        		var button;
        		switch (site) {
        			case "twitter": 
        				button = this._generateTwitterButton(opts);
        				break;
    				case "gplus":
    				    button = this._generateGPlusButton(opts);
    					break;
        			case "facebook":
        				button = this._generateFBookButton(opts);
        				break;
        			case "linkedin":
        				button = this._generateLinkedInButton(opts);
        				break;
        			case "delicious":
        				button = this._generateDeliciousButton(opts);
        				break;
        		}
        		if (button) {
        		    $('<li>')
        		    .append(button)
        		    .appendTo(parent);
        		}
        	}
        },
        
        _generateTwitterButton: function (options) {
        	var self = this;
        
    		s7ng6n.twitterShare = function (text) {
            	var width = 626;
            	var height = 253;
            	
            	var wLeft = (document.width - width) / 2;
            	var wTop = (document.height - height) / 2;
            	
            	text = encodeURIComponent(text);
            	
            	window.open('https://twitter.com/share?text=' + text, 
	            	'sharer', 
	            	'toolbar=0,status=0,width=' + width + ',height=' + height + ',left=' + wLeft + ',top=' + wTop);
            	return false;
        	}
        	
        	var button = $("<div></div>");
        	
            var twitterElem = $("<a href='https://twitter.com/share?text=" + encodeURI(options.text) + "' onclick='return s7ng6n.twitterShare(\"" + options.text + "\");' target='_blank'><img src='./data/img/twitter-tiny-square2.png' alt='Share on Twitter' title='Share on Twitter'/></a>");
        	twitterElem.appendTo(button);
        	
			return button.addClass('s7ng6n-social_panel' + '-button-twitter');
        	
        },
        
        _generateGPlusButton: function (options) {
        	var self = this;
            
            var button = $("<div></div>");
    		
    		var gplusElem = $('<g:plusone size="medium" count="false"></g:plusone>')
            .appendTo(button);
            
			return button.addClass('s7ng6n-social_panel' + '-button-gplus');
            
        },
        
        _generateFBookButton: function (options) {
        	var self = this;
            
            s7ng6n.facebookShare = function () {
            	var url   = location.href;
            	var title = document.title;
            	var width = 626;
            	var height = 436;
            	
            	var wLeft = (document.width - width) / 2;
            	var wTop = (document.height - height) / 2;
            	
            	window.open('http://www.facebook.com/sharer.php?u=' + 
	            	encodeURIComponent(url) + '&t=' + 
	            	encodeURIComponent(title), 
	            	'sharer', 
	            	'toolbar=0,status=0,width=' + width + ',height=' + height + ',left=' + wLeft + ',top=' + wTop);
            	return false;
        	};
        	
        	var button = $("<div></div>");
        	
            var url = encodeURIComponent(document.location.href);
            var fbookElem = $("<a href='http://www.facebook.com/share.php?u=" + url + "' onclick='return s7ng6n.facebookShare();' target='_blank'><img src='./data/img/facebook-tiny-square2.png' alt='Share on Facebook' title='Share on Facebook'/></a>");
        	fbookElem.appendTo(button);
        	
			return button.addClass('s7ng6n-social_panel' + '-button-facebook');
        },
        
        _generateLinkedInButton: function (options) {
        	var self = this;
            
        	var button = $("<div></div>");
        	
        	s7ng6n.linkedInShare = function () {
            	var url   = location.href;
            	var title = document.title;
            	var width = 626;
            	var height = 436;
            	
            	var wLeft = (document.width - width) / 2;
            	var wTop = (document.height - height) / 2;
            	
            	window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + 
	            	encodeURIComponent(url) + '&title=' + 
	            	encodeURIComponent(title), 
	            	'sharer', 
	            	'toolbar=0,status=0,width=' + width + ',height=' + height + ',left=' + wLeft + ',top=' + wTop);
            	return false;
        	};
        	
            var url = encodeURIComponent(document.location.href);
        	var title = encodeURIComponent(document.title);
        	var linkedinElem = $("<a href='http://www.linkedin.com/shareArticle?mini=true&url=" + url + "&title=" + title + "' onclick='return s7ng6n.linkedInShare();' target='_blank'><img src='./data/img/linkedin-square-32.png' alt='Share on LinkedIn' title='Share on LinkedIn'/></a>");
       		linkedinElem.appendTo(button);
       		
       		return button.addClass('s7ng6n-social_panel' + '-button-linkedin');       
        },
        
        _generateDeliciousButton: function (options) {
        	var self = this;
            
        	var button = $("<div></div>");
        	
        	s7ng6n.deliciousShare = function () {
            	var url   = location.href;
            	var title = document.title;
            	var width = 550;
            	var height = 400;
            	
            	var wLeft = (document.width - width) / 2;
            	var wTop = (document.height - height) / 2;
            	
            	window.open('http://delicious.com/save?v=5&noui&jump=close&url=' + 
	            	encodeURIComponent(url) + '&title=' + 
	            	encodeURIComponent(title), 
	            	'sharer', 
	            	'toolbar=0,status=0,width=' + width + ',height=' + height + ',left=' + wLeft + ',top=' + wTop);
            	return false;
        	};
        	
        	var url   = location.href;
        	var title = document.title;
        	
        	var deliciousElem = $("<a href='http://delicious.com/save?v=5&noui&jump=close&url=" + encodeURIComponent(url) + "&title=" + encodeURIComponent(title) + "' onclick='return s7ng6n.deliciousShare();' target='_blank'><img src='./data/img/delicious-square32.png' alt='Share on Delicious' title='Share on Delicious'/></a>");
       		deliciousElem.appendTo(button);
        	
        	return button.addClass('s7ng6n-social_panel' + '-button-delicious');
        }
	})
}) (jQuery);


s7ng6n.foldLeftNav = function (event) {
    event.preventDefault();
    event.stopPropagation();
    $(this).next().toggle('blind', 800);
};

s7ng6n.saveNavInit = function () {
	var obj = {};

	$('#leftnav .cat').each(function () {
		var $this = $(this);
		var $next = $this.next();
		var label = $this.text().trim();
		
		if ($next.css('display') !== 'none') {
			obj[label] = 1;
		}
	});
	$.cookie('nav_init', JSON.stringify(obj));
};

s7ng6n.handleLink = function (target) {
	var elem = $(this);

	if (elem.get(0).tagName.toLowerCase() === 'a') {
		//only useful for links within anchor tags
		
		if (target && target.indexOf('#!') === 0) {
			//if href attribute is set and begins with #! tag
			if (window.History && window.History.enabled) {
				History.pushState({state:1}, "State 1", "?state=1"); // logs {state:1}, "State 1", "?state=1"
			}
			$('#content').html("Germi wins  (" + target + ")!");
			
		}
	}
	return false;
};

s7ng6n.sizeOf = function (obj) {
	if (typeof obj === 'object') {
		var count = 0;
		for (k in obj) 
			if (obj.hasOwnProperty (k) ) 
				count++;
		return count;
	} else if (typeof obj === 'array') {
		return obj.length;
	}
	else {
		return -1;
	}
};

}).call(this);



