function sfMediaLibrary_Engine()
{
  // Browser check
  var ua = navigator.userAgent;
  this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
  this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
  this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
  this.isGecko = ua.indexOf('Gecko') != -1;
  this.isSafari = ua.indexOf('Safari') != -1;
  this.isOpera = ua.indexOf('Opera') != -1;
  this.isMac = ua.indexOf('Mac') != -1;
  this.isNS7 = ua.indexOf('Netscape/7') != -1;
  this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
  this.isTinyMCE = false;

  // Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
  if (this.isOpera) {
    this.isMSIE = true;
    this.isGecko = false;
    this.isSafari =  false;
  }
}

sfMediaLibrary_Engine.prototype = {
  init : function(url)
  {
    this.url = url;
    
  },
  
  fileBrowserReturn : function (url,PopUp)
  {
 
	if (this.fileBrowserType == 'image')
	{
		this.fileBrowserWin.ImageDialog.showPreviewImage(url);
	} 
	var win =PopUp.getWindowArg("window");
	var input=PopUp.getWindowArg("input");
	win.document.getElementById(input).value=url; 
    	PopUp.close();
 
    

  },
  fileBrowserCallBack : function (field_name, url, type, win)
  {
	//Set TinyMCE to true
	this.isTinyMCE=true;
    //Store the URL
	var url = this.url;
    //Check the type of image

    var cmsURL = window.location.toString();    // script URL - use an absolute path!
    if (cmsURL.indexOf("?") < 0) {
        //add the type as the only query parameter
        cmsURL = cmsURL + "?type=" + type;
    }
    else {
        //add the type as an additional query parameter
        // (PHP session ID is now included if there is one at all)
        cmsURL = cmsURL + "&type=" + type;
    }

     
    this.isTinyMCE = true;
    var template = new Array();
    template['title']  = 'Assets';
    var url = this.url;
    if (type == 'image')
      url += '/images_only/1';
    tinyMCE.activeEditor.windowManager.open({ 
    	file: url,
    	title: "Please select an image",
    	width: 640,
    	height: 480,
    	resizable: "yes",
    	inline:    "yes",
    	scrollbars: "yes",
    	close_previous: "yes"
    	}
    , {
        window : win,
        input : field_name,
        mediaLibrary: this
    });
    template['file']   = url;
    template['width']  = 550;
    template['height'] = 600;
    template['close_previous'] = 'no';
    template['resizable'] = 'yes';
    template['inline'] = 'yes';

    this.fileBrowserWin = win;
    this.fileBrowserFormName = 0;
    this.fileBrowserFieldName = field_name;
    this.fileBrowserType = type;
    
/*    tinyMCE.activeEditor.windowManager.open(template, {
        window : win,
        input : field_name
    });
    return */false;
  },

  openWindow : function(options)
  {
    var width, height, x, y, resizable, scrollbars, url;

    if (!options)
      return;
    if (!options['field_name'])
      return;
    if (!options['url'] && !this.url)
      return;
    this.fileBrowserWin = self;
    this.fileBrowserFormName = (options['form_name'] == '') ? 0 : options['form_name'];
    this.fileBrowserFieldName = options['field_name'];
    if (options['element_id']) this.fileBrowserFieldId=options['element_id'];
    this.fileBrowserType = options['type'];

    url = this.url;
    if (options['type'] == 'image')
      url += '/images_only/1';

    if (!(width = parseInt(options['width'])))
      width = 550;

    if (!(width = parseInt(options['width'])))
      width = 550;

    if (!(height = parseInt(options['height'])))
      height = 600;

    // Add to height in M$ due to SP2 WHY DON'T YOU GUYS IMPLEMENT innerWidth of windows!!
    if (sfMediaLibrary.isMSIE)
      height += 40;
    else
      height += 20;

    x = parseInt(screen.width / 2.0) - (width / 2.0);
    y = parseInt(screen.height / 2.0) - (height / 2.0);

    resizable = (options && options['resizable']) ? options['resizable'] : "no";
    scrollbars = (options && options['scrollbars']) ? options['scrollbars'] : "no";

    var modal = (resizable == "yes") ? "no" : "yes";

    if (sfMediaLibrary.isGecko && sfMediaLibrary.isMac)
      modal = "no";

    if (options['close_previous'] != "no")
      try {sfMediaLibrary.lastWindow.close();} catch (ex) {}

    var win = window.open(url, "sfPopup" + new Date().getTime(), "top=" + y + ",left=" + x + ",scrollbars=" + scrollbars + ",dialog=" + modal + ",minimizable=" + resizable + ",modal=" + modal + ",width=" + width + ",height=" + height + ",resizable=" + resizable);
    
    if (options['close_previous'] != "no")
      sfMediaLibrary.lastWindow = win;

    eval('try { win.resizeTo(width, height); } catch(e) { }');

    // Make it bigger if statusbar is forced
    if (sfMediaLibrary.isGecko)
    {
      if (win.document.defaultView.statusbar.visible)
        win.resizeBy(0, sfMediaLibrary.isMac ? 10 : 24);
    }

    win.focus();

  }
}
var SfMediaLibrary = sfMediaLibrary_Engine; // Compatiblity with gzip compressors
var sfMediaLibrary = new sfMediaLibrary_Engine();

    var props=0;
    function resize_dialog( name, current_path,count, original_width, original_height )
    {
      if(original_height>0) props=original_width/original_height;
      else return false;
      $('resample_width').value=original_width;
      $('resample_height').value=original_height;
      $('resample_name').value=name;
      $('resample_current_path').value=current_path;
      $('resample_count').value=count;
      $('resample_original_width').value = original_width;
      $('resample_original_height').value = original_height;      
      show_resize_dialog();
      //Modalbox.show($('resample_dialog'), {title: 'Změnit rozměry obrázku: '+name, width: 300}); 
    }
    function show_resize_dialog()
    {
      $('resample_dialog').style.display='block';
      $('file_browser').style.display='none';
    }
    function hide_resize_dialog()
    {
      $('resample_dialog').style.display='none';
      $('file_browser').style.display='block';
    }
    function check_scale(elm)
    { 
      if($('resample_scale').checked && props>0)
      {
        if(elm=='resample_height')
        {
          $('resample_width').value=parseInt($F('resample_height')*props);
        }
        else
        {
          $('resample_height').value=parseInt($F('resample_width')/props);
        }
      }
    }
