﻿/*
 * Fishbox 1.0
 * Copyright (c) 2010 Hiroaki Komatsu
*/


(function($){

$.fn.extend({
	fishbox:function(b,c,d){
		return this.each(function(){
			if(typeof b=="string"){
				var a=$.data(this,"fishbox");
				a[b].apply(a,[c,d])
			}else{
				new $.fishbox(this,b,c)
			}
		})
	}
});

$.fishbox=function(a,b){
	$.data(a,"fishbox",this);
	this.init(a,b);
}

$.fishbox.open=function(hr,f){
	$.fishbox.prototype.initOpen(hr,f);
}
$.fishbox.close=function(call){
	var event={};
	event.data=[call];
	$.fishbox.prototype.remove(event);
}

$.extend($.fishbox.prototype,{
	h:{style:null,loadTmp:'<p style="color:#FFF;">loading...</p>',margin:60,animate:null,closed:null,params:null,is_close:false,macFFBG:1},
	init:function(e,f){
		var g=this;
		this.opts=$.extend(this.h,f);
		var j=this.root=$(e);
		$(j).bind("click.fishbox",function(){g.click(this);return false;});
	},
	initOpen:function(hr,f){
		this.opts=$.extend(this.h,f);
		this.open(hr);
	},
	click:function(b){
		var hr=b.href||b.alt;
		var rel=b.rel||false;
		var t=b.title||b.name||null;
		this.open(hr,rel,t);
	},
	open:function(hr,rel,t){
		var g=this;
		try{
			var closeTmp=g.opts["closeTmp"];
			var closed=g.opts["closed"];
			var an=g.opts["animate"];
			var macFFBG=g.opts["macFFBG"];
			
			// overlay
			var FB_overlay=$('<div id="FB_overlay">');
			$(FB_overlay).bind("click.fishbox",[closed],g.remove);
			var FB_overlay_in=$('<div id="FB_overlay_in">');
			$(FB_overlay).append(FB_overlay_in);
			if(g.detectMacXFF()){
				if(macFFBG==2){
					$(FB_overlay_in).addClass("FB_overlayMacFFBGHack2");
					animate=null;
				}else{
					$(FB_overlay_in).addClass("FB_overlayMacFFBGHack");
				}
			}else{
				$(FB_overlay_in).addClass("FB_overlayBG");
			}
			
			// body
			if(typeof document.body.style.maxHeight==="undefined"){//if IE 6
				$("body","html").css({height:"100%",width:"100%"});
				/*$("html").css("overflow","hidden");*/
				$("body","html").css("overflow-x","hidden");
				$("body","html").css("overflow-y","hidden");
				if(document.getElementById("FB_HideSelect")===null){//iframe to hide select elements in ie6
					$("body").append("<iframe src='return false;' id='FB_HideSelect'>");
					$(FB_overlay).css("height",document.body.clientHeight+200);
					$("body").append(FB_overlay);
				}
			}
			if(document.getElementById("FB_overlay")===null){
				$("body").append(FB_overlay);
			}
			
			if(an){
				$(FB_overlay).css("display","none");
				$(FB_overlay).fadeIn(an,function(){g.show(hr,rel,t);});
			}else{
				g.show(hr,rel,t);
			}
		}catch(e){
			alert(e);
		}
	},
	show:function(hr,rel,t){
		var g=this;
		try{
			var FB_class=g.opts["style"];
			var loadTmp=g.opts["loadTmp"];
			var an=g.opts["animate"];
			var closed=g.opts["closed"];
			
			// window
			var FB_window=$('<div id="FB_window">');
			if(FB_class)$(FB_window).addClass(FB_class);
			$("body").append(FB_window);
			
			// close
			var FB_close=$('<div>');
			var FB_closeBtn=$('<a href="#" id="FB_closeWindowButton" title="Close">CLOSE</a>');
			$(FB_closeBtn).bind("click.fishbox",[closed],g.remove);
			$(FB_close).append(FB_closeBtn);
			
			// load
			var FB_load=$('<div id="FB_load">');
			$(FB_load).append($(loadTmp));
			$("body").append(FB_load);
			$(FB_load).show();
			
			// Ajax
			var url=(hr.indexOf("?")!==-1)?hr.substr(0,hr.indexOf("?")):hr;
			if(url.toLowerCase().match(/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/)){//code to show images
				$(FB_close).attr("id","FB_closeWindow");
				
				var FB_caption=(t)?$('<div id="FB_caption">'+t+'</div>'):null;
				var FB_secondLine=g.getSecondLine(hr,rel);
				var imgPreloader=new Image();
				imgPreloader.onload=function(){
					imgPreloader.onload = null;
					$(FB_load).remove();
					
					var pagesize=g.getPageSize();
					var x=pagesize[0]-150;
					var y=pagesize[1]-150;
					var imageWidth=imgPreloader.width;
					var imageHeight=imgPreloader.height;
					
					var FB_img=$('<img id="FB_Image">');
					$(FB_img).attr("src",imgPreloader.src);
					$(FB_img).attr("width",imageWidth);
					$(FB_img).attr("height",imageHeight);
					$(FB_img).attr("alt","");
					
					var w=imageWidth+30;
					var h=imageHeight+60;
					
					var func=function(){
						$(FB_window).attr("width",w);
						$(FB_window).attr("height",h);
						$(FB_window).append(FB_img);
						if(FB_caption)$(FB_window).append(FB_caption);
						if(FB_secondLine)$(FB_window).append(FB_secondLine);
						$(FB_window).append(FB_close);
						$(FB_window).css({display:"block"});
					}
					if(an){$(FB_window).animate(g.getPosition(w,h),an,func);}
					else{
						var o=g.getPosition(w,h);
						$(FB_window).css("marginLeft",o["marginLeft"]);
						$(FB_window).css("marginTop",o["marginTop"]);
						func();
					}
				}
				imgPreloader.src=hr;
			}
			else if(url.toLowerCase().match(/\.swf$/)){//code to show swf
				$(FB_close).attr("id","FB_closeSWFWindow");
				var is_close=g.opts["is_close"]||false;
				
				var params=g.opts["params"]||{};
				var q=g.getQueryParams(hr.substr(hr.indexOf("?")));
				//var w="100%",h="100%";
				var w=550,h=400;
				if(q.width){w=parseInt(q.width,10);if(isNaN(w))w=550;}
				if(q.height){h=parseInt(q.height,10);if(isNaN(h))h=400;}
				//if(params["wmode"]=="transparent")$(FB_window).css("backgroundColor","transparent");
				
				var FB_object_value='<object id="FB_swfcontent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+w+'" height="'+h+'">';
				FB_object_value+='<param name="movie" value="'+hr+'" />';
				for(var e in params)FB_object_value+='<param name="'+e+'" value="'+params[e]+'" />';
				var isMSIE=/*@cc_on!@*/false; 
				if(!isMSIE){
					FB_object_value+='<object type="application/x-shockwave-flash" data="'+hr+'" width="'+w+'" height="'+h+'">';
					for(var e in params)FB_object_value+='<param name="'+e+'" value="'+params[e]+'" />';
					FB_object_value+='</object>';
				}
				FB_object_value+='</object>';
				
				$(FB_load).remove();
				var o=g.getPosition(w,h);
				//if(w=="100%"&&h=="100%"){
				//	$(FB_window).css("top",0);
				//	$(FB_window).css("left",0);
				//}else{
					$(FB_window).css("marginLeft",o["marginLeft"]);
					$(FB_window).css("marginTop",o["marginTop"]);
				//}
				$(FB_window).css("width",w);
				$(FB_window).css("height",h);
				//g.setLayPosition(FB_window);
				var func=function(){
					$(FB_window).html(FB_object_value);
					if(is_close)$(FB_window).append(FB_close);
					if(navigator.userAgent.indexOf('MSIE')!=-1)document.title=String(document.title).replace(/(.+)#.*?$/,"$1");
					//if(w=="100%"&&h=="100%"){
						//var intervalId=setInterval(function(){g.setLayPosition(FB_object_value);},100);
					//}
					
				}
				if(an){
					$(FB_window).css("display","none");
					$(FB_window).fadeIn(an,func);
				}else{
					$(FB_window).css("display","block");
					func();
				}
			}
			else{//code to show html
				$(FB_close).attr("id","FB_closeAjaxWindow");
				var q=g.getQueryParams(hr.substr(hr.indexOf("?")));
				var w=0,h=0;
				if(q.width){w=parseInt(q.width,10);if(isNaN(w))w=0;}
				if(q.height){h=parseInt(q.height,10);if(isNaN(h))h=0;}
				if(w<=0)w=940;
				if(h<=0){
					var margin=g.opts["margin"];
					var tbH=0;
					if(window.innerHeight){
						tbH=window.innerHeight-10;
					}else if(document.documentElement&&0!=document.documentElement.clientHeight){
						tbH=document.documentElement.clientHeight-10;
					}else if(document.body){
						tbH=document.body.clientHeight-10;
					}
					tbH=Math.max(360,tbH);
					h=tbH-margin*2;
				}
				var iW=w;
				var iH=h;
				
				var FB_iframe=$('<iframe id="FB_iframeContent" frameborder="0" hspace="0">');
				$(FB_iframe).attr("src",hr);
				$(FB_iframe).css({"width":iW,"height":iH});
				$(FB_iframe).load(function(){
					$(FB_iframe).unbind('load',$(FB_iframe).onload);
					$(FB_load).remove();
					var func=function(){
						$(FB_window).attr("width",w);
						$(FB_window).attr("height",h);
						$(FB_window).append(FB_iframe);
						$(FB_window).append(FB_close);
						$(FB_window).css({display:"block"});
					}
					if(an){$(FB_window).animate(g.getPosition(w,h),an,func);}
					else{
						var o=g.getPosition(w,h);
						$(FB_window).css("marginLeft",o["marginLeft"]);
						$(FB_window).css("marginTop",o["marginTop"]);
						func();
					}
				});
				$(FB_iframe).triggerHandler('load');
			}
		}catch(e){
			alert(e);
		}
	},
	remove:function(event){
		$("#FB_closeWindowButton").hide();
		$("#FB_imageOff").unbind("click");
		$("#FB_closeWindowButton").unbind("click");
		$("#FB_iframe").remove();
		var unload=function(){
			$('#FB_window,#FB_overlay,#FB_HideSelect,#FB_iframe,#FB_swfcontent').trigger("unload").unbind().remove();
			if(event){
				if(event.data){
					var call=event.data[0];
					if(typeof(call)=='function')call();
				}
			}
		};
		if(document.getElementById("FB_window")){
			$("#FB_window").fadeOut("fast",function(){$("#FB_overlay").fadeOut("fast",unload);});
		}else{
			$("#FB_overlay").fadeOut("fast",unload);
		}
		//$("#FB_window").fadeOut("fast",function(){$('#FB_window,#FB_overlay,#FB_HideSelect,#FB_iframe').trigger("unload").unbind().remove();});
		$("#FB_load").remove();
		if(typeof document.body.style.maxHeight=="undefined"){//if IE 6
			$("body","html").css({height:"auto",width:"auto"});
			$("body","html").css("overflow-x","");
			$("body","html").css("overflow-y","");
		}
		document.onkeydown = "";
		document.onkeyup = "";
		return false;
	},
	getSecondLine:function(hr,rel){
		var g=this;
		var FB_secondLine=null;
		try{
			if(rel){
				var FB_Prev=null;
				var FB_Next=null;
				var FB_FoundURL=false;
				var FB_imageCount=null;
				var FB_TempArray=$('a[rel='+rel+']').get();
				var n=FB_TempArray.length;
				for(var i=0;i<n;i++){
					var url=FB_TempArray[i].href;
					var title=FB_TempArray[i].title;
					if(url!=hr){
						if(FB_FoundURL){
							FB_Next=$('<span id="FB_next"></span>');
							$(FB_Next).append('<a href="'+url+'" title="'+title+'">Next &gt;</a>');
							break;
						}else{
							FB_Prev=$('<span id="FB_Prev"></span>');
							$(FB_Prev).append('<a href="'+url+'" title="'+title+'">&lt; Prev</a>');
						}
					}else{
						FB_FoundURL=true;
						FB_imageCount=$('<span>Image '+(i+1)+' / '+n+'</span>');
					}
				}
				
				var FB_secondLine=$('<div id="FB_secondLine">');
				if(FB_imageCount){
					$(FB_secondLine).append(FB_imageCount);
				}
				if(FB_Prev){
					$('a',FB_Prev).bind("click.fishbox",function(){
						if($(document).unbind("click",$(this).onclick)){$(document).unbind("click",$(this).onclick);}
						$("#FB_window").remove();
						g.show(this.href,rel,this.title);
						return false;
					});
					$(FB_secondLine).append(FB_Prev);
				}
				if(FB_Next){
					$('a',FB_Next).bind("click.fishbox",function(){
						if($(document).unbind("click",$(this).onclick)){$(document).unbind("click",$(this).onclick);}
						$("#FB_window").remove();
						g.show(this.href,rel,this.title);
						return false;
					});
					$(FB_secondLine).append(FB_Next);
				}
			}
		}catch(e){
		}
		return FB_secondLine;
	},
	getPageSize:function(){
		var de=document.documentElement;
		var w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth;
		var h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;
		return [w,h];
	},
	getPosition:function(w,h){
		var o={};
		o["width"]=w;
		o["height"]=h;
		o["marginLeft"]=0-parseInt((w/2),10);
		if(!(jQuery.browser.msie&&jQuery.browser.version<7)){
			o["marginTop"]=0-parseInt((h/2),10);
		}else{
			var doc=document;
			var ch=doc.documentElement.clientHeight?doc.documentElement.clientHeight:doc.body.clientHeight;
			var top=doc.documentElement?doc.documentElement.scrollTop:doc.body.scrollTop;
			o["marginTop"]=parseInt(((ch-h)/2),10)+top;
		}
		return o;
	},
	detectMacXFF:function(){
		var ua=navigator.userAgent.toLowerCase();
		if(ua.indexOf('mac')!=-1&&ua.indexOf('firefox')!=-1){
			return true;
		}
	},
	getQueryParams:function(u){
		var r={};
		if(u){
			var t=u.substring(1).split('&');
			var l=t.length;
			for(var i=0;i<l;i++){
				var p=t[i].indexOf('=');
				r[t[i].substring(0,p)]=t[i].substring(p+1);
			}
		}
		return r;
	},
	setLayPosition:function(o){
		var prop=this.getWindowProperty();
		$(o).css("width",prop.width);
		$(o).css("height",prop.height);
		//o.style.top=this.getScrollTop()+"px";
		//o.style.left=this.getScrollLeft()+"px";
	},
	getWindowProperty:function(){
		if(document.compatMode&&document.compatMode!="BackCompat"){
			// DTD Standards
			var prop=new Object();
			prop.height=document.documentElement.clientHeight;
			prop.width=document.documentElement.clientWidth;
			return prop;
		}else if(document.compatMode=="BackCompat"){
			// No DTD
			var prop=new Object();
			prop.height=document.body.clientHeight;
			prop.width=document.body.clientWidth;
			return prop;
		}else{
			if(self.innerHeight){
				// Mac Safari 1
				var prop=new Object();
				prop.height=self.innerHeight;
				prop.width=self.innerWidth;
				return prop;
			}else{
				// Mac IE 5.2 (?)
				var prop=new Object();
				prop.height=document.body.clientHeight;
				prop.width=document.body.clientWidth;
				return prop;
			}
		}
	}
});


})(jQuery);

