/*
 * Class for additional gallery plugin image page functionality: 
 * - fullzise popup.
 * 
 * Denis Zenkovich <denis.zenkovich@picturesurf.org> Sep 16 2008
 */
	iG_ImagePage = function()
	{
		var that = this;
		this.init = function()
		{
			var ImageTitle = iG$j(".gallery-image-page-header").get(0);
			if(ImageTitle)
				ImageTitle.scrollIntoView();
			this.lnkflsz = iG$j(".ig-fullsizer");
			if(this.lnkflsz.length < 1)
				return; //exit if no link found
			this.origSrc = unescape(this.lnkflsz.attr("data-orig"));
			this.origW = this.lnkflsz.attr("data-w");
			this.origH = this.lnkflsz.attr("data-h");
			this.lnkflsz.click( function(){ that.showPopup(); return false; } );
		};
		this.constrPopup = function()
		{
			var id = "";
			if(window.iGWwp_list && window.iGWwp_list[0].code)
				id = "Gallery_"+window.iGWwp_list[0].code+"-popup";
			var html = '<div class="gallery-widget-popup" id="'+id+'"><div class="ig-ipp-controls"><div class="ig-ipp-ctrlcont"><div class="ig-ipp-paler"></div>';
			html+= '<div class="ig-ipp-links"><a class="ig-ipp-fitscr" href="#">Fit screen</a><a class="ig-ipp-fulsz" href="#">Original size</a></div></div></div>';
			html+= '<img class="ig-ipp-image"/></div>';
			this.popup = iG$j(html).appendTo(iG$j(document.body));
			this.popup.ctrlblock = this.popup.find(".ig-ipp-controls");
			this.popup.lnfitscr = this.popup.find(".ig-ipp-fitscr");
			this.popup.lnfulsz = this.popup.find(".ig-ipp-fulsz");
			this.popup.img = this.popup.find(".ig-ipp-image");
			this.popup.lnfitscr.click( function(){ that.fitScreen(); return false } );
			this.popup.lnfulsz.click( function(){ that.fullSize(); return false } );
			this.popup.click(function(e){ e.stopPropagation() });
			this.popup.img.attr("src", this.origSrc);
			this.popup.img.click( function(){ that.closePopup() } );
			this.popup.ctrlblock.find(".ig-ipp-paler").css({opacity: 0.6});
			this.popup.ctrlblock.block = this.popup.ctrlblock.find(".ig-ipp-ctrlcont")
			iG$j(document.documentElement).click(function(e){ that.closePopup() }).keyup(function(e)
			{
				if(!that.Popened)
					return;
				if(e.keyCode == 13 || e.keyCode == 27)
					that.closePopup();
			});
		};
		this.positionPopup = function()
		{
			var s = iG$j.winSize();
			var ps = {x: this.popup.popwidth, y: this.popup.popheight};
	        var wt = Math.round( (s.sz.y - ps.y)/2+s.scr.y );
	        var wl = Math.round( (s.sz.x - ps.x)/2+s.scr.x );
			if(wt<5) wt = 5;
			if(wl<5) wl = 5;
			this.popup.css({left: wl, top: wt});
		};
		this.showPopup = function()
		{
			if(!this.popup)
				this.constrPopup();
			
			this.fitScreen();
			iGW_GlobalCover.show();
			if(this.showeffects)
				this.popup.fadeIn(100);
			else
				this.popup.css("display", "block");
			this.positionPopup();			
			this.Popened = true;
		};
		this.closePopup = function()
		{
			if(this.showeffects)
				this.popup.fadeOut(100);
			else
				this.popup.css("display", "none");
			this.Popened = false;				
			iGW_GlobalCover.hide();
		};
		this.fullSize = function()
		{
			this.popup.popwidth = this.origW*1+2;
			this.popup.popheight = this.origH*1+2;
			this.popup.width(this.popup.popwidth).height(this.popup.popheight);
			this.popup.img.width(this.origW*1).height(this.origH*1);
			this.popup.lnfulsz.css("display", "none");
			this.popup.lnfitscr.css("display", "block");
			this.popup.css({left: 0, top: 0});
			iGW_GlobalCover.show();
			this.positionPopup();
		};
		this.fitScreen = function()
		{
			var s = iG$j.winSize();
			if(!this.lastfit.screen || s.sz != this.lastfit.screen)
			{
				this._calcFitScreen(s);
			}
			this.popup.popwidth = this.lastfit.img.w*1+2;
			this.popup.popheight = this.lastfit.img.h*1+2;
			this.popup.width(this.popup.popwidth).height(this.popup.popheight);
			this.popup.img.width(this.lastfit.img.w).height(this.lastfit.img.h);
			this.popup.lnfitscr.css("display", "none");
			this.popup.lnfulsz.css("display", "block");			
			this.popup.css({left: 0, top: 0});
			iGW_GlobalCover.show();
			this.positionPopup();
		};
		this._calcFitScreen = function(s)
		{
			var sc = Math.max( this.origW/(s.sz.x), this.origH/(s.sz.y) );
			if(sc > 1)
			{
				this.lastfit.screen = s.sz;
				this.lastfit.img = { w: Math.round(this.origW/sc-30), h: Math.round(this.origH/sc-50) };
				this.popup.ctrlblock.css("display", "block");
			}
			else
			{
				this.lastfit.img = { w: this.origW, h: this.origH };
				this.popup.ctrlblock.css("display", "none");
			}
		};
		
		this.lastfit = {};
		this.showeffects = true;
		iG$j(document).ready( function(){ that.init() } );
	};
	
	var iG_ImagePageIns = new iG_ImagePage();
