/**
 * jDTLightBox
 *
 * @version      $Rev$
 * @author       nori (http://twitter.com/5509)
 * @copyright    (c) 2010 誰が使うの？何の役にも立たないjQueryプラグイン
 * @license      The MIT License
 * @link         http://jdtplugins.googlecode.com/svn/trunk/jdtlightbox/index.html
 *
 * $Date$
 */
/**
 * qBox (Inspired From jDTLightBox)
 *
 * @version      $Rev$
 * @author       yakumo27 (http://twitter.com/yakumo27)
 * @copyright    (c) 2010 quoit
 * @license      The MIT License
 * @link         http://quoit.jp/
 *
 * $Date$
 */

(function($){
	$.fn.qBox=function(options){
		var c=$.extend({
			href:''
		}, options);

		if ( !$('#qBoxLayer') || $('#qBoxLayer').length == 0) {
			$('body').append(
				"<div id='qBoxLayer' />"+
				"<div id='qBoxContainer'>"+
					"<img id='qBoxClose' class='qBoxClose' src='/common/js/qBox/img/close.png' alt='Close' />"+
				"</div>"
			);
		}
		
		var t=$(this),
		d = document.documentElement,
		dW = d.clientWidth,
		dH = d.clientHeight,
		layer=$("#qBoxLayer"),
		close=$(".qBoxClose"),
		container=$("#qBoxContainer"),
		qBoxClose=function(){
			close.hide();
			container.hide();
			layer.hide();
		}

		// get window size when changed
		$(window).resize(function() {dW = d.clientWidth;dH = d.clientHeight;});

		layer.click(qBoxClose);
		close.click(qBoxClose);

		t.click(function() {
			if(c.href !== ''){
				var hrefStr = c.href;
			}else{
				var hrefStr = this.href;
			}
			
			$("#qBoxContent").remove();
			container.append('<iframe id="qBoxContent" />');
			var content=$("#qBoxContent");

			content.attr("src",hrefStr);
			container.css({width:2,height:2}).show();
			content.load(function(){
				if(hrefStr.match(/\.(jpg|jpeg|gif|png)$/)){
					content.contents().find("body").css({margin:0,padding:0});

					image = content.contents().find("img").get(0);
					image.removeAttribute("width");
					image.removeAttribute("height");
					if (typeof image.naturalWidth !== 'undefined'){// for Firefox, Safari, Chrome
						cW = image.naturalWidth;
						cH = image.naturalHeight;
					}else if(typeof image.runtimeStyle !== 'undefined'){// for IE
						var run = image.runtimeStyle;
						run.width  = "auto";
						run.height = "auto";
						cW = image.width;
						cH = image.height;
					}else{// for Opera
						cW = image.width;
						cH = image.height;
					}
				}else{
					ifmc=content.contents();
					cH = $(ifmc).height();
					cW = $(ifmc).width();
				}
				
				if(c.height!==undefined){
					h = c.height;
				}else if(cH < dH){
					h = cH;
				}else{
					h = dH-100;
				}
				if(c.width!==undefined){
					w = c.width;
				}else if(cW < dW){
					w = cW;
				}else{
					w = dW-100;
				}
				
				container.animate({
					marginTop: -h/2 + $(document).scrollTop(),
					marginLeft: -w/2,
					width: w,
					height: h
				},0,function(){
					layer.css("opacity","0.6").show();
					close.show();
				});
			});
			
			return false;
		});
	};

	$.qBox=function(opt){
		$(this).qBox(opt);
	}

	$.qBox.close = function(){
		var layer=$("#qBoxLayer"),
		close=$(".qBoxClose"),
		container=$("#qBoxContainer");
		close.hide();
		container.hide();
		layer.hide();
	};
})(jQuery);

$(function(){
	$.qBox.close;
});


