  
  var win;
  var currentAction;
  var currentItemID;
 
  var isReady = false;
  Ext.onReady(function () {
    isReady = true;
  });

  var eventDetails;
  var eventFiles;
  var eventUpload;
  var eventUploadImage;
  
  var dialogHeight;

  function buildLink(mode, itemID, dataAction, frame){
    var tmpFrame = '';
    if (frame == true)
      tmpFrame = 'frame=true&';
    return '/ajax.cfm?' + tmpFrame + 
		   's=' + currentSchoolAlias +
           '&a=edit&mode=' + mode + 
           '&id=' + itemID +
           '&new=' + dataAction;
  }
  
  
  function getTitle(ID, mgaction){

    var modTitle = 'Edit';
    if (ID == 0)
      var modTitle = 'Add';	
    
    var windowHeading = 'Item';    
    var windowAddition = '';
    switch(mgaction){
      case 'hmk':
        var windowHeading = 'Homework &amp; Projects';
        break;
      
      case 'assignment':
        var windowHeading = 'Assignment';
        break;
      
      case 'jobs':
        var windowHeading = 'Job';
        var windowAddition = ' employment details, File Sharing and more!';
        break;  
        
      case 'cal':
        var windowHeading = 'Event';
        var windowAddition = ' Event details, File Sharing and more!';
        break;
        
      case 'podcast':
        windowHeading = 'Podcast';
        var windowAddition = ' Podcasts and uploading media!';
        break;
		
	    case 'class':
	    case 'Class':
        windowHeading = 'Class';
        var windowAddition = ' Starting your new Class!';
        break;
		
	    case 'department':
	    case 'Department':
        windowHeading = 'Department';
        var windowAddition = ' Starting your new Department!';
        break;
        
	    case 'bookings':
        windowHeading = 'Booking';
        var windowAddition = 'Reserving!';
        break;
        
      case 'news':
        var windowHeading = 'Announcements';
        var windowAddition = ' details, share files and more!';
        break;

      case 'blog':
        var windowHeading = 'Blog';
        var windowAddition = ' details, share files and more!';
        break;
	
	    case 'gallery':
        var windowHeading = 'Gallery Item';
        var windowAddition = 'Upload photos to share!';
        break;
	 
	    case 'scope':
        var windowHeading = 'Scope & Sequence';
        var windowAddition = 'Plan out your class for the year!';
        break;
		
	    case 'page':
        var windowHeading = 'Page';
        var windowAddition = 'Edit New Pages in a flash!';
        break;
        
      case 'category':
        var windowHeading = 'Category';
        var windowAddition = ' making categories!';
        break;
      
      case 'location':
        var windowHeading = 'Location';
        var windowAddition = ' making Location!';
        break;

      case 'channel':
        var windowHeading = 'Channel';
        var windowAddition = 'making Channels';
        break;	
    }

    var helpTitle = modTitle + ' a ' + windowHeading;
    var helpText = 'Click here to learn more about ' + windowAddition;


    return '<strong style="font-size: 13px; font-weight: bold">' + helpTitle + '</strong><br /><a href="#">' + helpText + '</a>';
  }
  
  
  function viewFilesTab(){
      evtFiles = Ext.getCmp('tab_files');
      evtFiles.show();
      $('filesFormWin').src = $('filesFormWin').src;
  }
  function add2Edit(ID, currentAction){
    win.setTitle('Updated Item #' + ID + '...');
    if (ID > 0 && currentAction != 'gallery'){
      currentID = ID;

      switch(currentAction){
        case 'podcast':
          linkFileTabType = 'files_process';
          linkUploadTabType = 'upload_process';
          break;

        default:
          linkFileTabType = 'files';
          linkUploadTabType = 'upload';
          break;
      }

      evtFiles = Ext.getCmp('tab_files');
      evtFiles.enable();
      evtFiles.autoLoad = buildLink(linkFileTabType, ID, currentAction, false);

      helpPane = Ext.getCmp('help_pane');
      helpPane.body.update(getTitle(ID, currentAction));
      
      eventUploadImage = Ext.getCmp('tab_uploadImage');      
      eventUploadImage.enable();
      eventUploadImage.show();
      eventUploadImage.body.update('<iframe id="uploadImageFormWin" src="' + buildLink('upload_image', ID, currentAction, true) + '" style="border: 0; width: 100%; height: ' + dialogHeight + 'px"></iframe>');
	  
	    eventUpload = Ext.getCmp('tab_upload');      
      eventUpload.show();
	    eventUpload.enable();
      eventUpload.body.update('<iframe id="uploadFilesFormWin" src="' + buildLink(linkUploadTabType, ID, currentAction, true) + '" style="border: 0; width: 100%; height: ' + dialogHeight + 'px"></iframe>');
	  
    } else {
		
	    helpPane = Ext.getCmp('help_pane');
      helpPane.body.update(getTitle(ID, currentAction));
      
      eventUploadImage = Ext.getCmp('tab_uploadImage');      
      eventUploadImage.enable();
      eventUploadImage.show();
      eventUploadImage.body.update('<iframe id="uploadImageFormWin" src="' + buildLink('upload_image', ID, currentAction, true) + '" style="border: 0; width: 100%; height: ' + dialogHeight + 'px"></iframe>');	
		
	  }
  }
  
  function pluginEdit(el, ID, gcdID, ccID, startDate, mgaction, highlight, classPeriodID){
    var gatewayAction = mgaction;
    if (gatewayAction == 'event')
      var gatewayAction = 'cal';
    return editItem(el, highlight, ID, startDate, gatewayAction, mgaction, gcdID, ccID, classPeriodID);
  }
  
  function setEditTitle(titleStr){
    if (currentID > 0)
      win.setTitle(titleStr);
  }
  
  //(el, ID, gcdID, ccID, startDate, mgaction, highlight, classPeriodID){
  function editItem(mainContainer, itemContainer, itemID, dateHint, gatewayAction, newDataAction, gcdID, ccID, classPeriodID){
    podVideoShowing = false;
    if (podVideoShowing != 'undefined' && podVideoShowing == true){
        $f(0).hide();
    }
  
    //Set our highlight ID for when we refresh.
    currentPageHighlight = itemContainer;
    currentID = itemID;
    
    //Ensure these values are
    if (dateHint == '')
      var dateHint = 0;
    if (gcdID == '')
      var gcdID = 0;
    if (classPeriodID == '')
      var classPeriodID = 0;
    if (ccID == '')
      var ccID = 0;
    
    var windowTitle = 'Loading Item #' + itemID + '...';
    if (itemID == 0)
      var windowTitle = 'New Entry';
      
    switch(gatewayAction){
      case 'hmk':
      case 'assignment':
      case 'jobs':
      case 'news':
      case 'blog':
	    case 'scope':
      case 'cal':
        linkFileTabType = 'files';
        linkUploadTabType = 'upload';
		    imagetab = 'Thumbnail';
        break;
        
      case 'podcast':
        linkFileTabType = 'files_process';
        linkUploadTabType = 'upload_process';
		    imagetab = 'Thumbnail';
        break;
		
      case 'bookings':
	    case 'gallery':
	    case 'page':
        linkFileTabType = 'files';
        linkUploadTabType = 'upload';
		    imagetab = 'Upload Image';
        break;
        
 	  default:
        alert('Module not found in switch');
        return false;
        break;
    }
      
    dialogHeight = 550;
    dialogWidth = 680;
    bookingDetails = {
        title: 'Details',
        id: 'tab_detail',
        height: dialogHeight,
        html: '<iframe src="/ajax.cfm?frame=true&a=edit&mode=editbookings&id=' + itemID + '&s=' + currentSchoolAlias + '&gcditemID=' + gcdID + '&gcdPeriod=' + classPeriodID + '&ccitemID=' + ccID + '&date=' + dateHint + '&new=' + newDataAction + '" style="width: 100%; border: 0; height: 100%"></iframe>'
      };
    eventDetails = {
        title: 'Details',
        id: 'tab_detail',
        height: dialogHeight,
        html: '<iframe src="/ajax.cfm?frame=true&a=edit&mode=edit&id=' + itemID + '&s=' + currentSchoolAlias + '&gcditemID=' + gcdID + '&gcdPeriod=' + classPeriodID + '&ccitemID=' + ccID + '&date=' + dateHint + '&new=' + newDataAction + '" style="width: 100%; border: 0; height: 100%"></iframe>'
      };
    eventSyndication = {
        title: 'Syndicate',
        id: 'tab_share',
        height: dialogHeight,
        html: '<iframe src="/ajax.cfm?frame=true&a=edit&mode=share&id=' + itemID + '&s=' + currentSchoolAlias + '&gcditemID=' + gcdID + '&gcdPeriod=' + classPeriodID + '&ccitemID=' + ccID + '&date=' + dateHint + '&new=' + newDataAction + '" style="width: 100%; border: 0; height: 100%"></iframe>'
      };
    eventExamCreator = {
        title: 'Exam',
        id: 'tab_exam',
        height: dialogHeight,
        html: '<iframe src="/ajax.cfm?frame=true&a=edit&mode=exam&id=' + itemID + '&s=' + currentSchoolAlias + '&gcditemID=' + gcdID + '&gcdPeriod=' + classPeriodID + '&ccitemID=' + ccID + '&date=' + dateHint + '&new=' + newDataAction + '" style="width: 100%; border: 0; height: 100%"></iframe>'
      };
    eventFiles = {
        title: 'Files',
        id: 'tab_files',
        height: dialogHeight,
        html: '<iframe id="filesFormWin" src="' + buildLink(linkFileTabType, itemID, gatewayAction, true) + '" style="border: 0; width: 100%; height: 100%"></iframe>'
      };    
    eventUpload = {
        title: 'Upload Files',
        id: 'tab_upload',
        height: dialogHeight,
        html: '<iframe id="uploadFilesFormWin" src="' + buildLink(linkUploadTabType, itemID, gatewayAction, true) + '" style="border: 0; width: 100%; height: 100%"></iframe>'
      };
    eventUploadImage = {
        title: imagetab,
        id: 'tab_uploadImage',
        height: dialogHeight,
        html: '<iframe id="uploadImageFormWin" src="' + buildLink('upload_image', itemID, gatewayAction, true) + '" style="border: 0; width: 100%; height: 100%"></iframe>'
      };

                   
	if (itemID == 0){
      	eventFiles.disabled = true;
      	eventUpload.autoLoad = '';
      	eventUpload.disabled = true;
      	eventUpload.html = '';
		    eventUploadImage.autoLoad = '';
		    eventUploadImage.disabled = true;
		    eventUploadImage.html = '';
    	}
	
	  
	if (gatewayAction == "gallery"){
	    var tabHolder = new Ext.TabPanel({
					activeTab: 0,                                                                                
					region: 'center',
					height: dialogHeight,
					items: [eventDetails,							
						      eventUploadImage ]
				});
	}else if (gatewayAction == "bookings") {
	    var tabHolder = new Ext.TabPanel({
					activeTab: 0,                                                                                
					region: 'center',
					height: dialogHeight,
					items: [bookingDetails]
				});
	}else if (gatewayAction == "cal") {
    var tabHolder = new Ext.TabPanel({
        activeTab: 0,                                                                                
        region: 'center',
        height: dialogHeight,
        items: [eventDetails,
                eventSyndication,
                eventFiles,
                eventUpload,
                eventUploadImage ]
      });
	}else if (gatewayAction == "assignment") {
    var tabHolder = new Ext.TabPanel({
        activeTab: 0,                                                                                
        region: 'center',
        height: dialogHeight,
        items: [eventDetails,
                eventExamCreator,
                eventFiles,
                eventUpload,
                eventUploadImage ]
      });
	}else{
	    var tabHolder = new Ext.TabPanel({
					activeTab: 0,                                                                                
					region: 'center',
					height: dialogHeight,
					items: [eventDetails,
							    eventFiles,
							    eventUpload,
						      eventUploadImage ]
				});
	}

    //,eventUploadImage

  
    var helpBox = {region: 'north', id: 'help_pane', baseCls: '', cls: 'calendarHelpTitle', height: 48, border: 0, html: getTitle(itemID, newDataAction)};

    win = new Ext.Window({
        title: windowTitle,
        minWidth: dialogWidth,
        width: dialogWidth,
        minHeight: dialogHeight - 48,
        height: dialogHeight - 48,
        layout: 'border',
        modal: true,
        closeAction: 'hide',
        maximizable: false,
        closable: false,
        items: [helpBox, tabHolder],
        buttons: [{
                    text: 'Close',
                    handler: function(){
                      processUpdate(mainContainer, itemID);
                      win.hide();
                    }
                  }]
      });
    win.show();
  }

  function processUpdate(el, ID){
    currentID=0;
    var eURL = buildURL();
    ajaxifyURL(eURL, el);
    if (podVideoShowing == true){
        $f(0).show();
    }
	
  }
  
  
  	function pluginDel(el, id){
		
		Ext.Msg.confirm('Item Deletion','Are you sure you would like to delete this item?', 
						function(btn){
								if(btn == 'yes') {
									deleteItem(el, id);
									new Ajax.Request('/ajax/delete_item.cfm', { 
									method: 'post', parameters: {data: id}, 
									onSuccess: function(response) {
											if (response.responseText.trim() == "true")
											{window.location = "index.cfm"}
										}

									});
								}
							}
						);
	
	}	

  	function widgetDel(el, id){
		
		Ext.Msg.confirm('Delete Widget','Are you sure you would like to delete this widget?', 
						function(btn){
								if(btn == 'yes') {
									deleteItem(el, id);
									new Ajax.Request('/ajax/delete_widget.cfm?wid=' +id , { method: 'get'});
								}
							}
						);
	
	}
					

	function deleteItem(el, id){
		  $$('.'+el).each(function(s){new Effect.Fade(s);});
		}
	
