﻿/* a00 */

function a00 ()
{
	this.services = {
		'yandex': [0,'Яндекс','http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl={url}&lname={title}'],
		'twitter': [4,'Twitter','http://twitter.com/home?status={title} {url}'],
		'memori': [1,'Memori','http://memori.ru/link/?sm=1&u_data[url]={url}&u_data[name]={title}'],
		'google': [3,'Google','http://www.google.com/bookmarks/mark?op=add&bkmk={url}&title={title}'],
		'bobrdobr': [2,'БобрДобр','http://bobrdobr.ru/addext.html?url={url}&title={title}'],
		'delicious': [5,'del.icio.us','http://del.icio.us/post?v=4&noui&jump=close&url={url}&title={title}'],
		'news2': [6,'News2','http://news2.ru/add_story.php?url={url}'],
		'mister-wong': [7,'Мистер Вонг','http://www.mister-wong.ru/index.php?action=addurl&bm_url={url}&bm_description={title}'],
		'moemesto': [8,'МоёМесто','http://moemesto.ru/post.php?url={url}&title={title}'],
		'smi2': [9,'СМИ 2','http://smi2.ru/add/?url={url}&precaption={title}'],
		'vaau': [10,'Ваау!','http://www.vaau.ru/submit/?action=step2&url={url}'],
		'100zakladok': [14,'Сто Закладок','http://www.100zakladok.ru/save/?bmurl={url}&bmtitle={title}'],
		//'myscoop': [11,'AddScoop','http://myscoop.ru/add/?URL={url}&title={title}'],
		'linkstore': [12,'LinkStore','http://www.linkstore.ru/servlet/LinkStore?a=add&url={url}&title={title}'],
		'ruspace': [13,'RuSpace','http://www.ruspace.ru/index.php?link=bookmark&action=bookmarkNew&bm=1&url={url}&title={title}']
		};
	
	this.__construct = function ()
	{
		//...
	}
	
	this.AddCSS = function (css)
	{
		if (document.styleSheets.length == 0)
		{
			var head = document.getElementsByTagName('head');
			if (!head)
			{
				var head = document.createElement('head');
				document.appendChild(head);
			}
			var style = document.createElement('style');
			style.type = 'text/css';
			head[0].appendChild(style);
		}
		if (typeof css == 'object')
		{
			for (var i=0; i<css.length; i++)
			{
				this.AddCSS(css[i]);
			}
		}
		else
		{
			var sheet = document.styleSheets[0];
			if (sheet.insertRule)
			{
				sheet.insertRule(css,0); // sheet.cssRules.length
			}
			else
			{
				m = css.match(/^(.+?)\s*{(.+?)\}/);
				sheet.addRule(m[1],m[2]);
			}
		}
	}
	
	this.CallService = function (token)
	{
		var service = this.services[token];
		var url = service[2];
		url = url.replace(/\{url\}/,encodeURIComponent(location.href)).replace(/\{title\}/,encodeURIComponent(document.title));
		window.open(url);
	}
	
	this.EncodeHTML = function (s)
	{
		return s.replace(/\&/g,'&amp;').replace(/\"/g,'&quot;').replace(/\</g,'&lt;').replace(/\>/g,'&gt;');
	}
	
	this.__construct();
}

function a00_window ()
{
	this.strings = {};
	
	/*this.strings.en = {
		'bbcode_group': 'For forums (BBCode)',
		'bookmarks_group': 'Bookmarks',
		'html_group': 'For blogs (HTML)',
		'window_title': 'Web-2.0 Tools'
		};*/
		
	this.strings.ru = {
		'about_tip': 'Поставить такую же фишку к себе на сайт!',
		'bbcode_group': 'Для форумов (BBCode)',
		'bookmarks_group': 'Закладки',
		'html_group': 'Для блогов (HTML)',
		'window_title': 'Web-2.0 инструменты'
		};
	// background: url("http://x.a00.ru/api/h2_bg.png") no-repeat; 
	this.css = [
		'.a00_window { display: none; position: absolute; z-index: 10; top: 50%; left: 50%; width: 319px; height: 314px; background: url("http://x.a00.ru/api/bg.png") no-repeat; padding: 14px 13px 0px 13px; font: 12px Verdana; }',
		'.a00_window .a00_close_btn { display: block; float: right; margin-right: 25px; width: 22px; height: 22px; border: 0px; background: url("http://x.a00.ru/api/close_btn.png") no-repeat; cursor: pointer; }',
		'.a00_window .a00_group { padding-bottom: 8px; }',
		'.a00_window .a00_h2 {  margin: 0px; padding: 4px 0px 6px 2px; font: bold 12px Tahoma; color: #505050; }',
		'.a00_window .a00_bookmarks_box { padding-left: 8px; }',
		'.a00_window .a00_bookmark_btn { display: block; float: left; width: 110px; height: 16px; margin: 2px 12px 2px 0px; padding-left: 18px; text-decoration: none; color: #505050; background: url("http://x.a00.ru/api/icons_map.png") no-repeat; } ',
		'.a00_window .a00_bookmark_btn:hover { text-decoration: underline; color: #000000; } ',
		'.a00_window .a00_copypaste { width: 286px; font: 11px Courier New; color: #000000; }',
		'.a00_window .a00_about_link {display: block; margin: 4px 0px 0px 150px; width: 140px; height: 10px;}',
		'.a00_background { display: none; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; z-index: 9; }' /* background-color: #000000; opacity: 0.3; */
		];
	
	this.__construct = function ()
	{
		this.Deploy();
	}
	
	this.AddCSS = function ()
	{
		window.a00.AddCSS(this.css);
	}
	
	this.BuildGUI = function ()
	{
		this.gui = {}
		
		// back
		this.gui.background = document.createElement('div');
		this.gui.background.className = 'a00_background';
		document.body.appendChild(this.gui.background);
		
		// container
		this.gui.container = document.createElement('span');
		this.gui.container.className = 'a00_window';
		document.body.appendChild(this.gui.container);
		
		// close
		this.gui.close_btn = document.createElement('input');
		this.gui.close_btn.owner = this;
		this.gui.close_btn.className = 'a00_close_btn';
		this.gui.container.appendChild(this.gui.close_btn);
		this.gui.close_btn.onclick = function () { this.owner.Hide(); };
		
		// bookmarks
		this.gui.bookmarks = document.createElement('div');
		this.gui.bookmarks.className = 'a00_group';
		this.gui.container.appendChild(this.gui.bookmarks);
		
		this.gui.bookmarks_h2 = document.createElement('div');
		this.gui.bookmarks_h2.className = 'a00_h2';
		this.gui.bookmarks_h2.appendChild(document.createTextNode(this.GetStr('bookmarks_group')));
		this.gui.bookmarks.appendChild(this.gui.bookmarks_h2);
		
		this.bookmarks_box = document.createElement('div');
		this.bookmarks_box.className = 'a00_bookmarks_box';
		this.gui.bookmarks.appendChild(this.bookmarks_box);
		
		this.gui.services = {};
		var i, key, service, btn;
		for (key in window.a00.services)
		{
			service = window.a00.services[key];
			btn = document.createElement('a');
			btn.owner = this;
			btn.className = 'a00_bookmark_btn';
			btn.service_key = key;
			btn.setAttribute('href','javascript:void(0);');
			btn.style.backgroundPosition = '0px -'+(service[0]*16)+'px';
			btn.appendChild(document.createTextNode(service[1]));
			this.bookmarks_box.appendChild(btn);
			btn.onclick = function () { this.owner.Hide(); window.a00.CallService(this.service_key); };
		}
		var clear_box = document.createElement('div');
		//clear_box.style.border = '1px solid red';
		clear_box.style.clear = 'both';
		this.gui.bookmarks.appendChild(clear_box);
		
		// html
		this.gui.html = document.createElement('div');
		this.gui.html.className = 'a00_group';
		this.gui.container.appendChild(this.gui.html);
		
		this.gui.html_h2 = document.createElement('div');
		this.gui.html_h2.className = 'a00_h2';
		this.gui.html_h2.appendChild(document.createTextNode(this.GetStr('html_group')));
		this.gui.html.appendChild(this.gui.html_h2);
		
		this.gui.html_box = document.createElement('input');
		this.gui.html_box.setAttribute('type','text');
		this.gui.html_box.className = 'a00_copypaste';
		this.gui.html.appendChild(this.gui.html_box);
		this.gui.html_box.onclick = function () { this.focus(); this.select(); };
		
		// bbcode
		this.gui.bbcode = document.createElement('div');
		this.gui.bbcode.className = 'a00_group';
		this.gui.container.appendChild(this.gui.bbcode);
		
		this.gui.bbcode_h2 = document.createElement('div');
		this.gui.bbcode_h2.className = 'a00_h2';
		this.gui.bbcode_h2.appendChild(document.createTextNode(this.GetStr('bbcode_group')));
		this.gui.bbcode.appendChild(this.gui.bbcode_h2);
		
		this.gui.bbcode_box = document.createElement('input');
		this.gui.bbcode_box.setAttribute('type','text');
		this.gui.bbcode_box.className = 'a00_copypaste';
		this.gui.bbcode.appendChild(this.gui.bbcode_box);
		this.gui.bbcode_box.onclick = function () { this.focus(); this.select(); };
		
		// link
		this.gui.about_link = document.createElement('a');
		this.gui.about_link.owner = this;
		this.gui.about_link.className = 'a00_about_link';
		this.gui.about_link.setAttribute('href','javascript:void(0);');
		this.gui.about_link.setAttribute('title',this.GetStr('about_tip'));
		this.gui.container.appendChild(this.gui.about_link);
		this.gui.about_link.onclick = function () { this.owner.Hide(); window.open('http://x.a00.ru/get'); };
	}
	
	this.Configure = function ()
	{//alert(''+typeof(window.a00.EncodeHTML));
		this.gui.html_box.setAttribute('value','<a href="'+window.a00.EncodeHTML(location.href)+'">'+window.a00.EncodeHTML(document.title)+'</a>');
		this.gui.bbcode_box.setAttribute('value','[url='+location.href+']'+document.title+'[/url]');
	}
	
	this.Deploy = function ()
	{
		this.AddCSS();
		this.BuildGUI();
		this.Configure();
		this.Move();
		this.Hide();
	}
	
	this.GetStr = function (token)
	{
		return this.strings.ru[token];
	}
	
	this.Hide = function ()
	{
		this.gui.container.style.display = 'none';
		this.gui.background.style.display = 'none';
	}
	
	this.Move = function ()
	{
		var screen_w = window.innerWidth;
		var screen_h = window.innerHeight;
		
		var scroll_x = (typeof(window.pageXOffset) == 'number') ? window.pageXOffset : document.body.scrollLeft;
		var scroll_y = (typeof(window.pageYOffset) == 'number') ? window.pageYOffset : document.body.scrollTop;
		
		var window_w = this.gui.container.offsetWidth;
		var window_h = this.gui.container.offsetHeight;
		
		this.gui.container.style.top = (scroll_y+Math.round(screen_h/2-window_h/2))+'px';
		this.gui.container.style.left = (scroll_x+Math.round(screen_w/2-window_w/2))+'px';
		
		//alert('scroll:'+window.scrollTop+','+window.scrollLeft);
		
		this.gui.background.style.width = document.body.offsetWidth+'px';
		this.gui.background.style.height = document.body.offsetHeight+'px';
	}
	
	this.Show = function ()
	{
		this.gui.container.style.display = 'block';
		this.gui.background.style.display = 'block';
		this.Move();
	}
	
	this.__construct();
}

var a00 = new a00();
a00.window = new a00_window();
