if (typeof dynamo == "undefined") { 
  var dynamo = new Object();
}

if (typeof rootPath == "undefined") { 
  var rootPath = "./";
}

if (typeof rootWysiwygPath == "undefined") { 
  var rootWysiwygPath = "dynamo/_config/";
}


if (typeof serverDynamoURL == "undefined") {
  var serverDynamoURL = "dynamo";
}

if (typeof serverDynamoURL == "undefined") {
  var theRatingValue = "";
}

if (typeof dynamo.entriesInterface == "undefined") {
  dynamo.entriesInterface = function() {
    this.ajax = new luckymarble.ajaxUtil();
    this.debugMode = true;
    this.entryID = "";
    var oldonload = window.onload;
    var starHover = new Image();
    starHover.src = "dynamo/_config/@blog/images/star_hover.gif";
    var starSelected = new Image();
    starSelected.src = "dynamo/_config/@blog/images/star_selected.gif"; 
    var starUnselected = new Image();
    starUnselected.src = "dynamo/_config/@blog/images/star_unselected.gif";

    if (typeof window.onload != 'function') {
      window.onload = function() {
        entries.initialize();
      }
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        entries.initialize();
      }
    }
  }
  
  dynamo.entriesInterface.prototype = {    
    
    getPage: function(pageNumber) {
      //alert(pageNumber);
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=blog&component=entries_page-ajax&cmd=render&start=" + pageNumber + "&bypass=1", this.processRender, "txt");
    },

    addComment: function(entryNumber, backTo) {
      theRatingValue = "";
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=blog&component=comments_page-ajax&cmd=add&backTo=" + backTo + "&blog_id=" + entryNumber + "&bypass=1", this.processRender, "txt");
    },
    
    viewComment: function(entryNumber, backTo) {
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=blog&component=comments_page-ajax&cmd=view&backTo=" + backTo + "&blog_id=" + entryNumber + "&bypass=1", this.processRender, "txt");
    },
    
    saveComments: function(entryNumber) {
      // in the event that this is on a local system, alert the user
      if (window.location.href.indexOf("http")) {
        alert(languageParam["dynamo_error-not_active"]);
      // else handle the parameters from the form
      } else {
        
        if (document.getElementById("rating")) {
           theRatingValue = document.getElementById("rating").value;
        }
        
        updateTextArea("entry_comment");
        myForm     = document.forms['add_comment'];
        parameters = "cmd=save";
        
        for (var i = 0; i < myForm.elements.length; i++) {
          var myField = myForm.elements[i];
          if (myField.name != "") {
            inputField = myField.value.replace(/&/gi, "%26");
            //inputField = inputField.replace(/<[pP]>/gi, "");
            //inputField = inputField.replace(/<br>/gi, "%0D%0A");
            //inputField = inputField.replace(/<br>/gi, "%0d");
            parameters = parameters + "&" + myField.name + "=" + inputField;
          }
        }
        //alert(parameters);
        this.ajax.postAjaxRequest(rootPath + "dynamo/?module=blog&component=comments_page-ajax&blog_id=" + entryNumber + "&bypass=1", parameters, this.processRender, "txt");
      }
    },
        
    /* Welcome to the RATING solution.
    ** The folowing methods monitor the star selection system for rating an entry **/
    starFeedBack: function (aStar) {
      for (var i = 1; i <= 5; i++) {
        aTargetDiv = document.getElementById("rating_star_" + i);
        if (i <= aStar ) {
          aTargetDiv.src = "dynamo/_config/@blog/images/star_hover.gif";
        } else {
          aTargetDiv.src = "dynamo/_config/@blog/images/star_unselected.gif";
        }
      }
    },
    
    /* OnClick this method takes care of changing the graphics to the selected image
      and writes the rating number to the hidden input form*/
    starSelect: function (aStar) {
      hiddenTargetInput = document.getElementById("rating");
      hiddenTargetInput.value = aStar;
      for (var i = 1; i <= 5; i++) {
        aTargetDiv = document.getElementById("rating_star_" + i);
        if (i <= aStar) {
          //alert(hiddenTargetInput.value);
          aTargetDiv.src = "dynamo/_config/@blog/images/star_selected.gif";
        } else {
          aTargetDiv.src = "dynamo/_config/@blog/images/star_unselected.gif";
        }
      }
    },
    
    /*Onmouseout this method checks the hidden input field to determine which 
      stars should remain selected*/
    getCurrentRating: function () {
      hiddenTargetInput = document.getElementById("rating");
      aStar = hiddenTargetInput.value;
      for (var i = 1; i <= 5; i++) {
        aTargetDiv = document.getElementById("rating_star_" + i);
        if (i <= aStar) {
          //alert(hiddenTargetInput.value);
          aTargetDiv.src = "dynamo/_config/@blog/images/star_selected.gif";
        } else {
          aTargetDiv.src = "dynamo/_config/@blog/images/star_unselected.gif";
        }
      }
    },
    
    // render the "view" based on information retrieved via AJAX
    processRender: function() {
      //alert(rootPath);
      var myAjax     = dynamo.entriesInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.entriesInterfaceObj.ajax.fileType;

      if (myAjax.readyState == "4") {     
        if (myAjax.status == 500 && dynamo.entriesInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              var myTargetDiv = document.getElementById("dynamo_blog_entries_container");
              //alert(myAjax.responseText);
			  myTargetDiv.innerHTML = myAjax.responseText;
			  if (document.getElementById("entry_comment")) {
			    generate_wysiwyg('entry_comment');
			    
			  }
			  
			  if (document.getElementById("rating")) {
			    entries.starSelect(theRatingValue);
			    theRatingValue = "";
			  }			  
            }
          } 
        } else  {
          alert(languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },

    // handle initialization
    initialize: function() {        
      //alert(rootPath + "dynamo/", "module=blog&component=entries_page-ajax&cmd=render&bypass=1");  
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=blog&component=entries_page-ajax&cmd=render&bypass=1", this.processRender, "txt");
    } 
  }

  dynamo.entriesInterfaceObj = new dynamo.entriesInterface();
  entries = dynamo.entriesInterfaceObj;
}
