﻿<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE extension SYSTEM "ext-1.0.dtd">


<extension engine="1.0">
	<id>fancy_image</id>
	<title>Fancy Image</title>
	<version>0.9.6</version>
	<description>Show image from image hostings in fancybox</description>
	<author>dimkalinux@gmail.com</author>

	<minversion>1.4RC1</minversion>
	<maxtestedon>1.4.2</maxtestedon>

	<dependencies>
		<dependency>fancy_jquery_addons</dependency>
	</dependencies>


	<hooks>
		<hook id="ps_handle_img_tag_end" priority="3"><![CDATA[
			if (!$is_signature && $forum_user['show_img'] == '0') {
				$img_tag = '<a href="'.$url.'" class="fancy_zoom">&lt;'.$lang_common['Image link'].'&gt;</a>';
			}

			if (!$is_signature && $forum_user['show_img'] != '0') {

				// PIC.LG.UA
				if (FALSE !== strpos($url, 'pic.lg.ua')) {
					$preview_url = preg_replace('!^(http://pic.lg.ua/x)/(\d+)/(.*)/(sm|md)(_.+)!is', '${1}/${2}/${3}/pv${5}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'imageshack.us')) {
					// IMAGESHACK
					$preview_url = preg_replace('!^(http://img\d+\.imageshack\.us)/(img\d+)/(\d+)/(.+)\.th\.(.+)!is', '${1}/${2}/${3}/${4}.${5}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'radikal.ru')) {
					// RADIKAL
					$preview_url = preg_replace('!^(http://.+\.radikal.ru)/(.*)/(.*)(t|x)\.(jpg|jpeg|png|gif|bmp|tif|tiff)!is', '${1}/${2}/${3}.${5}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'photobucket.com')) {
					// PHOTOBUCKET
					$preview_url = preg_replace('!^(http://i\d+\.photobucket\.com/albums)/(x\d+)/(.+)/th_(.+)!is', '${1}/${2}/${3}/${4}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'piccy.info')) {
					// PICCY.INFO
					$preview_url = preg_replace('!^(http://.*piccy.info/.*/\d+/\d+/\d+/)(.*)_(240|500|800)(\..*)!is', '${1}${2}${4}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'imagepost.ru')) {
					// IMAGEPOST.RU
					$preview_url = preg_replace('!^(http://)(imagepost\.ru)/(thumbs)/(\d+)/(.*)!is', '${1}${2}/images/${4}/${5}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'savepic.')) {
					// SAVEPIC
					$preview_url = preg_replace('!^(http://savepic\.)(org|net)/(\d+)m(\..+)!is', '${1}${2}/${3}${4}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'imageshost.ru')) {
					// IMAGESHOST
					$preview_url = preg_replace('!^(http://img\d+\.imageshost\.ru/img/\d+/\d+/\d+/image_)(.*)_small\.(jpg|jpeg|png|gif|bmp|tif|tiff)!is', '${1}${2}.${3}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				} else if (FALSE !== strpos($url, 'ipicture.ru')) {
					// IPICTURE.RU
					$preview_url = preg_replace('!^(http://s\d+\.ipicture\.ru/uploads/\d+/)(thumbs)/(.*)\.(jpg|jpeg|png|gif|bmp|tif|tiff)!is', '${1}${3}.${4}', $url);
					$img_tag = '<span class="postimg fancyimagethumbs"><img data-fancy="false" src="'.$url.'" alt="'.forum_htmlencode($alt).'" rel="'.forum_htmlencode($preview_url).'"/></span>';
				}
			}
		]]></hook>


		<hook id="hd_head"><![CDATA[
		    if (defined('FORUM_PAGE')) {
				if (in_array(FORUM_PAGE, array('news', 'postdelete', 'modtopic', 'post', 'viewtopic', 'searchposts', 'pun_pm-inbox', 'pun_pm-outbox'))) {
					// LOAD LANG
					if (!isset($lang_fancy_image)) {
						if ($forum_user['language'] != 'English' && file_exists($ext_info['path'].'/lang/'.$forum_user['language'].'/'.$ext_info['id'].'.php')) {
							require $ext_info['path'].'/lang/'.$forum_user['language'].'/'.$ext_info['id'].'.php';
						} else {
							require $ext_info['path'].'/lang/English/'.$ext_info['id'].'.php';
						}
					}

					$forum_loader->add_css('a.fancy_zoom:hover { text-decoration: none !important; }', array('type' => 'inline'));

					$fancy_image_js_env = '
						PUNBB.env.fancy_image = {
							lang_title: "'.forum_htmlencode($lang_fancy_image['Original']).'",
						};';


					$forum_loader->add_js($fancy_image_js_env, array('type' => 'inline'));

					//$forum_loader->add_js($ext_info['url'].'/js/fancy_image.min.js', array('type' => 'url', 'async' => true));

					// For speed use inline JS
					$forum_loader->add_js('PUNBB.fancy_image=(function(){function c(){$(".fancyimagethumbs > img").each(function(){if($(this).data("fancy")===true){return;}$(this).data("fancy",true);var f=$(this),d=f.attr("rel"),e=f.parents("a").eq(0),g=f.parents(".post").find("h4").attr("id");if(!e){return;}if(d){e.attr("href",d);if(g){e.attr("rel",g);}e.addClass("fancy_zoom");f.removeAttr("rel");}e.fancybox({"zoomSpeedIn":100,"zoomSpeedOut":100,"padding":0,"cyclic":true,"overlayShow":false,"showCloseButton":true,"changeSpeed":100,"changeFade":100,"hideOnContentClick":true,"transitionIn":"none","transitionOut":"none","centerOnScroll":false,"titleFormat":a,"showNavArrows":true,"titleShow":true,"enableEscapeButton":true,"titlePosition":"over","onStart":function(j,h,i){if(b(j[h].href)===null){i.titleShow=false;}},"onComplete":function(l,h,i){var k=b(l[h].href),j=0;if(k!==null){$("#fancy_title_link").attr("href",k);}if($("#fancybox-img").length>0){j=$("#fancybox-img").height()*$("#fancybox-img").width();if(j<300000){$("#fancybox-title").hide();}}}});});}function a(g,f,d,e){return"<div id=\"fancybox-title-over\"><a id=\"fancy_title_link\" href=\"\">"+PUNBB.env.fancy_image.lang_title+"</a></div>";}function b(d){var e=null;if(d.indexOf("pic.lg.ua")>0){e=d.replace("pv_","");}else{if(d.indexOf("iteam.net.ua/uploads/")>0){e=d.replace("N_","O_");}else{if(d.indexOf("imageshack.us")>0){e=d.replace(".th.",".");}else{if(d.indexOf("radikal.ru")>0){e=null;}else{if(d.indexOf("piccy.info")>0){e=null;}else{if(d.indexOf("imagepost.ru")>0){e=null;}else{if(d.indexOf("ipicture.ru")>0){e=null;}else{if(d.indexOf("imageshost.ru")>0){e=null;}}}}}}}}return e;}return{init:function(){$(document).bind("run.fancy_image",c).trigger("run.fancy_image");}};}());$(document).ready(PUNBB.fancy_image.init);', array('type' => 'inline'));
				}
		    }
		]]></hook>
	</hooks>
</extension>
