// use frameworks/prototype/prototype.js
Popup = Class.create();
Popup.prototype = 
{
	initialize:function(link)
	{
		this.link = $(link);
		
    this.options = {
      url: this.link.href,
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:Element.hasClassName(this.link, "normal")
    };

    Object.extend(this.options, this.getDimensions(this.link.title) || {});
		this.link.onclick = this.linkClick.bind(this);
	},
	getDimensions:function(title)
	{
		var pattern = /.*\(\s*(.*)\s*x\s*(.*)\s*\)$/;
		dimensions = title.match(pattern);
		if (dimensions[1] && dimensions[2])
		{
			width = this.getDimension(dimensions[1], screen.availWidth);
			height = this.getDimension(dimensions[2], screen.availHeight);
			if (width && height)
				return {width: width, height: height, top:(screen.availHeight - height)/2, left:(screen.availWidth - width)/2};
		}
		return false;
	},
	getDimension:function(xstring, relativeDimension)
	{
		if (!relativeDimension) relativeDimension = 100;
		var percentPattern = /\s*([0-9]*)%\s*$/;

		// Avem un intreg
		if (parseInt(xstring) == xstring)
			return xstring;
			
		m = xstring.match(percentPattern);
		if (m[1])
		{
			var x = m[1];
			return x/100*relativeDimension;
		}
		return false;
	},
	linkClick: function()
	{
		try
		{
			/*
	 		if (Element.hasClassName(this.link, "normal")) {
				try	{
					newWindow = window.open(this.link.href, this.link.target);
				} catch(e) {
					newWindow = window.open(this.link.href);
				};
			}
			else {
				strOptions="scrollbars=1,resize=1,height="+this.height+",width="+this.width;
				newWindow = window.open(this.link.href, this.link.target, strOptions);
			}
			*/
			newWindow = this.open(this.options);

			if (newWindow && window.focus)
				newWindow.focus();
		}
		catch(e) 
		{
			alert("Popup.linkClick: " + e.message);
		}
		return false;
	},
  open: function(options)
  {
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    return window.open(this.options.url, this.options.name,openoptions );
  }
}
