﻿/* *******************************************************
J  a  v  a  c  a  t  z
Filename:   socialbookmark.js
Purpose:    This file is created to hold the scripts
            to generate javacatzSocialBookmark layer.
            Please do not put other codes in this file.
			Need Prototype.js & Scriptaculous.js
Created by: KY 2009.11.05
******************************************************* */

var javacatzSocialBookmark = {
	version: '2.0.3.2011.11.07',
	currentNode: null,
	skipList: new Hash(),
	includeList: new Array(),
	init: function() {
		if(!Prototype || parseFloat(Prototype.Version) < 1.6) return;
		var url = encodeURIComponent(document.location);
		var title = encodeURIComponent(document.title);
		
		/* for v1 backward compatibility to old sites */
		var defaultList_v1 = ['delicious','digg','facebook','google','linkedin','myspace','newsvine','reddit','stumbleupon','twitter','windowslive','yahoo'];
		if(javacatzSocialBookmark.includeList.length == 0) {
			javacatzSocialBookmark.includeList = defaultList_v1;
		}
		
		/* full details of list items */
		var fullList = new Hash();	// fullList = { key: [ title, class, url ] }
		fullList.set('delicious', [ 'Del.icio.us', 'delicious','http://del.icio.us/post?url=' + url + '&title=' + title ]);
		fullList.set('digg', [ 'Digg', 'digg', 'http://digg.com/submit?phase=2&url=' + url + '&title=' + title ]);
		fullList.set('facebook', [ 'Facebook', 'facebook', 'http://www.facebook.com/share.php?u=' + url ]);
		fullList.set('google', [ 'Google Bookmarks', 'google', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '&title=' + title ]);
		fullList.set('linkedin', [ 'LinkedIn', 'linkedin', 'http://www.linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + title ]);
		fullList.set('myspace', [ 'MySpace', 'myspace', 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + url + '&t=' + title ]);
		fullList.set('newsvine', [ 'Newsvine', 'newsvine', 'http://www.newsvine.com/_wine/save?popoff=0&u=' + url + '&h=' + title ]);
		fullList.set('reddit', [ 'Reddit', 'reddit', 'http://reddit.com/submit?url=' + url + '&title=' + title ]);
		fullList.set('stumbleupon', [ 'StumbleUpon', 'stumbleupon', 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title ]);
		fullList.set('twitter', [ 'Twitter', 'twitter', 'http://twitthis.com/twit?url=' + url + '&title=' + title ]);
		fullList.set('windowslive', [ 'Windows Live', 'windows-live', 'https://favorites.live.com/quickadd.aspx?url=' + url + '&title=' + title + '&top=1' ]);
		fullList.set('yahoo', [ 'Yahoo!', 'yahoo', 'http://myweb.yahoo.com/myresults/bookmarklet?t=' + title + '&u=' + url + '&ei=UTF' ]);
		
		var titleText = 'Share/Bookmark this page';
		var logoText = 'All logos belong to their rightful owners';
		if($$('body').first().hasClassName('zh-cn')
			|| $$('body').first().hasClassName('zh-sg')
		) {
			titleText = '分享/添加到收藏夹';
			logoText = '所有标志属于其注册拥有者';
		} else if($$('body').first().hasClassName('zh-tw')
			|| $$('body').first().hasClassName('zh-mo')
			|| $$('body').first().hasClassName('zh-hk')
		) {
			titleText = '分享/添加到收藏夾';
			logoText = '所有標誌屬於其註冊擁有者';
		}
		var layer = new Element('div', { 'id':'layer-javacatzSocialBookmark' }).hide();
		$$('body')[0].insert({'bottom':layer});
		var innerLayer = new Element('div', { 'class':'padding' });
		$(layer).update(innerLayer);
		$(innerLayer).insert(new Element('div', { 'class':'title' }).update(titleText).insert(new Element('div', { 'class':'closeButton', title:'Close' }).update('&times;').observe('click', function() { javacatzSocialBookmark.toggle(); })));
		// build up social bookmarking list
		var sbList = new Element('ul');
		$(innerLayer).insert(sbList);
		fullList.keys().each(function (key) {
			if(javacatzSocialBookmark.includeList.indexOf(key) != -1 && !javacatzSocialBookmark.skipList.get(key)) $(sbList).insert(new Element('li').update(new Element('a', { 'class':fullList.get(key)[1], 'title':fullList.get(key)[0], 'target':'_blank' }).update('<span>' + fullList.get(key)[0] + '</span>').writeAttribute('href', fullList.get(key)[2])));
		});
		
		// powered by Javacatz
		var poweredByJavacatz = new Element('div', { 'class':'poweredByJavacatz' }).update(new Element('a', { 'href':'http://www.javacatz.com/', 'target':'_blank', 'title':'Javacatz' }).update('Powered by Javacatz')).insert({'top': new Element('div', { 'class':'notice' }).update(logoText)});
		$(innerLayer).insert(poweredByJavacatz);
		
		window.onresize = javacatzSocialBookmark.onResize;
	},
	toggle: function(node) {
		javacatzSocialBookmark.currentNode = node;
		if($(node) && !$('layer-javacatzSocialBookmark').visible()) {
			if(parseFloat(Prototype.Version) < 1.7) {
				$('layer-javacatzSocialBookmark').setStyle({
					'left':$(node).cumulativeOffset().left + 'px',
					'top':$(node).getOffsetParent().cumulativeOffset().top + $(node).getOffsetParent().getHeight() + 2 + 'px'
				});
			} else {
				$('layer-javacatzSocialBookmark').setStyle({
					'left':$(node).cumulativeOffset().left + 'px',
					'top':$(node).getOffsetParent().cumulativeOffset().top + $(node).measure('border-box-height') + $(node).measure('top') + 2 + 'px'
				});
			}
		}
		new Effect.toggle($('layer-javacatzSocialBookmark'), 'appear', { 'duration':0.3, afterSetup: function() {
			if($('layer-javacatzSocialBookmark').cumulativeOffset().left + $('layer-javacatzSocialBookmark').getWidth() > $('layer-javacatzSocialBookmark').getOffsetParent().getWidth()) {
				$('layer-javacatzSocialBookmark').setStyle({ 'left':'auto', 'right':'10px' });
			}
		} });
	},
	onResize: function() {
		var node = javacatzSocialBookmark.currentNode;
		if($(node) && $('layer-javacatzSocialBookmark')) $('layer-javacatzSocialBookmark').setStyle({
			'left':$(node).cumulativeOffset()[0] + 'px',
			'top':$(node).getOffsetParent().cumulativeOffset().top + $(node).getOffsetParent().getHeight() + 2 + 'px'
		});
		if($('layer-javacatzSocialBookmark').cumulativeOffset().left + $('layer-javacatzSocialBookmark').getWidth() > $('layer-javacatzSocialBookmark').getOffsetParent().getWidth()) {
			$('layer-javacatzSocialBookmark').setStyle({ 'left':'auto', 'right':'10px' });
		}
	}
};

