function setup_elements(&$mform)
    {
        global $CFG, $RESOURCE_WINDOW_OPTIONS;
        $isNew = true;
        if (KalturaHelpers::getPlatformKey("partner_id", "none") == "none") {
            //        $basic = get_string('needreg', 'kaltura');
            //        $str = str_replace("##SERVER##", $CFG->wwwroot . "/admin/settings.php?section=modsettingkaltura", $basic);
            //        $mform->addElement('static', 'pleasereg',$str, '');
            redirect($CFG->wwwroot . "/admin/module.php?module=kaltura");
            die;
            //        return;
        }
        if (isset($_GET['update'])) {
            $isNew = false;
            $item_id = $_GET['update'];
            $result = get_record('course_modules', 'id', $item_id);
            $result = get_record('resource', 'id', $result->instance);
            $entry = get_record('kaltura_entries', 'context', "R_" . "{$result->id}");
            $default_entry = $entry;
            $url = $CFG->wwwroot . '/mod/kaltura/kswfdoc.php?entry_id=' . $entry->entry_id . '&context=' . $this->course->id;
            $editSyncButton = '<button onclick="kalturaInitModalBox(\'' . $url . '\', {width:780, height:400});return false;">' . get_string('editsyncpoints', 'kaltura') . '</button>';
            $mform->addElement('static', 'edit_sync', get_string('editsyncpoints', 'kaltura'), $editSyncButton);
        } else {
            $last_entry_id = get_field('kaltura_entries', 'max(id)', 'id', 'id');
            if (!empty($last_entry_id)) {
                $default_entry = get_record('kaltura_entries', 'id', "{$last_entry_id}");
                $default_entry->title = "";
            } else {
                $default_entry = new kaltura_entry();
            }
        }
        $hidden_alltext = new HTML_QuickForm_hidden('alltext', $default_entry->dimensions, array('id' => 'id_alltext'));
        $mform->addElement($hidden_alltext);
        $hidden_popup = new HTML_QuickForm_hidden('popup', '', array('id' => 'id_popup'));
        $mform->addElement($hidden_popup);
        $hidden_dimensions = new HTML_QuickForm_hidden('dimensions', $default_entry->dimensions, array('id' => 'id_dimensions'));
        $mform->addElement($hidden_dimensions);
        $hidden_size = new HTML_QuickForm_hidden('size', $default_entry->size, array('id' => 'id_size'));
        $mform->addElement($hidden_size);
        $hidden_custom_width = new HTML_QuickForm_hidden('custom_width', $default_entry->custom_width, array('id' => 'id_custom_width'));
        $mform->addElement($hidden_custom_width);
        $hidden_design = new HTML_QuickForm_hidden('design', $default_entry->design, array('id' => 'id_design'));
        $mform->addElement($hidden_design);
        $hidden_title = new HTML_QuickForm_hidden('title', $default_entry->title, array('id' => 'id_title'));
        $mform->addElement($hidden_title);
        $hidden_entry_type = new HTML_QuickForm_hidden('entry_type', $default_entry->entry_type, array('id' => 'id_entry_type'));
        $mform->addElement($hidden_entry_type);
        $hidden_ppt = new HTML_QuickForm_hidden('ppt_input', $ppt_id, array('id' => 'id_ppt_input'));
        $mform->addElement($hidden_ppt);
        $hidden_video = new HTML_QuickForm_hidden('video_input', $video_id, array('id' => 'id_video_input'));
        $mform->addElement($hidden_video);
        $hidden_ppt_dnld = new HTML_QuickForm_hidden('ppt_dnld_url', $dnld_url, array('id' => 'id_ppt_dnld_url'));
        $mform->addElement($hidden_ppt_dnld);
        $cw_url = $CFG->wwwroot . '/mod/kaltura/kcw.php?mod=ppt_resource';
        $resource = $this->resource;
        $kaltura_client = KalturaHelpers::getKalturaClient();
        $thumbnail = "";
        $ppt_id = $video_id = $dnld_url = '';
        $vid_thumb = '';
        $has_ppt = $has_video = '0';
        if ($isNew) {
            $uploader = '<div id="swfdoc_section">
      <div style="border:1px solid #bcbab4;background-color:#f5f1e9;width:140px;height:105px;float:left;margin-right:80px;text-align:center;font-size:85%" id="thumb_video_holder">
      ' . $vid_thumb . '&nbsp;</div>
      <div style="border:1px solid #bcbab4;background-color:#f5f1e9;width:140px;height:105px;float:left;text-align:center;font-size:85%" id="thumb_doc_holder">' . $ppt_thumb . '&nbsp;</div><br/>
      <div style="width:140px;float:left;margin-right:80px;text-align:center;margin-top:10px;">
	      <input type="button" id="btn_selectvideo" value="' . get_string("selectvideo", "resource_kalturaswfdoc") . '" onclick="kalturaInitModalBox(\'' . $cw_url . '\', {width:760, height:422});return false;">
      </div>

      <div id="flashContainer" style="width:140px;float:left;text-align:center;margin-top:10px;">
      <script>	
	      pptIdHolder = document.getElementById("id_ppt_input");
	      pptThumbHolder = document.getElementById("thumb_doc_holder");
	      videoIdHolder = document.getElementById("id_video_input");
	      videoThumbHolder = document.getElementById("thumb_video_holder");
	      pptDnldUrlHolder = document.getElementById("id_ppt_dnld_url");
        
	      var has_ppt = ' . $has_ppt . ';
	      var has_video = ' . $has_video . ';
      	
	      $("document").ready(function(){ if(has_ppt && has_video) document.getElementById("sync_btn").disabled = false; });
      	
	      txt_document = "<br/>The Document is now being converted.<br/><br/><a href=\\"javascript:check_ready(\'ppt\')\\">Click here</a> to check if conversion is done";
	      function check_ready(theType){
		      if (theType == "ppt") {
			      theId = pptIdHolder.value;
			      theThumb = pptThumbHolder;
            theUrl = pptDnldUrlHolder.value;
		      }
		      var ksoa = new SWFObject("' . $CFG->wwwroot . '/mod/kaltura/images/Pleasewait.swf", "kwait", "140", "105", "9", "#ffffff");
		      ksoa.addParam("allowScriptAccess", "always");
		      ksoa.addParam("allowFullScreen", "TRUE");
		      ksoa.addParam("allowNetworking", "all");
		      ksoa.addParam("wmode","transparent");
		      if(ksoa.installedVer.major >= 9) {
			      ksoa.write("thumb_doc_holder");
		      }
      		
		      $.ajax({ 
		        type: "POST", 
		        url: "' . $CFG->wwwroot . '/mod/kaltura/kcheck_status.php", 
		        data: "type=ppt&downloadUrl="+theUrl, 
		        success: function(msg){ 
			      if (msg == "200") {
				      if (theType != "ppt") { 
					      theThumb.innerHTML = "<img src=\\"' . $kaltura_cdn_url . '/p/' . $CFG->kaltura_partner_id . '/sp/' . $CFG->kaltura_subp_id . '/thumbnail/entry_id/"+theId+"/width/140/height/105/type/3/bgcolor/ffffff\\">";
					      has_video = 1;
					      if (has_ppt) { document.getElementById("sync_btn").disabled = false; }
				      } else {
					      theThumb.innerHTML = "<img src=\\"' . $CFG->wwwroot . '/mod/kaltura/images/V_ico.png\\" style=\\"margin:12px;\\">";
					      has_ppt = 1;
					      if (has_video) { document.getElementById("sync_btn").disabled = false; }
				      }
      					
			      } else {
				      document.getElementById("thumb_doc_holder").innerHTML = txt_document;
			      } 
		        } 
		      });

	      }
	      var has_swfdoc = false;
              function set_has_swfdoc(val)
              {
                  has_swfdoc = val;
              }	
	      function create_swfdoc(){
                      if (has_swfdoc)
                      {
                          entry_id = document.getElementById("id_alltext").value;
                          url = "' . $CFG->wwwroot . '/mod/kaltura/kswfdoc.php?entry_id=" + entry_id + "&context=' . $this->course->id . '";
                          kalturaInitModalBox(url, {width:780, height:400});		
                      } else
                      {  	
		          $.ajax({ 
			          type: "POST", 
			          url: "' . $CFG->wwwroot . '/mod/kaltura/kcreate.php", 
			          data: "action=swfdoc&ppt=" + document.getElementById("id_ppt_input").value + "&video=" + document.getElementById("id_video_input").value + "&name=" + document.getElementById("id_name").value + "&downloadUrl="+pptDnldUrlHolder.value,
			          success: function(entry_id){ 
				          if (entry_id){
					          set_has_swfdoc(true);
					          document.getElementById("id_alltext").value = entry_id;
					          url = "' . $CFG->wwwroot . '/mod/kaltura/kswfdoc.php?entry_id=" + entry_id + "&context=' . $this->course->id . '";
					          kalturaInitModalBox(url, {width:780, height:400});
				          }
			          }
		          });
		      }
	      }
      		
	      function user_selected()
	      {
		      document.getElementById("uploader").upload();
	      }
      	
	      function uploaded()
	      {
        
	        document.getElementById("uploader").addEntries();
	      }
      	
	      function uploading(){
		      has_ppt = 0;

            var ksoa = new SWFObject("' . $CFG->wwwroot . '/mod/kaltura/images/Pleasewait.swf", "kwait", "140", "105", "9", "#ffffff");
            ksoa.addParam("allowScriptAccess", "always");
            ksoa.addParam("allowFullScreen", "TRUE");
            ksoa.addParam("allowNetworking", "all");
            ksoa.addParam("wmode","transparent");
            if(ksoa.installedVer.major >= 9) {
              ksoa.write("thumb_doc_holder");
            }

	      }
      	
	      function entries_added(obj)
	      {
		      document.getElementById("thumb_doc_holder").innerHTML = txt_document;
		      myobj = obj[0];
		      document.getElementById("id_ppt_input").value = myobj.entryId;

	        $.ajax({ 
			      type: "POST", 
			      url: "' . $CFG->wwwroot . '/mod/kaltura/kcreate.php", 
			      data: "action=ppt&ppt=" + document.getElementById("id_ppt_input").value,
            success: function(url){
                if( url.substring(0,2) == "y:")
                {
                  pptDnldUrlHolder.value = url.substring(2);
                }
            }
		      });
	          document.getElementById("uploader").removeFiles(0,0);
      	  
	      }
      	
	      delegate = { selectHandler: user_selected, progressHandler: uploading, allUploadsCompleteHandler: uploaded, entriesAddedHandler: entries_added };
      	
      	
      	
      </script>
          <span><input type="button" id="btn_uploaddoc" value="' . get_string("uploaddocument", "resource_kalturaswfdoc") . '.">
          <span style="border: 0px solid black; position: relative; top: -20px; width: 110px;" id="divKalturaKupload">
          
          <script type="text/javascript">
            var kso = new SWFObject("' . $kaltura_client->getConfig()->serviceUrl . '/kupload/ui_conf_id/1002613", "uploader", "110", "25", "9", "#ffffff");
            kso.addParam("flashVars", "ks=' . $kaltura_client->getKs() . '&uid=' . $USER->id . '&partnerId=' . $CFG->kaltura_partner_id . '&subPId=' . $CFG->kaltura_subp_id . '&entryId=-2&conversionProfile=5&maxUploads=10&maxFileSize=128&maxTotalSize=200&uiConfId=1002613&jsDelegate=delegate");
            kso.addParam("allowScriptAccess", "always");
            kso.addParam("allowFullScreen", "TRUE");
            kso.addParam("allowNetworking", "all");
            kso.addParam("wmode","transparent");
            if(kso.installedVer.major >= 9) {
              kso.write("divKalturaKupload");
            } else {
              document.getElementById("divKalturaKupload").innerHTML = "Flash player version 9 and above is required. <a href=\\"http://get.adobe.com/flashplayer/\\">Upgrade your flash version</a>";
            }
            
          function do_on_wait()
          {
            has_video = 1;
            if(has_ppt && has_video) document.getElementById("sync_btn").disabled = false;
          }

          </script>
	      </span></span>
      </div>
      <script>
      function save_sync() {
      
          create_swfdoc();
          document.getElementById("btn_uploaddoc").disabled = true;
          document.getElementById("btn_selectvideo").disabled = true;
          document.getElementById("divKalturaKupload").innerHTML = "";
      }
      </script>
      <div style="clear:both;text-align:center;width:370px;padding-top:20px;margin:0 auto;margin-left:-10px">' . get_string('syncdescription', 'resource_kalturaswfdoc') . '</div>
      <div style="clear:both;text-align:center;width:370px;padding-top:20px"><input type="button" id="sync_btn" onclick="save_sync()" value="' . get_string('syncpoints', 'kaltura') . '" DISABLED></div>

      </div>';
            $mform->addElement('static', 'divWait', '', get_wait_image("thumb_video_holder", "id_video_input"));
            $mform->addElement('static', 'ppt_thing', '', $uploader);
        }
        $mform->addElement('header', 'displaysettings', get_string('display', 'resource'));
    }
    function setup_elements(&$mform)
    {
        global $CFG, $RESOURCE_WINDOW_OPTIONS;
        if (KalturaHelpers::getPlatformKey("partner_id", "none") == "none") {
            //        $basic = get_string('needreg', 'kaltura');
            //        $str = str_replace("##SERVER##", $CFG->wwwroot . "/admin/settings.php?section=modsettingkaltura", $basic);
            //        $mform->addElement('static', 'pleasereg',$str, '');
            redirect($CFG->wwwroot . "/admin/module.php?module=kaltura");
            die;
            //        return;
        }
        if (isset($_GET['update'])) {
            $item_id = $_GET['update'];
            $result = get_record('course_modules', 'id', $item_id);
            $result = get_record('resource', 'id', $result->instance);
            $entry = get_record('kaltura_entries', 'context', "R_" . "{$result->id}");
            $default_entry = $entry;
        } else {
            $last_entry_id = get_field('kaltura_entries', 'max(id)', 'id', 'id');
            if (!empty($last_entry_id)) {
                $default_entry = get_record('kaltura_entries', 'id', "{$last_entry_id}");
                $default_entry->title = "";
            } else {
                $default_entry = new kaltura_entry();
            }
        }
        $hidden_alltext = new HTML_QuickForm_hidden('alltext', $default_entry->dimensions, array('id' => 'id_alltext'));
        $mform->addElement($hidden_alltext);
        $hidden_popup = new HTML_QuickForm_hidden('popup', '', array('id' => 'id_popup'));
        $mform->addElement($hidden_popup);
        $hidden_dimensions = new HTML_QuickForm_hidden('dimensions', $default_entry->dimensions, array('id' => 'id_dimensions'));
        $mform->addElement($hidden_dimensions);
        $hidden_size = new HTML_QuickForm_hidden('size', $default_entry->size, array('id' => 'id_size'));
        $mform->addElement($hidden_size);
        $hidden_custom_width = new HTML_QuickForm_hidden('custom_width', $default_entry->custom_width, array('id' => 'id_custom_width'));
        $mform->addElement($hidden_custom_width);
        $hidden_design = new HTML_QuickForm_hidden('design', $default_entry->design, array('id' => 'id_design'));
        $mform->addElement($hidden_design);
        $hidden_title = new HTML_QuickForm_hidden('title', $default_entry->title, array('id' => 'id_title'));
        $mform->addElement($hidden_title);
        $hidden_entry_type = new HTML_QuickForm_hidden('entry_type', $default_entry->entry_type, array('id' => 'id_entry_type'));
        $mform->addElement($hidden_entry_type);
        $text_video = new HTML_QuickForm_static('video_text', null, '<span id="spanExplain"><table style="width:100%;font-size:9px;"><tr><td width="25%">' . get_string('videotext', 'resource_kalturavideo') . '</td><td style="width:40%;padding-left:25px;">' . get_string('videoremixtext', 'resource_kalturavideo') . '</td><td width="35%">&nbsp;</td></tr></table></span>');
        //   $text_video_remix = new HTML_QuickForm_static('video_text_remix',null, 'bbb');
        $button = new HTML_QuickForm_input();
        $button->setName('addvid');
        $button->setType('button');
        $button->setValue('Add Video');
        $button_editable = new HTML_QuickForm_input();
        $button_editable->setName('addeditvid');
        $button_editable->setType('button');
        $button_editable->setValue('Add Editable Video');
        $button_replace = new HTML_QuickForm_input();
        $button_replace->setName('replacevid');
        $button_replace->setType('button');
        $button_replace->setValue('Replace Video');
        $button_preview = new HTML_QuickForm_input();
        $button_preview->setName('previewvid');
        $button_preview->setType('button');
        $button_preview->setValue('Preview Video');
        $button_preview_edit = new HTML_QuickForm_input();
        $button_preview_edit->setName('previeweditvid');
        $button_preview_edit->setType('button');
        $button_preview_edit->setValue('Preview & Edit Video');
        $videolabel = get_string('addvideo', 'resource_kalturavideo');
        $videoeditablelabel = get_string('editablevideo', 'resource_kalturavideo');
        $replacelabel = get_string('replacevideo', 'resource_kalturavideo');
        $previewlabel = get_string('previewvideo', 'resource_kalturavideo');
        $previeweditlabel = get_string('previeweditvideo', 'resource_kalturavideo');
        $cw_url = $CFG->wwwroot . '/mod/kaltura/kcw.php?';
        $cw_url_init = $cw_url;
        $edit_url = $CFG->wwwroot . '/mod/kaltura/keditor.php?';
        $edit_url_init = $edit_url;
        $preview_url = $CFG->wwwroot . '/mod/kaltura/kpreview.php?';
        $preview_url_init = $preview_url;
        if (!empty($entry)) {
            $cw_url_init .= 'id=' . $entry->id;
            $preview_url_init .= 'entry_id=' . $entry->entry_id . '&design=' . $entry->design . '&width=' . get_width($entry) . '&dimensions=' . $entry->dimensions;
            $edit_url_init .= 'entry_id=' . $entry->entry_id;
        }
        $button_attributes = array('type' => 'button', 'onclick' => 'set_entry_type(' . KalturaEntryType::MEDIA_CLIP . ');kalturaInitModalBox(\'' . $cw_url_init . '&upload_type=video' . '\', {width:760, height:422});', 'id' => 'id_addvideo', 'value' => $videolabel, 'style' => empty($entry) ? 'display:inline' : 'display:none');
        $button_attributes_editable = array('type' => 'button', 'onclick' => 'set_entry_type(' . KalturaEntryType::MIX . ');kalturaInitModalBox(\'' . $cw_url_init . '&upload_type=mix' . '\', {width:760, height:422});', 'id' => 'id_addeditablevideo', 'value' => $videoeditablelabel, 'style' => empty($entry) ? 'display:inline;margin-left:90px;' : 'display:none');
        $button_attributes_replace = array('type' => 'button', 'onclick' => 'kalturaInitModalBox(\'' . $cw_url_init . (empty($entry) ? '' : ($entry->entry_type == KalturaEntryType::MEDIA_CLIP ? '&upload_type=video' : '&upload_type=mix')) . '\', {width:760, height:422});', 'id' => 'id_replace', 'value' => $replacelabel, 'style' => empty($entry) ? 'display:none' : 'display:inline');
        $button_attributes_preview = array('type' => 'button', 'onclick' => 'kalturaInitModalBox(\'' . $preview_url_init . '\', ' . (empty($entry) ? '{width:400, height:382}' : '{width:' . get_width($entry) . ', height:' . (get_height($entry) + 50) . '}') . ');', 'id' => 'id_preview', 'value' => $previewlabel, 'style' => empty($entry) || $entry->entry_type != KalturaEntryType::MEDIA_CLIP ? 'display:none' : 'display:inline');
        $button_attributes_preview_edit = array('type' => 'button', 'onclick' => 'kalturaInitModalBox(\'' . $edit_url_init . '\', {width:890, height:546});', 'id' => 'id_preview_edit', 'value' => $previeweditlabel, 'style' => empty($entry) || $entry->entry_type != KalturaEntryType::MIX ? 'display:none' : 'display:inline');
        $resource = $this->resource;
        $thumbnail = "";
        if (isset($_GET['update'])) {
            if (!empty($entry)) {
                $thumbnail = '<img id="id_thumb" src="' . KalturaHelpers::getThumbnailUrl(null, $entry->entry_id, 140, 105) . '" />';
                //	    $mform->addElement('static', 'video_thumb', get_string('video', 'resource_kalturavideo'), $thumbnail);
            }
        }
        $button->setAttributes($button_attributes);
        $button_editable->setAttributes($button_attributes_editable);
        $button_replace->setAttributes($button_attributes_replace);
        $button_preview->setAttributes($button_attributes_preview);
        $button_preview_edit->setAttributes($button_attributes_preview_edit);
        $objs = array();
        $objs[] =& $button;
        $objs[] =& $button_editable;
        $objs[] =& $button_replace;
        $objs[] =& $button_preview;
        $objs[] =& $button_preview_edit;
        $text_objs = array();
        $text_objs[] = $text_video;
        $divWait = '<div style="border:1px solid #bcbab4;background-color:#f5f1e9;width:140px;height:105px;float:left;text-align:center;;font-size:85%;display:' . (empty($thumbnail) ? 'none' : 'inline') . '" id="divWait">' . $thumbnail . '</div>
  <script type="text/javascript">
   function set_entry_type(type)
   {
      document.getElementById("id_entry_type").value = type;
   }
  
   function get_height()
   {
      if (get_field("id_dimensions") == "' . KalturaAspectRatioType::ASPECT_4_3 . '")
      {
        switch(get_field("id_size"))
        {
          case "' . KalturaPlayerSize::LARGE . '":
            return 445;
            break;
          case "' . KalturaPlayerSize::SMALL . '":
            return 340;
            break;
          case "' . KalturaPlayerSize::CUSTOM . '":
            return parseInt(get_field("id_custom_width"))*3/4 + 65 + 80;
            break;
          default:
            return 445;
           break;
        }
      }
      else
      {
        switch(get_field("id_size"))
        {
          case "' . KalturaPlayerSize::LARGE . '":
            return 370;
           break;
          case "' . KalturaPlayerSize::SMALL . '":
            return 291;
            break;
          case "' . KalturaPlayerSize::CUSTOM . '":
            return parseInt(get_field("id_custom_width"))*9/16 + 65 + 80;
            break;
          default:
            return 370;
            break;
        }
      
      }
   
   }
  
   function get_width()
   {
    switch(get_field("id_size"))
    {
      case "' . KalturaPlayerSize::LARGE . '":
        return 450;
        break;
      case "' . KalturaPlayerSize::SMALL . '":
        return 310;
        break;
      case "' . KalturaPlayerSize::CUSTOM . '":
        return parseInt(get_field("id_custom_width")) + 50;
        break;
      default:
        return 450;
        break;
    }   
   }
      
   function do_on_wait()
   {
      var entryId = document.getElementById("id_alltext").value;
      document.getElementById("id_addvideo").style.display="none";
      document.getElementById("id_addeditablevideo").style.display="none";
      document.getElementById("id_replace").style.display="inline";
      if (document.getElementById("spanExplain") != null)
      {
        document.getElementById("spanExplain").style.display = "none";
      }
      
      if (document.getElementById("id_entry_type").value == ' . KalturaEntryType::MEDIA_CLIP . ')
      {
        var design = get_field("id_design");
        var width = get_width();
        var dimensions = get_field("id_dimensions");
        document.getElementById("id_preview").style.display="inline";
//        document.getElementById("id_preview").onclick=new Function("kalturaInitModalBox(\'' . $preview_url . 'entry_id=" + entryId + "\', {width:400, height:382})");
        document.getElementById("id_preview").onclick=new Function("kalturaInitModalBox(\'' . $preview_url . 'entry_id=" + entryId + "&design=" + design + "&width=" + width + "&dimensions=" + dimensions + "\', {width:get_width(), height:get_height()})"); //width:get_width()+10
     }
      else
      {
        document.getElementById("id_preview_edit").style.display="inline";
        document.getElementById("id_preview_edit").onclick=new Function("kalturaInitModalBox(\'' . $edit_url . 'entry_id=" + entryId + "\', {width:890, height:546})");
        document.getElementById("id_replace").onclick=new Function("kalturaInitModalBox(\'' . $cw_url . '&upload_type=mix\', {width:760, height:422})");
      }
   }
   </script>';
        $mform->addElement('static', 'divWait', '', get_wait_image("divWait", "id_alltext"));
        $mform->addElement('static', 'please_wait', empty($entry) ? '' : get_string('video', 'resource_kalturavideo'), $divWait);
        $mform->addElement('group', 'videogroup', empty($entry) ? get_string('video', 'resource_kalturavideo') : '', $objs);
        if (!isset($_GET['update'])) {
            $mform->addElement('group', 'videotextgroup', '', $text_objs);
        }
        $mform->addElement('header', 'displaysettings', get_string('display', 'resource'));
        return;
    }