/**
 * sCMS is a SEO-friendly Content Management System for small projects.
 * sCMS organized as a compact core with supporting module extensibility.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author		Andrey Khrolenok <limych at users.sourceforge.net>
 * @copyright	Copyright &copy; 2008 by Andrey Khrolenok
 * @license		http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version		$Id: jquery.flyup.js 136 2009-04-17 12:55:50Z limych $
 * @package		sCMS
 * @subpackage	jquery.flyup
 */

(function($){
	$.fn.flyup = function(o){
		var p = {
				outSpeed	: 1000,
				inSpeed		: 500,
				outEase		: 'swing',
				inEase		: 'swing',
				flyupClass	: 'fuFloater',
				flyupImage	: 'img:first',
				maxWidth	: 750,
				maxHeight	: 650,
				loadingSrc	: null,
				loadingText	: 'Loading... Please Wait'
			};

		return this.each(function(){
			var	opt		= $.extend({}, p, $.flyup.params, o),
				handler = $(opt.flyupImage, this).parent('a:first').get(0);

			if(typeof(handler) == 'undefined')	return;

			if(handler._flyup){
				H[handler._flyup].opt = $.extend({}, H[handler._flyup].opt, o);
				return;
			}
			handler._flyup = ++s;
			H[s] = {
				s	: s,
				opt	: opt,
				el	: $(this),
				img	: $(opt.flyupImage, this)
			};
			if(opt.loadingSrc){
				H[s].loader_img = new Image();
				H[s].loader_img.src = opt.loadingSrc;
			}

			$(handler).click(function(){
				var	hh		= H[this._flyup],
					el_o	= hh.el.offset(),
					el_w	= hh.el.get(0).offsetWidth,
					el_h	= hh.el.get(0).offsetHeight,
					img_w	= hh.img.get(0).offsetWidth,
					img_h	= hh.img.get(0).offsetHeight,
					holder	= hh.img.parent('a:first'),
					big_img	= new Image();

				if((typeof(FO) == 'undefined') || (holder.length == 0))
					return F;

				FO.addClass(hh.opt.flyupClass).empty().append(
					hh.el.clone().css({
						width	: ''
					})
				);
				if(hh.opt.loadingSrc){
					loader.empty().append(
						$('<img/>').attr({
							alt	: hh.opt.loadingText,
							src	: hh.opt.loadingSrc
						}).css({
							position	: 'relative',
							top			: '0.5em',
							left		: '0.5em'
						})
					);
				}else{
					loader.empty().append(
						$('<span></span>').text(hh.opt.loadingText).css({
							position		: 'relative',
							top				: '0.5em',
							left			: '1em',
							color			: '#FFF',
							'font-size'		: '10pt',
							'font-weight'	: 'bold'
						})
					);
				}
				Hs = {
					s		: hh.s,
					img		: $(hh.opt.flyupImage, FO),
					top		: el_o.top,
					left	: el_o.left,
					width	: img_w,
					height	: img_h,
					dw		: (el_w - img_w),
					dh		: (el_h - img_h),
					kh		: img_h / img_w
				};
				loader.show();
				Hs.img.parent('a:first').click(function(){
					FO.jqmHide();
					return F;
				});
				$(big_img).load(function(){
					loader.hide();
					Hs.img.attr('src', big_img.src);
				});
				big_img.src = holder.attr('href');
				FO.css({
					top			: Hs.top,
					left		: Hs.left,
					width		: Hs.width + Hs.dw
				}).jqmShow();

				return F;
			});
		});
	};



	/*************************************************************************************************/

	$.flyup = {
		hash	: {},
		params	: {},
		imgStep	: function(h, fx){
				var	hh		= H[Hs.s],
					ldr		= $(':first-child', loader),
					img_w	= Math.round(fx.now - Hs.dw);

				if(img_w < 0)	img_w = 0;

				var	img_h	= Math.round(img_w * Hs.kh),
					img_o	= Hs.img.attr({
						width	: img_w,
						height	: img_h
					}).offset(),
					scr_top		= self.pageYOffset
									|| (document.documentElement && document.documentElement.scrollTop)
									|| (document.body && document.body.scrollTop),
					scr_left	= self.pageXOffset
									|| (document.documentElement && document.documentElement.scrollLeft)
									|| (document.body && document.body.scrollLeft);

				loader.css({
					top			: img_o.top - scr_top,
					left		: img_o.left - scr_left,
					width		: img_w,
					height		: img_h
				});
				ldr.css({
					top			: Math.round((img_h - ldr.height()) / 2),
					left		: Math.round((img_w - ldr.width()) / 2)
				});
			},
		imgOpen	: function(h){
				var	hh			= H[Hs.s],
					win			= $(window),
					scr_top		= self.pageYOffset
									|| (document.documentElement && document.documentElement.scrollTop)
									|| (document.body && document.body.scrollTop),
					scr_left	= self.pageXOffset
									|| (document.documentElement && document.documentElement.scrollLeft)
									|| (document.body && document.body.scrollLeft),
					el_w		= hh.opt.maxWidth,
					el_h		= Math.round((hh.opt.maxWidth - Hs.dw) * Hs.height / Hs.width + Hs.dh);

				if(el_h > hh.opt.maxHeight){
					el_w = Math.round(el_w * hh.opt.maxHeight / el_h);
					el_h = hh.opt.maxHeight;
				}
				if(el_h > win.width()){
					el_h = Math.round(el_h * win.width() / el_w);
					el_w = win.width();
				}
				if(el_h > win.height()){
					el_w = Math.round(el_w * win.height() / el_h);
					el_h = win.height();
				}
				img_o	= hh.img.offset();
				loader.css({
					'z-index'	: parseInt(h.w.css('z-index')) + 1
				});
				h.w.css({
					position	: 'fixed',
					top		: parseInt(h.w.css('top')) - scr_top,
					left	: parseInt(h.w.css('left')) - scr_left
				}).show().animate({
					top		: Math.round((win.height() - el_h) / 2),
					left	: Math.round((win.width() - el_w) / 2),
					width	: el_w
				}, {
					duration	: hh.opt.outSpeed,
					easing		: hh.opt.outEase,
					step		: $.flyup.imgStep
				});
			},
		imgClose	: function(h){
				var	hh			= H[Hs.s],
					scr_top		= self.pageYOffset
									|| (document.documentElement && document.documentElement.scrollTop)
									|| (document.body && document.body.scrollTop),
					scr_left	= self.pageXOffset
									|| (document.documentElement && document.documentElement.scrollLeft)
									|| (document.body && document.body.scrollLeft);

				h.w.animate({
					top		: Hs.top - scr_top,
					left	: Hs.left - scr_left,
					width	: Hs.width + Hs.dw
				}, {
					duration	: hh.opt.inSpeed,
					easing		: hh.opt.inEase,
					step		: $.flyup.imgStep,
					complete	: function(){
						h.w.hide();
						loader.hide();
						if(h.o)	h.o.remove();
					}
				});
			}
	};



	/*************************************************************************************************/

	var s	= 0,
		H	= $.flyup.hash,
		Hs	= {},
		F	= false,
		FO,
		loader;

	$(function(){
		FO	= $('<div></div>').insertBefore('body > *:first-child').css({
//			border		: '1px solid red',
			display		: 'none'
		}).jqm({
			onShow	: $.flyup.imgOpen,
			onHide	: $.flyup.imgClose
		});
		loader	= $('<div></div>').appendTo('body').css({
			position	: 'fixed',
			opacity		: 0.5,
			'background-color'	: '#000',
			display		: 'none'
		}).click(function(){
			FO.jqmHide();
			return F;
		});
	});
})(jQuery);
