$(document).ready(function() {

// Блок социалок, кнопка "получить код для блога"
$("#inline").colorbox({width:"700px", inline:true, href:"#htmled"});

$('a[rel="gall"]').colorbox({
	current:'{current} из {total}',
	transition:'elastic'
	});
	
//$('.imgcap').biggerlink({hoverclass:'roll'});
//$('.img_nocap').biggerlink({hoverclass:'roll2',follow:false});

function BindForumPreviewColorbox (container, colorbox_group_id)
{
	$(container).find("div.img_zoom a.img_tn").colorbox({rel:'group'+ colorbox_group_id});
	
	$(container).find('div.img_zoom img.forum_external_image').each (function () {
		// Навесим обработчик на событие загрузки картинки, проверяющий ее ширину
		var image = $(this);
		
		$(image).load (function ()
		{
			if ($(this).parent().hasClass("iholder")) return;
			
			if ($(this).width() > 320 && $(this).height() > 50)
			{
				$(this).wrap ("<a class=\"img_tn\"><span class=\"iholder\"></span></a>");
				var iholder = $(this).parent();
				var anchor = iholder.parent();
				anchor.attr ("href", $(this).attr ("src"));
				iholder.append('<span class="izoom" title="Увеличить"></span>');
				
				source_h = $(this).height();
				source_w = $(this).width();
				
				if (source_h < 300)
				{
					new_w = 320;
					new_h = source_h;
				}
				else
				{
					new_w = 320;
					new_h = 300;
				}
				
				if (source_h / source_w < new_h / new_w)
				{
					result_h = new_h;
					result_w = result_h * source_w / source_h;
					
					offset_x = (result_w - new_w) / 2;
					offset_y = 0;
				}
				else
				{
					result_w = new_w;
					result_h = result_w * source_h / source_w;
					
					offset_x = 0;
					offset_y = (result_h - new_h) / 2;
				}
				
				anchor.parent().height (Math.round (new_h + 10));
				$(this).height (Math.round (result_h));
				
				anchor.parent().width (Math.round (new_w + 10));
				$(this).width (Math.round (result_w));
				
				$(this).css("margin-left", "-" + Math.round (offset_x) + "px");
				$(this).css("margin-top", "-" + Math.round (offset_y) + "px");
				
				anchor.colorbox({rel:'group'+ colorbox_group_id});
				
				//anchor.parent().biggerlink({hoverclass:'roll2',follow:false});
				anchor.parent().addClass ("imgcap");
			}
			else
			{
				$(this).unwrap();
			}
		}).each (function () {
			if(this.complete) $(this).trigger("load");
		});
	});
}


$('.img_tn .iholder').append('<span class="izoom" title="Увеличить"></span>');

$(".forum-post-entry").each(function() {
	BindForumPreviewColorbox (this, $(this).children(".forum-post-text").attr("id"));
});

$(".review_message").each (function () {
	BindForumPreviewColorbox (this, $(this).attr ("id"));
});

$('.content_container a:not(.advert a, .img_tn, .maybe_img_tn, a[href*="javascript"])').filter(function() {

    return this.hostname
    && this.hostname !== location.hostname
    && this.hostname !== 'javascript'
    && this.hostname !== 'www.greencard.by'
    && this.hostname !== 'www.green-card.by'
	&& this.hostname !== 'www.green-card.ru'
	&& this.hostname !== 'www.green-card.ua'
	&& this.hostname !== 'www.greencard.ua'
	&& this.hostname !== 'www.greencard.com.ua'
	&& this.hostname !== 'www.greencard.kz'
	&& this.hostname !== 'www.green-card.kz'
	&& this.hostname !== 'www.green-card.md'
	&& this.hostname !== 'www.usa-green-card-lottery.com'
    && this.hostname !== 'greencard.by'
    && this.hostname !== 'green-card.by'
	&& this.hostname !== 'green-card.ru'
	&& this.hostname !== 'green-card.ua'
	&& this.hostname !== 'greencard.ua'
	&& this.hostname !== 'greencard.com.ua'
	&& this.hostname !== 'greencard.kz'
	&& this.hostname !== 'green-card.kz'
	&& this.hostname !== 'green-card.md'
	&& this.hostname !== 'usa-green-card-lottery.com';

  }).addClass('ext_link');

});

