var Class;

(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  Class = function(){
	  this.a = "o";
  };
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

PopupWindow = Class.extend({
	init : function(src, settings) {	
		//this._super(settings);
		this.settings = jQuery.extend({}, settings);
		this.settings.type = this.settings.type || 'img';
		this.settings.iframeWidth = this.settings.iframeWidth || 960;
		this.settings.iframeHeight = this.settings.iframeHeight || 645;
		this.src = src;
		
		this.initPopup();
		
		this.popup();
	},
	
	setSettings : function(settings) {
		jQuery.extend(this.settings, settings);
	},
	
	initPopup : function(){
		var outer = this;
		
		this.$detailContainer = $("<div class='imageDetailContainer'></div>");
		
		this.$closeIcon = $("<div class='imageDetailCloseIcon'></div>");
		this.$closeIcon.click(function() {
			outer.closePopupWindow();
		});
		
		$("body").append(this.$closeIcon);
		$("body").append(this.$detailContainer);
	},
	
	popup : function() {
		var outer = this;

		var $obj = $("<" + this.settings.type + " frameBorder='0' class='popupWindow'></" + this.settings.type + ">");
		
		//if type is img wait for loading
		if(this.settings.type == 'img'){
			$obj.bind("load", function() {
	            outer.resizePopup($obj);
			});
		}
		else{
			outer.resizePopup($obj);
		}

		$obj.bind("error", function() {
            alert("Could not large File");
		});
		
		$obj.bind("click", function() {
			outer.closePopupWindow();
		});
		
		$obj.attr("src", this.src);
	},
	
	resizePopup : function($obj) {
		var outer = this;
		
		var resize = function(img, maxh, maxw) {
          var ratio = maxh/maxw;
          if (img.height/img.width > ratio){
             // height is the problem
            if (img.height > maxh){
              img.width = Math.round(img.width*(maxh/img.height));
              img.height = maxh;
            }
          }
          else {
            // width is the problem
            if (img.width > maxw){
              img.height = Math.round(img.height*(maxw/img.width));
              img.width = maxw;
            }
          }
        };
	        
		var winW = $(window).width();
		//Get the full page height including the scroll area
		var winH = $(window).height();

		if(this.settings.type == 'img'){
			if(outer.settings.maxHeight && outer.settings.maxWidth){
				resize($obj, outer.settings.maxHeight, outer.settings.maxWidth);
			}
			else{
				resize($obj, winH-50, winW-50);
			}
		}
		else{
			$obj.css('width', this.settings.iframeWidth + 'px');
			$obj.css('height', this.settings.iframeHeight + 'px');
		}
		
		$('body').append($obj);
		var width = $obj.attr("width");
		var height = $obj.attr("height");

		if(this.settings.type == 'iframe'){
			width = this.settings.iframeWidth;
			height = this.settings.iframeHeight;
		}
		
        var vertPadding = this.cssInt(outer.$detailContainer, "paddingTop") + this.cssInt(outer.$detailContainer, "paddingBottom");
        var horizPadding = this.cssInt(outer.$detailContainer, "paddingLeft") + this.cssInt(outer.$detailContainer, "paddingRight");

		outer.$detailContainer.css({
			width: (width) + "px",
			height: (height) + "px"
		});
		outer.$detailContainer.html("");
		outer.$detailContainer.append($obj);

        var topPos =  winH/2 - ((height + vertPadding) /2);
        var leftPos =  winW/2 - ((width + horizPadding) /2);

		//Set the popup window to center if required
		outer.$detailContainer.css({
					'top' : topPos,
					'left' : leftPos
				}).fadeIn();

        var closeW = this.cssInt(outer.$closeIcon, "width");
        var closeH = this.cssInt(outer.$closeIcon, "height");

        outer.$closeIcon.css({
            top: (topPos - closeH / 2) + "px",
            left: (leftPos   - closeW / 2) + "px"
        }).show();
	},
	
	closePopupWindow : function() {
		var outer = this;
		
		this.$closeIcon.remove();
		this.$detailContainer.remove();
	},
	cssInt : function($el, prop) {
		if (!$el) {
			return 0;
		}
		var prop = $el.css(prop);
		if (!prop || prop == "auto") {
			return 0;
		}
		return parseInt(prop.replace(/px/,""));
	}
});

