
if(!window.__rageLib){
  window.__rageLib = {};
  window.__rageLib.instances = {};
}

__rageLib.rageDynamicUpload = function(){
  this.ajax = null;
  this.construct();
};

__rageLib.rageDynamicUpload.prototype = {
 
  actTabIndex: [], //actual tab (by id)
  tabs: [], //tab array

  construct: function(){
    var __rduiInit = new Array();
    __rduiInit['ON_SUCCESS'] = function(XmlHttp){
       var __t = XmlHttp.responseText.split('#&*$');
       switch(__t[0]){ //action
          case 'GET_NEW_UPLOAD_FIELD':
            var elem = document.getElementById('__rdaf_'+__t[1]+'_'+__t[2]+'_uploadfields');
            var child = document.createElement('DIV');
            child.innerHTML = __t[3];
            elem.appendChild(child);
          	break;
         	case 'LOAD_FILE_OPTIONS_AREA':
            //var elem = document.getElementById('__rdaf_content_'+__t[1]+'_'+__t[2]+'_options');
            //elem.innerHTML = __t[3];
            __rlDynUpload.setTabContent(__t[1],__t[2],'options',__t[3]);
            __rlDynUpload.openTab('options', __t[1], __t[2]);
          	break;
     	    case 'DELETE_ARTICLE_FILE':
            __rageViewController.getView('__rdaf_view_'+__t[2]+'_'+__t[1]).reload();
          	break;
          default:
       		  alert(XmlHttp.responseText);
       			break;
       }
    }
    this.ajax = new __rageLib.rageAJAX(__rduiInit);
  },
  
  setTabContent: function(form_id, field_key, tab, content){
    document.getElementById('__rdaf_content_'+form_id+'_'+field_key+'_'+tab).innerHTML = content;
  },
  
  getTabElem: function(form_id, field_key, tab){
    return document.getElementById('__rdaf_tab_'+form_id+'_'+field_key+'_'+tab);
  },
  
  addUploadField: function(form_id, field_key, proc_path){
    this.ajax.postURL = proc_path+',processor';
	  this.ajax.postParams = 'request=GET_NEW_UPLOAD_FIELD'+
                            '&form_id='+encodeURIComponent(form_id)+
                            '&field_key='+encodeURIComponent(field_key);
    this.ajax.process();
  },
  
  deleteFile: function(file_id, view_id, fname, proc_path){
    if(confirm('Czy na pewno chcesz skasować ten plik?')){
      this.ajax.postURL = proc_path+',processor';
  	  this.ajax.postParams = 'request=DELETE_ARTICLE_FILE'+
                              '&file_id='+encodeURIComponent(file_id)+
                              '&view_id='+encodeURIComponent(view_id)+
                              '&fname='+encodeURIComponent(fname);
      this.ajax.process();
    }
  },
  
  loadOptionsTab: function(action, file_id, form_id, fname, proc_path){
    this.ajax.postURL = proc_path+',processor';
	  this.ajax.postParams = 'request=LOAD_FILE_OPTIONS_AREA'+
	                          '&action='+encodeURIComponent(action)+ 
                            '&file_id='+encodeURIComponent(file_id)+
                            '&view_id='+encodeURIComponent(form_id)+
                            '&fname='+encodeURIComponent(fname);
    this.ajax.process();
    this.setTabContent(form_id,fname,'options','Trwa wczytywanie');
    this.openTab('options',form_id, fname);
    //document.getElementById('__rdaf_'+form_id+'_'+fname+'_options').innerHTML = 'Trwa wczytywanie';
  },
  
  clearOptionsTab: function(form_id, field_key){
    this.setTabContent(form_id,field_key,'options','');
    this.getTabElem(form_id,field_key,'options').style.display = 'none';
    this.openTab('filelist',form_id,field_key);
  },
  
  codeReplace: function(form_id, fname, code){
    tinyMCE.execInstanceCommand(fname, 'mceReplaceContent', false, code); 
    this.clearOptionsTab(form_id, fname);
  },
  
  insertLink: function(form_id, fname, link, file_id){
    var code = '<a href='+link+'>' +
               document.getElementById('link_label_'+file_id).value +
               '</a>';
  
    this.codeReplace(form_id, fname, code);
  },
  
  insertImage: function(form_id, fname, link){
    var code = '<img src='+link+' />';
  
    this.codeReplace(form_id, fname, code);
  },
  
  insertSwf: function(form_id, fname, link, w, h, properRatio){
    var newRatio = w / h; 
    if(newRatio > properRatio){
      w = h * properRatio;
    }else if(newRatio < properRatio){
      h = w / properRatio;
    }
  
    var code = "<object width='"+w+"' height='"+h+"' data='"+link+"' type='application/x-shockwave-flash\ >"+
               "<param name='quantity' value='high' />"+
               "<param name='src' value='"+link+"' />"+
               "</obect>";
    
    this.codeReplace(form_id, fname, code);
  },
  
  insertYouTube: function(form_id, fname, yt_id, w, h){
    var properRatio = 640 / 390; // 1.64
    var newRatio = w / h;  // 100 / 1000 = 0.1
    if(newRatio > properRatio){ //to much w // constain h
      w = h * properRatio;
    }else if(newRatio < properRatio){
      h = w / properRatio;
    }
    var code = "<object width='"+w+"' height='"+h+"' style=\"background-image: url('http://img.youtube.com/vi/"+yt_id+"/2.jpg'); \">"+
               "<param name='movie' value='http://www.youtube.com/v/"+yt_id+"?fs=1&amp;hl=en_US'></param>"+
               "<param name='allowFullScreen' value='true'></param>"+
               "<param name='allowscriptaccess' value='always'></param>"+
               "<embed src='http://www.youtube.com/v/"+yt_id+"?fs=1&amp;hl=en_US' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+w+"' height='"+h+"'></embed>"+
               "</object>";
    
    this.codeReplace(form_id, fname, code);
  },
  
  initRDA: function(form_id, field_key){
    if(!this.actTabIndex[form_id])
      this.actTabIndex[form_id] = new Array();
    if(!this.tabs[form_id])
      this.tabs[form_id] = new Array();
    
    this.actTabIndex[form_id][field_key] = 'filelist';
  },
  
  initTabs: function(form_id, field_key, tabs){
    this.tabs[form_id][field_key] = tabs.split(',');
  },
  
  openTab: function(tabName, form_id, field_key){
    var act = this.actTabIndex[form_id][field_key];
    
    if(act == tabName) 
      return true;
    
    //close the active one
    var tab = document.getElementById('__rdaf_tab_'+form_id+'_'+field_key+'_'+act);
    var tabContent = document.getElementById('__rdaf_content_'+form_id+'_'+field_key+'_'+act);

    tabContent.style.display = 'none';
    tab.className = 'rdafTab';
    
    //open new one
    var tab = document.getElementById('__rdaf_tab_'+form_id+'_'+field_key+'_'+tabName);
    var tabContent = document.getElementById('__rdaf_content_'+form_id+'_'+field_key+'_'+tabName);
 
    tabContent.style.display = 'block';
    tab.className = 'rdafTab active';
    if(tabName == 'options'){
      tab.style.display = 'block';
    }
    
    //set actual
    this.actTabIndex[form_id][field_key] = tabName;
  },
  
  getActTab: function(form_id, field_key){
    return this.actTabIndex[form_id][field_key];
  },
  
  onFileUploadSubmit: function(form_id, file_id){
    var fname = document.getElementById('rageDUI_frame_'+file_id).contentWindow.document.getElementById('file_'+file_id).value;
    if(fname){
      this.startFileUpload(form_id, file_id);
      return true;
    }else{
      return false; 
    }
  },
  
  startFileUpload: function(form_id, file_id){
    var refDoc = document.getElementById('rageDUI_frame_'+file_id).contentWindow.document;
	  //refDoc.getElementById('file_area').style.display = 'none';
  	refDoc.getElementById('file_name_temp').innerHTML = refDoc.getElementById('file_'+file_id).value; 
    refDoc.getElementById('file_substitute_area').style.display = 'block';
  }
  
};

var __rlDynUpload = new __rageLib.rageDynamicUpload();

