/*
 * Thumbshot Popups script for Thumbshots.RU
 * Powered by jQuery (http://www.jquery.com)
 * 
 * Author: Sonorth Corp. - {@link http://www.sonorth.com/}
 * License: Creative Commons Attribution-ShareAlike 3.0 Unported
 * License info: {@link http://creativecommons.org/licenses/by-sa/3.0/}
 *
 * Version: 1.1
 * Date: 8/11/2010
 *
 * Параметры запроса http://www.thumbshots.ru/api
 *
 * По умолчанию скрипт добавляет всплывающие тамбшоты к:
 *    - ссылкам с классом "thumbshot-popup" (class="thumbshot-popup")
 *    - ко всем внешним ссылкам (не ведущим на текущий домен)
 *
 * В обоих случаях адрес ссылки должен быть абсолютным, т.е. начинаться с http:\\
 *
 */
function ThumbshotPopups(tclass, target) {
	if (!target) {
		target = jQuery("a[href^=\'http:\']").not("[href*=\'" + window.location.host.replace(/\w+\./,'') + "\']").get();
		target = target.concat(jQuery("a.thumbshot-popup[href^=\'http:\']").get());
		target = jQuery.unique(target);
		//alert(target.length);
	}
	if (target.length < 1) return;
	var img_height = 90;
	var host = 'http://get.thumbshots.ru/';
	var params = new Array();
	params['size'] = 'M'; // XS, S, M, L
	// Дополнительные параметры
	//params['lang'] = 'EN';
	//params['w'] = 640;
	//params['h'] = 200;
	jQuery('<style type="text/css"> .' + tclass + ' {position:absolute;left:-20000px;display:none;z-index:10;border:1px solid #ccc; background:#333; padding:2px;color:#fff} .' + tclass + ' img {margin:0;padding:0;border:none} </style>').appendTo('head');
	var query = [];
	for (var v in params) {
		if (typeof params[v] != 'function') {
			query.push(encodeURIComponent(v) + '=' + encodeURIComponent(params[v]))
		}
	}
	jQuery(target).each(function (i) {
		jQuery(this).hover(function () {
			//jQuery(this).attr('title','');
			var src = host + "?" + query.join('&') + "&url=" + jQuery(this).attr('href');
			jQuery("body").append("<div class='" + tclass + "' id='" + tclass + i + "'><img src='" + src + "' alt='Loading preview' /></div>");
			jQuery("#" + tclass + i).css({
				opacity: 1,
				display: "none"
			}).fadeIn(50)
		}, function () {
			jQuery("#" + tclass + i).fadeOut(50)
		}).mousemove(function (kmouse) {
			jQuery("#" + tclass + i).css({
				left: kmouse.pageX,
				top: kmouse.pageY - 50 - img_height
			})
		})
	})
}